Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
@listed
def delscript(self, query):
""" Delete a script on the server.
:param query: the tql query used to select scripts to delete
"""
objects = self.client.list(query, show=('r', 'name'), method='script')
errs = Reporter()
for obj in objects:
if obj['r'] != 'script':
errs.error(obj['id'], 'not a script')
else:
try:
self.server.scripts.delete(obj['name'])
except RepositoryOperationError as err:
errs.error(obj['id'], 'error: %s' % err)
else:
errs.success(obj['id'], 'script deleted')
return errs.get_dict()
@listed
def runscript(self, query, script, batch=None, *args):
""" Execute a script on matching nodes.
"""
# Load the script:
sha1_hash, _ = self.server.scripts.load(script)
objects = self.client.list(query, show=('r', ), method='script')
errs = Reporter()
for obj in objects:
if obj['r'] not in ('host', 'hv'):
errs.error(obj['id'], 'not a host')
else:
try:
node = self.server.get_client(obj['id'])
except KeyError:
errs.error(obj['id'], 'node not connected')
continue
try:
job_id = node.script_run(sha1_hash, script,
self.client.login, batch=batch,
*args)
except RpcError as err:
errs.error(obj['id'], '%s (exc: %s)' % (err.message,
err.exception))
else:
job_id = '.'.join((obj['id'], job_id))
errs.success(obj['id'], 'ok.', jobs=job_id)
return errs.get_dict()
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
#
# Plugins management:
#
@listed
def loadplugin(self, name):
""" Get the content of a plugin stored ont he server.
:param name: name of the plugin to load
"""
self.client.check('plugin')
return self.server.plugins.load(name, empty_if_missing=True)[1]
@listed
def saveplugin(self, name, content):
""" Save a plugin on the server.
:param name: name of the plugin to save
:param content: content of the plugin to save
"""
self.client.check('plugin')
return self.server.plugins.save(name, content)
@listed
def delplugin(self, query):
""" Delete a plugin on the server.
:param query: the tql query used to select plugins to delete
"""
objects = self.client.list(query, show=('r', 'name'), method='plugin')
errs = Reporter()
for obj in objects:
if obj['r'] != 'plugin':
errs.error(obj['id'], 'not a plugin')
else:
try:
self.server.plugins.delete(obj['name'])
except RepositoryOperationError as err:
errs.error(obj['id'], 'error: %s' % err)
else:
errs.success(obj['id'], 'plugin deleted')
return errs.get_dict()
@listed
def installplugin(self, query, plugin):
""" Install a plugin on matching nodes.
"""
# Load the plugin:
sha1_hash, _ = self.server.plugins.load(plugin)
objects = self.client.list(query, show=('r', ), method='plugin')
errs = Reporter()
for obj in objects:
if obj['r'] not in ('host', 'hv'):
errs.error(obj['id'], 'not a host')
else:
try:
node = self.server.get_client(obj['id'])
except KeyError:
errs.error(obj['id'], 'node not connected')
continue
try:
node.plugin_install(sha1_hash, plugin)
except RpcError as err:
errs.error(obj['id'], '%s (exc: %s)' % (err.message,
err.exception))
else:
errs.success(obj['id'], 'plugin installed')
return errs.get_dict()
@listed
def uninstallplugin(self, query, plugin):
""" Install a plugin on matching nodes.
"""
objects = self.client.list(query, show=('r', ), method='plugin')
errs = Reporter()
for obj in objects:
if obj['r'] not in ('host', 'hv'):
errs.error(obj['id'], 'not a host')
else:
try:
node = self.server.get_client(obj['id'])
except KeyError:
errs.error(obj['id'], 'node not connected')
continue
try:
node.plugin_uninstall(plugin)
except RpcError as err:
errs.error(obj['id'], '%s (exc: %s)' % (err.message,
err.exception))
else:
errs.success(obj['id'], 'plugin uninstalled')
return errs.get_dict()
@listed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
def helpplugin(self, query, plugin, method):
""" Get help about a plugin installed on the matching host.
"""
objects = self.client.list(query, show=('r', ), method='plugin')
errs = Reporter()
for obj in objects:
if obj['r'] not in ('host', 'hw'):
errs.error(obj['id'], 'not a host')
else:
try:
node = self.server.get_client(obj['id'])
except KeyError:
errs.error(obj['id'], 'node not connected')
continue
try:
help = node.plugin_help(plugin, method)
except RpcError as err:
errs.error(obj['id'], '%s (exc: %s)' % (err.message,
err.exception))
else:
errs.success(obj['id'], 'ok.', output=help)
return errs.get_dict()
@listed
def runplugin(self, query, plugin, method, batch=None, **kwargs):
""" Execute a plugin method on matching nodes.
"""
# Load the plugin:
sha1_hash, _ = self.server.plugins.load(plugin)
objects = self.client.list(query, show=('r', ), method='plugin')
errs = Reporter()
for obj in objects:
if obj['r'] not in ('host', 'hv'):
errs.error(obj['id'], 'not a host')
else:
try:
node = self.server.get_client(obj['id'])
except KeyError:
errs.error(obj['id'], 'node not connected')
continue
try:
job_id = node.plugin_run(plugin, method, self.client.login,
batch=batch, **kwargs)
except RpcError as err:
errs.error(obj['id'], '%s (exc: %s)' % (err.message,
err.exception))
else:
job_id = '.'.join((obj['id'], job_id))
errs.success(obj['id'], 'ok.', jobs=job_id)
return errs.get_dict()
# Election / Migration / Cloning / Allocation
@listed
def electiontypes(self):
return Elector.ALGO_BY_TYPES
@listed
def election(self, query_vm, query_dest, mtype='cold', algo='fair', **kwargs):
""" Consult the server for the migration of specified vm on
an hypervisor pool.
:param query_vm: the tql query to select VMs to migrate
:param query_dest: the tql query to select destination hypervisors
candidates
:param mtype: type of migration
:param algo: algo used for distribution
"""
elector = Elector(self.server, query_vm, query_dest, self.client)
return elector.election(mtype, algo, **kwargs)
@listed
def migrate(self, migration_plan):
""" Launch the provided migration plan.
:param migration_plan: the plan of the migration.
:return: a standard error report
"""
errs = Reporter()
for migration in migration_plan:
# Check if the migration type is know:
if migration['type'] in MIGRATION_TYPES:
mtype = MIGRATION_TYPES[migration['type']]
else:
errmsg = '%r unknown migration type' % migration['type']
errs.error(migration['sid'], errmsg)
continue
vm = self.server.db.get_by_id(migration['sid'], ('h', 'hv', 'p'))
# Construct the migration properties:
migration_properties = {
'client': self.client,
'vm_name': vm['h'],
'hv_source': vm['p'],
'hv_dest': migration['did']
}
# Create the job:
job = self.client.spawn_job(mtype, settings=migration_properties)
errs.success(migration['sid'], 'migration launched, id:%s' % job.id)
return errs.get_dict()
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
@listed
def migrate2(self, tql_vm, tql_target, batch=None, live=False, flags=None):
""" Launch a migration.
"""
self.client.check('migrate')
vms = self.client.list(tql_vm, show=('r', ), method='migrate')
errs = Reporter()
for vm in vms:
if vm['r'] != 'vm':
errs.error(vm['id'], 'not a vm')
else:
settings = {'server': self.server,
'client': self.client,
'vm_id': vm['id'],
'tql_target': tql_target,
'live': live,
'flags': flags}
job = self.client.spawn_job(MigrationJob, batch=batch, settings=settings)
errs.success(vm['id'], 'migration launched, id:%s' % job.id)
return errs.get_dict()
@listed
def diskcopy(self, source_id, source_pool, source_vol, dest_id, dest_pool, dest_vol):
""" Launch a migration.
"""
self.client.check('diskcopy')
settings = {'server': self.server,
'client': self.client,
'source_id': source_id,
'source_pool': source_pool,
'source_vol': source_vol,
'dest_id': dest_id,
'dest_pool': dest_pool,
'dest_vol': dest_vol}
job = self.client.spawn_job(DiskCopyJob, settings=settings)
return job.id
@listed
def clone(self, tql_vm, tql_dest, name):
""" Create and launch a clone job.
:param tql_vm: a tql matching one vm object (the cloned vm)
:param tql_dest: a tql matching one hypervisor object (the destination
hypervisor)
:param name: the new name of the VM
"""
vm = self.client.list(tql_vm, show=('r', 'h', 'p'), method='clone')
if len(vm) != 1:
raise CloneError('VM Tql must select ONE vm')
elif vm[0]['r'] != 'vm':
raise CloneError('Destination Tql must select a vm')
else:
vm = vm[0]
dest = self.client.list(tql_dest, show=('r',), method='clone')
if len(dest) != 1:
raise CloneError('Destination Tql must select ONE hypervisor')
elif dest[0]['r'] != 'hv':
raise CloneError('Destination Tql must select an hypervisor')
else:
dest = dest[0]
job = self.client.spawn_job(CloneJob, settings={'server': self.server,
'client': self.client,
'vm_name': vm['h'],
'new_vm_name': name,
'hv_source': vm['p'],
'hv_dest': dest['id']})
return job.id
@listed
def allocate(self, vmspec, tql_target):
""" Allocate new VMs.
:param vmspec: a vmspec structure
:param tql_target: a TQL used as target restriction
"""
self.client.check('allocate')
# Check and expand vmspec input:
expanded_vmspec = expand_vmspec(vmspec)
job = self.client.spawn_job(AllocationJob, settings={'server': self.server,
'client': self.client,
'expanded_vmspec': expanded_vmspec,
'tql_target': tql_target})
return job.id
#
# Remote console:
#
@listed
def console(self, tql):
""" Start a remote console on object matching the provided tql.
:param tql: tql matching only one object on which start the console
:return: the label of the created tunnel
"""
objects = self.client.list(tql, show=('r', 'p', 'h'), method='console')
if not objects:
raise NotImplementedError('No objects matched by query')
elif len(objects) != 1:
raise NotImplementedError('Console only support one tunnel at time for now')
errs = Reporter()
for obj in objects:
if obj['r'] in ('vm',):
client = self.server.get_client(obj['p'])
try:
srv_to_host_tun = client.console(obj['h'])
except Exception as err:
errs.error(obj['id'], str(err))
else:
cli_tun = self.client.register_tunnel('console', client, srv_to_host_tun)
errs.success(obj['id'], 'tunnel started.', output=cli_tun.label)
else:
errs.error(obj['id'], 'bad role')
return errs.get_dict()
#
# Remote shell:
#
def shell(self, tql):
""" Start a remote shell on object matching the provided tql.
:param tql: tql matching only one object on which start the shell
:return: the label of the created tunnel
objects = self.client.list(tql, show=('r', 'p'), method='shell')
if not objects:
raise NotImplementedError('No objects matched by query')
elif len(objects) != 1:
raise NotImplementedError('Shell only support one tunnel at time for now')
errs = Reporter()
for obj in objects:
if obj['r'] in ('host', 'hv'):
client = self.server.get_client(obj['id'])
srv_to_host_tun = client.shell()
cli_tun = self.client.register_tunnel('shell', client, srv_to_host_tun)
errs.success(obj['id'], 'tunnel started.', output=cli_tun.label)
else:
errs.error(obj['id'], 'bad role')
return errs.get_dict()
@listed
def resize(self, label, row, col, xpixel, ypixel):
""" Send a resize event to the remote shell's tty.
:param label: label of the tty tunnel to resize
:param row: number of rows
:param col: number of columns
:param xpixel: unused
:param ypixel: unused
if label is None:
tuns = [(c, st.label) for t, c, ct, st
in self.client.tunnels.values() if t == 'shell']
else:
ttype, client, ctun, stun = self.client.get_tunnel(label)
if ttype != 'shell':
raise ValueError('Label does not refers on a shell')
tuns = [(client, stun.label)]
for client, label in tuns:
client.resize(label, row, col, xpixel, ypixel)
#
# Port forwarding:
#
def forward(self, login, port, destination='127.0.0.1'):
""" Forward a TCP port to the client.
:param login: login of the remote client on which establish the tunnel
:param port: port on which establish the tunnel on destination
:param destination: tunnel destination (from the remote client side)
self.client.check('forward', query='id=%s' % login)
# Create the tunnel to the node:
try:
host_client = self.server.get_client(login)
except KeyError:
raise KeyError('Specified client is not connected')
s2n_tun = host_client.forward(port, destination)
# Create tunnel to the CLI
c2s_tun = self.client.register_tunnel('forward', host_client, s2n_tun)
return c2s_tun.label
#
# Debug:
#
@listed
def dbstats(self):
""" Get statistics about tql database.
"""
return self.server.db.stats()
def forward_call(self, login, func, *args, **kwargs):
""" Forward a call to a connected client and return result.
:param login: login of the connected client
:param func: function to execute on the client
:param \*args, \*\*kwargs: arguments of the call
"""
self.client.check('forward_call')
client = self.server.get_client(login)
return client.conn.call(func, *args, **kwargs)
def inspect(self, object_id):
""" Inspect tags of an object.
"""
self.client.check('inspect')
obj = self.server.db.get(object_id)
return [repr(x) for x in obj.itertags()]
def analyze(self, query):
""" Analyse a TQL query.
"""
self.client.check('analyze')
result, analyze = self.server.db.query(query, analyze=True)
return analyze.to_list()
class CliClient(Client):
""" A cli client connected to the cc-server.
"""
ROLE = 'cli'
RPC_HANDLER = CliHandler
KILL_ALREADY_CONNECTED = True
def __init__(self, *args, **kwargs):
super(CliClient, self).__init__(*args, **kwargs)
self._tunnels = {} # Running tunnels for this client (as client)
@property
def tunnels(self):
""" Get active client tunnels by label (a dict).
"""
return self._tunnels
def spawn_job(self, job_class, **kwargs):
return self._server.jobs.spawn(job_class, self.account, **kwargs)
def register_tunnel(self, ttype, client, tun, label=None):
""" Create and register a tunnel for this client.
:param ttype: type of tunnel
:param client: client where the tunnel go
:param tun: the tunnel of this client
:param label: label of the tunnel to create
"""
def cb_on_shutdown(tun):
tun.cb_default_on_shutdown(tun)
# Delete the tunnel from the current running tunnels:
self.unregister_tunnel(tun.label)
ctun = self.conn.create_tunnel(label=label, endpoint=tun.socket,
on_shutdown=cb_on_shutdown,
close_endpoint_on_shutdown=False)
self._tunnels[ctun.label] = (ttype, client, ctun, tun)
return ctun
def get_tunnel(self, label):
""" Get the tunnel binded to the provided label.
:return: a tuple (type, remote_client, tunnel, remote_client_tunnel)
where: **type** is a string provided on tunnel creation,
**remote_client** the client object of the remote client on which
the tunnel is established, **tunnel** the cli-to-server tunnel
object from the sjRpc, **remote_client_tunnel** the
server-to-remote-client tunnel object from the sjRpc.
"""
return self._tunnels[label]
def unregister_tunnel(self, label):
try:
del self._tunnels[label]
except KeyError:
pass
def wall(self, sender, message):
""" Send a wall to the client.
"""
self.conn.call('wall', sender, message)
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
class MultiCliClient(CliClient):
""" A bootstrap client connected to the cc-server.
"""
ROLE = 'mcli'
KILL_ALREADY_CONNECTED = False
def _get_tql_object(self):
tql_object = SObject(self.login)
tql_object.register(StaticTag('r', self.role))
tql_object.register(StaticTag('a', self._login))
self._server.db.register(tql_object)
return tql_object
@property
def login(self):
return '%s.%s' % (self._login, self.conn.get_fd())
@property
def role(self):
return 'cli'
def shutdown(self):
super(MultiCliClient, self).shutdown()
# Also, remote the object from the db:
self._server.db.unregister(self.login)
Client.register_client_class(MultiCliClient)
Client.register_client_class(CliClient)