Commit 1018b59e authored by Nicolas Wavrant's avatar Nicolas Wavrant

runner: opening Software Release supplies a new one, instead of destroying all data

Creates a new view with a more fitting name, replacing an older one.
Also, requesting instance should happen only if there is no current
instance, otherwise it may erase data.
parent 8d4e892b
......@@ -106,7 +106,7 @@ $(document).ready(function () {
}
$.ajax({
type: "POST",
url: $SCRIPT_ROOT + '/setCurrentProject',
url: $SCRIPT_ROOT + '/supplySoftwareRelease',
data: "path=" + $("input#path").val(),
success: function (data) {
if (data.code === 1) {
......
......@@ -33,7 +33,6 @@
<div id="fileTree" class="file_tree"></div>
<div id="file_info" class="file_info">
<img src="{{ url_for('static', filename='images/check.png') }}" class="check" id="check" alt=""/>
<p><font color="red">WARNING:</font> Opening a new software release <font color="red">WILL ERASE YOUR EXISTING INSTANCE</font>. Proceed with caution!</p></div>
<input type="hidden" name="path" id="path" value="" />
<input type="submit" name="open" id ="open" value="Open Software" class="button"/>
</div>
......
......@@ -134,6 +134,21 @@ def getCurrentSoftwareReleaseProfile(config):
except IOError:
return ''
def getCurrentUsedSoftwareReleaseProfile(config):
"""
Returns used Software Release profile as a string.
"""
slap = slapos.slap.slap()
slap.initializeConnection(config['master_url'])
# Try to return the SR related to the current instance
try:
computer_partition = slap.registerComputerPartition(config['computer_id'], 'slappart0')
software_release = computer_partition.getSoftwareRelease()
return software_release.getURI()
# If there are no instances, return the last requested Software Release
except NotFoundError:
return ''
def requestInstance(config, software_type=None):
"""
......@@ -162,7 +177,7 @@ def requestInstance(config, software_type=None):
return slap.registerOpenOrder().request(
profile,
partition_reference=getSoftwareReleaseName(config),
partition_reference='slaprunner', # we want only one instance in webrunner
partition_parameter_kw=partition_parameter_kw,
software_type=software_type,
filter_kw=None,
......@@ -170,6 +185,29 @@ def requestInstance(config, software_type=None):
shared=False)
def supplySoftwareRelease(config, sr_path):
"""
Act like "slapos node supply" by registering a new SR to current slapproxy
"""
folder = realpath(config, sr_path)
if not folder:
return
sup_process.stopProcess(config, 'slapgrid-cp')
sup_process.stopProcess(config, 'slapgrid-sr')
updateProxy(config)
slap = slapos.slap.slap()
slap.initializeConnection(config['master_url'])
slap.registerSupply().supply(
os.path.join(realpath(config, sr_path), config['software_profile']),
computer_guid=config['computer_id'],
)
# Don't replace .project if there is a running instance
if not getCurrentUsedSoftwareReleaseProfile(config):
setCurrentSoftwareRelease(config, sr_path)
def updateProxy(config):
"""
Configure Slapos Node computer and partitions.
......@@ -179,10 +217,8 @@ def updateProxy(config):
if not os.path.exists(config['instance_root']):
os.mkdir(config['instance_root'])
slap = slapos.slap.slap()
profile = getCurrentSoftwareReleaseProfile(config)
slap.initializeConnection(config['master_url'])
slap.registerSupply().supply(profile, computer_guid=config['computer_id'])
computer = slap.registerComputer(config['computer_id'])
prefix = 'slappart'
slap_config = {
......@@ -303,7 +339,7 @@ def runSlapgridWithLock(config, step, process_name, lock=False):
os.remove(log_file)
if not updateProxy(config):
return 1
if step == 'instance' and not requestInstance(config):
if step == 'instance' and not getCurrentUsedSoftwareReleaseProfile(config):
return 1
try:
sup_process.runProcess(config, process_name)
......@@ -567,17 +603,12 @@ def configNewSR(config, projectpath):
Returns:
True if all is done well, otherwise return false.
"""
folder = realpath(config, projectpath)
if folder:
sup_process.stopProcess(config, 'slapgrid-cp')
sup_process.stopProcess(config, 'slapgrid-sr')
logger.warning("User opened a new SR. Removing all instances...")
removeCurrentInstance(config)
with open(os.path.join(config['etc_dir'], ".project"), 'w') as f:
f.write(projectpath)
return True
else:
return False
supplySoftwareRelease(config, projectpath)
def setCurrentSoftwareRelease(config, relative_project_path):
with open(os.path.join(config['etc_dir'], ".project"), 'w') as f:
f.write(relative_project_path)
def newSoftware(folder, config, session):
......
......@@ -16,6 +16,7 @@ from flask import (Flask, request, redirect, url_for, render_template,
import slapos
from slapos.util import bytes2str
import utils
from slapos.runner.utils import (checkSoftwareFolder, configNewSR, checkUserCredential,
createNewUser, getBuildAndRunParams,
getProfilePath, getSlapgridResult,
......@@ -314,11 +315,21 @@ def checkFolder():
def setCurrentProject():
if configNewSR(app.config, request.form['path']):
session['title'] = getProjectTitle(app.config)
return jsonify(code=1, result="")
else:
return jsonify(code=0, result=("Can not setup this Software Release"))
logger.warn('setCurrentProject is deprecated')
return supplySoftwareRelease()
def supplySoftwareRelease():
if request.form['path']:
sr_absolute_path = realpath(app.config, request.form['path'])
sr_absolute_path = sr_absolute_path and os.path.join(sr_absolute_path, 'software.cfg') # XXX: find where this one was added before
if os.path.exists(sr_absolute_path):
utils.supplySoftwareRelease(app.config, request.form['path'])
# request instance if no instance has been requested yet
if not utils.getCurrentUsedSoftwareReleaseProfile(app.config):
utils.requestInstance(app.config)
return jsonify(code=1, result="")
return jsonify(code=0, result=("Can not setup this Software Release"))
def getProjectStatus():
......@@ -868,6 +879,8 @@ app.add_url_rule('/openProject/<method>', 'openProject', openProject,
methods=['GET'])
app.add_url_rule("/setCurrentProject", 'setCurrentProject', setCurrentProject,
methods=['POST'])
app.add_url_rule("/supplySoftwareRelease", 'supplySoftwareRelease', supplySoftwareRelease,
methods=['POST'])
app.add_url_rule("/checkFolder", 'checkFolder', checkFolder, methods=['POST'])
app.add_url_rule('/createSoftware', 'createSoftware', createSoftware,
methods=['POST'])
......
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