#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import datetime from autobahn.wamp.exception import ApplicationError from zephir.i18n import _ from zephir.controller import ZephirCommonController, run from zephir.http import register as register_http from zephir.wamp import register as register_wamp from server.server.lib import Server from server.server.error import (ServerError, ServerErrorDatabaseNotAvailable, ServerErrorPeeringConfNotAvailable, ServerErrorDbConnection, ServerErrorInvalidServerId, ServerErrorInvalidServerModelId, ServerErrorServerNameNotProvided, ServerErrorUnknownServerId, ServerErrorUnknownServerModelId) from server.serverselection.lib import ServerSelection from server.serverselection.error import (ServerSelectionError, ServerSelectionErrorDatabaseNotAvailable, ServerSelectionErrorDbConnection, ServerSelectionErrorInvalidServerSelectionId, ServerSelectionErrorServerSelectionNameNotProvided, ServerSelectionErrorUnknownServerSelectionId, ServerSelectionErrorDuplicateServerSelectionName, ServerSelectionEmptyRecordDatabaseError) from zephir.config import ServiceConfig class ServerRunner(ZephirCommonController): """Server controller """ def __init__(self, *args, **kwargs): # Create instance of working code super().__init__(*args, **kwargs) self.server = Server() self.serverselection = ServerSelection() self.conn = None # GET ROLE def get_profil_for_server(self, cursor, uri, message_arguments): 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': return 'root' else: profils = self._get_serverselection_user_server_role(cursor, server_id, message_arguments['_session_user']['username']) for profil in profils: if 'role' in profil: role = profil['role'] if self.policy.enforce(role, uri, 'allowed'): return role return None def get_profil_for_serverselection(self, cursor, uri, message_arguments): serverselection_id = message_arguments['serverselectionid'] if '_session_user' not in message_arguments or message_arguments['_session_user']['profil'] == 'root': return 'root' else: if message_arguments['_session_user']['profil'] == 'root': role = message_arguments['_session_user']['profil'] else: profil = self._get_serverselection_user_role(cursor, serverselection_id, message_arguments['_session_user']['username']) if not 'role' in profil: return None role = profil['role'] if self.policy.enforce(role, uri, 'allowed'): return role return None def get_profil_for_all(self, cursor, uri, message_arguments): serverselection_role = self.get_profil_for_serverselection(cursor, uri, message_arguments) if serverselection_role is None : return None server_role = self.get_profil_for_server(cursor, uri, message_arguments) if server_role is None : return None else: return server_role @register_wamp('v1.server.list', notification_uri=None, database=True) async def list_servers(self, cursor, _session_user): try: if _session_user['profil'] == 'root': return self.server.list_servers(cursor) else: ret = [] for server_id in self._list_user_servers(cursor, _session_user).get('serverselectionserversid', []): ret.append(self._describe_server(cursor, server_id, False, False)) return ret except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerError as err: 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): return self._describe_server(cursor, serverid, configuration, True) def _describe_server(self, cursor, serverid, configuration, environment): try: server = self.server.describe_server(cursor, serverid, environment) if configuration: server['configuration'] = serverid return server except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerErrorInvalidServerId as err: raise ApplicationError('server.error.invalid-server-id', reason=str(err)) except ServerErrorUnknownServerId as err: raise ApplicationError('server.error.unknown-server-id', reason=str(err)) except ServerError as err: raise ApplicationError('server.error', reason=str(err)) @register_http('v1.server.describe', param='configuration', database=True) async def describe_configuration(self, cursor, secret): values = self.server.fetch_configuration(cursor, secret) if values: return json.dumps(values).encode() else: return b'{}' #FIXME a supprimer, cf server.describe avec configuration=True @register_wamp('v1.server.config.get', notification_uri=None, database=True) async def get_config(self, cursor, serverid): return {'configuration': serverid} @register_http('v1.server.config.get', param='configuration', database=True) async def get_config_file(self, cursor, secret): values = self.server.fetch_configuration(cursor, secret) if values: return json.dumps(values).encode() else: return b'{}' #FIXME END @register_wamp('v1.server.create', notification_uri='v1.server.created', database=True) async def create_server(self, cursor, _session_user, servername, serverdescription, servermodelid, serverpassphrase): try: result = self.server.create_server(cursor, servername, serverdescription, servermodelid) return_code = await self.call('v1.vault.secret.set', secretkey="{}_passphrase".format(result['serverid']), secret={"passphrase" : serverpassphrase}) if return_code: defaultserverselection = self._default_user_serverselection(cursor, _session_user) self.serverselection.add_server_to_selection(cursor, result['serverid'], defaultserverselection['serverselectionid']) return result else: raise ServerError('put passphrase return code status not available') except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerErrorInvalidServerModelId as err: raise ApplicationError('server.error.invalid-servermodel-id', reason=str(err)) except ServerErrorUnknownServerModelId as err: raise ApplicationError('server.error.unknown-servermodel-id', reason=str(err)) except ServerErrorServerNameNotProvided as err: raise ApplicationError('server.error.servername-not-provided', reason=str(err)) except ServerError as 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') async def update_server(self, cursor, _session_user, serverid, servername, serverdescription): try: return self.server.update_server(cursor, serverid, servername, serverdescription) except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerErrorInvalidServerId as err: raise ApplicationError('server.error.invalid-server-id', reason=str(err)) except ServerErrorUnknownServerId as err: raise ApplicationError('server.error.unknown-server-id', reason=str(err)) except ServerErrorServerNameNotProvided as err: raise ApplicationError('server.error.servername-not-provided', reason=str(err)) except ServerError as 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') async def delete_server(self, cursor,_session_user, serverid): try: self.serverselection.remove_server_from_all_selections(cursor, serverid) return self.server.delete_server(cursor, serverid) except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerErrorInvalidServerId as err: raise ApplicationError('server.error.invalid-server-id', reason=str(err)) except ServerErrorUnknownServerId as err: raise ApplicationError('server.error.unknown-server-id', reason=str(err)) except ServerError as 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') async def get_peering_conf(self, cursor, _session_user, serverid): try: secret = await self.call('v1.vault.secret.get', secretkey="{}_peeringconf".format(serverid)) return secret['secret'] except ServerErrorPeeringConfNotAvailable as err: raise ApplicationError('server.error.peering-conf-not-available', reason=str(err)) 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) 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} @register_wamp('v1.execution.salt.peer.registered', notification_uri='v1.server.salt.registered', database=True) async def salt_register(self, cursor, serverid, automation): try: self.server.register_server_for_automation(cursor, serverid, automation) return {'serverid': serverid} except Exception as err: raise ApplicationError('server.registering.error', 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): return await self._exec_cmd_on_server(cursor, server_id, command) async def _exec_cmd_on_server(self, cursor, server_id, command): """ Transfer command transmitted to automation (salt, ...) """ automation, automation_command = self.server.get_automation_command(cursor, server_id) if automation == 'salt': result = await self.call('v1.execution.salt.exec', minion_pattern=str(server_id), command=automation_command, arg=command, client_mode='local_async') else: raise Exception(_('Automation engine not supported: {}').format(automation)) if result['minions'] != [str(server_id)]: raise Exception(_('Job ({}) not executed only in selected server, all client affected : {}').format(result['jid'], result['minions'])) return {'job_id': result['jid'], 'server_id': server_id, 'command': command, 'automation': automation, 'executed': False} def result_to_dict(self, result, automation): dico = {'job_id': result['jid'], 'server_id': int(result['minion']), 'automation': automation, 'executed': result['executed']} if result['command'] == 'deploy': dico['command'] = 'v1.server.exec.deploy' else: dico['command'] = result['arg'] if dico['executed']: if 'success' in result : dico['success'] = result['success'] dico['retcode'] = result['retcode'] dico['return'] = result['return'] 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): automation, automation_command = self.server.get_automation_command(cursor, server_id) if automation == 'salt': results = await self.call('v1.execution.salt.job.list', minion_pattern=str(server_id)) else: raise Exception(_('Automation engine not supported: {}').format(automation)) ret = [] for result in results: ret.append(self.result_to_dict(result, automation)) 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_on_server(self, cursor, _session_user, 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)) else: raise Exception(_('Automation engine not supported: {}').format(automation)) dico = {'job_id': result['jid'], 'command': 'v1.server.exec.deploy', 'automation': automation, 'server_id': server_id, 'executed': False} return dico @register_wamp('v1.server.exec.describe', notification_uri=None) async def exec_describe(self, job_id, automation): if automation == 'salt': results = await self.call('v1.execution.salt.job.describe', jid=str(job_id)) else: raise Exception(_('Automation engine not supported: {}').format(automation)) ret = [] for result in results: ret.append(self.result_to_dict(result, automation)) return ret @register_wamp('v1.config.configuration.server.updated', None, database=True) async def update_configuration(self, cursor, server_id): try: configuration = await self.call('v1.config.configuration.server.get', server_id=server_id) except: print(f'No configuration available for server {server_id}') return self.server.update_configuration(cursor, server_id, configuration['configuration']) @register_wamp('v1.server.peer-connection.update', notification_uri=None, database=True) async def update_peerconnection(self, cursor, serverid): try: lastpeerconnection = datetime.datetime.now() return self.server.update_peerconnection(cursor, serverid, lastpeerconnection) except ServerErrorDatabaseNotAvailable as err: raise ApplicationError('server.error.database-not-available', reason=str(err)) except ServerErrorDbConnection as err: raise ApplicationError('server.error.db-connection', reason=str(err)) except ServerErrorInvalidServerId as err: raise ApplicationError('server.error.invalid-server-id', reason=str(err)) except ServerErrorUnknownServerId as err: raise ApplicationError('server.error.unknown-server-id', reason=str(err)) except ServerErrorServerNameNotProvided as err: raise ApplicationError('server.error.servername-not-provided', reason=str(err)) except ServerError as err: raise ApplicationError('server.error', reason=str(err)) def _list_user_servers(self, cursor, _session_user): try: username = _session_user['username'] return self.serverselection.list_user_servers(cursor, username) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.list', notification_uri=None, database=True) async def list_serverselections(self, cursor, _session_user): try: if _session_user['profil'] == 'root': return self.serverselection.list_serverselections(cursor) else: return self._list_user_serverselections(cursor, _session_user) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.list', notification_uri=None, database=True) async def list_user_serverselections(self, cursor, _session_user): return self._list_user_serverselections(cursor, _session_user) def _list_user_serverselections(self, cursor, _session_user): try: username = _session_user['username'] return self.serverselection.list_user_serverselections(cursor, username) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.describe', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def describe_serverselection(self, cursor, _session_user, serverselectionid): return self._describe_serverselection(cursor, _session_user, serverselectionid) def _describe_serverselection(self, cursor, _session_user, serverselectionid): try: return self.serverselection.describe_serverselection(cursor, serverselectionid) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_server_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_server_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.default', notification_uri=None, database=True) async def default_user_serverselection(self, cursor, _session_user): return self._default_user_serverselection(cursor, _session_user) def _default_user_serverselection(self, cursor, _session_user): try: username = _session_user['username'] return self.serverselection.default_user_serverselection(cursor, username) except ServerSelectionEmptyRecordDatabaseError as err: serverselectionname = username + "_DEFAULT" serverselectiondescription = username + " server selection" self._create_default_user_serverselection(cursor, serverselectionname, serverselectiondescription, username) return self.serverselection.default_user_serverselection(cursor, username) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) def _create_default_user_serverselection(self, cursor, serverselectionname, serverselectiondescription, username): try: return self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, username) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database-not-available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorDuplicateServerSelectionName as err: raise ApplicationError('serverselection.static.create.error.duplicate_serverselection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.create', notification_uri=None, database=True) async def create_serverselection(self, cursor, serverselectionname, serverselectiondescription, _session_user): try: if _session_user['profil'] in ['root', 'admin'] or ('_DEFAULT' not in serverselectionname and _session_user['profil'] not in ['root', 'admin']): username = _session_user['username'] return self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, username) else: raise Exception(_('Unable to create _DEFAULT serverselection')) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database-not-available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorDuplicateServerSelectionName as err: raise ApplicationError('serverselection.static.create.error.duplicate_serverselection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.update', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def update_serverselection(self, cursor,_session_user, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete): try: if '_DEFAULT' not in serverselectionname: return self.serverselection.update_serverselection(cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete) else: raise Exception(_('Unable to update _DEFAULT serverselection')) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database-not-available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.delete', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def delete_serverselection(self, cursor,_session_user, serverselectionid): try: return self.serverselection.delete_serverselection(cursor, serverselectionid) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.server.add', notification_uri=None, database=True, profil_adapter='get_profil_for_all') async def add_server_to_selection(self, cursor, _session_user, serverid, serverselectionid): try: return self.serverselection.add_server_to_selection(cursor, serverid, serverselectionid) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database-not-available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.server.remove', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def remove_server_from_selection(self, cursor,_session_user, serverid, serverselectionid): try: return self.serverselection.remove_server_from_selection(cursor, serverid, serverselectionid) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.add', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def add_user_to_serverselection(self, cursor,_session_user, serverselectionid, username, role): try: return self.serverselection.add_user_to_serverselection(cursor, serverselectionid, username, role) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.remove', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def remove_user_from_serverselection(self, cursor, _session_user, serverselectionid, username): try: if self._get_serverselection_user_role(cursor, serverselectionid, username)['role'] != 'owner': return self.serverselection.remove_user_from_serverselection(cursor, serverselectionid, username) else: raise Exception(_('Can not remove user with role owner from serverseleciton')) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.update', notification_uri=None, database=True, profil_adapter='get_profil_for_serverselection') async def update_user_to_serverselection(self, cursor,_session_user, serverselectionid, username, role): try: return self.serverselection.update_user_to_serverselection(cursor, serverselectionid, username, role) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.role.get', notification_uri=None, database=True) async def get_serverselection_user_role(self, cursor, _session_user, serverselectionid, username): return self._get_serverselection_user_role(cursor, serverselectionid, _session_user['username']) def _get_serverselection_user_role(self, cursor, serverselectionid, username): try: return self.serverselection.get_serverselection_user_role(cursor, serverselectionid, username) except ServerSelectionEmptyRecordDatabaseError as err: return {} except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.user.role.server.get', notification_uri=None, database=True) async def get_serverselection_user_server_role(self, cursor, _session_user, serverid, username): return self._get_serverselection_user_server_role(cursor, serverid, _session_user['username']) def _get_serverselection_user_server_role(self, cursor, serverid, username): try: return self.serverselection.get_serverselection_user_server_role(cursor, serverid, username) except ServerSelectionErrorDatabaseNotAvailable as err: raise ApplicationError('serverselection.error.database_not_available', reason=str(err)) except ServerSelectionErrorDbConnection as err: raise ApplicationError('serverselection.error.db-connection', reason=str(err)) except ServerSelectionErrorInvalidServerSelectionId as err: raise ApplicationError('serverselection.error.invalid_serverselection_id', reason=str(err)) except ServerSelectionErrorUnknownServerSelectionId as err: raise ApplicationError('serverselection.error.unknown_serverselection_id', reason=str(err)) except ServerSelectionError as err: raise ApplicationError('serverselection.error', reason=str(err)) @register_wamp('v1.serverselection.exec.command', notification_uri=None, database=True) #FIXME notification async def exec_cmd_on_serverserverselection(self, cursor, serverselection_id, command): """ Transfer command transmitted to automation (salt, ...) """ servers = self.serverselection.describe_serverselection(cursor, serverselection_id)['serverselectionserversid'] ret = [] for server_id in servers: try: ret.append(await self._exec_cmd_on_server(cursor, server_id, command)) except ServerErrorUnknownServerId as err: ret.append({'job_id': '', 'server_id': server_id, 'command': command, 'automation': 'salt', 'executed': True, 'success': False, 'retcode': 1, 'return': str(err)}) return ret @register_wamp('v1.serverselection.exec.deploy', notification_uri=None, database=True) #FIXME notification async def exec_deploy_on_serverserverselection(self, cursor, serverselection_id): """ Transfer command transmitted to automation (salt, ...) """ servers = self.serverselection.describe_serverselection(cursor, serverselection_id)['serverselectionserversid'] ret = [] for server_id in servers: try: ret.append(await self._exec_deploy_on_server(cursor, server_id)) except ServerErrorUnknownServerId as err: ret.append({'job_id': '', 'server_id': server_id, 'command': 'deploy', 'automation': 'salt', 'executed': False}) return ret if __name__ == '__main__': run(ServerRunner)