operations.py 62.9 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
Bach Dániel committed
19 20
from base64 import encodestring
from hashlib import md5
Dudás Ádám committed
21
from logging import getLogger
Bach Dániel committed
22
import os
23
from re import search
Őry Máté committed
24
from string import ascii_lowercase
Bach Dániel committed
25 26 27
from StringIO import StringIO
from tarfile import TarFile, TarInfo
import time
Kálmán Viktor committed
28
from urlparse import urlsplit
Dudás Ádám committed
29

30
from django.core.exceptions import PermissionDenied, SuspiciousOperation
31
from django.core.urlresolvers import reverse
Dudás Ádám committed
32
from django.utils import timezone
33
from django.utils.translation import ugettext_lazy as _, ugettext_noop
Kálmán Viktor committed
34
from django.conf import settings
Bach Dániel committed
35
from django.db.models import Q
Dudás Ádám committed
36

37 38
from sizefield.utils import filesizeformat

39 40
from celery.contrib.abortable import AbortableAsyncResult
from celery.exceptions import TimeLimitExceeded, TimeoutError
41

42 43 44
from common.models import (
    create_readable, humanize_exception, HumanReadableException
)
45
from common.operations import Operation, register_operation, SubOperationMixin
Bach Dániel committed
46
from manager.scheduler import SchedulerError
47 48 49
from .tasks.local_tasks import (
    abortable_async_instance_operation, abortable_async_node_operation,
)
50
from .models import (
51
    Instance, InstanceActivity, InstanceTemplate, Interface, Node,
52
    NodeActivity, pwgen
53
)
Bach Dániel committed
54
from .tasks import agent_tasks, vm_tasks
Dudás Ádám committed
55

Kálmán Viktor committed
56
from dashboard.store_api import Store, NoStoreException
Bach Dániel committed
57 58
from firewall.models import Host
from monitor.client import Client
59
from storage.tasks import storage_tasks
Kálmán Viktor committed
60

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


64 65 66 67 68
class RemoteOperationMixin(object):

    remote_timeout = 30

    def _operation(self, **kwargs):
Őry Máté committed
69
        args = self._get_remote_args(**kwargs)
70 71 72 73 74 75 76 77 78
        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()


79 80 81 82 83 84 85
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())
86
        for i in xrange(0, self.remote_timeout, self.remote_step):
87 88 89 90 91 92 93
            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)
94
        raise TimeLimitExceeded()
95 96


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

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

    def check_precond(self):
111 112
        if self.instance.destroyed_at:
            raise self.instance.InstanceDestroyedError(self.instance)
113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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)
127 128

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

134
        super(InstanceOperation, self).check_auth(user=user)
135

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

140 141
    def create_activity(self, parent, user, kwargs):
        name = self.get_activity_name(kwargs)
142 143 144 145 146 147 148 149 150 151
        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.")

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

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

168

169 170 171 172 173 174 175 176 177 178 179
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
180 181 182 183 184 185 186 187 188 189 190 191 192 193
class EnsureAgentMixin(object):
    accept_states = ('RUNNING', )

    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",
194 195
                started__gt=last_boot_time, instance=self.instance
            ).latest("started")
Bach Dániel committed
196 197 198 199 200 201 202 203 204
        except InstanceActivity.DoesNotExist:  # no agent since last boot
            raise self.instance.NoAgentError(self.instance)


class RemoteAgentOperation(EnsureAgentMixin, RemoteInstanceOperation):
    remote_queue = ('agent', )
    concurrency_check = False


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

214 215 216 217 218 219 220
    def rollback(self, net, activity):
        with activity.sub_activity(
            'destroying_net',
                readable_name=ugettext_noop("destroy network (rollback)")):
            net.destroy()
            net.delete()

221
    def _operation(self, activity, user, system, vlan, managed=None):
222
        if not vlan.has_level(user, 'user'):
223 224 225
            raise humanize_exception(ugettext_noop(
                "User acces to vlan %(vlan)s is required."),
                PermissionDenied(), vlan=vlan)
226 227 228 229 230 231 232
        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:
233
            try:
234 235
                self.instance._attach_network(
                    interface=net, parent_activity=activity)
236 237 238 239
            except Exception as e:
                if hasattr(e, 'libvirtError'):
                    self.rollback(net, activity)
                raise
240
            net.deploy()
Bach Dániel committed
241 242
            self.instance._change_ip(parent_activity=activity)
            self.instance._restart_networking(parent_activity=activity)
243

244 245 246 247
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop("add %(vlan)s interface"),
                               vlan=kwargs['vlan'])

248

249
@register_operation
250
class CreateDiskOperation(InstanceOperation):
251

252 253
    id = 'create_disk'
    name = _("create disk")
254
    description = _("Create and attach empty disk to the virtual machine.")
255
    required_perms = ('storage.create_empty_disk', )
256
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
257

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

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

272
        if self.instance.is_running:
273 274 275 276
            with activity.sub_activity(
                'deploying_disk',
                readable_name=ugettext_noop("deploying disk")
            ):
277
                disk.deploy()
278
            self.instance._attach_disk(parent_activity=activity, disk=disk)
279

280
    def get_activity_name(self, kwargs):
281 282 283
        return create_readable(
            ugettext_noop("create disk %(name)s (%(size)s)"),
            size=filesizeformat(kwargs['size']), name=kwargs['name'])
284 285


286
@register_operation
287
class ResizeDiskOperation(RemoteInstanceOperation):
288 289 290 291 292

    id = 'resize_disk'
    name = _("resize disk")
    description = _("Resize the virtual disk image. "
                    "Size must be greater value than the actual size.")
293
    required_perms = ('storage.resize_disk', )
294 295
    accept_states = ('RUNNING', )
    async_queue = "localhost.man.slow"
296 297
    remote_queue = ('vm', 'slow')
    task = vm_tasks.resize_disk
298

299 300 301
    def _get_remote_args(self, disk, size, **kwargs):
        return (super(ResizeDiskOperation, self)
                ._get_remote_args(**kwargs) + [disk.path, size])
302 303 304 305 306 307

    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)

308
    def _operation(self, disk, size):
309 310 311
        if not disk.is_resizable:
            raise HumanReadableException.create(ugettext_noop(
                'Disk type "%(type)s" is not resizable.'), type=disk.type)
312 313 314 315
        super(ResizeDiskOperation, self)._operation(disk=disk, size=size)
        disk.size = size
        disk.save()

316

317
@register_operation
318 319 320
class DownloadDiskOperation(InstanceOperation):
    id = 'download_disk'
    name = _("download disk")
321 322 323 324
    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.")
325
    abortable = True
326
    has_percentage = True
327
    required_perms = ('storage.download_disk', )
328
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
329
    async_queue = "localhost.man.slow"
330

331
    def _operation(self, user, url, task, activity, name=None):
Bach Dániel committed
332 333
        from storage.models import Disk

334
        disk = Disk.download(url=url, name=name, task=task)
335 336 337 338
        devnums = list(ascii_lowercase)
        for d in self.instance.disks.all():
            devnums.remove(d.dev_num)
        disk.dev_num = devnums.pop(0)
339
        disk.full_clean()
340
        disk.save()
341
        self.instance.disks.add(disk)
342 343
        activity.readable_name = create_readable(
            ugettext_noop("download %(name)s"), name=disk.name)
344

345 346 347 348
        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
349
        # TODO iso (cd) hot-plug is not supported by kvm/guests
350
        if self.instance.is_running and disk.type not in ["iso"]:
351
            self.instance._attach_disk(parent_activity=activity, disk=disk)
352

353

354
@register_operation
355
class DeployOperation(InstanceOperation):
Dudás Ádám committed
356 357
    id = 'deploy'
    name = _("deploy")
358 359
    description = _("Deploy and start the virtual machine (including storage "
                    "and network configuration).")
360
    required_perms = ()
361
    deny_states = ('SUSPENDED', 'RUNNING')
362
    resultant_state = 'RUNNING'
Dudás Ádám committed
363

364 365
    def is_preferred(self):
        return self.instance.status in (self.instance.STATUS.STOPPED,
366
                                        self.instance.STATUS.PENDING,
367 368
                                        self.instance.STATUS.ERROR)

369 370 371
    def on_abort(self, activity, error):
        activity.resultant_state = 'STOPPED'

Dudás Ádám committed
372 373
    def on_commit(self, activity):
        activity.resultant_state = 'RUNNING'
374
        activity.result = create_readable(
Guba Sándor committed
375
            ugettext_noop("virtual machine successfully "
376 377
                          "deployed to node: %(node)s"),
            node=self.instance.node)
Dudás Ádám committed
378

379
    def _operation(self, activity, node=None):
Dudás Ádám committed
380 381
        # Allocate VNC port and host node
        self.instance.allocate_vnc_port()
382 383 384 385 386
        if node is not None:
            self.instance.node = node
            self.instance.save()
        else:
            self.instance.allocate_node()
Dudás Ádám committed
387 388

        # Deploy virtual images
389 390 391 392 393 394
        try:
            self.instance._deploy_disks(parent_activity=activity)
        except:
            self.instance.yield_node()
            self.instance.yield_vnc_port()
            raise
Dudás Ádám committed
395 396

        # Deploy VM on remote machine
397
        if self.instance.state not in ['PAUSED']:
398
            self.instance._deploy_vm(parent_activity=activity)
Dudás Ádám committed
399 400

        # Establish network connection (vmdriver)
401 402 403
        with activity.sub_activity(
            'deploying_net', readable_name=ugettext_noop(
                "deploy network")):
Dudás Ádám committed
404 405
            self.instance.deploy_net()

406 407 408 409 410
        try:
            self.instance.renew(parent_activity=activity)
        except:
            pass

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

413 414 415
        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
416

417
    @register_operation
Őry Máté committed
418
    class DeployVmOperation(SubOperationMixin, RemoteInstanceOperation):
419 420
        id = "_deploy_vm"
        name = _("deploy vm")
Őry Máté committed
421
        description = _("Deploy virtual machine.")
422 423 424
        remote_queue = ("vm", "slow")
        task = vm_tasks.deploy

Őry Máté committed
425
        def _get_remote_args(self, **kwargs):
426 427 428 429 430 431 432 433 434
            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
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    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()

452
    @register_operation
Őry Máté committed
453
    class ResumeVmOperation(SubOperationMixin, RemoteInstanceOperation):
454 455 456 457 458
        id = "_resume_vm"
        name = _("boot virtual machine")
        remote_queue = ("vm", "slow")
        task = vm_tasks.resume

Dudás Ádám committed
459

460
@register_operation
461
class DestroyOperation(InstanceOperation):
Dudás Ádám committed
462 463
    id = 'destroy'
    name = _("destroy")
464 465
    description = _("Permanently destroy virtual machine, its network "
                    "settings and disks.")
466
    required_perms = ()
467
    resultant_state = 'DESTROYED'
Dudás Ádám committed
468

469 470 471
    def on_abort(self, activity, error):
        activity.resultant_state = None

472
    def _operation(self, activity, system):
473
        # Destroy networks
474 475 476
        with activity.sub_activity(
                'destroying_net',
                readable_name=ugettext_noop("destroy network")):
477
            if self.instance.node:
478
                self.instance.shutdown_net()
479
            self.instance.destroy_net()
Dudás Ádám committed
480

481
        if self.instance.node:
482
            self.instance._delete_vm(parent_activity=activity)
Dudás Ádám committed
483 484

        # Destroy disks
485 486 487
        with activity.sub_activity(
                'destroying_disks',
                readable_name=ugettext_noop("destroy disks")):
Dudás Ádám committed
488
            self.instance.destroy_disks()
Dudás Ádám committed
489

Dudás Ádám committed
490 491
        # Delete mem. dump if exists
        try:
492
            self.instance._delete_mem_dump(parent_activity=activity)
Dudás Ádám committed
493 494 495 496 497 498
        except:
            pass

        # Clear node and VNC port association
        self.instance.yield_node()
        self.instance.yield_vnc_port()
Dudás Ádám committed
499 500 501 502

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

503
    @register_operation
504
    class DeleteVmOperation(SubOperationMixin, RemoteInstanceOperation):
505 506 507 508 509
        id = "_delete_vm"
        name = _("destroy virtual machine")
        task = vm_tasks.destroy
        # if e.libvirtError and "Domain not found" in str(e):

510 511 512 513 514 515 516 517 518 519 520 521 522 523
    @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
524

525
@register_operation
526
class MigrateOperation(RemoteInstanceOperation):
Dudás Ádám committed
527 528
    id = 'migrate'
    name = _("migrate")
529 530
    description = _("Move a running virtual machine to an other worker node "
                    "keeping its full state.")
531
    required_perms = ()
532
    superuser_required = True
533
    accept_states = ('RUNNING', )
534
    async_queue = "localhost.man.slow"
535 536
    task = vm_tasks.migrate
    remote_queue = ("vm", "slow")
537
    remote_timeout = 1000
538

539
    def _get_remote_args(self, to_node, live_migration, **kwargs):
540 541
        return (super(MigrateOperation, self)._get_remote_args(**kwargs) +
                [to_node.host.hostname, live_migration])
Dudás Ádám committed
542

543
    def rollback(self, activity):
544 545 546
        with activity.sub_activity(
            'rollback_net', readable_name=ugettext_noop(
                "redeploy network (rollback)")):
547 548
            self.instance.deploy_net()

549
    def _operation(self, activity, to_node=None, live_migration=True):
Dudás Ádám committed
550
        if not to_node:
Bach Dániel committed
551 552 553 554 555 556
            with activity.sub_activity('scheduling',
                                       readable_name=ugettext_noop(
                                           "schedule")) as sa:
                to_node = self.instance.select_node()
                sa.result = to_node

557
        try:
558 559 560
            with activity.sub_activity(
                'migrate_vm', readable_name=create_readable(
                    ugettext_noop("migrate to %(node)s"), node=to_node)):
561 562
                super(MigrateOperation, self)._operation(
                    to_node=to_node, live_migration=live_migration)
563 564 565
        except Exception as e:
            if hasattr(e, 'libvirtError'):
                self.rollback(activity)
Bach Dániel committed
566
            raise
Dudás Ádám committed
567

568
        # Shutdown networks
569 570 571
        with activity.sub_activity(
            'shutdown_net', readable_name=ugettext_noop(
                "shutdown network")):
572 573
            self.instance.shutdown_net()

Dudás Ádám committed
574 575 576
        # Refresh node information
        self.instance.node = to_node
        self.instance.save()
577

Dudás Ádám committed
578
        # Estabilish network connection (vmdriver)
579 580 581
        with activity.sub_activity(
            'deploying_net', readable_name=ugettext_noop(
                "deploy network")):
Dudás Ádám committed
582
            self.instance.deploy_net()
Dudás Ádám committed
583 584


585
@register_operation
586
class RebootOperation(RemoteInstanceOperation):
Dudás Ádám committed
587 588
    id = 'reboot'
    name = _("reboot")
589 590
    description = _("Warm reboot virtual machine by sending Ctrl+Alt+Del "
                    "signal to its console.")
591
    required_perms = ()
592
    accept_states = ('RUNNING', )
593
    task = vm_tasks.reboot
Dudás Ádám committed
594

595 596
    def _operation(self, activity):
        super(RebootOperation, self)._operation()
597 598 599
        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
600 601


602
@register_operation
603 604 605
class RemoveInterfaceOperation(InstanceOperation):
    id = 'remove_interface'
    name = _("remove interface")
606 607 608
    description = _("Remove the specified network interface and erase IP "
                    "address allocations, related firewall rules and "
                    "hostnames.")
609
    required_perms = ()
610
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
611

612 613
    def _operation(self, activity, user, system, interface):
        if self.instance.is_running:
614 615
            self.instance._detach_network(interface=interface,
                                          parent_activity=activity)
616 617 618 619 620
            interface.shutdown()

        interface.destroy()
        interface.delete()

621 622
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop("remove %(vlan)s interface"),
623
                               vlan=kwargs['interface'].vlan)
624

625

626
@register_operation
627 628 629 630 631
class RemovePortOperation(InstanceOperation):
    id = 'remove_port'
    name = _("close port")
    description = _("Close the specified port.")
    concurrency_check = False
632
    acl_level = "operator"
633
    required_perms = ('vm.config_ports', )
634 635 636 637

    def _operation(self, activity, rule):
        interface = rule.host.interface_set.get()
        if interface.instance != self.instance:
638
            raise SuspiciousOperation()
639 640 641 642 643 644 645
        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
646 647 648 649 650
class AddPortOperation(InstanceOperation):
    id = 'add_port'
    name = _("open port")
    description = _("Open the specified port.")
    concurrency_check = False
651
    acl_level = "operator"
652
    required_perms = ('vm.config_ports', )
653 654 655

    def _operation(self, activity, host, proto, port):
        if host.interface_set.get().instance != self.instance:
656
            raise SuspiciousOperation()
657 658 659 660 661 662 663
        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
664 665 666
class RemoveDiskOperation(InstanceOperation):
    id = 'remove_disk'
    name = _("remove disk")
667 668
    description = _("Remove the specified disk from the virtual machine, and "
                    "destroy the data.")
669
    required_perms = ()
670
    accept_states = ('STOPPED', 'PENDING', 'RUNNING')
671 672

    def _operation(self, activity, user, system, disk):
673
        if self.instance.is_running and disk.type not in ["iso"]:
674
            self.instance._detach_disk(disk=disk, parent_activity=activity)
675 676 677 678
        with activity.sub_activity(
            'destroy_disk',
            readable_name=ugettext_noop('destroy disk')
        ):
679
            disk.destroy()
680
            return self.instance.disks.remove(disk)
681

682 683 684
    def get_activity_name(self, kwargs):
        return create_readable(ugettext_noop('remove disk %(name)s'),
                               name=kwargs["disk"].name)
685 686


687
@register_operation
688
class ResetOperation(RemoteInstanceOperation):
Dudás Ádám committed
689 690
    id = 'reset'
    name = _("reset")
691
    description = _("Cold reboot virtual machine (power cycle).")
692
    required_perms = ()
693
    accept_states = ('RUNNING', )
694
    task = vm_tasks.reset
Dudás Ádám committed
695

696 697
    def _operation(self, activity):
        super(ResetOperation, self)._operation()
698 699 700
        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
701 702


703
@register_operation
704
class SaveAsTemplateOperation(InstanceOperation):
Dudás Ádám committed
705 706
    id = 'save_as_template'
    name = _("save as template")
707 708 709 710
    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.")
711
    has_percentage = True
712
    abortable = True
713
    required_perms = ('vm.create_template', )
714
    accept_states = ('RUNNING', 'STOPPED')
715
    async_queue = "localhost.man.slow"
Dudás Ádám committed
716

717 718 719 720
    def is_preferred(self):
        return (self.instance.is_base and
                self.instance.status == self.instance.STATUS.RUNNING)

721 722 723 724 725 726
    @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)
727
        else:
728 729
            v = 1
        return "%s v%d" % (name, v)
730

731
    def on_abort(self, activity, error):
732
        if hasattr(self, 'disks'):
733 734 735
            for disk in self.disks:
                disk.destroy()

736
    def _operation(self, activity, user, system, name=None,
737
                   with_shutdown=True, clone=False, task=None, **kwargs):
738 739
        try:
            self.instance._cleanup(parent_activity=activity, user=user)
740
        except:
741 742
            pass

743
        if with_shutdown:
744
            try:
745 746
                self.instance.shutdown(parent_activity=activity,
                                       user=user, task=task)
747 748 749
            except Instance.WrongStateError:
                pass

Dudás Ádám committed
750 751 752 753 754 755 756 757
        # 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,
758
            'name': name or self._rename(self.instance.name),
Dudás Ádám committed
759 760
            'num_cores': self.instance.num_cores,
            'owner': user,
761
            'parent': self.instance.template or None,  # Can be problem
Dudás Ádám committed
762 763 764 765 766 767
            '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
768
        params.pop("parent_activity", None)
Dudás Ádám committed
769

770 771
        from storage.models import Disk

Dudás Ádám committed
772 773
        def __try_save_disk(disk):
            try:
774
                return disk.save_as(task)
Dudás Ádám committed
775 776 777
            except Disk.WrongDiskTypeError:
                return disk

778
        self.disks = []
779 780 781 782 783 784 785
        for disk in self.instance.disks.all():
            with activity.sub_activity(
                'saving_disk',
                readable_name=create_readable(
                    ugettext_noop("saving disk %(name)s"),
                    name=disk.name)
            ):
786 787
                self.disks.append(__try_save_disk(disk))

Dudás Ádám committed
788 789 790 791
        # create template and do additional setup
        tmpl = InstanceTemplate(**params)
        tmpl.full_clean()  # Avoiding database errors.
        tmpl.save()
792
        # Copy traits from the VM instance
793
        tmpl.req_traits.add(*self.instance.req_traits.all())
794 795
        if clone:
            tmpl.clone_acl(self.instance.template)
Guba Sándor committed
796
            # Add permission for the original owner of the template
797 798
            tmpl.set_level(self.instance.template.owner, 'owner')
            tmpl.set_level(user, 'owner')
Dudás Ádám committed
799
        try:
800
            tmpl.disks.add(*self.disks)
Dudás Ádám committed
801 802 803 804 805 806 807
            # create interface templates
            for i in self.instance.interface_set.all():
                i.save_as_template(tmpl)
        except:
            tmpl.delete()
            raise
        else:
808 809 810 811
            return create_readable(
                ugettext_noop("New template: %(template)s"),
                template=reverse('dashboard.views.template-detail',
                                 kwargs={'pk': tmpl.pk}))
Dudás Ádám committed
812 813


814
@register_operation
815 816
class ShutdownOperation(AbortableRemoteOperationMixin,
                        RemoteInstanceOperation):
Dudás Ádám committed
817 818
    id = 'shutdown'
    name = _("shutdown")
819 820 821 822
    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
823
    abortable = True
824
    required_perms = ()
825
    accept_states = ('RUNNING', )
826
    resultant_state = 'STOPPED'
827 828
    task = vm_tasks.shutdown
    remote_queue = ("vm", "slow")
829
    remote_timeout = 180
Dudás Ádám committed
830

831 832
    def _operation(self, task):
        super(ShutdownOperation, self)._operation(task=task)
Dudás Ádám committed
833
        self.instance.yield_node()
Dudás Ádám committed
834

835 836 837 838 839 840 841 842 843 844 845
    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
846

847
@register_operation
848
class ShutOffOperation(InstanceOperation):
Dudás Ádám committed
849 850
    id = 'shut_off'
    name = _("shut off")
851 852 853 854 855 856 857
    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.")
858
    required_perms = ()
859
    accept_states = ('RUNNING', 'PAUSED')
860
    resultant_state = 'STOPPED'
Dudás Ádám committed
861

862
    def _operation(self, activity):
Dudás Ádám committed
863
        # Shutdown networks
864 865 866
        with activity.sub_activity('shutdown_net',
                                   readable_name=ugettext_noop(
                                       "shutdown network")):
Dudás Ádám committed
867
            self.instance.shutdown_net()
Dudás Ádám committed
868

869
        self.instance._delete_vm(parent_activity=activity)
Dudás Ádám committed
870
        self.instance.yield_node()
Dudás Ádám committed
871 872


873
@register_operation
874
class SleepOperation(InstanceOperation):
Dudás Ádám committed
875 876
    id = 'sleep'
    name = _("sleep")
877 878 879 880 881 882 883 884
    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.")
885
    required_perms = ()
886
    accept_states = ('RUNNING', )
887
    resultant_state = 'SUSPENDED'
888
    async_queue = "localhost.man.slow"
Dudás Ádám committed
889

890 891 892 893
    def is_preferred(self):
        return (not self.instance.is_base and
                self.instance.status == self.instance.STATUS.RUNNING)

Dudás Ádám committed
894 895 896 897 898 899
    def on_abort(self, activity, error):
        if isinstance(error, TimeLimitExceeded):
            activity.resultant_state = None
        else:
            activity.resultant_state = 'ERROR'

900
    def _operation(self, activity, system):
901 902 903
        with activity.sub_activity('shutdown_net',
                                   readable_name=ugettext_noop(
                                       "shutdown network")):
Dudás Ádám committed
904
            self.instance.shutdown_net()
905
        self.instance._suspend_vm(parent_activity=activity)
906
        self.instance.yield_node()
Dudás Ádám committed
907

908 909 910 911
    @register_operation
    class SuspendVmOperation(SubOperationMixin, RemoteInstanceOperation):
        id = "_suspend_vm"
        name = _("suspend virtual machine")
912
        task = vm_tasks.sleep
913
        remote_queue = ("vm", "slow")
914
        remote_timeout = 1000
Dudás Ádám committed
915

916 917
        def _get_remote_args(self, **kwargs):
            return (super(SleepOperation.SuspendVmOperation, self)
918 919
                    ._get_remote_args(**kwargs) +
                    [self.instance.mem_dump['path']])
Dudás Ádám committed
920 921


922
@register_operation
923
class WakeUpOperation(InstanceOperation):
Dudás Ádám committed
924 925
    id = 'wake_up'
    name = _("wake up")
926 927 928
    description = _("Wake up sleeping (suspended) virtual machine. This will "
                    "load the saved memory of the system and start the "
                    "virtual machine from this state.")
929
    required_perms = ()
930
    accept_states = ('SUSPENDED', )
931
    resultant_state = 'RUNNING'
932
    async_queue = "localhost.man.slow"
Dudás Ádám committed
933

934
    def is_preferred(self):
935
        return self.instance.status == self.instance.STATUS.SUSPENDED
936

Dudás Ádám committed
937
    def on_abort(self, activity, error):
Bach Dániel committed
938 939 940 941
        if isinstance(error, SchedulerError):
            activity.resultant_state = None
        else:
            activity.resultant_state = 'ERROR'
Dudás Ádám committed
942

943
    def _operation(self, activity):
Dudás Ádám committed
944
        # Schedule vm
Dudás Ádám committed
945
        self.instance.allocate_vnc_port()
946
        self.instance.allocate_node()
Dudás Ádám committed
947 948

        # Resume vm
949
        self.instance._wake_up_vm(parent_activity=activity)
Dudás Ádám committed
950 951

        # Estabilish network connection (vmdriver)
952 953 954
        with activity.sub_activity(
            'deploying_net', readable_name=ugettext_noop(
                "deploy network")):
Dudás Ádám committed
955
            self.instance.deploy_net()
Dudás Ádám committed
956

957 958 959 960
        try:
            self.instance.renew(parent_activity=activity)
        except:
            pass
Dudás Ádám committed
961

962 963 964 965 966 967
    @register_operation
    class WakeUpVmOperation(SubOperationMixin, RemoteInstanceOperation):
        id = "_wake_up_vm"
        name = _("resume virtual machine")
        task = vm_tasks.wake_up
        remote_queue = ("vm", "slow")
968
        remote_timeout = 1000
969 970 971

        def _get_remote_args(self, **kwargs):
            return (super(WakeUpOperation.WakeUpVmOperation, self)
972 973
                    ._get_remote_args(**kwargs) +
                    [self.instance.mem_dump['path']])
974

Dudás Ádám committed
975

976
@register_operation
977 978 979
class RenewOperation(InstanceOperation):
    id = 'renew'
    name = _("renew")
980 981 982 983
    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.")
984
    acl_level = "operator"
985
    required_perms = ()
986
    concurrency_check = False
987

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    def set_time_of_suspend(self, activity, suspend, force):
        with activity.sub_activity(
            'renew_suspend', concurrency_check=False,
                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(
            'renew_delete', concurrency_check=False,
                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
1010
    def _operation(self, activity, lease=None, force=False, save=False):
1011
        suspend, delete = self.instance.get_renew_times(lease)
1012 1013 1014 1015 1016 1017 1018 1019 1020
        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
1021 1022
        if save:
            self.instance.lease = lease
1023

1024
        self.instance.save()
1025

1026
        return create_readable(ugettext_noop(
1027
            "Renewed to suspend at %(suspend)s and destroy at %(delete)s."),
1028 1029
            suspend=self.instance.time_of_suspend,
            delete=self.instance.time_of_suspend)
1030 1031


1032
@register_operation
1033
class ChangeStateOperation(InstanceOperation):
Guba Sándor committed
1034
    id = 'emergency_change_state'
1035 1036 1037 1038 1039 1040
    name = _("emergency state change")
    description = _("Change the virtual machine state to NOSTATE. This "
                    "should only be used if manual intervention was needed in "
                    "the virtualization layer, and the machine has to be "
                    "redeployed without losing its storage and network "
                    "resources.")
1041
    acl_level = "owner"
Guba Sándor committed
1042
    required_perms = ('vm.emergency_change_state', )
1043
    concurrency_check = False
1044

1045 1046
    def _operation(self, user, activity, new_state="NOSTATE", interrupt=False,
                   reset_node=False):
1047
        activity.resultant_state = new_state
1048 1049 1050 1051 1052 1053 1054
        if interrupt:
            msg_txt = ugettext_noop("Activity is forcibly interrupted.")
            message = create_readable(msg_txt, msg_txt)
            for i in InstanceActivity.objects.filter(
                    finished__isnull=True, instance=self.instance):
                i.finish(False, result=message)
                logger.error('Forced finishing activity %s', i)
1055

1056 1057 1058 1059
        if reset_node:
            self.instance.node = None
            self.instance.save()

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
@register_operation
class RedeployOperation(InstanceOperation):
    id = 'redeploy'
    name = _("redeploy")
    description = _("Change the virtual machine state to NOSTATE "
                    "and redeploy the VM. This operation allows starting "
                    "machines formerly running on a failed node.")
    acl_level = "owner"
    required_perms = ('vm.redeploy', )
    concurrency_check = False

    def _operation(self, user, activity, with_emergency_change_state=True):
        if with_emergency_change_state:
            ChangeStateOperation(self.instance).call(
                parent_activity=activity, user=user,
                new_state='NOSTATE', interrupt=False, reset_node=True)
        else:
            ShutOffOperation(self.instance).call(
                parent_activity=activity, user=user)

        self.instance._update_status()

        DeployOperation(self.instance).call(
            parent_activity=activity, user=user)

1086

1087
class NodeOperation(Operation):
1088
    async_operation = abortable_async_node_operation
1089
    host_cls = Node
1090 1091
    online_required = True
    superuser_required = True
1092 1093 1094 1095 1096

    def __init__(self, node):
        super(NodeOperation, self).__init__(subject=node)
        self.node = node

1097 1098 1099 1100 1101 1102 1103
    def check_precond(self):
        super(NodeOperation, self).check_precond()
        if self.online_required and not self.node.online:
            raise humanize_exception(ugettext_noop(
                "You cannot call this operation on an offline node."),
                Exception())

1104 1105
    def create_activity(self, parent, user, kwargs):
        name = self.get_activity_name(kwargs)
<