Commit bf85bdd7 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Refactor code in management commands

parent b2a213ed
......@@ -34,11 +34,10 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import format_bool
from synnefo.db.models import Flavor
from ._common import format_bool
class Command(BaseCommand):
help = "List flavors"
......
......@@ -34,14 +34,14 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_flavor
from synnefo.db.models import Flavor
class Command(BaseCommand):
args = "<flavor id>"
help = "Modify a flavor"
option_list = BaseCommand.option_list + (
make_option('--set-deleted',
action='store_true',
......@@ -52,20 +52,17 @@ class Command(BaseCommand):
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")
try:
flavor_id = int(args[0])
flavor = Flavor.objects.get(id=flavor_id)
except (ValueError, Flavor.DoesNotExist):
raise CommandError("Invalid flavor ID")
flavor = get_flavor(args[0])
if options.get('deleted'):
flavor.deleted = True
elif options.get('undeleted'):
flavor.deleted = False
flavor.save()
......@@ -34,10 +34,12 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import validate_network_info, get_backend
from synnefo.db.models import Network, Backend
from synnefo.db.models import Network
from synnefo.logic.backend import create_network
from _common import validate_network_info
from synnefo.api.util import net_resources
NETWORK_TYPES = ['PUBLIC_ROUTED', 'PRIVATE_MAC_FILTERED',
'PRIVATE_PHYSICAL_VLAN', 'CUSTOM_ROUTED',
......@@ -132,13 +134,7 @@ class Command(BaseCommand):
raise CommandError("Can not override PHYSICAL_VLAN link")
if backend_id:
try:
backend_id = int(backend_id)
backend = Backend.objects.get(id=backend_id)
except ValueError:
raise CommandError("Invalid backend ID")
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
backend = get_backend(backend_id)
default_link, default_mac_prefix = net_resources(net_type)
if not link:
......
......@@ -34,11 +34,10 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import format_bool
from synnefo.db.models import Network
from ._common import format_bool
class Command(BaseCommand):
help = "List networks"
......
......@@ -36,7 +36,7 @@ from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Network, pooled_rapi_client
from _common import validate_network_info
from synnefo.management.common import validate_network_info, get_network
HELP_MSG = \
"""Modify a network.
......@@ -102,13 +102,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a network ID")
try:
network_id = int(args[0])
network = Network.objects.get(id=network_id)
except ValueError:
raise CommandError("Invalid network ID")
except (ValueError, Network.DoesNotExist):
raise CommandError("Network not found in DB")
network = get_network(args[0])
# Validate subnet
if options.get('subnet'):
......
......@@ -29,8 +29,8 @@
#
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Network
from synnefo.logic.backend import delete_network
from synnefo.management.common import get_network
class Command(BaseCommand):
......@@ -45,13 +45,7 @@ class Command(BaseCommand):
if len(args) < 1:
raise CommandError("Please provide a network ID")
try:
network_id = int(args[0])
network = Network.objects.get(id=network_id)
except ValueError:
raise CommandError("Invalid network ID")
except Network.DoesNotExist:
raise CommandError("Network not found in DB")
network = get_network(args[0])
self.stdout.write('Trying to remove network: %s\n' % str(network))
......
......@@ -34,12 +34,12 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management import common
from synnefo.db.models import VirtualMachine, Backend, Flavor
from synnefo.db.models import VirtualMachine
from synnefo.logic.backend import create_instance
from synnefo.logic.backend_allocator import BackendAllocator
from synnefo.api.util import get_image, allocate_public_address
from synnefo.api.faults import ItemNotFound
from synnefo.api.util import allocate_public_address
HELP_MSG = """
......@@ -95,22 +95,11 @@ class Command(BaseCommand):
# Get Flavor
if flavor_id:
try:
flavor_id = int(flavor_id)
flavor = Flavor.objects.get(id=flavor_id)
except ValueError:
raise CommandError("Invalid flavor-id")
except Flavor.DoesNotExist:
raise CommandError("Flavor not found")
else:
raise CommandError("flavor-id is mandatory")
flavor = common.get_flavor(flavor_id)
# Get Image
if image_id:
try:
img = get_image(image_id, user_id)
except ItemNotFound:
raise CommandError("Image not found")
img = common.get_image(image_id, user_id)
properties = img.get('properties', {})
image = {}
......@@ -123,11 +112,7 @@ class Command(BaseCommand):
# Get Backend
if backend_id:
try:
backend_id = int(backend_id)
backend = Backend.objects.get(id=backend_id)
except (ValueError, Backend.DoesNotExist):
raise CommandError("Invalid Backend ID")
backend = common.get_backend(backend_id)
else:
ballocator = BackendAllocator()
backend = ballocator.allocate(user_id, flavor)
......
......@@ -31,16 +31,13 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from datetime import datetime
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.api.util import get_image
from synnefo.lib.utils import merge_time
from synnefo.db.models import VirtualMachine
from synnefo.logic.rapi import GanetiApiError
from ._common import format_vm_state
from synnefo.management import common
# Fields to print from a gnt-instance info
......@@ -56,6 +53,7 @@ GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
class Command(BaseCommand):
help = "Inspect a server on DB and Ganeti"
args = "<server ID>"
option_list = BaseCommand.option_list + (
make_option('--jobs', action='store_true',
......@@ -68,16 +66,10 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a server ID")
try:
vm_id = int(args[0])
vm = VirtualMachine.objects.get(id=vm_id)
except ValueError:
raise CommandError("Invalid server ID")
except VirtualMachine.DoesNotExist:
raise CommandError("Server not found in DB")
vm = common.get_vm(args[0])
try:
image = get_image(vm.imageid, vm.userid)['name']
image = common.get_image(vm.imageid, vm.userid)['name']
except:
image = vm.imageid
......@@ -86,9 +78,10 @@ class Command(BaseCommand):
'deleted', 'action', 'backendjobid', 'backendopcode',
'backendjobstatus', 'backend_time')
fields = (vm.name, vm.userid, vm.flavor.name, image,
format_vm_state(vm), str(vm.backend), str(vm.deleted),
str(vm.action), str(vm.backendjobid), str(vm.backendopcode),
str(vm.backendjobstatus), str(vm.backendtime))
common.format_vm_state(vm), str(vm.backend),
str(vm.deleted), str(vm.action), str(vm.backendjobid),
str(vm.backendopcode), str(vm.backendjobstatus),
str(vm.backendtime))
self.stdout.write(sep)
self.stdout.write('State of Server in DB\n')
......
......@@ -34,10 +34,10 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import format_vm_state, get_backend
from synnefo.api.util import get_image
from synnefo.db.models import VirtualMachine, Backend
from ._common import format_vm_state
from synnefo.db.models import VirtualMachine
class Command(BaseCommand):
......@@ -71,11 +71,8 @@ class Command(BaseCommand):
raise CommandError("Command doesn't accept any arguments")
if options['backend_id']:
try:
servers = Backend.objects.get(id=options['backend_id'])\
.virtual_machines
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
backend = get_backend(options['backend_id'])
servers = backend.virtual_machines
else:
servers = VirtualMachine.objects
......
......@@ -34,6 +34,7 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_vm
from synnefo.db.models import VirtualMachine
......@@ -77,11 +78,7 @@ class Command(BaseCommand):
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")
server = get_vm(args[0])
name = options.get('name')
if name is not None:
......
......@@ -32,11 +32,9 @@
# 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, format_vm_state
from synnefo.management.common import (format_bool, format_date,
format_vm_state, get_vm,
get_image)
class Command(BaseCommand):
......@@ -47,11 +45,7 @@ class Command(BaseCommand):
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")
server = get_vm(args[0])
flavor = '%s (%s)' % (server.flavor.id, server.flavor.name)
userid = server.userid
......@@ -64,7 +58,7 @@ class Command(BaseCommand):
image = '%s (%s)' % (imageid, image_name)
kv = {
'id': server_id,
'id': server.id,
'name': server.name,
'owner': userid,
'created': format_date(server.created),
......
......@@ -33,8 +33,7 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Backend
from synnefo.management.common import get_backend
class Command(BaseCommand):
......@@ -78,13 +77,7 @@ class Command(BaseCommand):
if len(args) != 1:
raise CommandError("Please provide a backend ID")
try:
backend_id = int(args[0])
backend = Backend.objects.get(id=backend_id)
except ValueError:
raise CommandError("Invalid backend ID")
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
backend = get_backend(args[0])
# Ensure fields correspondence with options and Backend model
fields = ('clustername', 'port', 'username', 'password', 'drained',
......
......@@ -29,7 +29,8 @@
#
from django.core.management.base import BaseCommand, CommandError
from synnefo.db.models import Backend, VirtualMachine, BackendNetwork
from synnefo.management.common import get_backend
from synnefo.db.models import VirtualMachine, BackendNetwork
class Command(BaseCommand):
......@@ -48,13 +49,7 @@ class Command(BaseCommand):
if len(args) < 1:
raise CommandError("Please provide a backend ID")
try:
backend_id = int(args[0])
backend = Backend.objects.get(id=backend_id)
except ValueError:
raise CommandError("Invalid backend ID")
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
backend = get_backend(args[0])
self.stdout.write('Trying to remove backend: %s\n' % backend.clustername)
......
......@@ -29,7 +29,9 @@
#
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from synnefo.management.common import get_backend
from synnefo import settings
import datetime
......@@ -58,18 +60,11 @@ class Command(BaseCommand):
def handle(self, **options):
if options['backend_id']:
try:
backend_id = int(options['backend_id'])
backends = [Backend.objects.get(id=backend_id)]
except ValueError:
raise CommandError("Wrong backend ID")
except Backend.DoesNotExist:
raise CommandError("Backend not found in DB")
backends = [get_backend(options['backend_id'])]
else:
backends = Backend.objects.filter(offline=False)
if not options['drained']:
backends = backends.filter(drained=False)
if not options['drained']:
backends = backends.filter(drained=False)
now = datetime.datetime.now()
if options['older_than'] is not None:
......
......@@ -34,8 +34,9 @@
import json
from django.core.management.base import BaseCommand, CommandError
from synnefo.management.common import get_network
from synnefo.db.models import (Backend, Network, BackendNetwork,
from synnefo.db.models import (Backend, BackendNetwork,
pooled_rapi_client)
from synnefo.logic.rapi import GanetiApiError
from util import pool_map_chunks
......@@ -47,15 +48,8 @@ class Command(BaseCommand):
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a network ID.")
try:
net_id = int(args[0])
except ValueError:
raise CommandError("Invalid network ID.")
try:
net = Network.objects.get(id=net_id)
except Network.DoesNotExist:
raise CommandError("Network not found in DB.")
net = get_network(args[0])
sep = '-' * 80 + '\n'
labels = ('name', 'backend-name', 'state', 'owner', 'subnet', 'gateway',
......
......@@ -31,11 +31,16 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
import ipaddr
from datetime import datetime
from django.utils.timesince import timesince, timeuntil
from django.core.management import CommandError
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor
from synnefo.api.util import get_image as backend_get_image
from synnefo.api.faults import ItemNotFound
from synnefo.api.util import validate_network_size
from synnefo.settings import MAX_CIDR_BLOCK
......@@ -93,3 +98,63 @@ def validate_network_info(options):
raise CommandError('Malformed gateway6')
return subnet, gateway, subnet6, gateway6
def get_backend(backend_id):
try:
backend_id = int(backend_id)
return Backend.objects.get(id=backend_id)
except ValueError:
raise CommandError("Invalid Backend ID: %s" % backend_id)
except Backend.DoesNotExist:
raise CommandError("Backend with ID %s not found in DB. "
" Use snf-manage backend-list to find"
" out available backend IDs." % backend_id)
def get_image(image_id, user_id):
if image_id:
try:
return backend_get_image(image_id, user_id)
except ItemNotFound:
raise CommandError("Image with ID %s not found."
" Use snf-manage image-list to find"
" out available image IDs." % image_id)
else:
raise CommandError("image-id is mandatory")
def get_vm(server_id):
try:
server_id = int(server_id)
return VirtualMachine.objects.get(id=server_id)
except ValueError:
raise CommandError("Invalid server ID: %s", server_id)
except VirtualMachine.DoesNotExist:
raise CommandError("Server with ID %s not found in DB."
" Use snf-manage server-list to find out"
" available server IDs." % server_id)
def get_network(network_id):
try:
network_id = int(network_id)
return Network.objects.get(id=network_id)
except ValueError:
raise CommandError("Invalid network ID: %s", network_id)
except Network.DoesNotExist:
raise CommandError("Network with ID %s not found in DB."
" Use snf-manage network-list to find out"
" available network IDs." % network_id)
def get_flavor(flavor_id):
try:
flavor_id = int(flavor_id)
return Flavor.objects.get(id=flavor_id)
except ValueError:
raise CommandError("Invalid flavor ID: %s", flavor_id)
except Flavor.DoesNotExist:
raise CommandError("Flavor with ID %s not found in DB."
" Use snf-manage flavor-list to find out"
" available flavor IDs." % flavor_id)
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