Commit 1dc7236d authored by Lionel Morin's avatar Lionel Morin

Merge branch 'develop' into 'updated_list'

# Conflicts:
#   scripts/controller
parents 6917c852 59ae4aaa
Pipeline #864 passed with stage
in 0 seconds
......@@ -15,7 +15,6 @@ p, admin, v1.serverselection.user.list, allowed
p, manager, v1.server.exec.deploy, allowed
p, manager, v1.server.exec.command, allowed
p, manager, v1.server.peering-conf.get, allowed
p, manager, v1.server.exec.list, allowed
p, manager, v1.server.exec.describe, allowed
......@@ -24,7 +23,6 @@ p, manager, v1.serverselection.exec.command, allowed
p, viewer, v1.server.list, allowed
p, viewer, v1.server.describe, allowed
p, viewer, v1.server.config.get, allowed
p, viewer, v1.server.serverselection.list, allowed
p, viewer, v1.serverselection.list, allowed
......
......@@ -86,25 +86,6 @@ class ServerRunner(ZephirCommonController):
else:
return server_role
def get_profil_for_servers_list(self, cursor, uri, message_arguments, origin_kwargs):
serveridList = message_arguments['serveridlist'].split(',')
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, origin_kwargs['_session_user']['username'])
if profils == []:
return None
for profil in profils:
if 'role' in profil:
role = profil['role']
if not self.policy.enforce(role, uri, 'allowed'):
return None
else:
return None
return role
@register_wamp('v1.server.list', notification_uri=None, database=True)
async def list_servers(self, cursor, _session_user):
try:
......@@ -113,7 +94,7 @@ class ServerRunner(ZephirCommonController):
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))
ret.append(await self._describe_server(cursor, server_id, False, False, False ))
return ret
except ServerErrorDatabaseNotAvailable as err:
raise ApplicationError('server.error.database-not-available', reason=str(err))
......@@ -125,14 +106,17 @@ 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, serverid, configuration):
return self._describe_server(cursor, serverid, configuration, True)
async def describe_server(self, cursor, serverid, configuration, environment, peering):
return await self._describe_server(cursor, serverid, configuration, environment, peering)
def _describe_server(self, cursor, serverid, configuration, environment):
async def _describe_server(self, cursor, serverid, configuration, environment, peering):
try:
server = self.server.describe_server(cursor, serverid, environment)
if configuration:
server['configuration'] = serverid
if peering:
server['peering'] = await self._get_peering_conf(cursor, serverid)
return server
except ServerErrorDatabaseNotAvailable as err:
raise ApplicationError('server.error.database-not-available', reason=str(err))
......@@ -153,24 +137,12 @@ class ServerRunner(ZephirCommonController):
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:
await self.call('v1.servermodel.describe', servermodelid=servermodelid)
result = self.server.create_server(cursor, servername, serverdescription, servermodelid)
return_code = await self.call('v1.vault.secret.set',
......@@ -231,8 +203,7 @@ class ServerRunner(ZephirCommonController):
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, 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']
......@@ -271,7 +242,7 @@ class ServerRunner(ZephirCommonController):
"""
Transfer command transmitted to automation (salt, ...)
"""
automation, automation_command = self.server.get_automation_command(cursor, server_id)
automation, automation_command = self.server.get_automation_command(cursor, server_id, if_peer=True)
if automation == 'salt':
result = await self.call('v1.execution.salt.exec',
minion_pattern=str(server_id),
......@@ -308,7 +279,7 @@ class ServerRunner(ZephirCommonController):
@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, 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_peer=True)
if automation == 'salt':
results = await self.call('v1.execution.salt.job.list',
minion_pattern=str(server_id))
......@@ -325,7 +296,7 @@ class ServerRunner(ZephirCommonController):
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_td_conf=True)
if automation == 'salt':
result = await self.call('v1.execution.salt.configuration.deploy', minion_pattern=str(server_id))
else:
......@@ -432,10 +403,6 @@ class ServerRunner(ZephirCommonController):
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']
......@@ -584,22 +551,6 @@ class ServerRunner(ZephirCommonController):
except ServerSelectionError as err:
raise ApplicationError('serverselection.error', reason=str(err))
@register_wamp('v1.serverselection.server.set', notification_uri=None, database=True, profil_adapter='get_profil_for_servers_list')
async def set_serverselection_server_list(self, cursor,_session_user, serverselectionid, serveridlist):
try:
return self.serverselection.set_serverselection_server_list(cursor, serverselectionid, serveridlist)
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:
......
......@@ -73,17 +73,23 @@ class Server():
"""
return set_automation_value(cursor, serverid, automation) == 'UPDATE 1'
def get_automation_command(self, cursor, serverid):
def get_automation_command(self, cursor, serverid, if_peer=False, if_td_conf=False):
"""
Get automation engine associated with serverid and return
command suitable for this engine.
"""
server = fetch_server(cursor, serverid)
server = fetch_server(cursor, serverid, with_td_conf=if_td_conf)
automation = server.get('automation')
if automation == 'salt':
if if_peer and not server['lastpeerconnection']:
raise ServerError('Server without peer connection')
if if_td_conf and not server['serverconfigurationtd']:
raise ServerError('Server without configuration')
if not automation:
raise ServerError('Automation engine not available, please try later')
elif automation == 'salt':
command = 'cmd.run'
else:
raise ServerError('Automation engine not supported or not available')
raise ServerError('Automation engine "{}" not supported'.format(automation))
return automation, command
def erase_server(self, cursor):
......
......@@ -22,7 +22,15 @@ FETCH_SERVER_ENV = '''
Fetch one server based on its ID
"""
FETCH_SERVER = '''
SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, serverenvironment, lastpeerconnection
SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, lastpeerconnection
FROM server
WHERE serverid = %s
'''
"""
Fetch one server based on its ID with configuration
"""
FETCH_SERVER_TD_CONF = '''
SELECT serverid, servername, serverdescription, servermodelid, zoneid, machineid, automation, lastpeerconnection, serverconfigurationtd
FROM server
WHERE serverid = %s
'''
......@@ -173,7 +181,10 @@ def fetch_server_dict(cursor, serverid: int, environment: bool):
return server_row_to_dict(server, serverenvironment=environment)
def fetch_server(cursor, serverid: int):
def fetch_server(cursor, serverid: int, with_td_conf: bool=False):
if with_td_conf:
fetched = fetchone(cursor, FETCH_SERVER_TD_CONF, (serverid,))
else:
fetched = fetchone(cursor, FETCH_SERVER, (serverid,))
if fetched is None:
raise ServerErrorUnknownServerId(_('unable to find a server with ID {}').format(serverid))
......
......@@ -17,7 +17,6 @@ from .query import (list_all_serverselections,
fetch_serverselection_user_role,
fetch_serverselection_user_server_role,
erase_serverselection,
set_list_server_serverselection,
list_server_serverselections
)
......@@ -122,15 +121,6 @@ class ServerSelection():
"""
return remove_server_from_all_serverselections(cursor, serverid)
def set_serverselection_server_list(self, cursor, serverselectionid, serveridlist):
"""Set the list of servers in a serverselection
:param int serverselectionid: serverselection identifier
:param string serveridlist: servers list
:return bool: True for addition success, False either
"""
return set_list_server_serverselection(cursor, serverselectionid, serveridlist)
def add_user_to_serverselection(self, cursor, serverselectionid, username, role):
"""Add a user to a serverselection
......
......@@ -99,18 +99,6 @@ SERVERSELECTION_REMOVE_SERVER_FROM_ALL = '''
RETURNING *
'''
"""
Set a servers list in a serverselection
"""
SERVERSELECTION_SET_SERVER_LIST = '''
UPDATE serverselection
SET serverselectionserversid = %s
WHERE serverselectionid = %s
RETURNING *
'''
"""
Add User and Role to serverselection
"""
......@@ -296,10 +284,6 @@ def remove_server_from_serverselection(cursor, serverselectionserversid, servers
def remove_server_from_all_serverselections(cursor, serverselectionserversid ):
return serverselection_row_to_dict(fetchone(cursor, SERVERSELECTION_REMOVE_SERVER_FROM_ALL, (serverselectionserversid,), raises=True))
def set_list_server_serverselection(cursor, serverselectionid, serveridlist):
serveridlist = '{'+serveridlist+'}'
return serverselection_row_to_dict(fetchone(cursor, SERVERSELECTION_SET_SERVER_LIST, (serveridlist,serverselectionid), raises=True))
def add_user_to_serverselection(cursor, serverselectionid, username, role):
serverselectionuserrolehstore = username + '=>' + role
return serverselection_row_to_dict(fetchone(cursor, SERVERSELECTION_ADD_USER, (serverselectionuserrolehstore, serverselectionid), raises=True))
......
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