Commit 6b97b5c6 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Improve management commands

parent 317c3556
......@@ -37,32 +37,32 @@ from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Flavor
from ._common import format_bool, format_date
class Command(BaseCommand):
output_transaction = True
args = "<cpu>[,<cpu>,...] " \
"<ram>[,<ram>,...] " \
"<disk>[,<disk>,...] " \
"<disk template>[,<disk template>,...]"
help = "Create one or more flavors"
help = "Create one or more flavors.\n\nThe 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")
cpus = args[0].split(',')
rams = args[1].split(',')
disks = args[2].split(',')
templates = args[3].split(',')
flavors = []
for cpu, ram, disk, template in product(cpus, rams, disks, templates):
try:
flavors.append((int(cpu), int(ram), int(disk), template))
except ValueError:
raise CommandError("Invalid values")
for cpu, ram, disk, template in flavors:
flavor = Flavor.objects.create(cpu=cpu, ram=ram, disk=disk,
disk_template=template)
......
......@@ -41,23 +41,36 @@ from synnefo.logic.backend_allocator import BackendAllocator
from synnefo.api.util import get_image, allocate_public_address
from synnefo.api.faults import ItemNotFound
HELP_MSG = """
Create a new VM without authenticating the user or checking the resource
limits of the user. Also the allocator can be bypassed by specifing a
backend-id.
"""
class Command(BaseCommand):
help = "Create a new VM."
help = "Create a new VM." + HELP_MSG
output_transaction = True
option_list = BaseCommand.option_list + (
make_option("--backend-id", dest="backend_id",
help="ID of the Ganeti backend"),
help="Unique identifier of the Ganeti backend."
" Use snf-manage backend-list to find out"
" available backends."),
make_option("--name", dest="name",
help="Name of the server."),
help="An arbitrary string for naming the server"),
make_option("--user-id", dest="user_id",
help="ID of the Owner of the server."),
help="Unique identifier of the owner of the server"),
make_option("--image-id", dest="image_id",
help="ID of the image."),
help="Unique identifier of the image."
" Use snf-manage image-list to find out"
" available images."),
make_option("--flavor-id", dest="flavor_id",
help="ID of the flavor"),
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")
)
......
......@@ -37,13 +37,11 @@ from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import VirtualMachine
from ._common import format_bool, format_date
class Command(BaseCommand):
args = "<server ID>"
help = "Modify a server"
option_list = BaseCommand.option_list + (
make_option('--name',
dest='name',
......@@ -74,25 +72,25 @@ class Command(BaseCommand):
dest='unsuspended',
help="Mark a server as not suspended")
)
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a server ID")
try:
server_id = int(args[0])
server = VirtualMachine.objects.get(id=server_id)
except (ValueError, VirtualMachine.DoesNotExist):
raise CommandError("Invalid server ID")
name = options.get('name')
if name is not None:
server.name = name
owner = options.get('owner')
if owner is not None:
server.userid = owner
state = options.get('state')
if state is not None:
allowed = [x[0] for x in VirtualMachine.OPER_STATES]
......@@ -100,15 +98,15 @@ class Command(BaseCommand):
msg = "Invalid state, must be one of %s" % ', '.join(allowed)
raise CommandError(msg)
server.operstate = state
if options.get('deleted'):
server.deleted = True
elif options.get('undeleted'):
server.deleted = False
if options.get('suspended'):
server.suspended = True
elif options.get('unsuspended'):
server.suspended = False
server.save()
......@@ -48,7 +48,11 @@ class Command(BaseCommand):
help="Update statistics of only this backend"),
make_option('--older-than', dest='older_than', metavar="MINUTES",
help="Update only backends that have not been updated for\
MINUTES. Set to 0 to force update.")
MINUTES. Set to 0 to force update."),
make_option('--include-drained', dest='drained',
default=False,
action='store_true',
help="Also update statistics of drained backends")
)
def handle(self, **options):
......@@ -62,8 +66,10 @@ class Command(BaseCommand):
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
else:
# XXX:filter drained ?
backends = Backend.objects.all()
backends = Backend.objects.filter(offline=False)
if not options['drained']:
backends = backends.filter(drained=False)
now = datetime.datetime.now()
if options['older_than'] is not None:
......
......@@ -36,6 +36,7 @@ from pprint import pprint
class Command(BaseCommand):
args = "<image_id>"
help = "Display available information about an image"
def handle(self, *args, **options):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment