Commit b85b36f2 authored by Giorgos Verigakis's avatar Giorgos Verigakis
Browse files

Add management commands

* listservers
* showserver
* modifyserver
parent 190c32cd
# 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 GRNET S.A. ``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 GRNET S.A 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 datetime import datetime
from django.utils.timesince import timesince, timeuntil
def format_bool(b):
return 'YES' if b else 'NO'
def format_date(d):
if not d:
return ''
if d < datetime.now():
return timesince(d) + ' ago'
else:
return 'in ' + timeuntil(d)
# 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 GRNET S.A. ``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 GRNET S.A 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 optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.api.util import get_image
from synnefo.db.models import VirtualMachine
class Command(BaseCommand):
help = "List servers"
option_list = BaseCommand.option_list + (
make_option('-c',
action='store_true',
dest='csv',
default=False,
help="Use pipes to separate values"),
make_option('--build',
action='store_true',
dest='build',
default=False,
help="List only servers in the building state"),
)
def handle(self, *args, **options):
if args:
raise CommandError("Command doesn't accept any arguments")
servers = VirtualMachine.objects.all()
if options['build']:
servers = servers.filter(operstate='BUILD')
labels = ('id', 'name', 'owner', 'flavor', 'image', 'state')
columns = (3, 12, 20, 11, 12, 9)
if not options['csv']:
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n')
sep = '-' * len(line)
self.stdout.write(sep + '\n')
for server in servers:
id = str(server.id)
flavor = server.flavor.name
image = get_image(server.imageid, server.userid)['name']
fields = (id, server.name, server.userid, flavor, image,
server.operstate)
if options['csv']:
line = '|'.join(fields)
else:
line = ' '.join(f.rjust(w) for f, w in zip(fields, columns))
self.stdout.write(line.encode('utf8') + '\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 GRNET S.A. ``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 GRNET S.A 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 optparse import make_option
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',
metavar='NAME',
help="Set server's name"),
make_option('--owner',
dest='owner',
metavar='USER_ID',
help="Set server's owner"),
make_option('--state',
dest='state',
metavar='STATE',
help="Set server's state"),
make_option('--set-deleted',
action='store_true',
dest='deleted',
help="Mark a server as deleted"),
make_option('--set-undeleted',
action='store_true',
dest='undeleted',
help="Mark a server as not deleted"),
make_option('--set-suspended',
action='store_true',
dest='suspended',
help="Mark a server as suspended"),
make_option('--set-unsuspended',
action='store_true',
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]
if state not in allowed:
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()
# 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 GRNET S.A. ``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 GRNET S.A 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.api.util import get_image
from synnefo.db.models import VirtualMachine
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'))
kv = {
'id': server_id,
'name': server.name,
'owner': server.userid,
'created': format_date(server.created),
'updated': format_date(server.updated),
'image': image,
'host id': server.hostid,
'flavor': flavor,
'deleted': format_bool(server.deleted),
'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'))
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