Commit 51927bd7 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

pep8 convension to cyclades management commands

parent 1d59e0b3
......@@ -40,10 +40,11 @@ from os import path
class Command(NoArgsCommand):
help = "Export account quota policies"
option_list = NoArgsCommand.option_list + (
make_option('--location',
dest='location',
default='exported_quota',
help="Where to save the output file"),
make_option(
'--location',
dest='location',
default='exported_quota',
help="Where to save the output file"),
)
def handle_noargs(self, **options):
......@@ -65,7 +66,7 @@ class Command(NoArgsCommand):
f.write('\n')
for user, value in nets_per_user.items():
f.write(' '.join([user, "cyclades.network.private", "%s" % value,
'0', '0', '0']))
'0', '0', '0']))
f.write('\n')
f.close()
......
......@@ -44,9 +44,11 @@ class Command(BaseCommand):
"<ram>[,<ram>,...] " \
"<disk>[,<disk>,...] " \
"<disk template>[,<disk template>,...]"
help = "Create one or more flavors.\n\nThe flavors that will be created are"\
help = "Create one or more flavors.\n\n"\
" The flavors that will be created are"\
" those belonging to the cartesian product of the arguments"\
def handle(self, *args, **options):
if len(args) != 4:
raise CommandError("Invalid number of arguments")
......
......@@ -46,23 +46,26 @@ class Command(BaseCommand):
help = "List flavors"
option_list = BaseCommand.option_list + (
make_option('-c',
make_option(
'-c',
action='store_true',
dest='csv',
default=False,
help="Use pipes to separate values"),
make_option('--deleted',
make_option(
'--deleted',
action='store_true',
dest='deleted',
default=False,
help="Include deleted flavors"),
make_option('--filter-by',
make_option(
'--filter-by',
dest='filter_by',
help="Filter results. Comma seperated list of key=val pairs"
" that displayed entries must satisfy. e.g."
" --filter-by \"cpu=1,ram!=1024\"."
"Available keys are: %s" % ", ".join(FIELDS))
)
)
def handle(self, *args, **options):
if args:
......
......@@ -37,27 +37,27 @@ from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_flavor
class Command(BaseCommand):
args = "<flavor id>"
help = "Modify a flavor"
option_list = BaseCommand.option_list + (
make_option('--set-deleted',
make_option(
'--set-deleted',
action='store_true',
dest='deleted',
help="Mark a server as deleted"),
make_option('--set-undeleted',
make_option(
'--set-undeleted',
action='store_true',
dest='undeleted',
help="Mark a server as not deleted"),
)
)
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a flavor ID")
flavor = get_flavor(args[0])
if options.get('deleted'):
......
......@@ -50,66 +50,80 @@ class Command(BaseCommand):
help = "Create a new network"
option_list = BaseCommand.option_list + (
make_option('--name',
make_option(
'--name',
dest='name',
help="Name of network"),
make_option('--owner',
make_option(
'--owner',
dest='owner',
help="The owner of the network"),
make_option('--subnet',
make_option(
'--subnet',
dest='subnet',
default=None,
# required=True,
help='Subnet of the network'),
make_option('--gateway',
make_option(
'--gateway',
dest='gateway',
default=None,
help='Gateway of the network'),
make_option('--subnet6',
make_option(
'--subnet6',
dest='subnet6',
default=None,
help='IPv6 subnet of the network'),
make_option('--gateway6',
make_option(
'--gateway6',
dest='gateway6',
default=None,
help='IPv6 gateway of the network'),
make_option('--dhcp',
make_option(
'--dhcp',
dest='dhcp',
action='store_true',
default=False,
help='Automatically assign IPs'),
make_option('--public',
make_option(
'--public',
dest='public',
action='store_true',
default=False,
help='Network is public'),
make_option('--flavor',
make_option(
'--flavor',
dest='flavor',
default=None,
choices=NETWORK_FLAVORS,
help='Network flavor. Choices: ' + ', '.join(NETWORK_FLAVORS)),
make_option('--mode',
make_option(
'--mode',
dest='mode',
default=None,
help="Overwrite flavor connectivity mode."),
make_option('--link',
make_option(
'--link',
dest='link',
default=None,
help="Overwrite flavor connectivity link."),
make_option('--mac-prefix',
make_option(
'--mac-prefix',
dest='mac_prefix',
default=None,
help="Overwrite flavor connectivity MAC prefix"),
make_option('--tags',
make_option(
'--tags',
dest='tags',
default=None,
help='The tags of the Network (comma separated strings)'),
make_option('--backend-id',
make_option(
'--backend-id',
dest='backend_id',
default=None,
help='ID of the backend that the network will be created. Only for'
' public networks'),
)
)
def handle(self, *args, **options):
if args:
......@@ -154,23 +168,23 @@ class Command(BaseCommand):
subnet, gateway, subnet6, gateway6 = validate_network_info(options)
if not link or not mode:
raise CommandError("Can not create network. No connectivity link or mode")
network = Network.objects.create(
name=name,
userid=options['owner'],
subnet=subnet,
gateway=gateway,
gateway6=gateway6,
subnet6=subnet6,
dhcp=options['dhcp'],
flavor=flavor,
public=public,
mode=mode,
link=link,
mac_prefix=mac_prefix,
tags=tags,
state='PENDING')
raise CommandError("Can not create network."
" No connectivity link or mode")
network = Network.objects.create(name=name,
userid=options['owner'],
subnet=subnet,
gateway=gateway,
gateway6=gateway6,
subnet6=subnet6,
dhcp=options['dhcp'],
flavor=flavor,
public=public,
mode=mode,
link=link,
mac_prefix=mac_prefix,
tags=tags,
state='PENDING')
if public:
# Create BackendNetwork only to the specified Backend
......
......@@ -45,27 +45,32 @@ class Command(BaseCommand):
help = "List networks"
option_list = BaseCommand.option_list + (
make_option('-c',
make_option(
'-c',
action='store_true',
dest='csv',
default=False,
help="Use pipes to separate values"),
make_option('--deleted',
make_option(
'--deleted',
action='store_true',
dest='deleted',
default=False,
help="Include deleted networks"),
make_option('--public',
make_option(
'--public',
action='store_true',
dest='public',
default=False,
help="List only public networks"),
make_option('--ipv6',
make_option(
'--ipv6',
action='store_true',
dest='ipv6',
default=False,
help="Show IPv6 information of the network"),
make_option('--filter-by',
make_option(
'--filter-by',
dest='filter_by',
help="Filter results. Comma seperated list of key 'cond' val pairs"
" that displayed entries must satisfy. e.g."
......@@ -77,7 +82,7 @@ class Command(BaseCommand):
dest='use_uuids',
default=False,
help="Display UUIDs instead of user emails"),
)
)
def handle(self, *args, **options):
if args:
......@@ -97,7 +102,7 @@ class Command(BaseCommand):
networks = filter_results(networks, filter_by)
headers = ['id', 'name', 'flavor', 'owner',
'mac_prefix', 'dhcp', 'state', 'link', 'vms', 'public']
'mac_prefix', 'dhcp', 'state', 'link', 'vms', 'public']
if options['ipv6']:
headers.extend(['IPv6 Subnet', 'IPv6 Gateway'])
......
......@@ -38,8 +38,7 @@ from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Network, pooled_rapi_client
from synnefo.management.common import validate_network_info, get_network
HELP_MSG = \
"""Modify a network.
HELP_MSG = """Modify a network.
This management command will only modify the state of the network in Cyclades
DB. The state of the network in the Ganeti backends will remain unchanged. You
......@@ -57,42 +56,54 @@ class Command(BaseCommand):
output_transaction = True
option_list = BaseCommand.option_list + (
make_option('--name',
make_option(
'--name',
dest='name',
metavar='NAME',
help="Set network's name"),
make_option('--userid',
make_option(
'--userid',
dest='userid',
help="Set the userid of the network owner"),
make_option('--subnet',
make_option(
'--subnet',
dest='subnet',
help="Set network's subnet"),
make_option('--gateway',
make_option(
'--gateway',
dest='gateway',
help="Set network's gateway"),
make_option('--subnet6',
make_option(
'--subnet6',
dest='subnet6',
help="Set network's IPv6 subnet"),
make_option('--gateway6',
make_option(
'--gateway6',
dest='gateway6',
help="Set network's IPv6 gateway"),
make_option('--dhcp',
make_option(
'--dhcp',
dest='dhcp',
help="Set if network will use nfdhcp"),
make_option('--state',
make_option(
'--state',
dest='state',
metavar='STATE',
help="Set network's state"),
make_option('--link',
make_option(
'--link',
dest='link',
help="Set the connectivity link"),
make_option('--mac-prefix',
make_option(
'--mac-prefix',
dest="mac_prefix",
help="Set the MAC prefix"),
make_option('--add-reserved-ips',
make_option(
'--add-reserved-ips',
dest="add_reserved_ips",
help="Comma seperated list of IPs to externally reserve."),
make_option('--remove-reserved-ips',
make_option(
'--remove-reserved-ips',
dest="remove_reserved_ips",
help="Comma seperated list of IPs to externally release."),
......
......@@ -56,25 +56,25 @@ class Command(BaseCommand):
help = "Create a new VM." + HELP_MSG
option_list = BaseCommand.option_list + (
make_option("--backend-id", dest="backend_id",
help="Unique identifier of the Ganeti backend."
" Use snf-manage backend-list to find out"
" available backends."),
make_option("--name", dest="name",
help="An arbitrary string for naming the server"),
make_option("--user-id", dest="user_id",
help="Unique identifier of the owner of the server"),
make_option("--image-id", dest="image_id",
help="Unique identifier of the image."
" Use snf-manage image-list to find out"
" available images."),
make_option("--flavor-id", dest="flavor_id",
help="Unique identifier of the flavor"
" Use snf-manage flavor-list to find out"
" available flavors."),
make_option("--password", dest="password",
help="Password for the new server")
)
make_option("--backend-id", dest="backend_id",
help="Unique identifier of the Ganeti backend."
" Use snf-manage backend-list to find out"
" available backends."),
make_option("--name", dest="name",
help="An arbitrary string for naming the server"),
make_option("--user-id", dest="user_id",
help="Unique identifier of the owner of the server"),
make_option("--image-id", dest="image_id",
help="Unique identifier of the image."
" Use snf-manage image-list to find out"
" available images."),
make_option("--flavor-id", dest="flavor_id",
help="Unique identifier of the flavor"
" Use snf-manage flavor-list to find out"
" available flavors."),
make_option("--password", dest="password",
help="Password for the new server")
)
@transaction.commit_manually
def handle(self, *args, **options):
......@@ -106,7 +106,7 @@ class Command(BaseCommand):
image = {}
image['backend_id'] = img['location']
image['format'] = img['disk_format']
image['metadata'] = dict((key.upper(), val) \
image['metadata'] = dict((key.upper(), val)
for key, val in properties.items())
else:
raise CommandError("image-id is mandatory")
......@@ -136,7 +136,7 @@ class Command(BaseCommand):
# Get Public address
(network, address) = util.allocate_public_address(backend)
if address is None:
raise CommandError("Can not allocate a public address."\
raise CommandError("Can not allocate a public address."
" No available public network.")
nic = {'ip': address, 'network': network.backend_id}
......
......@@ -65,28 +65,38 @@ class Command(BaseCommand):
output_transaction = True
option_list = BaseCommand.option_list + (
make_option("--backend-id", dest="backend_id",
help="Unique identifier of the Ganeti backend that"
" hosts the VM. Use snf-manage backend-list to"
" find out available backends."),
make_option("--user-id", dest="user_id",
help="Unique identifier of the owner of the server"),
make_option("--image-id", dest="image_id",
default=None,
help="Unique identifier of the image."
" Use snf-manage image-list to find out"
" available images."),
make_option("--flavor-id", dest="flavor_id",
help="Unique identifier of the flavor"
" Use snf-manage flavor-list to find out"
" available flavors."),
make_option("--new-nics", dest='new_nics',
default=False,
action="store_true",
help="Remove old NICs of instance, and create"
" a new NIC connected to a public network of"
" Synnefo.")
)
make_option(
"--backend-id",
dest="backend_id",
help="Unique identifier of the Ganeti backend that"
" hosts the VM. Use snf-manage backend-list to"
" find out available backends."),
make_option(
"--user-id",
dest="user_id",
help="Unique identifier of the owner of the server"),
make_option(
"--image-id",
dest="image_id",
default=None,
help="Unique identifier of the image."
" Use snf-manage image-list to find out"
" available images."),
make_option(
"--flavor-id",
dest="flavor_id",
help="Unique identifier of the flavor"
" Use snf-manage flavor-list to find out"
" available flavors."),
make_option(
"--new-nics",
dest='new_nics',
default=False,
action="store_true",
help="Remove old NICs of instance, and create"
" a new NIC connected to a public network of"
" Synnefo.")
)
REQUIRED = ("user-id", "backend-id", "image-id", "flavor-id")
......@@ -127,8 +137,8 @@ def import_server(instance_name, backend_id, flavor_id, image_id, user_id,
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))
raise CommandError("Instance %s does not exist in backend %s"
% (instance_name, backend))
else:
raise CommandError("Unexpected error" + str(e))
......@@ -137,7 +147,7 @@ def import_server(instance_name, backend_id, flavor_id, image_id, user_id,
stream=stream)
(network, address) = allocate_public_address(backend)
if address is None:
raise CommandError("Can not allocate a public address."\
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,
......@@ -187,10 +197,10 @@ def check_instance_nics(instance):
networks = map(id_from_network_name, networks)
except Network.InvalidBackendIdError:
raise CommandError("Instance %s has NICs that do not belong to a"
" network belonging to synnefo. Either manually"
" modify the instance NICs or specify --new-nics"
" to clear the old NICs and create a new NIC to"
" a public network of synnefo." % instance_name)
" network belonging to synnefo. Either manually"
" modify the instance NICs or specify --new-nics"
" to clear the old NICs and create a new NIC to"
" a public network of synnefo." % instance_name)
def remove_instance_nics(instance, backend_client, stream=sys.stdout):
......@@ -218,7 +228,7 @@ def shutdown_instance(instance, backend_client, stream=sys.stdout):
instance_name = instance['name']
if instance['status'] != 'ADMIN_down':
stream.write("Instance is not down. Shutting down"
" instance\n")
" instance\n")
jobid = backend_client.ShutdownInstance(instance_name)
(status, error) = wait_for_job(backend_client, jobid)
if status != 'success':
......@@ -229,7 +239,7 @@ def rename_instance(old_name, new_name, backend_client, stream=sys.stdout):
stream.write("Renaming instance to %s\n" % new_name)
jobid = backend_client.RenameInstance(old_name, new_name,
ip_check=False, name_check=False)
ip_check=False, name_check=False)
(status, error) = wait_for_job(backend_client, jobid)
if status != 'success':
raise CommandError("Can not rename instance: %s" % error)
......
......@@ -56,12 +56,14 @@ class Command(BaseCommand):
args = "<server ID>"
option_list = BaseCommand.option_list + (
make_option('--jobs',
make_option(
'--jobs',
action='store_true',
dest='jobs',
default=False,
help="Show non-archived jobs concerning server."),
make_option('--uuids',
make_option(
'--uuids',
action='store_true',
dest='use_uuids',
default=False,
......@@ -102,8 +104,9 @@ class Command(BaseCommand):
self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n')
self.stdout.write('\n')
for nic in vm.nics.all():
self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s, Network: %s\n"\
% (nic.index, nic.ipv4, nic.mac, nic.ipv6, nic.network))
msg = "nic/%d: IPv4: %s, MAC: %s, IPv6:%s, Network: %s\n"\
% (nic.index, nic.ipv4, nic.mac, nic.ipv6, nic.network)
self.stdout.write(msg)
client = vm.get_client()
try:
......@@ -138,14 +141,15 @@ class Command(BaseCommand):
for j in jobs:
info = client.GetJobStatus(j)
summary = ' '.join(info['summary'])
if summary.startswith("INSTANCE") and \
summary.find(vm.backend_vm_id) != -1:
job_is_relevant = summary.startswith("INSTANCE") and\
(summary.find(vm.backend_vm_id) != -1)
if job_is_relevant:
for i in GANETI_JOB_FIELDS:
value = info[i]
if i.find('_ts') != -1: