Commit c309f39a authored by Bruno Boiget's avatar Bruno Boiget Committed by Matthieu Lamalle

426 conf to deploy

parent e7356d1b
......@@ -12,6 +12,7 @@ CREATE TABLE Server (
ZoneId INTEGER,
MachineId INTEGER,
ServerConfiguration JSON,
ServerConfigurationTD JSON,
Automation VARCHAR(25),
ServerEnvironment JSON,
LastPeerConnection TIMESTAMP DEFAULT NULL
......
......@@ -29,7 +29,7 @@ from server.serverselection.error import (ServerSelectionError,
ServerSelectionEmptyRecordDatabaseError)
from zephir.config import ServiceConfig
from zephir.config import ServiceConfig, DEBUG
class ServerRunner(ZephirCommonController):
......@@ -44,16 +44,16 @@ class ServerRunner(ZephirCommonController):
self.conn = None
# GET ROLE
def get_profil_for_server(self, cursor, uri, message_arguments):
def get_profil_for_server(self, cursor, uri, message_arguments, origin_kwargs):
if 'serverid' in message_arguments:
server_id = message_arguments['serverid']
if 'server_id' in message_arguments:
server_id = message_arguments['server_id']
if '_session_user' not in message_arguments or message_arguments['_session_user']['profil'] == 'root':
if '_session_user' not in origin_kwargs or origin_kwargs['_session_user']['profil'] == 'root':
return 'root'
else:
profils = self._get_serverselection_user_server_role(cursor, server_id, message_arguments['_session_user']['username'])
profils = self._get_serverselection_user_server_role(cursor, server_id, origin_kwargs['_session_user']['username'])
for profil in profils:
if 'role' in profil:
role = profil['role']
......@@ -62,15 +62,15 @@ class ServerRunner(ZephirCommonController):
return None
def get_profil_for_serverselection(self, cursor, uri, message_arguments):
def get_profil_for_serverselection(self, cursor, uri, message_arguments, origin_kwargs):
serverselection_id = message_arguments['serverselectionid']
if '_session_user' not in message_arguments or message_arguments['_session_user']['profil'] == 'root':
if '_session_user' not in origin_kwargs or origin_kwargs['_session_user']['profil'] == 'root':
return 'root'
else:
if message_arguments['_session_user']['profil'] == 'root':
role = message_arguments['_session_user']['profil']
if self.origin_kwargs['_session_user']['profil'] == 'root':
role = self.origin_kwargs['_session_user']['profil']
else:
profil = self._get_serverselection_user_role(cursor, serverselection_id, message_arguments['_session_user']['username'])
profil = self._get_serverselection_user_role(cursor, serverselection_id, origin_kwargs['_session_user']['username'])
if not 'role' in profil:
return None
role = profil['role']
......@@ -78,7 +78,7 @@ class ServerRunner(ZephirCommonController):
return role
return None
def get_profil_for_all(self, cursor, uri, message_arguments):
def get_profil_for_all(self, cursor, uri, message_arguments, origin_kwargs):
serverselection_role = self.get_profil_for_serverselection(cursor, uri, message_arguments)
if serverselection_role is None :
return None
......@@ -89,14 +89,14 @@ class ServerRunner(ZephirCommonController):
else:
return server_role
def get_profil_for_servers_list(self, cursor, uri, message_arguments):
def get_profil_for_servers_list(self, cursor, uri, message_arguments, origin_kwargs):
serveridList = message_arguments['serveridlist'].split(',')
if '_session_user' not in message_arguments or message_arguments['_session_user']['profil'] == 'root':
if '_session_user' not in origin_kwargs or origin_kwargs['_session_user']['profil'] == 'root':
return 'root'
else:
role=None
for server_id in serveridList :
profils = self._get_serverselection_user_server_role(cursor, server_id, message_arguments['_session_user']['username'])
profils = self._get_serverselection_user_server_role(cursor, server_id, origin_kwargs['_session_user']['username'])
if profils == []:
return None
for profil in profils:
......@@ -128,7 +128,7 @@ class ServerRunner(ZephirCommonController):
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.server.describe', notification_uri=None, database=True, profil_adapter='get_profil_for_server')
async def describe_server(self, cursor,_session_user, serverid, configuration):
async def describe_server(self, cursor, serverid, configuration):
return self._describe_server(cursor, serverid, configuration, True)
def _describe_server(self, cursor, serverid, configuration, environment):
......@@ -200,7 +200,7 @@ class ServerRunner(ZephirCommonController):
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.server.update', notification_uri='v1.server.updated', database=True, profil_adapter='get_profil_for_server')
async def update_server(self, cursor, _session_user, serverid, servername, serverdescription):
async def update_server(self, cursor, serverid, servername, serverdescription):
try:
return self.server.update_server(cursor, serverid, servername, serverdescription)
......@@ -218,7 +218,7 @@ class ServerRunner(ZephirCommonController):
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.server.delete', notification_uri='v1.server.deleted', database=True, profil_adapter='get_profil_for_server')
async def delete_server(self, cursor,_session_user, serverid):
async def delete_server(self, cursor, serverid):
try:
self.serverselection.remove_server_from_all_selections(cursor, serverid)
return self.server.delete_server(cursor, serverid)
......@@ -235,7 +235,7 @@ class ServerRunner(ZephirCommonController):
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.server.peering-conf.get', notification_uri='v1.server.peering-conf.sent', database=True, profil_adapter='get_profil_for_server')
async def get_peering_conf(self, cursor, _session_user, serverid):
async def get_peering_conf(self, cursor, serverid):
try:
secret = await self.call('v1.vault.secret.get', secretkey="{}_peeringconf".format(serverid))
return secret['secret']
......@@ -245,16 +245,17 @@ class ServerRunner(ZephirCommonController):
except ServerError as err:
raise ApplicationError('server.error', reason=str(err))
@register_wamp('v1.execution.salt.master.event.ready', notification_uri='v1.server.environment.updated', database=True)
@register_wamp('v1.execution.salt.master.event.ready', None, database=True)
async def minion_ready(self,
cursor,
server_id):
environ = await self.call('v1.execution.salt.environment.get',
server_id=server_id)
self.server.update_environment(cursor,
server_id,
json.dumps(environ))
return {'server_id': server_id}
if self.server.check_environment_modified(cursor, server_id, environ):
self.server.update_environment(cursor,
server_id,
json.dumps(environ))
self.publish('v1.server.environment.updated', server_id=server_id)
@register_wamp('v1.execution.salt.peer.registered', notification_uri='v1.server.salt.registered', database=True)
async def salt_register(self, cursor, serverid, automation):
......@@ -266,7 +267,7 @@ class ServerRunner(ZephirCommonController):
reason=str(err))
@register_wamp('v1.server.exec.command', notification_uri='v1.server.executed', database=True, profil_adapter='get_profil_for_server')
async def exec_cmd_on_server(self, cursor, _session_user, server_id, command):
async def exec_cmd_on_server(self, cursor, server_id, command):
return await self._exec_cmd_on_server(cursor, server_id, command)
async def _exec_cmd_on_server(self, cursor, server_id, command):
......@@ -308,7 +309,7 @@ class ServerRunner(ZephirCommonController):
return dico
@register_wamp('v1.server.exec.list', notification_uri=None, database=True, profil_adapter='get_profil_for_server')
async def exec_job_on_server(self, cursor, _session_user, server_id):
async def exec_job_on_server(self, cursor, server_id):
automation, automation_command = self.server.get_automation_command(cursor, server_id)
if automation == 'salt':
results = await self.call('v1.execution.salt.job.list',
......@@ -321,15 +322,14 @@ class ServerRunner(ZephirCommonController):
return ret
@register_wamp('v1.server.exec.deploy', notification_uri=None, database=True)
async def exec_deploy(self, cursor, _session_user, server_id):
return await self._exec_deploy_on_server(cursor, _session_user, server_id)
async def exec_deploy(self, cursor, server_id):
return await self._exec_deploy_on_server(cursor, server_id)
async def _exec_deploy_on_server(self, cursor, _session_user, server_id):
async def _exec_deploy_on_server(self, cursor, server_id):
self.publish('v1.config.configuration.server.deploy', server_id=server_id)
automation, automation_command = self.server.get_automation_command(cursor, server_id)
if automation == 'salt':
result = await self.call('v1.execution.salt.configuration.deploy',
_session_user=_session_user,
minion_pattern=str(server_id))
result = await self.call('v1.execution.salt.configuration.deploy', minion_pattern=str(server_id))
else:
raise Exception(_('Automation engine not supported: {}').format(automation))
dico = {'job_id': result['jid'],
......@@ -353,14 +353,18 @@ class ServerRunner(ZephirCommonController):
return ret
@register_wamp('v1.config.configuration.server.updated', None, database=True)
async def update_configuration(self, cursor, server_id):
async def update_configuration(self, cursor, server_id, deploy):
try:
configuration = await self.call('v1.config.configuration.server.get', server_id=server_id)
configuration = await self.call('v1.config.configuration.server.get', server_id=server_id, deploy=deploy)
except:
print(f'No configuration available for server {server_id}')
if DEBUG:
if deploy:
print(f'No configuration available for server {server_id}')
else:
print(f'No undeployed configuration available for server {server_id}')
return
self.server.update_configuration(cursor, server_id, configuration['configuration'])
self.server.update_configuration(cursor, server_id, configuration['configuration'], deploy=deploy)
self.publish('v1.server.configuration.updated', server_id=server_id)
@register_wamp('v1.server.peer-connection.update', notification_uri=None, database=True)
async def update_peerconnection(self, cursor, serverid):
......
......@@ -89,8 +89,12 @@ class Server():
def erase_server(self, cursor):
erase_server(cursor)
def update_configuration(self, cursor, serverid, configuration):
return update_configuration(cursor, serverid, configuration) == 'UPDATE 1'
def update_configuration(self, cursor, serverid, configuration, deploy):
return update_configuration(cursor, serverid, configuration, deploy) == 'UPDATE 1'
def check_environment_modified(self, cursor, serverid, environment):
server = self.describe_server(cursor, serverid, environment=True)
return server['serverenvironment'] != environment
def update_environment(self, cursor, serverid, environment):
return update_environment(cursor, serverid, environment) == 'UPDATE 1'
......
......@@ -82,6 +82,11 @@ SERVER_UPDATE_CONFIGURATION = '''
SET serverconfiguration = %s
WHERE serverid = %s
'''
SERVER_UPDATE_CONFIGURATION_TD = '''
UPDATE server
SET serverconfigurationtd = %s
WHERE serverid = %s
'''
"""
Get configuration content for given server
......@@ -188,8 +193,11 @@ def update_environment(cursor, serverid, serverenvironment):
return cursor.statusmessage
def update_configuration(cursor, serverid, serverconfiguration):
cursor.execute(SERVER_UPDATE_CONFIGURATION, (serverconfiguration, serverid))
def update_configuration(cursor, serverid, serverconfiguration, deploy):
if deploy:
cursor.execute(SERVER_UPDATE_CONFIGURATION, (serverconfiguration, serverid))
else:
cursor.execute(SERVER_UPDATE_CONFIGURATION_TD, (serverconfiguration, serverid))
return cursor.statusmessage
......
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