Esempio n. 1
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("Please provide a volume ID")

        credentials = Credentials("snf_manage", is_admin=True)
        force = options['force']
        message = "volumes" if len(args) > 1 else "volume"
        self.confirm_deletion(force, message, args)

        for volume_id in args:
            self.stdout.write("\n")
            try:
                volume = common.get_resource("volume", volume_id)
                volumes.delete(volume.id, credentials)

                wait = parse_bool(options["wait"])
                if volume.machine is not None:
                    volume.machine.task_job_id = volume.backendjobid
                    common.wait_server_task(volume.machine,
                                            wait,
                                            stdout=self.stdout)
                else:
                    self.stdout.write("Successfully removed volume %s\n" %
                                      volume)
            except CommandError as e:
                self.stdout.write("Error -- %s\n" % e.message)
Esempio n. 2
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("Please provide a server ID")

        force = options['force']
        message = "servers" if len(args) > 1 else "server"
        self.confirm_deletion(force, message, args)

        for server_id in args:
            self.stdout.write("\n")
            try:
                server = get_resource("server", server_id)

                self.stdout.write("Trying to remove server '%s' from backend "
                                  "'%s' \n" %
                                  (server.backend_vm_id, server.backend))

                credentials = Credentials("snf-manage", is_admin=True)
                server = servers.destroy(server_id, credentials=credentials)
                jobID = server.task_job_id

                self.stdout.write("Issued OP_INSTANCE_REMOVE with id: %s\n" %
                                  jobID)

                wait = parse_bool(options["wait"])
                wait_server_task(server, wait, self.stdout)
            except (CommandError, faults.BadRequest) as e:
                self.stdout.write("Error -- %s\n" % e.message)
Esempio n. 3
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        network_id = options['network_id']
        address = options['address']
        user = options['user']
        project = options['project']

        if not user:
            raise CommandError("'user' is required for floating IP creation")
        if not project:
            project = user

        if network_id is not None:
            network = common.get_resource("network", network_id)
            if network.deleted:
                raise CommandError("Network '%s' is deleted" % network.id)
            if not network.floating_ip_pool:
                raise CommandError("Network '%s' is not a floating IP pool." %
                                   network)

        credentials = Credentials(user)
        floating_ip = ips.create_floating_ip(credentials,
                                             project=project,
                                             network_id=network_id,
                                             address=address)

        self.stdout.write("Created floating IP '%s'.\n" % floating_ip)
Esempio n. 4
0
def do_action(request, op, id, data):
    """Apply the requested action on the specified ip."""
    actions = get_permitted_actions(cached_actions, request.user)

    if op == "contact":
        user = get_user_or_404(id)
        actions[op].apply(user, request)
    else:
        credentials = Credentials("admin-app", is_admin=True)
        actions[op].apply(id, credentials=credentials)
Esempio n. 5
0
    def setUp(self):
        """Common setup method for this suite.

        This setUp method creates a simple IP Pool from which IPs can be
        created.
        """
        self.subnet = mfactory.IPv4SubnetFactory(
            network__floating_ip_pool=True)
        self.network = self.subnet.network
        self.credentials = Credentials("1134", is_admin=True)
Esempio n. 6
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options['name']
        user_id = options['user_id']
        project = options['project']
        backend_id = options['backend_id']
        image_id = options['image_id']
        flavor_id = options['flavor_id']
        password = options['password']
        volumes = options['volumes']
        helper_vm = options['helper_vm']

        if not name:
            raise CommandError("name is mandatory")
        if not user_id:
            raise CommandError("user is mandatory")
        if not project:
            project = user_id
        if not password:
            raise CommandError("password is mandatory")
        if not flavor_id:
            raise CommandError("flavor is mandatory")
        if not image_id and not volumes:
            raise CommandError("image is mandatory")

        flavor = common.get_resource("flavor", flavor_id)
        if image_id is not None:
            common.get_image(image_id, user_id)

        if backend_id:
            backend = common.get_resource("backend", backend_id)
        else:
            backend = None

        connection_list = parse_connections(options["connections"])
        volumes_list = parse_volumes(volumes)
        credentials = Credentials(user_id)
        server = servers.create(credentials,
                                name,
                                password,
                                flavor,
                                image_id,
                                networks=connection_list,
                                volumes=volumes_list,
                                use_backend=backend,
                                helper=helper_vm,
                                project=project)
        pprint.pprint_server(server, stdout=self.stdout)

        wait = parse_bool(options["wait"])
        common.wait_server_task(server, wait, self.stdout)
Esempio n. 7
0
def do_action(request, op, id, data):
    """Apply the requested action on the specified user."""
    actions = get_permitted_actions(cached_actions, request.user)

    if op == "contact":
        user = get_user_or_404(id)
        actions[op].apply(user, request)
    else:
        credentials = Credentials("admin-app", is_admin=True)
        kwargs = {"credentials": credentials}
        if op == 'reboot':
            kwargs["reboot_type"] = "SOFT"
        actions[op].apply(id, **kwargs)
Esempio n. 8
0
 def setUp(self):
     self.debian_rescue_image = mfactory.RescueImageFactory(
         target_os_family='linux',
         target_os='debian',
         location='test-path.iso',
         name='Test Rescue Image')
     self.windows_rescue_image = mfactory.RescueImageFactory(
         target_os_family='windows',
         target_os='windows',
         location='test-path-win.iso',
         name='Test Windows Rescue Image',
         is_default=True)
     self.vm = mfactory.VirtualMachineFactory()
     self.credentials = Credentials("test")
Esempio n. 9
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("Please provide a floating-ip ID")

        force = options['force']
        message = "floating IPs" if len(args) > 1 else "floating IP"
        self.confirm_deletion(force, message, args)

        credentials = Credentials("snf-manage", is_admin=True)
        for floating_ip_id in args:
            self.stdout.write("\n")
            try:
                ips.delete_floating_ip(floating_ip_id, credentials)
                self.stdout.write("Deleted floating IP '%s'.\n" %
                                  floating_ip_id)
            except CommandError as e:
                self.stdout.write("Error -- %s\n" % e.message)
Esempio n. 10
0
    def setUp(self):
        # Volume types
        self.archip_vt = mf.VolumeTypeFactory(name="archipelago",
                                              disk_template="ext_archipelago")
        self.userid = "test_user"
        self.archip_vm = mf.VirtualMachineFactory(
            userid=self.userid, flavor__volume_type=self.archip_vt)

        self.volume_name = "Unfortunate volume"
        self.size = 1
        self.credentials = Credentials("test_user")
        self.kwargs = {
            "credentials": self.credentials,
            "size": self.size,
            "volume_type_id": self.archip_vt.id,
            "server_id": None,
            "name": self.volume_name,
        }
Esempio n. 11
0
    def handle(self, *args, **options):
        if not args or len(args) > 1:
            raise CommandError("Command accepts exactly one argument")

        floating_ip_id = args[0]  # this is the floating-ip address
        device = options['machine']

        if not device:
            raise CommandError('Please give either a server or a router id')

        #get the vm
        vm = common.get_resource("server", device)
        floating_ip = common.get_resource("floating-ip", floating_ip_id)
        credentials = Credentials(vm.userid)
        servers.create_port(credentials, floating_ip.network_id,
                            address=floating_ip.address, machine_id=device)

        self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
Esempio n. 12
0
    def handle(self, *args, **options):
        if not args or len(args) > 1:
            raise CommandError("Command accepts exactly one argument")

        floating_ip_id = args[0]

        #get the floating-ip
        floating_ip = common.get_resource("floating-ip", floating_ip_id)

        if not floating_ip.nic:
            raise CommandError('This floating IP is not attached to a device')

        credentials = Credentials("snf-manage", is_admin=True)
        nic = floating_ip.nic
        vm = nic.machine
        servers.delete_port(nic.id, credentials)
        self.stdout.write("Detached floating IP %s from  %s.\n" %
                          (floating_ip_id, vm))
Esempio n. 13
0
    def setUp(self):
        # Volume types
        self.archip_vt = mf.VolumeTypeFactory(name="archipelago",
                                              disk_template="ext_archipelago")
        self.file_vt = mf.VolumeTypeFactory(name="file", disk_template="file")

        self.credentials = Credentials("test_user")
        # Common arguments
        self.userid = "test_user"
        self.size = 1
        self.kwargs = {
            "credentials": self.credentials,
        }

        # VMs
        self.archip_vm = mf.VirtualMachineFactory(
            userid=self.userid, flavor__volume_type=self.archip_vt)
        self.file_vm = mf.VirtualMachineFactory(
            userid=self.userid, flavor__volume_type=self.file_vt)
Esempio n. 14
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a volume ID")

        credentials = Credentials("snf_manage", is_admin=True)

        volume = common.get_resource("volume", args[0])

        name = options.get("name")
        description = options.get("description")
        delete_on_termination = options.get("delete_on_termination")
        if delete_on_termination is not None:
            delete_on_termination = parse_bool(delete_on_termination)

        volume = volumes.update(volume.id, name=name,
                                description=description,
                                delete_on_termination=delete_on_termination,
                                credentials=credentials)

        pprint.pprint_volume(volume, stdout=self.stdout)
        self.stdout.write('\n\n')
Esempio n. 15
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options["name"]
        user_id = options["user_id"]
        network_id = options["network_id"]
        server_id = options["server_id"]
        #router_id = options["router_id"]
        router_id = None
        # assume giving security groups comma separated
        security_group_ids = options["security-groups"]
        wait = parse_bool(options["wait"])

        if not name:
            name = ""

        if not network_id:
            raise CommandError("Please specify a 'network'")

        vm = None
        owner = None
        if server_id:
            owner = "vm"
            vm = common.get_resource("server", server_id)
            #if vm.router:
            #    raise CommandError("Server '%s' does not exist." % server_id)
        elif router_id:
            owner = "router"
            vm = common.get_resource("server", router_id)
            if not vm.router:
                raise CommandError("Router '%s' does not exist." % router_id)

        if user_id is None:
            if vm is not None:
                user_id = vm.userid
            else:
                raise CommandError("Please specify the owner of the port.")

        # Get either floating IP or fixed ip address
        ipaddress = None
        floating_ip_id = options["floating_ip_id"]
        ipv4_address = options["ipv4_address"]
        if floating_ip_id:
            ipaddress = common.get_resource("floating-ip", floating_ip_id)
            address = ipaddress.address
            if ipv4_address is not None and address != ipv4_address:
                raise CommandError("Floating IP address '%s' is different from"
                                   " specified address '%s'" %
                                   (address, ipv4_address))
        else:
            address = ipv4_address

        # validate security groups
        sg_list = []
        if security_group_ids:
            security_group_ids = security_group_ids.split(",")
            for gid in security_group_ids:
                sg = util.get_security_group(int(gid))
                sg_list.append(sg)

        credentials = Credentials(user_id)
        new_port = servers.create_port(credentials,
                                       network_id,
                                       machine=vm.id,
                                       name=name,
                                       address=address,
                                       security_groups=sg_list,
                                       device_owner=owner)
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
        pprint.pprint_port(new_port, stdout=self.stdout)
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
        self.stdout.write("\n")
        if vm is not None:
            common.wait_server_task(new_port.machine, wait, stdout=self.stdout)
Esempio n. 16
0
 def setUp(self):
     self.credentials = Credentials("admin_id", is_admin=True)
Esempio n. 17
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        size = options.get("size")
        user_id = options.get("user_id")
        project_id = options.get("project")
        server_id = options.get("server_id")
        volume_type_id = options.get("volume_type_id")
        wait = parse_bool(options["wait"])
        credentials = Credentials(user_id)

        display_name = options.get("name", "")
        display_description = options.get("description", "")

        if size is None:
            raise CommandError("Please specify the size of the volume")

        if server_id:
            vm = common.get_resource("server", server_id)
            if project_id is None:
                project_id = vm.project

        if user_id is None and server_id is None:
            raise CommandError("Please specify the id of a user or a server")
        elif user_id is None and server_id is not None:
            user_id = vm.userid

        if volume_type_id is not None:
            vtype = common.get_resource("volume-type", volume_type_id)
        elif server_id:
            vtype = vm.flavor.volume_type
        else:
            raise CommandError("Please specify the id of the volume type")

        # At this point, user_id, vtype must have been provided or extracted by
        # the server. The project_id is optional and will default to the user's
        # project.

        source_image_id = source_volume_id = source_snapshot_id = None
        source = options.get("source")
        if source is not None:
            try:
                source_type, source_uuid = source.split(":", 1)
            except (ValueError, TypeError):
                raise CommandError("Invalid '--source' option. Value must be"
                                   " of the form <source_type>:<source_uuid>")
            if source_type == "image":
                source_image_id = source_uuid
            elif source_type == "snapshot":
                source_snapshot_id = source_uuid
            else:
                raise CommandError("Unknown volume source type '%s'" %
                                   source_type)

        volume = volumes.create(credentials,
                                size,
                                server_id,
                                name=display_name,
                                description=display_description,
                                source_image_id=source_image_id,
                                source_snapshot_id=source_snapshot_id,
                                source_volume_id=source_volume_id,
                                volume_type_id=vtype.id,
                                metadata={},
                                project_id=project_id)

        self.stdout.write("Created volume '%s' in DB:\n" % volume.id)

        pprint.pprint_volume(volume, stdout=self.stdout)
        self.stdout.write("\n")
        if volume.machine is not None:
            volume.machine.task_job_id = volume.backendjobid
            common.wait_server_task(volume.machine, wait, stdout=self.stdout)
Esempio n. 18
0
 def setUp(self):
     self.credentials = Credentials("test")
Esempio n. 19
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a server ID")

        server_id = args[0]
        server = get_resource("server", server_id)

        credentials = Credentials("snf-manage", is_admin=True)
        new_name = options.get("name", None)
        if new_name is not None:
            old_name = server.name
            server = servers.rename(server_id, new_name, credentials)
            self.stdout.write("Renamed server '%s' from '%s' to '%s'\n" %
                              (server, old_name, new_name))

        suspended = options.get("suspended", None)
        if suspended is not None:
            suspended = parse_bool(suspended)
            server = servers.suspend(server_id, suspended, credentials)
            self.stdout.write("Set server '%s' as suspended=%s\n" %
                              (server, suspended))

        new_owner = options.get('user')
        if new_owner is not None:
            if "@" in new_owner:
                raise CommandError("Invalid user UUID.")
            if new_owner == server.userid:
                self.stdout.write("%s is already server owner.\n" % new_owner)
            else:
                servers.change_owner(server_id, new_owner, credentials)
                self.stdout.write(
                    "WARNING: User quotas should be out of sync now,"
                    " run `snf-manage reconcile-resources-cyclades'"
                    " to review and update them.\n")

        wait = parse_bool(options["wait"])
        new_flavor_id = options.get("flavor")
        if new_flavor_id is not None:
            new_flavor = get_resource("flavor", new_flavor_id)
            old_flavor = server.flavor
            msg = "Resizing server '%s' from flavor '%s' to '%s'.\n"
            self.stdout.write(msg % (server, old_flavor, new_flavor))
            server = servers.resize(server_id, new_flavor, credentials)
            wait_server_task(server, wait, stdout=self.stdout)

        action = options.get("action")
        if action is not None:
            if action == "start":
                server = servers.start(server_id, credentials=credentials)
            elif action == "stop":
                server = servers.stop(server_id, credentials=credentials)
            elif action == "reboot_hard":
                server = servers.reboot(server_id,
                                        reboot_type="HARD",
                                        credentials=credentials)
            elif action == "reboot_soft":
                server = servers.reboot(server_id,
                                        reboot_type="SOFT",
                                        credentials=credentials)
            elif action == "rescue":
                server = servers.rescue(server_id, credentials=credentials)
            elif action == "unrescue":
                server = servers.unrescue(server_id, credentials=credentials)
            else:
                raise CommandError("Unknown action.")
            wait_server_task(server, wait, stdout=self.stdout)
Esempio n. 20
0
import time
from synnefo.db.models import VirtualMachine, IPAddress, NetworkInterface,\
    Volume
from synnefo.logic import servers
from synnefo.logic import ips as logic_ips
from synnefo.logic import backend
from synnefo.volume import volumes as volumes_logic
from synnefo.lib.ordereddict import OrderedDict
from snf_django.lib.api import Credentials
import logging
logger = logging.getLogger(__name__)

MiB = 2**20
GiB = 2**30

credentials = Credentials("enforce", is_admin=True)


def _partition_by(f, l, convert=None):
    if convert is None:
        convert = lambda x: x
    d = {}
    for x in l:
        group = f(x)
        group_l = d.get(group, [])
        group_l.append(convert(x))
        d[group] = group_l
    return d


CHANGE = {