Newer
Older
from sjrpc.core import RpcError
from cloudcontrol.common.datastructures.orderedset import OrderedSet
from cloudcontrol.server.conf import CCConf
from cloudcontrol.server.exceptions import (ReservedTagError, BadObjectError,
CloneError, RightError)
from cloudcontrol.server.election import Elector
from cloudcontrol.server.repository import RepositoryOperationError
from cloudcontrol.server.handlers import listed, Reporter
from cloudcontrol.server.clients import Client, RegisteredCCHandler
from cloudcontrol.server.jobs import (ColdMigrationJob, HotMigrationJob,
CloneJob)
from cloudcontrol.common.tql.db.tag import StaticTag
MIGRATION_TYPES = {'cold': ColdMigrationJob,
'hot': HotMigrationJob,}
class CliHandler(RegisteredCCHandler):
""" Handler binded to the 'cli' role.
.. currentmodule:: cloudcontrol.server.clients.cli
.. autosummary::
CliHandler.list
CliHandler.start
CliHandler.stop
CliHandler.destroy
CliHandler.pause
CliHandler.resume
CliHandler.disablevirtiocache
CliHandler.autostart
CliHandler.undefine
CliHandler.passwd
CliHandler.addaccount
CliHandler.copyaccount
CliHandler.addtag
CliHandler.deltag
CliHandler.tags
CliHandler.delaccount
CliHandler.close
CliHandler.declose
CliHandler.kill
CliHandler.execute
CliHandler.shutdown
CliHandler.jobs
CliHandler.cancel
CliHandler.purge
CliHandler.attachment
CliHandler.loadscript
CliHandler.savescript
CliHandler.delscript
CliHandler.runscript
CliHandler.loadplugin
CliHandler.saveplugin
CliHandler.delplugin
CliHandler.installplugin
CliHandler.uninstallplugin
CliHandler.runplugin
CliHandler.electiontypes
CliHandler.election
CliHandler.migrate
CliHandler.clone
CliHandler.shell
CliHandler.resize
""" List all objects registered on this instance.
:param query: the query to select objects to show
"""
self.logger.debug('Executed list function with query %s', query)
objects = self.client.list(query, method=method)
order = OrderedSet(['id'])
#if tags is not None:
# order |= OrderedSet(tags)
return {'objects': objects, 'order': list(order)}
@listed
def wall(self, message):
""" Send a wall to all connected users.
"""
self.client.check('wall')
self.server.wall(self.client.login, message)
@listed
def loadmotd(self):
""" Load and return the message of the day.
"""
return self.server.load_motd()
@listed
def savemotd(self, motd):
""" Save a new message of the day.
"""
self.server.save_motd(motd)
#
# VM actions:
#
def _vm_action(self, query, method, *args, **kwargs):
""" Do an action on a virtual machine.
"""
errs = Reporter()
# Search all hypervisors of selected vms:
for vm in self.client.list(query, show=('r', 'h', 'p'), method=method):
if vm['r'] != 'vm':
errs.error(vm['id'], 'not a vm')
else:
hvclient = self.server.get_client(vm['p'])
if hvclient is None:
errs.error(vm['id'], 'offline hypervisor')
else:
try:
hvclient.vm_action(method, vm['h'], *args, **kwargs)
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
except Exception as err:
errs.error(vm['id'], str(err))
else:
errs.success(vm['id'], 'ok')
return errs.get_dict()
@listed
def start(self, query):
""" Start a virtual machine.
"""
return self._vm_action(query, 'vm_start')
@listed
def stop(self, query):
""" Stop a virtual machine.
"""
return self._vm_action(query, 'vm_stop')
@listed
def destroy(self, query):
""" Destroy (hard shutdown) a virtual machine.
"""
return self._vm_action(query, 'vm_destroy')
@listed
def pause(self, query):
""" Pause a virtual machine.
"""
return self._vm_action(query, 'vm_suspend')
@listed
def resume(self, query):
""" Resume a virtual machine.
"""
return self._vm_action(query, 'vm_resume')
@listed
def disablevirtiocache(self, query):
""" Set virtio cache to none on VMs disk devices.
:param query: tql query
"""
return self._vm_action(query, 'vm_disable_virtio_cache')
@listed
def autostart(self, query, flag):
""" Set autostart flag on VMs.
:param query: tql query
:param bool flag: autostart value to set
"""
return self._vm_action(query, 'vm_set_autostart', flag)
@listed
def undefine(self, query, delete_storage=True):
""" Undefine selected virtual machines.
:param query: the tql query to select objects.
:param delete_storage: delete storage of vm.
:return: a dict where key is the id of a selected object, and the value
a tuple (errcode, message) where errcode is (success|error|warn) and
message an error message or the output of the command in case of
success.
"""
objects = self.client.list(query, show=('r', 'p', 'h', 'disk*',), method='undefine')
errs = Reporter()
for obj in objects:
if obj['r'] != 'vm':
Loading full blame...