Commit 5e8e9dca by Sulyok Gabor

Bugfixes and reworking deployment[WIP]

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