Commit 4690a432 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Implement image-list and image-show commands

Implement two new management commands for plankton.
- image-list: lists public images or all images available
to a user
- image-show: displays information about a public image

Also, this commit fixes a bug in server-show, when trying
to display an image that has been deleted.
parent 104bb32e
......@@ -49,21 +49,23 @@ class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option("--backend-id", dest="backend_id",
help="The ID of the Ganeti backend"),
help="ID of the Ganeti backend"),
make_option("--name", dest="name",
help="Name of the server."),
make_option("--user-id", dest="user_id",
help="The owner of the server"),
make_option("--image-id", dest="image_id"),
make_option("--flavor-id", dest="flavor_id"),
make_option("--password", dest="password")
help="ID of the Owner of the server."),
make_option("--image-id", dest="image_id",
help="ID of the image."),
make_option("--flavor-id", dest="flavor_id",
help="ID of the flavor"),
make_option("--password", dest="password",
help="Password for the new server")
)
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']
......@@ -119,7 +121,6 @@ class Command(BaseCommand):
if not backend:
raise CommandError("Can not allocate VM")
# Get Public address
(network, address) = allocate_public_address(backend)
if address is None:
......
......@@ -42,25 +42,31 @@ from ._common import format_bool, format_date
class Command(BaseCommand):
args = "<server ID>"
help = "Show server info"
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")
flavor = '%s (%s)' % (server.flavor.id, server.flavor.name)
image = '%s (%s)' % (server.imageid,
get_image(server.imageid, server.userid).get('name'))
userid = server.userid
imageid = server.imageid
try:
image_name = get_image(imageid, userid).get('name')
except:
image_name = "None"
image = '%s (%s)' % (imageid, image_name)
kv = {
'id': server_id,
'name': server.name,
'owner': server.userid,
'owner': userid,
'created': format_date(server.created),
'updated': format_date(server.updated),
'image': image,
......@@ -70,7 +76,7 @@ class Command(BaseCommand):
'suspended': format_bool(server.suspended),
'state': server.operstate
}
for key, val in sorted(kv.items()):
line = '%s: %s\n' % (key.rjust(16), val)
self.stdout.write(line.encode('utf8'))
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of GRNET S.A.
#
from django.core.management.base import BaseCommand
from optparse import make_option
from synnefo.plankton.backend import ImageBackend
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--user-id', dest='userid',
help="List all images available to that user"),
)
def handle(self, **options):
userid = options['userid']
write = self.stdout.write
c = ImageBackend(userid) if userid else ImageBackend("")
images = c.list()
images.sort(key=lambda x: x['created_at'], reverse=True)
fields = ("id", "name", "owner", "public")
columns = (40, 30, 30, 7)
sep = "-" * 107
line = "".join(f.rjust(c) for f, c in zip(fields, columns))
write(line + "\n")
write(sep + "\n")
for img in images:
fields = (img["id"], img["name"], img["owner"], img["is_public"])
line = "".join(str(f).rjust(c) for f, c in zip(fields, columns))
write(line + "\n")
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of GRNET S.A.
#
from django.core.management.base import BaseCommand, CommandError
from synnefo.plankton.backend import ImageBackend
from pprint import pprint
class Command(BaseCommand):
args = "<image_id>"
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide an image ID")
image_id = args[0]
c = ImageBackend("")
image = c.get_image(image_id)
if not image:
raise CommandError("Image not Found")
pprint(image, stream=self.stdout)
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