Commit 5e8e9dca by Sulyok Gabor

Bugfixes and reworking deployment[WIP]

parent 1f4825c9
......@@ -6,6 +6,7 @@ from django.db import transaction
from saltstackhelper import *
import os
from vm.models import Instance
import logging
class SettyController:
salthelper = SaltStackHelper()
......@@ -113,7 +114,6 @@ class SettyController:
else:
raise PermissionDenied # TODO: something more meaningful
@staticmethod
def getMachineAvailableList(serviceId, usedHostnames, current_user):
saltMinions = SettyController.salthelper.getAllMinionsUngrouped()
......@@ -121,21 +121,23 @@ class SettyController:
savedHostNames = []
for machine in savedMachines:
savedHostNames.append( machine.hostname )
savedHostNames.append(machine.hostname)
userInstances = Instance.objects.filter(owner=current_user)
userInstances = Instance.objects.filter(
owner=current_user, destroyed_at=None)
userMachines = []
for instance in userInstances:
if instance.vm_name:
userMachines.append(instance.vm_name)
usedHostnamesByUser = set( savedHostNames + usedHostnames )
usedHostnamesByUser = set(savedHostNames + usedHostnames)
if not usedHostnamesByUser:
return {'machinedata':userMachines}#{'machinedata': [machineName for machineName in userMachines if machineName in saltMinions] }
return {'machinedata': [machineName for machineName in userMachines if machineName in saltMinions] }
availableInstanceNames = list( set(userMachines) - usedHostnamesByUser )
return {'machinedata': availableInstanceNames}#[ machineName for machineName in availableInstanceNames if machineName in saltMinions ]}
availableInstances = list(set(userMachines) - usedHostnamesByUser)
return {'machinedata': [ machineName for machineName in availableInstances if machineName in saltMinions ]}
@staticmethod
def addMachine(hostname):
......@@ -180,49 +182,57 @@ class SettyController:
if errorMessages:
return {'status': 'error',
'errors': errorMessages }
'errors': errorMessages}
elementConnections = ElementConnection.objects.filter(
Q(target__in=machines) | Q(source__in=machines))
firstLevelServiceNodes = []
# phase one: set the machine ptr in serviceNodes which can be accessed by
# connections from machines
logger = logging.getLogger('project.interesting.stuff')
for machine in machines:
for connection in elementConnections:
serviceNode = None
if connection.target.cast() == machine:
serviceNode = connection.source.cast()
serviceNode.setMachineForDeploy(machine)
elif connection.source.cast() == machine:
serviceNode = connection.target.cast()
serviceNode.setMachineForDeploy(machine)
else:
raise PermissionDenied
firstLevelServiceNodes.append(serviceNode)
# phase two: let the nodes create configurations recursively
configuratedNodes = list()
for serviceNode in firstLevelServiceNodes:
generatedNodes = serviceNode.generateConfigurationRecursively()
if isinstance(generatedNodes, list):
configuratedNodes = configuratedNodes + generatedNodes
else:
configuratedNodes.append(generatedNodes)
for serviceNode in serviveNodeList:
node = serviceNode.cast()
node.generateSaltCommands()
configuratedNodes.append( node )
# phase three: sort the nodes by deployment priority(lower the prio,
# later in the deployement)
configuratedNodes.sort(reverse=True)
return {'status': 'success'}
# deploy the nodes
# dbgCheck = []
# for node in configuratedNodes:
# SettyController.salthelper.deploy(
# node.machine.hostname, node.generatedConfig)
# return {'status': 'deployed'}
# commandDict = []
# for command in node.generatedCommands:
# commandDict.append( command.__dict__ )
# dbgCheck.append({ "nodeName": my_instance.__class__.__name__,
# "commands": commandDict })
# return dbgCheck
# phase four: deploy the nodes
for node in configuratedNodes:
deployErrorMessages = SettyController.salthelper.executeCommand(
node.generatedCommands)
if errorMessages:
errorMessages.append(deployErrorMessages)
# phase five: cleanup generated commands
for serviceNode in firstLevelServiceNodes:
serviceNode.generatedCommands = None
# cleanup the temporary data
''' for node in configuratedNodes:
node.deployCleanUp()'''
if errorMessages:
return {'status': 'error',
'errors': errorMessages}
return {'status': 'success'}
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('setty', '0025_AddDatabases'),
]
operations = [
migrations.RemoveField(
model_name='wordpressnode',
name='databaseListeningPort',
),
migrations.AddField(
model_name='wordpressnode',
name='databaseName',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='adminEmail',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='adminPassword',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='adminUsername',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='databaseHost',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='databasePass',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='databaseUser',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='siteTitle',
field=models.TextField(default=b''),
),
migrations.AlterField(
model_name='wordpressnode',
name='siteUrl',
field=models.TextField(default=b''),
),
]
......@@ -3,8 +3,16 @@ import salt.config
import salt.runner
import salt.client
class SaltCommand:
def __init__(self):
self.hostname = ""
self.command = ""
self.parameters = ""
# For debugging purposes only
def __str__(self):
return "Command: " + self.hostname + " - " + self.command + " - " + str(self.parameters)
SALTSTACK_STATE_FOLDER = "/srv/salt"
class SaltStackHelper:
def __init__(self):
self.master_opts = salt.config.client_config('/etc/salt/master')
......@@ -38,9 +46,7 @@ class SaltStackHelper:
def checkMinionExists(self, hostname):
query_res = self.salt_localclient.cmd( hostname,'network.get_hostname' );
print query_res
return query_res != {}
def deploy(self, hostname, configFilePath ):
print configFilePath
self.salt_localclient.cmd(hostname, 'state.apply', [configFilePath.split('.')[0]] )
\ No newline at end of file
def executeCommand(self, saltCommand):
return self.salt_localclient.cmd(saltCommand.hostname, "state.sls",[saltCommand.command],kwarg={"pillar":saltCommand.parameters} )
......@@ -501,7 +501,7 @@ jsPlumb.ready(function() {
$.post("", {
event: "addServiceNode",
data: JSON.stringify({
"elementTemplateId": $(this).attr("id") })
"elementTemplateId": $(elementTemplate).attr("id") })
}, function(result) {
addElement($(elementTemplate).attr("id"),
(++elementIndex) + "_" + $(elementTemplate).attr("id"),
......@@ -791,10 +791,15 @@ jsPlumb.ready(function() {
/* Registering events concerning persistence. */
$('body').on('click', '#deployService',function() {
if($("#serviceStatus").text() == "Unsaved") {
alert("Only saved services can be deployed");
return;
}
$.post("", {
event: "deploy",
}, function(result) {
if ( result.error )
if ( result.status )
alert( result.errors );
else
alert("Deploying....");
......
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 sign in to comment