operations.py 65.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

18
from __future__ import absolute_import, unicode_literals
19 20

from StringIO import StringIO
Bach Dániel committed
21 22
from base64 import encodestring
from hashlib import md5
Dudás Ádám committed
23
from logging import getLogger
Őry Máté committed
24
from string import ascii_lowercase
Bach Dániel committed
25
from tarfile import TarFile, TarInfo
Kálmán Viktor committed
26
from urlparse import urlsplit
Dudás Ádám committed
27

28 29 30 31 32
import os
import time
from celery.contrib.abortable import AbortableAsyncResult
from celery.exceptions import TimeLimitExceeded, TimeoutError
from django.conf import settings
33
from django.core.exceptions import PermissionDenied, SuspiciousOperation
34
from django.core.urlresolvers import reverse
35
from django.db.models import Q
Dudás Ádám committed
36
from django.utils import timezone
37
from django.utils.translation import ugettext_lazy as _, ugettext_noop
38
from re import search
39 40
from sizefield.utils import filesizeformat

41 42 43
from common.models import (
    create_readable, humanize_exception, HumanReadableException
)
44
from common.operations import Operation, register_operation, SubOperationMixin
45 46
from dashboard.store_api import Store, NoStoreException
from firewall.models import Host
Bach Dániel committed
47
from manager.scheduler import SchedulerError
48 49 50
from monitor.client import Client
from storage.models import Disk
from storage.tasks import storage_tasks
51
from .models import (
52
    Instance, InstanceActivity, InstanceTemplate, Interface, Node,
53
    NodeActivity, pwgen
54
)
Bach Dániel committed
55
from .tasks import agent_tasks, vm_tasks
56 57 58
from .tasks.local_tasks import (
    abortable_async_instance_operation, abortable_async_node_operation,
)
Kálmán Viktor committed
59

Dudás Ádám committed
60
logger = getLogger(__name__)
61 62


63 64 65 66
class RemoteOperationMixin(object):
    remote_timeout = 30

    def _operation(self, **kwargs):
Őry Máté committed
67
        args = self._get_remote_args(**kwargs)
68 69 70 71 72 73 74 75 76
        return self.task.apply_async(
            args=args, queue=self._get_remote_queue()
        ).get(timeout=self.remote_timeout)

    def check_precond(self):
        super(RemoteOperationMixin, self).check_precond()
        self._get_remote_queue()


77 78 79 80 81 82 83
class AbortableRemoteOperationMixin(object):
    remote_step = property(lambda self: self.remote_timeout / 10)

    def _operation(self, task, **kwargs):
        args = self._get_remote_args(**kwargs),
        remote = self.task.apply_async(
            args=args, queue=self._get_remote_queue())
84
        for i in xrange(0, self.remote_timeout, self.remote_step):
85 86 87 88 89 90 91
            try:
                return remote.get(timeout=self.remote_step)
            except TimeoutError as e:
                if task is not None and task.is_aborted():
                    AbortableAsyncResult(remote.id).abort()
                    raise humanize_exception(ugettext_noop(
                        "Operation aborted by user."), e)
92
        raise TimeLimitExceeded()
93 94


95
class InstanceOperation(Operation):
96
    acl_level = 'owner'
97
    async_operation = abortable_async_instance_operation
98
    host_cls = Instance
99
    concurrency_check = True
100 101
    accept_states = None
    deny_states = None
102
    resultant_state = None
Dudás Ádám committed
103

104
    def __init__(self, instance):
105
        super(InstanceOperation, self).__init__(subject=instance)
106 107 108
        self.instance = instance

    def check_precond(self):
109 110
        if self.instance.destroyed_at:
            raise self.instance.InstanceDestroyedError(self.instance)
111 112 113 114 115 116 117 118 119 120 121 122 123 124
        if self.accept_states:
            if self.instance.status not in self.accept_states:
                logger.debug("precond failed for %s: %s not in %s",
                             unicode(self.__class__),
                             unicode(self.instance.status),
                             unicode(self.accept_states))
                raise self.instance.WrongStateError(self.instance)
        if self.deny_states:
            if self.instance.status in self.deny_states:
                logger.debug("precond failed for %s: %s in %s",
                             unicode(self.__class__),
                             unicode(self.instance.status),
                             unicode(self.accept_states))
                raise self.instance.WrongStateError(self.instance)
125 126

    def check_auth(self, user):
127
        if not self.instance.has_level(user, self.acl_level):
128 129 130
            raise humanize_exception(ugettext_noop(
                "%(acl_level)s level is required for this operation."),
                PermissionDenied(), acl_level=self.acl_level)
131

132
        super(InstanceOperation, self).check_auth(user=user)
133

134 135
        if (self.instance.node and not self.instance.node.online and
                not user.is_superuser):
136 137
            raise self.instance.WrongStateError(self.instance)

138 139
    def create_activity(self, parent, user, kwargs):
        name = self.get_activity_name(kwargs)
140 141 142 143 144 145 146 147 148 149
        if parent:
            if parent.instance != self.instance:
                raise ValueError("The instance associated with the specified "
                                 "parent activity does not match the instance "
                                 "bound to the operation.")
            if parent.user != user:
                raise ValueError("The user associated with the specified "
                                 "parent activity does not match the user "
                                 "provided as parameter.")

150 151 152
            return parent.create_sub(
                code_suffix=self.get_activity_code_suffix(),
                readable_name=name, resultant_state=self.resultant_state)
153 154
        else:
            return InstanceActivity.create(
155 156
                code_suffix=self.get_activity_code_suffix(),
                instance=self.instance,
157
                readable_name=name, user=user,
158 159
                concurrency_check=self.concurrency_check,
                resultant_state=self.resultant_state)
160

161 162 163 164 165
    def is_preferred(self):
        """If this is the recommended op in the current state of the instance.
        """
        return False

166

167 168 169 170 171 172 173 174 175 176
class RemoteInstanceOperation(RemoteOperationMixin, InstanceOperation):
    remote_queue = ('vm', 'fast')

    def _get_remote_queue(self):
        return self.instance.get_remote_queue_name(*self.remote_queue)

    def _get_remote_args(self, **kwargs):
        return [self.instance.vm_name]


Bach Dániel committed
177
class EnsureAgentMixin(object):
Bálint Máhonfai committed
178
    accept_states = ('RUNNING',)
Bach Dániel committed
179 180 181 182 183 184 185 186 187 188 189 190

    def check_precond(self):
        super(EnsureAgentMixin, self).check_precond()

        last_boot_time = self.instance.activity_log.filter(
            succeeded=True, activity_code__in=(
                "vm.Instance.deploy", "vm.Instance.reset",
                "vm.Instance.reboot")).latest("finished").finished

        try:
            InstanceActivity.objects.filter(
                activity_code="vm.Instance.agent.starting",
191 192
                started__gt=last_boot_time, instance=self.instance
            ).latest("started")
Bach Dániel committed
193 194 195 196 197
        except InstanceActivity.DoesNotExist:  # no agent since last boot
            raise self.instance.NoAgentError(self.instance)


class RemoteAgentOperation(EnsureAgentMixin, RemoteInstanceOperation):
Bálint Máhonfai committed
198
    remote_queue = ('agent',)
Bach Dániel committed
199 200 201
    concurrency_check = False


202
@register_operation
203 204 205 206 207
class AddInterfaceOperation(InstanceOperation):
    id = 'add_interface'
    name = _("add interface")
    description = _("Add a new network interface for the specified VLAN to "
                    "the VM.")
208
    required_perms = ()
209
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
210

211 212
    def rollback(self, net, activity):
        with activity.sub_activity(
Bálint Máhonfai committed
213
                'destroying_net',
214 215 216 217
                readable_name=ugettext_noop("destroy network (rollback)")):
            net.destroy()
            net.delete()

218
    def _operation(self, activity, user, system, vlan, managed=None):
219
        if not vlan.has_level(user, 'user'):
220 221 222
            raise humanize_exception(ugettext_noop(
                "User acces to vlan %(vlan)s is required."),
                PermissionDenied(), vlan=vlan)
223 224 225 226 227 228 229
        if managed is None:
            managed = vlan.managed

        net = Interface.create(base_activity=activity, instance=self.instance,
                               managed=managed, owner=user, vlan=vlan)

        if self.instance.is_running:
230
            try:
231 232
                self.instance._attach_network(
                    interface=net, parent_activity=activity)
233 234 235 236
            except Exception as e:
                if hasattr(e, 'libvirtError'):
                    self.rollback(net, activity)
                raise
237
            net.deploy()
Bach Dániel committed
238 239
            self.instance._change_ip(parent_activity=activity)
            self.instance._restart_networking(parent_activity=activity)
240

241 242 243 244
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop("add %(vlan)s interface"),
                               vlan=kwargs['vlan'])

245

246
@register_operation
247 248 249
class CreateDiskOperation(InstanceOperation):
    id = 'create_disk'
    name = _("create disk")
250
    description = _("Create and attach empty disk to the virtual machine.")
Bálint Máhonfai committed
251
    required_perms = ('storage.create_empty_disk',)
252
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
253

254
    def _operation(self, user, size, activity, name=None):
Bach Dániel committed
255 256
        from storage.models import Disk

257 258 259
        if not name:
            name = "new disk"
        disk = Disk.create(size=size, name=name, type="qcow2-norm")
260
        disk.full_clean()
261 262 263 264
        devnums = list(ascii_lowercase)
        for d in self.instance.disks.all():
            devnums.remove(d.dev_num)
        disk.dev_num = devnums.pop(0)
265
        disk.save()
266 267
        self.instance.disks.add(disk)

268
        if self.instance.is_running:
269
            with activity.sub_activity(
Bálint Máhonfai committed
270 271
                    'deploying_disk',
                    readable_name=ugettext_noop("deploying disk")
272
            ):
273
                disk.deploy()
274
            self.instance._attach_disk(parent_activity=activity, disk=disk)
275

276
    def get_activity_name(self, kwargs):
277 278 279
        return create_readable(
            ugettext_noop("create disk %(name)s (%(size)s)"),
            size=filesizeformat(kwargs['size']), name=kwargs['name'])
280 281


282
@register_operation
283
class ResizeDiskOperation(RemoteInstanceOperation):
284 285 286 287
    id = 'resize_disk'
    name = _("resize disk")
    description = _("Resize the virtual disk image. "
                    "Size must be greater value than the actual size.")
Bálint Máhonfai committed
288 289
    required_perms = ('storage.resize_disk',)
    accept_states = ('RUNNING',)
290
    async_queue = "localhost.man.slow"
291 292
    remote_queue = ('vm', 'slow')
    task = vm_tasks.resize_disk
293

294 295 296
    def _get_remote_args(self, disk, size, **kwargs):
        return (super(ResizeDiskOperation, self)
                ._get_remote_args(**kwargs) + [disk.path, size])
297 298 299 300 301 302

    def get_activity_name(self, kwargs):
        return create_readable(
            ugettext_noop("resize disk %(name)s to %(size)s"),
            size=filesizeformat(kwargs['size']), name=kwargs['disk'].name)

303
    def _operation(self, disk, size):
304 305 306
        if not disk.is_resizable:
            raise HumanReadableException.create(ugettext_noop(
                'Disk type "%(type)s" is not resizable.'), type=disk.type)
307 308 309 310
        super(ResizeDiskOperation, self)._operation(disk=disk, size=size)
        disk.size = size
        disk.save()

311

312
@register_operation
313 314 315
class DownloadDiskOperation(InstanceOperation):
    id = 'download_disk'
    name = _("download disk")
316 317 318 319
    description = _("Download and attach disk image (ISO file) for the "
                    "virtual machine. Most operating systems do not detect a "
                    "new optical drive, so you may have to reboot the "
                    "machine.")
320
    abortable = True
321
    has_percentage = True
Bálint Máhonfai committed
322
    required_perms = ('storage.download_disk',)
323
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
324
    async_queue = "localhost.man.slow"
325

326
    def _operation(self, user, url, task, activity, name=None):
327
        disk = Disk.download(url=url, name=name, task=task)
328 329 330 331
        devnums = list(ascii_lowercase)
        for d in self.instance.disks.all():
            devnums.remove(d.dev_num)
        disk.dev_num = devnums.pop(0)
332
        disk.full_clean()
333
        disk.save()
334
        self.instance.disks.add(disk)
335 336
        activity.readable_name = create_readable(
            ugettext_noop("download %(name)s"), name=disk.name)
337

338 339 340 341
        activity.result = create_readable(ugettext_noop(
            "Downloading %(url)s is finished. The file md5sum "
            "is: '%(checksum)s'."),
            url=url, checksum=disk.checksum)
Őry Máté committed
342
        # TODO iso (cd) hot-plug is not supported by kvm/guests
343
        if self.instance.is_running and disk.type not in ["iso"]:
344
            self.instance._attach_disk(parent_activity=activity, disk=disk)
345

346

347
@register_operation
348 349 350 351 352 353
class ImportDiskOperation(InstanceOperation):
    id = 'import_disk'
    name = _('import disk')
    description = _('Import and attach a disk image to the virtual machine '
                    'from the user store. The disk image has to be in the '
                    'root directory of the store.')
354
    abortable = True
355 356 357 358 359 360 361 362 363 364 365
    required_perms = ('storage.import_disk',)
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
    async_queue = 'localhost.man.slow'

    def check_auth(self, user):
        super(ImportDiskOperation, self).check_auth(user)
        try:
            Store(user)
        except NoStoreException:
            raise PermissionDenied

366
    def _operation(self, user, name, disk_path, task):
367
        store = Store(user)
368 369
        download_link, port = store.request_ssh_download(disk_path)
        disk = Disk.import_disk(user, name, download_link, port, task)
370 371 372 373
        self.instance.disks.add(disk)


@register_operation
374 375 376
class ExportDiskOperation(InstanceOperation):
    id = 'export_disk'
    name = _('export disk')
377
    description = _('Export disk to the selected format.')
378
    abortable = True
379 380
    required_perms = ('storage.export_disk',)
    accept_states = ('STOPPED',)
381
    async_queue = 'localhost.man.slow'
382

383 384 385 386 387 388 389
    def check_auth(self, user):
        super(ExportDiskOperation, self).check_auth(user)
        try:
            Store(user)
        except NoStoreException:
            raise PermissionDenied

390
    def _operation(self, user, disk, exported_name, disk_format, task):
391
        store = Store(user)
392
        upload_link, port = store.request_ssh_upload()
393
        file_name = disk.export(disk_format, upload_link, port, task)
394
        store.ssh_upload_finished(file_name, '', exported_name + '.' + disk_format)
395 396 397


@register_operation
398
class DeployOperation(InstanceOperation):
Dudás Ádám committed
399 400
    id = 'deploy'
    name = _("deploy")
401 402
    description = _("Deploy and start the virtual machine (including storage "
                    "and network configuration).")
403
    required_perms = ()
404
    deny_states = ('SUSPENDED', 'RUNNING')
405
    resultant_state = 'RUNNING'
Dudás Ádám committed
406

407 408
    def is_preferred(self):
        return self.instance.status in (self.instance.STATUS.STOPPED,
409
                                        self.instance.STATUS.PENDING,
410 411
                                        self.instance.STATUS.ERROR)

412 413 414
    def on_abort(self, activity, error):
        activity.resultant_state = 'STOPPED'

Dudás Ádám committed
415 416
    def on_commit(self, activity):
        activity.resultant_state = 'RUNNING'
417
        activity.result = create_readable(
Guba Sándor committed
418
            ugettext_noop("virtual machine successfully "
419 420
                          "deployed to node: %(node)s"),
            node=self.instance.node)
Dudás Ádám committed
421

422
    def _operation(self, activity, node=None):
Dudás Ádám committed
423 424
        # Allocate VNC port and host node
        self.instance.allocate_vnc_port()
425 426 427 428 429
        if node is not None:
            self.instance.node = node
            self.instance.save()
        else:
            self.instance.allocate_node()
Dudás Ádám committed
430 431

        # Deploy virtual images
432 433 434 435 436 437
        try:
            self.instance._deploy_disks(parent_activity=activity)
        except:
            self.instance.yield_node()
            self.instance.yield_vnc_port()
            raise
Dudás Ádám committed
438 439

        # Deploy VM on remote machine
440
        if self.instance.state not in ['PAUSED']:
441
            self.instance._deploy_vm(parent_activity=activity)
Dudás Ádám committed
442 443

        # Establish network connection (vmdriver)
444
        with activity.sub_activity(
Bálint Máhonfai committed
445 446
                'deploying_net', readable_name=ugettext_noop(
                    "deploy network")):
Dudás Ádám committed
447 448
            self.instance.deploy_net()

449 450 451 452 453
        try:
            self.instance.renew(parent_activity=activity)
        except:
            pass

454
        self.instance._resume_vm(parent_activity=activity)
Dudás Ádám committed
455

456 457 458
        if self.instance.has_agent:
            activity.sub_activity('os_boot', readable_name=ugettext_noop(
                "wait operating system loading"), interruptible=True)
Dudás Ádám committed
459

460
    @register_operation
Őry Máté committed
461
    class DeployVmOperation(SubOperationMixin, RemoteInstanceOperation):
462 463
        id = "_deploy_vm"
        name = _("deploy vm")
Őry Máté committed
464
        description = _("Deploy virtual machine.")
465 466
        remote_queue = ("vm", "slow")
        task = vm_tasks.deploy
Bálint Máhonfai committed
467 468
        remote_timeout = 120

Őry Máté committed
469
        def _get_remote_args(self, **kwargs):
470 471 472 473 474 475 476 477 478
            return [self.instance.get_vm_desc()]
            # intentionally not calling super

        def get_activity_name(self, kwargs):
            return create_readable(ugettext_noop("deploy virtual machine"),
                                   ugettext_noop("deploy vm to %(node)s"),
                                   node=self.instance.node)

    @register_operation
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    class DeployDisksOperation(SubOperationMixin, InstanceOperation):
        id = "_deploy_disks"
        name = _("deploy disks")
        description = _("Deploy all associated disks.")

        def _operation(self):
            devnums = list(ascii_lowercase)  # a-z
            for disk in self.instance.disks.all():
                # assign device numbers
                if disk.dev_num in devnums:
                    devnums.remove(disk.dev_num)
                else:
                    disk.dev_num = devnums.pop(0)
                    disk.save()
                # deploy disk
                disk.deploy()

496
    @register_operation
Őry Máté committed
497
    class ResumeVmOperation(SubOperationMixin, RemoteInstanceOperation):
498 499 500 501 502
        id = "_resume_vm"
        name = _("boot virtual machine")
        remote_queue = ("vm", "slow")
        task = vm_tasks.resume

Dudás Ádám committed
503

504
@register_operation
505
class DestroyOperation(InstanceOperation):
Dudás Ádám committed
506 507
    id = 'destroy'
    name = _("destroy")
508 509
    description = _("Permanently destroy virtual machine, its network "
                    "settings and disks.")
510
    required_perms = ()
511
    resultant_state = 'DESTROYED'
Dudás Ádám committed
512

513 514 515
    def on_abort(self, activity, error):
        activity.resultant_state = None

516
    def _operation(self, activity, system):
517
        # Destroy networks
518 519 520
        with activity.sub_activity(
                'destroying_net',
                readable_name=ugettext_noop("destroy network")):
521
            if self.instance.node:
522
                self.instance.shutdown_net()
523
            self.instance.destroy_net()
Dudás Ádám committed
524

525
        if self.instance.node:
526
            self.instance._delete_vm(parent_activity=activity)
Dudás Ádám committed
527 528

        # Destroy disks
529 530 531
        with activity.sub_activity(
                'destroying_disks',
                readable_name=ugettext_noop("destroy disks")):
Dudás Ádám committed
532
            self.instance.destroy_disks()
Dudás Ádám committed
533

Dudás Ádám committed
534 535
        # Delete mem. dump if exists
        try:
536
            self.instance._delete_mem_dump(parent_activity=activity)
Dudás Ádám committed
537 538 539 540 541 542
        except:
            pass

        # Clear node and VNC port association
        self.instance.yield_node()
        self.instance.yield_vnc_port()
Dudás Ádám committed
543 544 545 546

        self.instance.destroyed_at = timezone.now()
        self.instance.save()

547
    @register_operation
548
    class DeleteVmOperation(SubOperationMixin, RemoteInstanceOperation):
549 550 551
        id = "_delete_vm"
        name = _("destroy virtual machine")
        task = vm_tasks.destroy
552
        remote_timeout = 120
553 554
        # if e.libvirtError and "Domain not found" in str(e):

555 556 557 558 559 560 561 562 563 564 565 566 567 568
    @register_operation
    class DeleteMemDumpOperation(RemoteOperationMixin, SubOperationMixin,
                                 InstanceOperation):
        id = "_delete_mem_dump"
        name = _("removing memory dump")
        task = storage_tasks.delete_dump

        def _get_remote_queue(self):
            return self.instance.mem_dump['datastore'].get_remote_queue_name(
                "storage", "fast")

        def _get_remote_args(self, **kwargs):
            return [self.instance.mem_dump['path']]

Dudás Ádám committed
569

570
@register_operation
571
class MigrateOperation(RemoteInstanceOperation):
Dudás Ádám committed
572 573
    id = 'migrate'
    name = _("migrate")
574 575
    description = _("Move a running virtual machine to an other worker node "
                    "keeping its full state.")
576
    required_perms = ()
577
    superuser_required = True
Bálint Máhonfai committed
578
    accept_states = ('RUNNING',)
579
    async_queue = "localhost.man.slow"
580 581
    task = vm_tasks.migrate
    remote_queue = ("vm", "slow")
582
    remote_timeout = 1000
583

584
    def _get_remote_args(self, to_node, live_migration, **kwargs):
585 586
        return (super(MigrateOperation, self)._get_remote_args(**kwargs) +
                [to_node.host.hostname, live_migration])
Dudás Ádám committed
587

588
    def rollback(self, activity):
589
        with activity.sub_activity(
Bálint Máhonfai committed
590 591
                'rollback_net', readable_name=ugettext_noop(
                    "redeploy network (rollback)")):
592 593
            self.instance.deploy_net()

594
    def _operation(self, activity, to_node=None, live_migration=True):
Dudás Ádám committed
595
        if not to_node:
Bach Dániel committed
596 597 598 599 600 601
            with activity.sub_activity('scheduling',
                                       readable_name=ugettext_noop(
                                           "schedule")) as sa:
                to_node = self.instance.select_node()
                sa.result = to_node

602
        try:
603
            with activity.sub_activity(
Bálint Máhonfai committed
604 605
                    'migrate_vm', readable_name=create_readable(
                        ugettext_noop("migrate to %(node)s"), node=to_node)):
606 607
                super(MigrateOperation, self)._operation(
                    to_node=to_node, live_migration=live_migration)
608 609 610
        except Exception as e:
            if hasattr(e, 'libvirtError'):
                self.rollback(activity)
Bach Dániel committed
611
            raise
Dudás Ádám committed
612

613
        # Shutdown networks
614
        with activity.sub_activity(
Bálint Máhonfai committed
615 616
                'shutdown_net', readable_name=ugettext_noop(
                    "shutdown network")):
617 618
            self.instance.shutdown_net()

Dudás Ádám committed
619 620 621
        # Refresh node information
        self.instance.node = to_node
        self.instance.save()
622

Dudás Ádám committed
623
        # Estabilish network connection (vmdriver)
624
        with activity.sub_activity(
Bálint Máhonfai committed
625 626
                'deploying_net', readable_name=ugettext_noop(
                    "deploy network")):
Dudás Ádám committed
627
            self.instance.deploy_net()
Dudás Ádám committed
628 629


630
@register_operation
631
class RebootOperation(RemoteInstanceOperation):
Dudás Ádám committed
632 633
    id = 'reboot'
    name = _("reboot")
634 635
    description = _("Warm reboot virtual machine by sending Ctrl+Alt+Del "
                    "signal to its console.")
636
    required_perms = ()
Bálint Máhonfai committed
637
    accept_states = ('RUNNING',)
638
    task = vm_tasks.reboot
Dudás Ádám committed
639

640 641
    def _operation(self, activity):
        super(RebootOperation, self)._operation()
642 643 644
        if self.instance.has_agent:
            activity.sub_activity('os_boot', readable_name=ugettext_noop(
                "wait operating system loading"), interruptible=True)
Dudás Ádám committed
645 646


647
@register_operation
648 649 650
class RemoveInterfaceOperation(InstanceOperation):
    id = 'remove_interface'
    name = _("remove interface")
651 652 653
    description = _("Remove the specified network interface and erase IP "
                    "address allocations, related firewall rules and "
                    "hostnames.")
654
    required_perms = ()
655
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
656

657 658
    def _operation(self, activity, user, system, interface):
        if self.instance.is_running:
659 660
            self.instance._detach_network(interface=interface,
                                          parent_activity=activity)
661 662 663 664 665
            interface.shutdown()

        interface.destroy()
        interface.delete()

666 667
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop("remove %(vlan)s interface"),
668
                               vlan=kwargs['interface'].vlan)
669

670

671
@register_operation
672 673 674 675 676
class RemovePortOperation(InstanceOperation):
    id = 'remove_port'
    name = _("close port")
    description = _("Close the specified port.")
    concurrency_check = False
677
    acl_level = "operator"
Bálint Máhonfai committed
678
    required_perms = ('vm.config_ports',)
679 680 681 682

    def _operation(self, activity, rule):
        interface = rule.host.interface_set.get()
        if interface.instance != self.instance:
683
            raise SuspiciousOperation()
684 685 686 687 688 689 690
        activity.readable_name = create_readable(
            ugettext_noop("close %(proto)s/%(port)d on %(host)s"),
            proto=rule.proto, port=rule.dport, host=rule.host)
        rule.delete()


@register_operation
691 692 693 694 695
class AddPortOperation(InstanceOperation):
    id = 'add_port'
    name = _("open port")
    description = _("Open the specified port.")
    concurrency_check = False
696
    acl_level = "operator"
Bálint Máhonfai committed
697
    required_perms = ('vm.config_ports',)
698 699 700

    def _operation(self, activity, host, proto, port):
        if host.interface_set.get().instance != self.instance:
701
            raise SuspiciousOperation()
702 703 704 705 706 707 708
        host.add_port(proto, private=port)
        activity.readable_name = create_readable(
            ugettext_noop("open %(proto)s/%(port)d on %(host)s"),
            proto=proto, port=port, host=host)


@register_operation
709 710 711
class RemoveDiskOperation(InstanceOperation):
    id = 'remove_disk'
    name = _("remove disk")
712 713
    description = _("Remove the specified disk from the virtual machine, and "
                    "destroy the data.")
714
    required_perms = ()
715
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
716 717

    def _operation(self, activity, user, system, disk):
718
        if self.instance.is_running and disk.type not in ["iso"]:
719
            self.instance._detach_disk(disk=disk, parent_activity=activity)
720
        with activity.sub_activity(
Bálint Máhonfai committed
721 722
                'destroy_disk',
                readable_name=ugettext_noop('destroy disk')
723
        ):
724
            disk.destroy()
725
            return self.instance.disks.remove(disk)
726

727 728 729
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop('remove disk %(name)s'),
                               name=kwargs["disk"].name)
730 731


732
@register_operation
733
class ResetOperation(RemoteInstanceOperation):
Dudás Ádám committed
734 735
    id = 'reset'
    name = _("reset")
736
    description = _("Cold reboot virtual machine (power cycle).")
737
    required_perms = ()
Bálint Máhonfai committed
738
    accept_states = ('RUNNING',)
739
    task = vm_tasks.reset
Dudás Ádám committed
740

741 742
    def _operation(self, activity):
        super(ResetOperation, self)._operation()
743 744 745
        if self.instance.has_agent:
            activity.sub_activity('os_boot', readable_name=ugettext_noop(
                "wait operating system loading"), interruptible=True)
Dudás Ádám committed
746 747


748
@register_operation
749
class SaveAsTemplateOperation(InstanceOperation):
Dudás Ádám committed
750 751
    id = 'save_as_template'
    name = _("save as template")
752 753 754 755
    description = _("Save virtual machine as a template so they can be shared "
                    "with users and groups.  Anyone who has access to a "
                    "template (and to the networks it uses) will be able to "
                    "start an instance of it.")
756
    has_percentage = True
757
    abortable = True
Bálint Máhonfai committed
758
    required_perms = ('vm.create_template',)
759
    accept_states = ('RUNNING', 'STOPPED')
760
    async_queue = "localhost.man.slow"
Dudás Ádám committed
761

762 763 764 765
    def is_preferred(self):
        return (self.instance.is_base and
                self.instance.status == self.instance.STATUS.RUNNING)

766 767 768 769 770 771
    @staticmethod
    def _rename(name):
        m = search(r" v(\d+)$", name)
        if m:
            v = int(m.group(1)) + 1
            name = search(r"^(.*) v(\d+)$", name).group(1)
772
        else:
773 774
            v = 1
        return "%s v%d" % (name, v)
775

776
    def on_abort(self, activity, error):
777
        if hasattr(self, 'disks'):
778 779 780
            for disk in self.disks:
                disk.destroy()

781
    def _operation(self, activity, user, system, name=None,
782
                   with_shutdown=True, clone=False, task=None, **kwargs):
783 784
        try:
            self.instance._cleanup(parent_activity=activity, user=user)
785
        except:
786 787
            pass

788
        if with_shutdown:
789
            try:
790 791
                self.instance.shutdown(parent_activity=activity,
                                       user=user, task=task)
792 793 794
            except Instance.WrongStateError:
                pass

Dudás Ádám committed
795 796 797 798 799 800 801 802
        # prepare parameters
        params = {
            'access_method': self.instance.access_method,
            'arch': self.instance.arch,
            'boot_menu': self.instance.boot_menu,
            'description': self.instance.description,
            'lease': self.instance.lease,  # Can be problem in new VM
            'max_ram_size': self.instance.max_ram_size,
803
            'name': name or self._rename(self.instance.name),
Dudás Ádám committed
804 805
            'num_cores': self.instance.num_cores,
            'owner': user,
806
            'parent': self.instance.template or None,  # Can be problem
Dudás Ádám committed
807 808 809 810 811 812
            'priority': self.instance.priority,
            'ram_size': self.instance.ram_size,
            'raw_data': self.instance.raw_data,
            'system': self.instance.system,
        }
        params.update(kwargs)
Bach Dániel committed
813
        params.pop("parent_activity", None)
Dudás Ádám committed
814

815 816
        from storage.models import Disk

Dudás Ádám committed
817 818
        def __try_save_disk(disk):
            try:
819
                return disk.save_as(task)
Dudás Ádám committed
820 821 822
            except Disk.WrongDiskTypeError:
                return disk

823
        self.disks = []
824 825
        for disk in self.instance.disks.all():
            with activity.sub_activity(
Bálint Máhonfai committed
826 827 828 829
                    'saving_disk',
                    readable_name=create_readable(
                        ugettext_noop("saving disk %(name)s"),
                        name=disk.name)
830
            ):
831 832
                self.disks.append(__try_save_disk(disk))

Dudás Ádám committed
833 834 835 836
        # create template and do additional setup
        tmpl = InstanceTemplate(**params)
        tmpl.full_clean()  # Avoiding database errors.
        tmpl.save()
837
        # Copy traits from the VM instance
838
        tmpl.req_traits.add(*self.instance.req_traits.all())
839 840
        if clone:
            tmpl.clone_acl(self.instance.template)
Guba Sándor committed
841
            # Add permission for the original owner of the template
842 843
            tmpl.set_level(self.instance.template.owner, 'owner')
            tmpl.set_level(user, 'owner')
Dudás Ádám committed
844
        try:
845
            tmpl.disks.add(*self.disks)
Dudás Ádám committed
846 847 848 849 850 851 852
            # create interface templates
            for i in self.instance.interface_set.all():
                i.save_as_template(tmpl)
        except:
            tmpl.delete()
            raise
        else:
853 854 855 856
            return create_readable(
                ugettext_noop("New template: %(template)s"),
                template=reverse('dashboard.views.template-detail',
                                 kwargs={'pk': tmpl.pk}))
Dudás Ádám committed
857 858


859
@register_operation
860 861
class ShutdownOperation(AbortableRemoteOperationMixin,
                        RemoteInstanceOperation):
Dudás Ádám committed
862 863
    id = 'shutdown'
    name = _("shutdown")
864 865 866 867
    description = _("Try to halt virtual machine by a standard ACPI signal, "
                    "allowing the operating system to keep a consistent "
                    "state. The operation will fail if the machine does not "
                    "turn itself off in a period.")
Kálmán Viktor committed
868
    abortable = True
869
    required_perms = ()
Bálint Máhonfai committed
870
    accept_states = ('RUNNING',)
871
    resultant_state = 'STOPPED'
872 873
    task = vm_tasks.shutdown
    remote_queue = ("vm", "slow")
874
    remote_timeout = 180
Dudás Ádám committed
875

876 877
    def _operation(self, task):
        super(ShutdownOperation, self)._operation(task=task)
Dudás Ádám committed
878
        self.instance.yield_node()
Dudás Ádám committed
879

880 881 882 883 884 885 886 887 888 889 890
    def on_abort(self, activity, error):
        if isinstance(error, TimeLimitExceeded):
            activity.result = humanize_exception(ugettext_noop(
                "The virtual machine did not switch off in the provided time "
                "limit. Most of the time this is caused by incorrect ACPI "
                "settings. You can also try to power off the machine from the "
                "operating system manually."), error)
            activity.resultant_state = None
        else:
            super(ShutdownOperation, self).on_abort(activity, error)

Dudás Ádám committed
891

892
@register_operation
893
class ShutOffOperation(InstanceOperation):
Dudás Ádám committed
894 895
    id = 'shut_off'
    name = _("shut off")
896 897 898 899 900 901 902
    description = _("Forcibly halt a virtual machine without notifying the "
                    "operating system. This operation will even work in cases "
                    "when shutdown does not, but the operating system and the "
                    "file systems are likely to be in an inconsistent state,  "
                    "so data loss is also possible. The effect of this "
                    "operation is the same as interrupting the power supply "
                    "of a physical machine.")
903
    required_perms = ()
904
    accept_states = ('RUNNING', 'PAUSED')
905
    resultant_state = 'STOPPED'
Dudás Ádám committed
906

907
    def _operation(self, activity):
Dudás Ádám committed
908
        # Shutdown networks
909 910 911
        with activity.sub_activity('shutdown_net',
                                   readable_name=ugettext_noop(
                                       "shutdown network")):
Dudás Ádám committed
912
            self.instance.shutdown_net()
Dudás Ádám committed
913

914
        self.instance._delete_vm(parent_activity=activity)
Dudás Ádám committed
915
        self.instance.yield_node()
Dudás Ádám committed
916 917


918
@register_operation
919
class SleepOperation(InstanceOperation):
Dudás Ádám committed
920 921
    id = 'sleep'
    name = _("sleep")
922 923 924 925 926 927 928 929
    description = _("Suspend virtual machine. This means the machine is "
                    "stopped and its memory is saved to disk, so if the "
                    "machine is waked up, all the applications will keep "
                    "running. Most of the applications will be able to "
                    "continue even after a long suspension, but those which "
                    "need a continous network connection may fail when "
                    "resumed. In the meantime, the machine will only use "
                    "storage resources, and keep network resources allocated.")
930
    required_perms = ()
Bálint Máhonfai committed
931
    accept_states = ('RUNNING',)
932
    resultant_state = 'SUSPENDED'
933
    async_queue = "localhost.man.slow"
Dudás Ádám committed
934

935 936 937 938
    def is_preferred(self):
        return (not self.instance.is_base and
                self.instance.status == self.instance.STATUS.RUNNING)

Dudás Ádám committed
939 940 941 942 943 944
    def on_abort(self, activity, error):
        if isinstance(error, TimeLimitExceeded):
            activity.resultant_state = None
        else:
            activity.resultant_state = 'ERROR'

945
    def _operation(self, activity, system):
946 947 948
        with activity.sub_activity('shutdown_net',
                                   readable_name=ugettext_noop(
                                       "shutdown network")):
Dudás Ádám committed
949
            self.instance.shutdown_net()
950
        self.instance._suspend_vm(parent_activity=activity)
951
        self.instance.yield_node()
Dudás Ádám committed
952

953 954 955 956
    @register_operation
    class SuspendVmOperation(SubOperationMixin, RemoteInstanceOperation):
        id = "_suspend_vm"
        name = _("suspend virtual machine")
957
        task = vm_tasks.sleep
958
        remote_queue = ("vm", "slow")
959
        remote_timeout = 1000
Dudás Ádám committed
960

961 962
        def _get_remote_args(self, **kwargs):
            return (super(SleepOperation.SuspendVmOperation, self)
963 964
                    ._get_remote_args(**kwargs) +
                    [self.instance.mem_dump['path']])
Dudás Ádám committed
965 966


967
@register_operation
968
class WakeUpOperation(InstanceOperation):
Dudás Ádám committed
969 970
    id = 'wake_up'
    name = _("wake up")
971 972 973
    description = _("Wake up sleeping (suspended) virtual machine. This will "
                    "load the saved memory of the system and start the "
                    "virtual machine from this state.")
974
    required_perms = ()
Bálint Máhonfai committed
975
    accept_states = ('SUSPENDED',)
976
    resultant_state = 'RUNNING'
977
    async_queue = "localhost.man.slow"
Dudás Ádám committed
978

979
    def is_preferred(self):
980
        return self.instance.status == self.instance.STATUS.SUSPENDED
981

Dudás Ádám committed
982
    def on_abort(self, activity, error):
Bach Dániel committed
983 984 985 986
        if isinstance(error, SchedulerError):
            activity.resultant_state = None
        else:
            activity.resultant_state = 'ERROR'
Dudás Ádám committed
987

988
    def _operation(self, activity):
Dudás Ádám committed
989
        # Schedule vm
Dudás Ádám committed
990
        self.instance.allocate_vnc_port()
991
        self.instance.allocate_node()
Dudás Ádám committed
992 993

        # Resume vm
994
        self.instance._wake_up_vm(parent_activity=activity)
Dudás Ádám committed
995 996

        # Estabilish network connection (vmdriver)
997
        with activity.sub_activity(
Bálint Máhonfai committed
998 999
                'deploying_net', readable_name=ugettext_noop(
                    "deploy network")):
Dudás Ádám committed
1000
            self.instance.deploy_net()
Dudás Ádám committed
1001

1002 1003 1004 1005
        try:
            self.instance.renew(parent_activity=activity)
        except:
            pass
Dudás Ádám committed
1006

1007 1008 1009 1010 1011 1012
    @register_operation
    class WakeUpVmOperation(SubOperationMixin, RemoteInstanceOperation):
        id = "_wake_up_vm"
        name = _("resume virtual machine")
        task = vm_tasks.wake_up
        remote_queue = ("vm", "slow")
1013
        remote_timeout = 1000
1014 1015 1016

        def _get_remote_args(self, **kwargs):
            return (super(WakeUpOperation.WakeUpVmOperation, self)
1017 1018
                    ._get_remote_args(**kwargs) +
                    [self.instance.mem_dump['path']])
1019

Dudás Ádám committed
1020

1021
@register_operation
1022 1023 1024
class RenewOperation(InstanceOperation):
    id = 'renew'
    name = _("renew")
1025 1026 1027 1028
    description = _("Virtual machines are suspended and destroyed after they "
                    "expire. This operation renews expiration times according "
                    "to the lease type. If the machine is close to the "
                    "expiration, its owner will be notified.")
1029
    acl_level = "operator"
1030
    required_perms = ()
1031
    concurrency_check = False
1032

1033 1034
    def set_time_of_suspend(self, activity, suspend, force):
        with activity.sub_activity(
Bálint Máhonfai committed
1035
                'renew_suspend', concurrency_check=False,
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
                readable_name=ugettext_noop('set time of suspend')):
            if (not force and suspend and self.instance.time_of_suspend and
                    suspend < self.instance.time_of_suspend):
                raise HumanReadableException.create(ugettext_noop(
                    "Renewing the machine with the selected lease would "
                    "result in its suspension time get earlier than before."))
            self.instance.time_of_suspend = suspend

    def set_time_of_delete(self, activity, delete, force):
        with activity.sub_activity(
Bálint Máhonfai committed
1046
                'renew_delete', concurrency_check=False,
1047 1048 1049 1050 1051 1052 1053 1054
                readable_name=ugettext_noop('set time of delete')):
            if (not force and delete and self.instance.time_of_delete and
                    delete < self.instance.time_of_delete):
                raise HumanReadableException.create(ugettext_noop(
                    "Renewing the machine with the selected lease would "
                    "result in its delete time get earlier than before."))
            self.instance.time_of_delete = delete

Őry Máté committed
1055
    def _operation(self, activity, lease=None, force=False, save=False):
1056
        suspend, delete = self.instance.get_renew_times(lease)
1057 1058 1059 1060 1061 1062 1063 1064 1065
        try:
            self.set_time_of_suspend(activity, suspend, force)
        except HumanReadableException:
            pass
        try:
            self.set_time_of_delete(activity, delete, force)
        except HumanReadableException:
            pass

Őry Máté committed
1066 1067
        if save:
            self.instance.lease = lease
1068

1069
        self.instance.save()