controller 30.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/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)
21
22
23
24
25
26
27
28
29
from server.serverselection.lib import ServerSelection
from server.serverselection.error import (ServerSelectionError,
                                 ServerSelectionErrorDatabaseNotAvailable,
                                 ServerSelectionErrorDbConnection,
                                 ServerSelectionErrorInvalidServerSelectionId,                                 
                                 ServerSelectionErrorServerSelectionNameNotProvided,
                                 ServerSelectionErrorUnknownServerSelectionId,
                                 ServerSelectionErrorDuplicateServerSelectionName,
                                 ServerSelectionEmptyRecordDatabaseError)
30

31
32
33
34
35
36
37
38
39
40
41
42

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()
43
        self.serverselection = ServerSelection()
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        self.conn = None

    @register_wamp('v1.server.list', notification_uri=None, database=True)
    async def list_servers(self, cursor):
        try:
            return self.server.list_servers(cursor)

        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)
    async def describe_server(self, cursor, serverid, configuration):
        try:
            server = self.server.describe_server(cursor, serverid)
            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'{}'

Matthieu Lamalle's avatar
Matthieu Lamalle committed
87
88
89
90
91
92
93
94
95
96
97
98
    @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'{}'

99
    @register_wamp('v1.server.create', notification_uri='v1.server.created', database=True)
100
    async def create_server(self, cursor, _session_user, servername, serverdescription, servermodelid, serverpassphrase):
101

102
103
104
105
106
107
        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})
108
            if return_code:
Matthieu Lamalle's avatar
Matthieu Lamalle committed
109
                defaultserverselection = self._default_user_serverselection(cursor, _session_user)
110
                self.serverselection.add_server_to_selection(cursor, result['serverid'], defaultserverselection['serverselectionid'])
111
112
                return result
            else:
113
                raise ServerError('put passphrase return code status not available')
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

        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)
    async def update_server(self, cursor, 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)
    async def delete_server(self, cursor, serverid):
        try:
            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')
    async def get_peering_conf(self, 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)
    async def exec_cmd_on_server(self, cursor, server_id, command):
193
194
195
        return self._exec_cmd_on_server(cursor, server_id, command)

    def _exec_cmd_on_server(self, cursor, server_id, command):
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        """
        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']:
227
228
229
230
            if 'success' in result :
                dico['success'] = result['success']
                dico['retcode'] = result['retcode']
                dico['return'] = result['return']
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        return dico

    @register_wamp('v1.server.exec.list', notification_uri=None, database=True)
    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',
                                      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, 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',
                                     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',
Matthieu Lamalle's avatar
Matthieu Lamalle committed
265
                                      jid=str(job_id))
266

267
268
269
270
271
272
273
        else:
            raise Exception(_('Automation engine not supported: {}').format(automation))
        ret = []
        for result in results:
            ret.append(self.result_to_dict(result, automation))
        return ret

Matthieu Lamalle's avatar
Matthieu Lamalle committed
274
    @register_wamp('v1.config.configuration.server.updated', None, database=True)
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
    async def update_configuration(self, cursor, server_id):
        configuration = await self.call('v1.config.configuration.server.get', server_id=server_id)
        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))

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    @register_wamp('v1.serverselection.list', notification_uri=None, database=True)
    async def list_serverselections(self, cursor):
        try:
            return self.serverselection.list_serverselections(cursor)

        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)
    async def describe_serverselection(self, cursor, 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.list', notification_uri=None, database=True)
    async def list_user_serverselections(self, cursor, _session_user):
        try:
            serverselectionuser = _session_user['username']
            return self.serverselection.list_user_serverselections(cursor, serverselectionuser)            
        
        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.default', notification_uri=None, database=True)
    async def default_user_serverselection(self, cursor, _session_user):
Matthieu Lamalle's avatar
Matthieu Lamalle committed
341
342
343
        return self._default_user_serverselection(cursor, _session_user)

    def _default_user_serverselection(self, cursor, _session_user):
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
        try:
            serverselectionuser = _session_user['username']            
            return self.serverselection.default_user_serverselection(cursor, serverselectionuser)  

        except ServerSelectionEmptyRecordDatabaseError as err:
            serverselectionname = serverselectionuser + "_DEFAULT"
            serverselectiondescription = serverselectionuser + " server selection"
            self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, serverselectionuser)
            return self.serverselection.default_user_serverselection(cursor, serverselectionuser)  

        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.server.list', notification_uri=None, database=True)
    async def list_user_servers(self, cursor, _session_user):
        try:
            serverselectionuser = _session_user['username']            
            return self.serverselection.list_user_servers(cursor, serverselectionuser)  

        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.create', notification_uri=None, database=True)
    async def create_serverselection(self, cursor, serverselectionname, serverselectiondescription, _session_user):
        try:
            serverselectionuser = _session_user['username']
            return self.serverselection.create_serverselection(cursor, serverselectionname, serverselectiondescription, serverselectionuser)

        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)
    async def update_serverselection(self, cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete):
        try:            
            return self.serverselection.update_serverselection(cursor, serverselectionid, serverselectionname, serverselectiondescription, dynamique, requete)

        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)
    async def delete_serverselection(self, cursor, 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)
    async def add_server_to_selection(self, cursor, serverselectionserversid, serverselectionid):
        try:
            return self.serverselection.add_server_to_selection(cursor, serverselectionserversid, 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)
    async def remove_server_from_selection(self, cursor, serverselectionserversid, serverselectionid):
        try:
            return self.serverselection.remove_server_from_selection(cursor, serverselectionserversid, 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)
    async def add_user_to_serverselection(self, cursor, serverselectionid, serverselectionuser, serverselectionuserrole):
        try:
            return self.serverselection.add_user_to_serverselection(cursor, serverselectionid, serverselectionuser, serverselectionuserrole)
        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)
    async def remove_user_from_serverselection(self, cursor, serverselectionid, serverselectionuser):
        try:
            return self.serverselection.remove_user_from_serverselection(cursor, serverselectionid, serverselectionuser)
        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)
    async def update_user_to_serverselection(self, cursor, serverselectionid, serverselectionuser, serverselectionuserrole):
        try:
            return self.serverselection.update_user_to_serverselection(cursor, serverselectionid, serverselectionuser, serverselectionuserrole)

        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)
Matthieu Lamalle's avatar
Matthieu Lamalle committed
496
    async def get_serverselection_user_role(self, cursor, serverselectionid, username):
497
        try:
Matthieu Lamalle's avatar
Matthieu Lamalle committed
498
            return self.serverselection.get_serverselection_user_role(cursor, serverselectionid, username)
499
500
501
502
503
504
505
506
507
508
509
510
511

        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)
Matthieu Lamalle's avatar
Matthieu Lamalle committed
512
    async def get_serverselection_user_server_role(self, cursor, serverid, username):
513
        try:
Matthieu Lamalle's avatar
Matthieu Lamalle committed
514
            return self.serverselection.get_serverselection_user_server_role(cursor, serverid, username)
515
516
517
518
519
520
521
522
523
524
525

        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))
Matthieu Lamalle's avatar
Matthieu Lamalle committed
526
    
527
528
529
530
531
532
533
534
535
536
537
538
539
    @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:
            ret.append(self._exec_cmd_on_server(cursor, server_id, command))

        return ret


540
541
if __name__ == '__main__':
    run(ServerRunner)
542