def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a network ID.") network = common.get_network(args[0]) displayname = options['displayname'] pprint.pprint_network(network, display_mails=displayname, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_network_subnets(network, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_network_backends(network, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_network_in_ganeti(network, stdout=self.stdout)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a network ID") network = get_network(args[0]) # Validate subnet if options.get('subnet'): validate_network_info(options) # Validate state state = options.get('state') if state: allowed = [x[0] for x in Network.OPER_STATES] if state not in allowed: msg = "Invalid state, must be one of %s" % ', '.join(allowed) raise CommandError(msg) dhcp = options.get("dhcp") if dhcp: options["dhcp"] = parse_bool(dhcp) drained = options.get("drained") if drained: options["drained"] = parse_bool(drained) fields = ('name', 'userid', 'subnet', 'gateway', 'subnet6', 'gateway6', 'dhcp', 'state', 'link', 'mac_prefix', 'drained') for field in fields: value = options.get(field, None) if value is not None: network.__setattr__(field, value) add_reserved_ips = options.get('add_reserved_ips') remove_reserved_ips = options.get('remove_reserved_ips') if add_reserved_ips or remove_reserved_ips: if add_reserved_ips: add_reserved_ips = add_reserved_ips.split(",") if remove_reserved_ips: remove_reserved_ips = remove_reserved_ips.split(",") for bnetwork in network.backend_networks.all(): with pooled_rapi_client(bnetwork.backend) as c: c.ModifyNetwork(network=network.backend_id, add_reserved_ips=add_reserved_ips, remove_reserved_ips=remove_reserved_ips) network.save()
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Command accepts only the subnet ID as an" " argument. Use snf-manage subnet-modify --help" " for more info.") subnet_id = args[0] name = options["name"] if not name: raise CommandError("--name is mandatory") subnet = common.get_subnet(subnet_id) user_id = common.get_network(subnet.network.id).userid subnets.update_subnet(sub_id=subnet_id, name=name, user_id=user_id)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options["network_id"] cidr = options["cidr"] if not network_id: raise CommandError("network-id is mandatory") if not cidr: raise CommandError("cidr is mandatory") user_id = common.get_network(network_id).userid name = options["name"] or "" allocation_pools = options["allocation_pools"] ipversion = options["ipversion"] or 4 ipversion = int(ipversion) gateway = options["gateway"] dhcp = parse_bool(options["dhcp"]) dns = options["dns"] host_routes = options["host_routes"] alloc = None if allocation_pools is not None: alloc = subnets.parse_allocation_pools(allocation_pools) alloc.sort() sub = subnets.create_subnet(name=name, network_id=network_id, cidr=cidr, allocation_pools=alloc, gateway=gateway, ipversion=ipversion, dhcp=dhcp, slaac=dhcp, dns_nameservers=dns, host_routes=host_routes, user_id=user_id) pprint.pprint_subnet_in_db(sub, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_ippool(sub, stdout=self.stdout)
def handle(self, *args, **options): if not args: raise CommandError("Please provide a network ID") force = options['force'] message = "networks" if len(args) > 1 else "network" self.confirm_deletion(force, message, args) for network_id in args: self.stdout.write("\n") try: network = get_network(network_id, for_update=True) self.stdout.write('Removing network: %s\n' % network.backend_id) networks.delete(network) self.stdout.write("Successfully submitted Ganeti jobs to" " remove network %s\n" % network.backend_id) except (CommandError, faults.BadRequest) as e: self.stdout.write("Error -- %s\n" % e.message)
def handle(self, *args, **options): if len(args) < 1: raise CommandError("Please provide a network ID") network = get_network(args[0]) self.stdout.write('Trying to remove network: %s\n' % str(network)) if network.machines.exists(): raise CommandError('Network is not empty. Can not delete') network.action = 'DESTROY' network.save() if network.userid: quotas.issue_and_accept_commission(network, delete=True) for bnet in network.backend_networks.exclude(operstate="DELETED"): delete_network(network, bnet.backend) self.stdout.write("Successfully submitted Ganeti jobs to" " remove network %s" % network.backend_id)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a network ID") network = get_network(args[0]) new_name = options.get("name") if new_name is not None: old_name = network.name network = networks.rename(network, new_name) self.stdout.write("Renamed network '%s' from '%s' to '%s'.\n" % (network, old_name, new_name)) drained = options.get("drained") if drained is not None: drained = parse_bool(drained) network.drained = drained network.save() self.stdout.write("Set network '%s' as drained=%s.\n" % (network, drained)) new_owner = options.get("userid") if new_owner is not None: if "@" in new_owner: raise CommandError("Invalid owner UUID.") old_owner = network.userid network.userid = new_owner network.save() msg = "Changed the owner of network '%s' from '%s' to '%s'.\n" self.stdout.write(msg % (network, old_owner, new_owner)) floating_ip_pool = options["floating_ip_pool"] if floating_ip_pool is not None: floating_ip_pool = parse_bool(floating_ip_pool) if floating_ip_pool is False and network.floating_ip_pool is True: if network.ips.filter(deleted=False, floating_ip=True)\ .exists(): msg = ("Cannot make network a non floating IP pool." " There are still reserved floating IPs.") raise CommandError(msg) network.floating_ip_pool = floating_ip_pool network.save() self.stdout.write("Set network '%s' as floating-ip-pool=%s.\n" % (network, floating_ip_pool)) if floating_ip_pool is True: for backend in Backend.objects.filter(offline=False): bnet, jobs =\ backend_mod.ensure_network_is_active(backend, network.id) if jobs: msg = ("Sent job to create network '%s' in backend" " '%s'\n" % (network, backend)) self.stdout.write(msg) add_reserved_ips = options.get('add_reserved_ips') remove_reserved_ips = options.get('remove_reserved_ips') if add_reserved_ips or remove_reserved_ips: if add_reserved_ips: add_reserved_ips = add_reserved_ips.split(",") for ip in add_reserved_ips: network.reserve_address(ip, external=True) if remove_reserved_ips: remove_reserved_ips = remove_reserved_ips.split(",") for ip in remove_reserved_ips: network.release_address(ip, external=True) add_to_backend = options["add_to_backend"] if add_to_backend is not None: backend = get_backend(add_to_backend) bnet, jobs = backend_mod.ensure_network_is_active(backend, network.id) if jobs: msg = "Sent job to create network '%s' in backend '%s'\n" self.stdout.write(msg % (network, backend)) remove_from_backend = options["remove_from_backend"] if remove_from_backend is not None: backend = get_backend(remove_from_backend) if network.nics.filter(machine__backend=backend, machine__deleted=False).exists(): msg = "Cannot remove. There are still connected VMs to this"\ " network" raise CommandError(msg) backend_mod.delete_network(network, backend, disconnect=True) msg = "Sent job to delete network '%s' from backend '%s'\n" self.stdout.write(msg % (network, backend))
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_vm(server_id, for_update=True) #if vm.router: # raise CommandError("Server '%s' does not exist." % server_id) elif router_id: owner = "router" vm = common.get_vm(router_id, for_update=True) 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 the network network = common.get_network(network_id) # 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_floating_ip_by_id(floating_ip_id, for_update=True) if ipv4_address is not None and ipaddress.address != ipv4_address: raise CommandError("Floating IP address '%s' is different from" " specified address '%s'" % (ipaddress.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) new_port = servers.create_port(user_id, network, machine=vm, name=name, use_ipaddress=ipaddress, address=ipv4_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)
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_vm(server_id, for_update=True) # if vm.router: # raise CommandError("Server '%s' does not exist." % server_id) elif router_id: owner = "router" vm = common.get_vm(router_id, for_update=True) 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 the network network = common.get_network(network_id) # 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_floating_ip_by_id(floating_ip_id, for_update=True) if ipv4_address is not None and ipaddress.address != ipv4_address: raise CommandError( "Floating IP address '%s' is different from" " specified address '%s'" % (ipaddress.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) new_port = servers.create_port( user_id, network, machine=vm, name=name, use_ipaddress=ipaddress, address=ipv4_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)
def handle(self, *args, **options): write = self.stdout.write if len(args) != 1: raise CommandError("Please provide a network ID.") net = get_network(args[0]) ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) displayname = options['displayname'] sep = '-' * 80 + '\n' labels = filter(lambda x: x is not Omit, ['name', 'backend-name', 'state', 'owner uuid', 'owner_name' if displayname else Omit, 'subnet', 'gateway', 'mac_prefix', 'link', 'public', 'dhcp', 'flavor', 'deleted', 'action', 'pool']) uuid = net.userid if displayname: dname = ucache.get_name(uuid) fields = filter(lambda x: x is not Omit, [net.name, net.backend_id, net.state, uuid or '-', dname or '-' if displayname else Omit, str(net.subnet), str(net.gateway), str(net.mac_prefix), str(net.link), str(net.public), str(net.dhcp), str(net.flavor), str(net.deleted), str(net.action), str(splitPoolMap(net.get_pool().to_map(), 64))]) write(sep) write('State of Network in DB\n') write(sep) for l, f in zip(labels, fields): write(l.ljust(20) + ': ' + f.ljust(20) + '\n') labels = ('Backend', 'State', 'Deleted', 'JobID', 'OpCode', 'JobStatus') for back_net in BackendNetwork.objects.filter(network=net): write('\n') fields = (back_net.backend.clustername, back_net.operstate, str(back_net.deleted), str(back_net.backendjobid), str(back_net.backendopcode), str(back_net.backendjobstatus)) for l, f in zip(labels, fields): write(l.ljust(20) + ': ' + f.ljust(20) + '\n') write('\n') write(sep) write('State of Network in Ganeti\n') write(sep) for backend in Backend.objects.exclude(offline=True): with pooled_rapi_client(backend) as client: try: g_net = client.GetNetwork(net.backend_id) write("Backend: %s\n" % backend.clustername) print json.dumps(g_net, indent=2) write(sep) except GanetiApiError as e: if e.code == 404: write('Network does not exist in backend %s\n' % backend.clustername) else: raise e