Commit 96e920c5 authored by Matthieu Lamalle's avatar Matthieu Lamalle
Browse files

Merge branch '426_conf_to_deploy' into 'develop'

426 conf to deploy

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