Exemplo n.º 1
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options['name']
        user_id = options['user_id']
        backend_id = options['backend_id']
        image_id = options['image_id']
        flavor_id = options['flavor_id']
        password = options['password']

        if not name:
            raise CommandError("name is mandatory")
        if not user_id:
            raise CommandError("user-id is mandatory")
        if not password:
            raise CommandError("password is mandatory")
        if not flavor_id:
            raise CommandError("flavor-id is mandatory")
        if not image_id:
            raise CommandError("image-id is mandatory")

        flavor = common.get_flavor(flavor_id)
        image = common.get_image(image_id, user_id)
        if backend_id:
            backend = common.get_backend(backend_id)

        do_create_server(user_id,
                         name,
                         password,
                         flavor,
                         image,
                         backend=backend)
Exemplo n.º 2
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options['name']
        user_id = options['user_id']
        backend_id = options['backend_id']
        image_id = options['image_id']
        flavor_id = options['flavor_id']
        password = options['password']

        if not name:
            raise CommandError("name is mandatory")
        if not user_id:
            raise CommandError("user-id is mandatory")
        if not password:
            raise CommandError("password is mandatory")
        if not flavor_id:
            raise CommandError("flavor-id is mandatory")
        if not image_id:
            raise CommandError("image-id is mandatory")

        flavor = common.get_flavor(flavor_id)
        image = common.get_image(image_id, user_id)
        if backend_id:
            backend = common.get_backend(backend_id)
        else:
            backend = None

        do_create_server(user_id, name, password, flavor, image,
                         backend=backend)
Exemplo n.º 3
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a server ID")

        server = get_vm(args[0], for_update=True)

        new_name = options.get("name", None)
        if new_name is not None:
            old_name = server.name
            server = servers.rename(server, new_name)
            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.suspended = suspended
            server.save()
            self.stdout.write("Set server '%s' as suspended=%s\n" %
                              (server, suspended))

        new_owner = options.get('owner')
        if new_owner is not None:
            if "@" in new_owner:
                raise CommandError("Invalid owner UUID.")
            old_owner = server.userid
            server.userid = new_owner
            server.save()
            msg = "Changed the owner of server '%s' from '%s' to '%s'.\n"
            self.stdout.write(msg % (server, old_owner, new_owner))

        wait = parse_bool(options["wait"])
        new_flavor_id = options.get("flavor")
        if new_flavor_id is not None:
            new_flavor = get_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, new_flavor)
            wait_server_task(server, wait, stdout=self.stdout)

        action = options.get("action")
        if action is not None:
            if action == "start":
                server = servers.start(server)
            elif action == "stop":
                server = servers.stop(server)
            elif action == "reboot_hard":
                server = servers.reboot(server, reboot_type="HARD")
            elif action == "reboot_stof":
                server = servers.reboot(server, reboot_type="SOFT")
            else:
                raise CommandError("Unknown action.")
            wait_server_task(server, wait, stdout=self.stdout)
Exemplo n.º 4
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a server ID")

        server = get_vm(args[0], for_update=True)

        new_name = options.get("name", None)
        if new_name is not None:
            old_name = server.name
            server = servers.rename(server, new_name)
            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.suspended = suspended
            server.save()
            self.stdout.write("Set server '%s' as suspended=%s\n" %
                              (server, suspended))

        new_owner = options.get('owner')
        if new_owner is not None:
            if "@" in new_owner:
                raise CommandError("Invalid owner UUID.")
            old_owner = server.userid
            server.userid = new_owner
            server.save()
            msg = "Changed the owner of server '%s' from '%s' to '%s'.\n"
            self.stdout.write(msg % (server, old_owner, new_owner))

        wait = parse_bool(options["wait"])
        new_flavor_id = options.get("flavor")
        if new_flavor_id is not None:
            new_flavor = get_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, new_flavor)
            wait_server_task(server, wait, stdout=self.stdout)

        action = options.get("action")
        if action is not None:
            if action == "start":
                server = servers.start(server)
            elif action == "stop":
                server = servers.stop(server)
            elif action == "reboot_hard":
                server = servers.reboot(server, reboot_type="HARD")
            elif action == "reboot_stof":
                server = servers.reboot(server, reboot_type="SOFT")
            else:
                raise CommandError("Unknown action.")
            wait_server_task(server, wait, stdout=self.stdout)
Exemplo n.º 5
0
def import_server(instance_name,
                  backend_id,
                  flavor_id,
                  image_id,
                  user_id,
                  new_public_nic,
                  stream=sys.stdout):
    flavor = common.get_flavor(flavor_id)
    backend = common.get_backend(backend_id)

    backend_client = backend.get_client()

    try:
        instance = backend_client.GetInstance(instance_name)
    except GanetiApiError as e:
        if e.code == 404:
            raise CommandError("Instance %s does not exist in backend %s" %
                               (instance_name, backend))
        else:
            raise CommandError("Unexpected error" + str(e))

    if not new_public_nic:
        check_instance_nics(instance)

    shutdown_instance(instance, backend_client, stream=stream)

    # Create the VM in DB
    stream.write("Creating VM entry in DB\n")
    vm = VirtualMachine.objects.create(name=instance_name,
                                       backend=backend,
                                       userid=user_id,
                                       imageid=image_id,
                                       flavor=flavor)

    quotas.issue_and_accept_commission(vm)

    if new_public_nic:
        remove_instance_nics(instance, backend_client, stream=stream)

    # Rename instance
    rename_instance(instance_name, vm.backend_vm_id, backend_client, stream)

    if new_public_nic:
        ports = servers.create_instance_ports(user_id)
        stream.write("Adding new NICs to server")
        [servers.associate_port_with_machine(port, vm) for port in ports]
        [connect_to_network(vm, port) for port in ports]

    # Startup instance
    startup_instance(vm.backend_vm_id, backend_client, stream=stream)

    backend.put_client(backend_client)
    return
Exemplo n.º 6
0
def import_server(instance_name, backend_id, flavor_id, image_id, user_id,
                  new_public_nic, stream=sys.stdout):
    flavor = common.get_flavor(flavor_id)
    backend = common.get_backend(backend_id)

    backend_client = backend.get_client()

    try:
        instance = backend_client.GetInstance(instance_name)
    except GanetiApiError as e:
        if e.code == 404:
            raise CommandError("Instance %s does not exist in backend %s"
                               % (instance_name, backend))
        else:
            raise CommandError("Unexpected error" + str(e))

    if not new_public_nic:
        check_instance_nics(instance)

    shutdown_instance(instance, backend_client, stream=stream)

    # Create the VM in DB
    stream.write("Creating VM entry in DB\n")
    vm = VirtualMachine.objects.create(name=instance_name,
                                       backend=backend,
                                       userid=user_id,
                                       imageid=image_id,
                                       flavor=flavor)

    quotas.issue_and_accept_commission(vm)

    if new_public_nic:
        remove_instance_nics(instance, backend_client,
                             stream=stream)

    # Rename instance
    rename_instance(instance_name, vm.backend_vm_id, backend_client,
                    stream)

    if new_public_nic:
        ports = servers.create_instance_ports(user_id)
        stream.write("Adding new NICs to server")
        [servers.associate_port_with_machine(port, vm)
         for port in ports]
        [connect_to_network(vm, port) for port in ports]

    # Startup instance
    startup_instance(vm.backend_vm_id, backend_client, stream=stream)

    backend.put_client(backend_client)
    return
Exemplo n.º 7
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a flavor ID")

        flavor = get_flavor(args[0])

        deleted = options['deleted']
        if deleted:
            deleted = parse_bool(deleted)
            log.info("Marking flavor %s as deleted=%s", flavor, deleted)
            flavor.deleted = deleted
            flavor.save()
        else:
            log.info("Nothing changed!")
Exemplo n.º 8
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a flavor ID")

        flavor = get_flavor(args[0])

        deleted = options['deleted']
        if deleted:
            deleted = parse_bool(deleted)
            log.info("Marking flavor %s as deleted=%s", flavor, deleted)
            flavor.deleted = deleted
            flavor.save()
        else:
            log.info("Nothing changed!")
Exemplo n.º 9
0
def import_server(instance_name,
                  backend_id,
                  flavor_id,
                  image_id,
                  user_id,
                  new_public_nic,
                  stream=sys.stdout):
    flavor = common.get_flavor(flavor_id)
    backend = common.get_backend(backend_id)

    backend_client = backend.get_client()

    try:
        instance = backend_client.GetInstance(instance_name)
    except GanetiApiError as e:
        if e.code == 404:
            raise CommandError("Instance %s does not exist in backend %s" %
                               (instance_name, backend))
        else:
            raise CommandError("Unexpected error" + str(e))

    if new_public_nic:
        remove_instance_nics(instance, backend_client, stream=stream)
        (network, address) = allocate_public_address(backend)
        if address is None:
            raise CommandError("Can not allocate a public address."
                               " No available public network.")
        nic = {'ip': address, 'network': network.backend_id}
        add_public_nic(instance_name, nic, backend_client, stream=stream)
    else:
        check_instance_nics(instance)

    shutdown_instance(instance, backend_client, stream=stream)

    # Create the VM in DB
    stream.write("Creating VM entry in DB\n")
    vm = VirtualMachine.objects.create(name=instance_name,
                                       backend=backend,
                                       userid=user_id,
                                       imageid=image_id,
                                       flavor=flavor)

    quotas.issue_and_accept_commission(vm)
    # Rename instance
    rename_instance(instance_name, vm.backend_vm_id, backend_client, stream)
    # Startup instance
    startup_instance(vm.backend_vm_id, backend_client, stream=stream)

    backend.put_client(backend_client)
    return
Exemplo n.º 10
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options['name']
        user_id = options['user_id']
        backend_id = options['backend_id']
        image_id = options['image_id']
        flavor_id = options['flavor_id']
        password = options['password']

        if not name:
            raise CommandError("name is mandatory")
        if not user_id:
            raise CommandError("user-id is mandatory")
        if not password:
            raise CommandError("password is mandatory")
        if not flavor_id:
            raise CommandError("flavor-id is mandatory")
        if not image_id:
            raise CommandError("image-id is mandatory")

        flavor = common.get_flavor(flavor_id)
        image = common.get_image(image_id, user_id)
        if backend_id:
            backend = common.get_backend(backend_id)
        else:
            backend = None

        connection_list = parse_connections(options["connections"])
        server = servers.create(user_id,
                                name,
                                password,
                                flavor,
                                image,
                                networks=connection_list,
                                use_backend=backend)
        pprint.pprint_server(server, stdout=self.stdout)

        wait = parse_bool(options["wait"])
        common.wait_server_task(server, wait, self.stdout)
Exemplo n.º 11
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a flavor ID")

        flavor = get_flavor(args[0], for_update=True)

        deleted = options['deleted']

        if deleted:
            deleted = parse_bool(deleted)
            log.info("Marking flavor %s as deleted=%s", flavor, deleted)
            flavor.deleted = deleted
            flavor.save()

        allow_create = options['allow_create']
        if allow_create:
            allow_create = parse_bool(allow_create)
            log.info("Marking flavor %s as allow_create=%s", flavor,
                     allow_create)
            flavor.allow_create = allow_create
            flavor.save()
Exemplo n.º 12
0
    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a flavor ID")

        flavor = get_flavor(args[0], for_update=True)

        deleted = options['deleted']

        if deleted:
            deleted = parse_bool(deleted)
            log.info("Marking flavor %s as deleted=%s", flavor, deleted)
            flavor.deleted = deleted
            flavor.save()

        allow_create = options['allow_create']
        if allow_create:
            allow_create = parse_bool(allow_create)
            log.info("Marking flavor %s as allow_create=%s", flavor,
                     allow_create)
            flavor.allow_create = allow_create
            flavor.save()