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