Commit b2c67199 authored by Tomas Pettersson's avatar Tomas Pettersson 🏸

refactor

parent 76230593
......@@ -46,9 +46,9 @@ if [ $1 -gt 1 ]; then
# Sleep just to be on the safe side
sleep 10
# Remove all but the run output directory
find /local_disk/%{name}/run -mindepth 1 -maxdepth 1 -depth -not -name output -print0 | xargs -0 rm -rf
find /local_disk/%{name} -mindepth 1 -maxdepth 1 -depth -not -name output/run -print0 | xargs -0 rm -rf
# Remove all log files older than 30 days
find /local_disk/%{name}/run/output -type f -mtime +30 -print0 | xargs -0 rm -rf
find /local_disk/%{name}/output/run -type f -mtime +30 -print0 | xargs -0 rm -rf
fi
......
[padmws]
url: https://padm-tst.smhi.se
usr: stw
pwd: IQqpM1L5
\ No newline at end of file
......@@ -25,5 +25,6 @@
"longitude": 11.390247344970703
}
],
"timeperiod": 3
"timeperiod": 3,
"domain" : "BF_NEMO"
}
\ No newline at end of file
......@@ -37,5 +37,6 @@
"longitude": 26.958385167216541
}
],
"timeperiod": 3
"timeperiod": 3,
"domain" : "HELCOM_NEMO"
}
\ No newline at end of file
......@@ -25,5 +25,7 @@
"longitude": 18.0615234375
}
],
"timeperiod": 3
"timeperiod": 3,
"domain" : "HELCOM_NEMO"
}
\ No newline at end of file
......@@ -25,5 +25,6 @@
"longitude": 13.6724853515625
}
],
"timeperiod": 3
"timeperiod": 3,
"domain" : "VN_NEMO"
}
\ No newline at end of file
#!/usr/bin/sh
cd /local_disk/SMHI-padmwsverify/run
./run.py
\ No newline at end of file
#!/usr/bin/python
# coding: utf-8
import sys
import random
from runner.model import Models
def main():
try:
v = Models()()
r = random.choice(v)
r();
sys.exit(0)
except Exception as error:
print error
sys.exit(1)
'''
To run in terminal call with python run.py
'''
if __name__ == "__main__":
main()
\ No newline at end of file
......@@ -4,43 +4,50 @@
import sys
import json
import requests
import random
from os import walk
from requests.auth import HTTPBasicAuth
from time import time, strftime, sleep
from datetime import datetime, timedelta
from ConfigParser import SafeConfigParser
global path
class Runner(object):
def __init__(self):
settings = SafeConfigParser()
settings.read(path+'/run.cfg')
self.url = settings.get('misc', 'url');
self.usr = settings.get('misc', 'usr');
self.pwd = settings.get('misc', 'pwd');
settings.read('env.cfg')
self.url = settings.get('padmws', 'url');
self.usr = settings.get('padmws', 'usr');
self.pwd = settings.get('padmws', 'pwd');
def onError(error, errarea):
raise ValueError(error, errarea)
def __call__(self, jdata, name, domain):
def __call__(self, jdata, name, timeperiod, output, timeout):
# create
rCreate = requests.post('/'.join([self.url, "workarea", "v2"]), data=None, json = None, auth=HTTPBasicAuth(self.usr, self.pwd))
if (rCreate.status_code == 401): onError("No access", "workarea")
wid = rCreate.headers['Location'].split('/')[-1]
# domain
domain = jdata['domain']
rDomain = requests.post('/'.join([self.url, "workarea", "v2", wid, "op", "domain"]), data=None, json = {"domain": domain}, auth=HTTPBasicAuth(self.usr, self.pwd))
# setting
setting = jdata['setting']
setting['start'] = datetime.utcnow().strftime("%Y-%m-%d %H:%M")+':00'
end = datetime.utcnow()
end += timedelta(hours=jdata['timeperiod'])
if (timeperiod):
end = datetime.utcnow()
end += timedelta(hours=timeperiod)
setting['end'] = end.strftime("%Y-%m-%d %H:%M")+':00'
rSetting = requests.post('/'.join([self.url, "workarea", "v2", wid, "op", "setting"]), data=None, json = jdata['setting'], auth=HTTPBasicAuth(self.usr, self.pwd))
# coordinates
rCoords = requests.post('/'.join([self.url, "workarea", "v2", wid, "op", "coordinates"]), data=None, json = {"coordinate": jdata['coordinate']}, auth=HTTPBasicAuth(self.usr, self.pwd))
# run
rRun = requests.post('/'.join([self.url, "runner", "v2"]), data='workareaId='+wid, json=None, auth=HTTPBasicAuth(self.usr, self.pwd))
if (rRun.status_code == 401): raise ValueError('No access', 'runner')
status = "";
start = time()
end = time()
while ((status != 'COMPLETE') and (end-start < 30)):
while ((status != 'COMPLETE') and (end-start < timeout)):
sleep(3)
try:
status = json.loads(requests.get('/'.join([self.url, "runner", "v2", wid, "progress"])).content)['status']
......@@ -49,67 +56,44 @@ class Runner(object):
end = time()
# done
hasError = requests.get('/'.join([self.url, "workarea", "v2", wid, "error.txt"])).status_code == 200
with open(path+'/output/run.txt', 'w') as outputfile:
if (output == 'run'):
with open('output/run/run.txt', 'w') as outputfile:
if (status != 'COMPLETE') or hasError:
outputfile.write('0')
with open('output/run/'+name+'_'+datetime.utcnow().strftime("%Y%m%d%H%M")+'.txt', 'w') as errorfile:
errorfile.write(wid)
else:
outputfile.write('1')
with open('output/run/statistics.txt', 'a') as statisticsfile:
statisticsfile.write(datetime.utcnow().strftime("%Y%m%d%H%M")+' '+wid+' '+name+'\n')
else:
if (status != 'COMPLETE') or hasError:
outputfile.write('0')
with open(path+'/output/'+name+'_'+datetime.utcnow().strftime("%Y%m%d%H%M")+'.txt', 'w') as errorfile:
with open('output/validate/'+name+'_'+datetime.utcnow().strftime("%Y%m%d%H%M")+'.txt', 'w') as errorfile:
errorfile.write(wid)
else:
outputfile.write('1')
with open(path+'/output/statistics.txt', 'a') as statisticsfile:
with open('output/validate/statistics.txt', 'a') as statisticsfile:
statisticsfile.write(datetime.utcnow().strftime("%Y%m%d%H%M")+' '+wid+' '+name+'\n')
class HELCOM_NEMO(Runner):
def __init__(self, value):
super(HELCOM_NEMO, self).__init__()
self.name = 'helcom_nemo_'+str(value)
with open(path+'/input/'+self.name+'.json') as file:
self.jdata = json.load(file)
def __call__(self):
super(HELCOM_NEMO, self).__call__(self.jdata, self.name, "HELCOM_NEMO")
class BF_NEMO(Runner):
def __init__(self, value):
super(BF_NEMO, self).__init__()
self.name = 'bf_nemo_'+str(value)
with open(path+'/input/'+self.name+'.json') as file:
class Model(Runner):
def __init__(self, inputfile):
super(Model, self).__init__()
self.name,suffix = inputfile.split('.')
with open('input/'+inputfile) as file:
self.jdata = json.load(file)
def __call__(self):
super(BF_NEMO, self).__call__(self.jdata, self.name, "BF_NEMO")
class VN_NEMO(Runner):
def __init__(self, value):
super(VN_NEMO, self).__init__()
self.name = 'vn_nemo_'+str(value)
with open(path+'/input/'+self.name+'.json') as file:
self.jdata = json.load(file)
def __call__(self):
super(VN_NEMO, self).__call__(self.jdata, self.name, "VN_NEMO")
def __call__(self, timeperiod = None, output = 'run', timeout = 30):
super(Model, self).__call__(self.jdata, self.name, timeperiod, output, timeout)
# TODO
# Change user to stw when ready
# Create packages to be able to run tests in separate files
# Install in prod
# Create Ester for monitor signal
class Models(object):
def __init__(self):
self.inputs = []
for (dirpath, dirnames, filenames) in walk('input'):
self.inputs.extend(filenames)
break
def main():
try:
v = [HELCOM_NEMO(1), HELCOM_NEMO(2), VN_NEMO(1), BF_NEMO(1)]
r = random.choice(v)
r();
sys.exit(0)
except Exception as error:
print error
sys.exit(1)
'''
To run in terminal call with python run/run.py run
'''
if __name__ == "__main__":
path = '.'
if (len(sys.argv) > 1):
path = sys.argv[1]
main()
\ No newline at end of file
def __call__(self):
models = []
for inputfile in self.inputs:
models.append(Model(inputfile));
return models
#!/usr/bin/python
# coding: utf-8
import sys
from time import sleep
from runner.model import Models
def main(method):
try:
mlist = Models()()
if method == 'all':
for m in mlist:
m(24, 'validate', 300);
sleep(3)
elif method == 'b':
raise ValueError('B is not implemented')
else:
raise ValueError('Validation method is not defined. Try using python validate.py all')
sys.exit(0)
except Exception as error:
print error
sys.exit(1)
'''
To run in terminal call with python validate.py all
'''
if __name__ == "__main__":
method = ''
if (len(sys.argv) > 1):
method = sys.argv[1]
main(method)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment