Commit b33bd1f2 authored by Nicolas Wavrant's avatar Nicolas Wavrant

slaprunner: Build&Run params new saved in json file

instead of hardcode
parent 8a3987fa
...@@ -78,6 +78,19 @@ def checkHtpasswd(config): ...@@ -78,6 +78,19 @@ def checkHtpasswd(config):
else: else:
return return
def checkJSONConfig(config):
"""create a default json file with some parameters inside
if the file has never been created"""
json_file = os.path.join(config['etc_dir'], 'config.json')
if not os.path.exists(json_file):
params = {
'run_instance' : True,
'run_software' : True,
'max_run_instance' : 3,
'max_run_software' : 2
}
open(json_file, "w").write(json.dumps(params))
def run(): def run():
"Run default configuration." "Run default configuration."
...@@ -107,6 +120,7 @@ def serve(config): ...@@ -107,6 +120,7 @@ def serve(config):
PERMANENT_SESSION_LIFETIME=datetime.timedelta(days=31), PERMANENT_SESSION_LIFETIME=datetime.timedelta(days=31),
) )
checkHtpasswd(app.config) checkHtpasswd(app.config)
checkJSONConfig(app.config)
if not os.path.exists(workdir): if not os.path.exists(workdir):
os.mkdir(workdir) os.mkdir(workdir)
if not os.path.exists(software_link): if not os.path.exists(software_link):
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# vim: set et sts=2: # vim: set et sts=2:
# pylint: disable-msg=W0311,C0301,C0103,C0111,W0141,W0142 # pylint: disable-msg=W0311,C0301,C0103,C0111,W0141,W0142
import json
import logging import logging
import md5 import md5
import multiprocessing import multiprocessing
...@@ -26,11 +27,6 @@ import slapos.slap ...@@ -26,11 +27,6 @@ import slapos.slap
logger = logging.getLogger('werkzeug') logger = logging.getLogger('werkzeug')
RUN_INSTANCE = True
RUN_SOFTWARE = True
MAX_RUN_INSTANCE = 3
MAX_RUN_SOFTWARE = 2
TRUE_VALUES = (1, '1', True, 'true', 'True') TRUE_VALUES = (1, '1', True, 'true', 'True')
html_escape_table = { html_escape_table = {
...@@ -41,28 +37,19 @@ html_escape_table = { ...@@ -41,28 +37,19 @@ html_escape_table = {
"<": "&lt;", "<": "&lt;",
} }
def getBuildAndRunParams(): def getBuildAndRunParams(config):
dict_params = { json_file = os.path.join(config['etc_dir'], 'config.json')
'run_instance' : RUN_INSTANCE, json_params = json.load(open(json_file))
'run_software' : RUN_SOFTWARE, return json_params
'max_run_instance' : MAX_RUN_INSTANCE,
'max_run_software' : MAX_RUN_SOFTWARE
}
return dict_params
def saveBuildAndRunParams(params): def saveBuildAndRunParams(config, params):
"""XXX-Nico parameters have to be correct. """XXX-Nico parameters have to be correct.
Works like that because this function do not care Works like that because this function do not care
about how you got the parameters""" about how you got the parameters"""
global RUN_INSTANCE json_file = os.path.join(config['etc_dir'], 'config.json')
global RUN_SOFTWARE open(json_file, "w").write(json.dumps(params))
global MAX_RUN_INSTANCE
global MAX_RUN_SOFTWARE
RUN_INSTANCE = params['run_instance']
RUN_SOFTWARE = params['run_software']
MAX_RUN_INSTANCE = params['max_run_instance']
MAX_RUN_SOFTWARE = params['max_run_software']
def html_escape(text): def html_escape(text):
"""Produce entities within text.""" """Produce entities within text."""
...@@ -820,11 +807,12 @@ def buildAndRun(config): ...@@ -820,11 +807,12 @@ def buildAndRun(config):
def runSlapgridUntilSuccess(config, step): def runSlapgridUntilSuccess(config, step):
"""Run slapgrid-sr or slapgrid-cp several times, """Run slapgrid-sr or slapgrid-cp several times,
in the maximum of the constant MAX_RUN_~~~~""" in the maximum of the constant MAX_RUN_~~~~"""
params = getBuildAndRunParams(config)
if step == "instance": if step == "instance":
max_tries = (MAX_RUN_INSTANCE if RUN_INSTANCE else 0) max_tries = (params['max_run_instance'] if params['run_instance'] else 0)
runSlapgridWithLock = runInstanceWithLock runSlapgridWithLock = runInstanceWithLock
elif step == "software": elif step == "software":
max_tries = (MAX_RUN_SOFTWARE if RUN_SOFTWARE else 0) max_tries = (params['max_run_software'] if params['run_software'] else 0)
runSlapgridWithLock = runSoftwareWithLock runSlapgridWithLock = runSoftwareWithLock
else: else:
return -1 return -1
...@@ -839,7 +827,7 @@ def runSlapgridUntilSuccess(config, step): ...@@ -839,7 +827,7 @@ def runSlapgridUntilSuccess(config, step):
max_tries -= counter max_tries -= counter
# run instance only if we are deploying the software release, # run instance only if we are deploying the software release,
# if it is defined so, and sr is correctly deployed # if it is defined so, and sr is correctly deployed
if step == "software" and RUN_INSTANCE and slapgrid: if step == "software" and params['run_instance'] and slapgrid:
return (max_tries, runSlapgridUntilSuccess(config, "instance")) return (max_tries, runSlapgridUntilSuccess(config, "instance"))
else: else:
return max_tries return max_tries
......
...@@ -88,7 +88,7 @@ def myAccount(): ...@@ -88,7 +88,7 @@ def myAccount():
account = getSession(app.config) account = getSession(app.config)
return render_template('account.html', username=account[0], return render_template('account.html', username=account[0],
email=account[2], name=account[3].decode('utf-8'), email=account[2], name=account[3].decode('utf-8'),
params=getBuildAndRunParams()) params=getBuildAndRunParams(app.config))
@app.route("/dologout") @app.route("/dologout")
...@@ -550,7 +550,7 @@ def updateBuildAndRun(): ...@@ -550,7 +550,7 @@ def updateBuildAndRun():
params['run_software'] = run_software params['run_software'] = run_software
params['max_run_instance'] = max_run_instance params['max_run_instance'] = max_run_instance
params['max_run_software'] = max_run_software params['max_run_software'] = max_run_software
saveBuildAndRunParams(params) saveBuildAndRunParams(app.config, params)
result = "Your parameters have correctly been updated" result = "Your parameters have correctly been updated"
return jsonify(code=code, result=result) return jsonify(code=code, result=result)
......
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