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

cyclades: Fix server-create and server-inspect

Fix output of server-inspect command. Also, add --wait option to
server-create command and pretty-print the newly created server.
parent c3d17faf
......@@ -34,7 +34,8 @@
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from synnefo.management import common
from synnefo.management import common, pprint
from snf_django.management.utils import parse_bool
from synnefo.logic import servers
......@@ -67,7 +68,15 @@ class Command(BaseCommand):
" Use snf-manage flavor-list to find out"
" available flavors."),
make_option("--password", dest="password",
help="Password for the new server")
help="Password for the new server"),
make_option(
'--wait',
dest='wait',
default="True",
choices=["True", "False"],
metavar="True|False",
help="Wait for Ganeti job to complete."),
)
@common.convert_api_faults
......@@ -100,5 +109,9 @@ class Command(BaseCommand):
else:
backend = None
servers.create(user_id, name, password, flavor, image,
use_backend=backend)
server = servers.create(user_id, name, password, flavor, image,
use_backend=backend)
pprint.pprint_server(server, stdout=self.stdout)
wait = parse_bool(options["wait"])
common.wait_server_task(server, wait, self.stdout)
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -31,28 +31,11 @@
# 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.lib.utils import merge_time
from snf_django.lib.astakos import UserCache
from synnefo.logic.rapi import GanetiApiError
from synnefo.management.common import Omit
from synnefo.management import common
from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
ASTAKOS_BASE_URL)
# Fields to print from a gnt-instance info
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
'pnode', 'snode', 'network_port', 'disk_template',
'disk_usage', 'oper_ram', 'oper_vcpus', 'mtime',
'nic.ips', 'nic.macs', 'nic.networks', 'nic.modes')
# Fields to print from a gnt-job info
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
'oplog', 'start_ts', 'end_ts')
from synnefo.management import pprint
class Command(BaseCommand):
......@@ -82,89 +65,10 @@ class Command(BaseCommand):
displayname = options['displayname']
ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN)
try:
image = common.get_image(vm.imageid, vm.userid)['name']
except:
image = vm.imageid
sep = '-' * 80 + '\n'
labels = filter(lambda x: x is not Omit,
['name', 'owner_uuid',
'owner_name' if displayname else Omit,
'flavor', 'image', 'state', 'backend', 'deleted',
'action', 'task', 'task_job', 'backendjobid',
'backendopcode', 'backendjobstatus', 'backend_time'])
uuid = vm.userid
if displayname:
dname = ucache.get_name(uuid)
fields = filter(lambda x: x is not Omit,
[vm.name, uuid, dname if displayname else Omit,
vm.flavor.name, image, common.format_vm_state(vm),
str(vm.backend), str(vm.deleted), str(vm.action),
str(vm.task), str(vm.task_job_id),
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')
self.stdout.write(sep)
for l, f in zip(labels, fields):
self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n')
self.stdout.write('\n')
for nic in vm.nics.all():
params = (nic.index, nic.ipv4, nic.mac, nic.ipv6, nic.network)
self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s,"
" Network: %s\n" % params)
client = vm.get_client()
try:
g_vm = client.GetInstance(vm.backend_vm_id)
self.stdout.write('\n')
self.stdout.write(sep)
self.stdout.write('State of Server in Ganeti\n')
self.stdout.write(sep)
for i in GANETI_INSTANCE_FIELDS:
try:
value = g_vm[i]
if i.find('time') != -1:
value = datetime.fromtimestamp(value)
self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n')
except KeyError:
pass
except GanetiApiError as e:
if e.code == 404:
self.stdout.write('Server does not exist in backend %s\n' %
vm.backend.clustername)
else:
raise e
if not options['jobs']:
return
self.stdout.write('\n')
self.stdout.write(sep)
self.stdout.write('Non-archived jobs concerning Server in Ganeti\n')
self.stdout.write(sep)
jobs = client.GetJobs()
for j in jobs:
info = client.GetJobStatus(j)
summary = ' '.join(info['summary'])
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:
value = merge_time(value)
try:
self.stdout.write(i.ljust(14) + ': ' + str(value) +
'\n')
except KeyError:
pass
self.stdout.write('\n' + sep)
# Return the RAPI client to pool
vm.put_client(client)
pprint.pprint_server(vm, display_mails=displayname, stdout=self.stdout)
self.stdout.write("\n")
pprint.pprint_server_nics(vm, stdout=self.stdout)
self.stdout.write("\n")
pprint.pprint_server_in_ganeti(vm, print_jobs=options["jobs"],
stdout=self.stdout)
self.stdout.write("\n")
......@@ -40,6 +40,7 @@ from synnefo.settings import (CYCLADES_SERVICE_TOKEN as ASTAKOS_TOKEN,
from synnefo.db.models import Backend, pooled_rapi_client
from synnefo.logic.rapi import GanetiApiError
from synnefo.logic.reconciliation import nics_from_instance
from synnefo.management.common import get_image
def pprint_network(network, display_mails=False, stdout=None, title=None):
......@@ -56,7 +57,7 @@ def pprint_network(network, display_mails=False, stdout=None, title=None):
("backend-name", network.backend_id),
("state", network.state),
("userid", userid),
("username", ucache.get_name(userid) if display_mails else userid),
("username", ucache.get_name(userid) if display_mails else None),
("public", network.public),
("floating_ip_pool", network.floating_ip_pool),
("external_router", network.external_router),
......@@ -214,3 +215,114 @@ def pprint_port_in_ganeti(port, stdout=None, title=None):
title=title)
vm.put_client(client)
def pprint_server(server, display_mails=False, stdout=None, title=None):
if stdout is None:
stdout = sys.stdout
if title is None:
title = "State of Server %s in DB" % server.id
ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN)
userid = server.userid
try:
image = get_image(server.imageid, server.userid)['name']
except:
image = server.imageid
server_dict = OrderedDict([
("id", server.id),
("name", server.name),
("userid", server.userid),
("username", ucache.get_name(userid) if display_mails else None),
("flavor_id", server.flavor_id),
("flavor_name", server.flavor.name),
("imageid", server.imageid),
("image_name", image),
("state", server.operstate),
("backend", server.backend),
("deleted", server.deleted),
("action", server.action),
("task", server.task),
("task_job_id", server.task_job_id),
("backendjobid", server.backendjobid),
("backendopcode", server.backendopcode),
("backendjobstatus", server.backendjobstatus),
("backend_time", server.backendtime),
])
pprint_table(stdout, server_dict.items(), None, separator=" | ",
title=title)
def pprint_server_nics(server, stdout=None, title=None):
if title is None:
title = "NICs of Server %s" % server.id
if stdout is None:
stdout = sys.stdout
nics = []
for nic in server.nics.all():
nics.append((nic.name, nic.index, nic.mac, nic.ipv4_address,
nic.ipv6_address, nic.network, nic.firewall_profile,
nic.state))
headers = ["Name", "Index", "MAC", "IPv4 Address", "IPv6 Address",
"Network", "Firewall", "State"]
pprint_table(stdout, nics, headers, separator=" | ",
title=title)
def pprint_server_in_ganeti(server, print_jobs=False, stdout=None, title=None):
if stdout is None:
stdout = sys.stdout
if title is None:
title = "State of Server %s in Ganeti" % server.id
client = server.get_client()
try:
server_info = client.GetInstance(server.backend_vm_id)
except GanetiApiError as e:
if e.code == 404:
stdout.write("NIC seems attached to server %s, but"
" server does not exist in backend.\n"
% server)
return
raise e
server.put_client(client)
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
'pnode', 'snode', 'network_port',
'disk_template', 'disk_usage',
'oper_ram', 'oper_vcpus', 'mtime')
server_dict = OrderedDict([(k, server_info.get(k))
for k in GANETI_INSTANCE_FIELDS])
pprint_table(stdout, server_dict.items(), None, separator=" | ",
title="NICs of Server %s in Ganeti" % server.id)
stdout.write("\n")
nics = nics_from_instance(server_info)
nics_keys = ["ip", "mac", "name", "network"]
nics_values = [[nic[key] for key in nics_keys] for nic in nics]
pprint_table(stdout, nics_values, nics_keys, separator=" | ",
title=title)
if not print_jobs:
return
client = server.get_client()
jobs = client.GetJobs(bulk=True)
server_jobs = filter(
lambda x: server.backend_vm_id in (" ".join(x.get("summary"))), jobs)
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
'oplog', 'start_ts', 'end_ts')
for server_job in server_jobs:
stdout.write("\n")
values = [server_job.get(k) for k in GANETI_JOB_FIELDS]
pprint_table(stdout, zip(GANETI_JOB_FIELDS, values), None,
separator=" | ",
title="Ganeti Job %s" % server_job["id"])
server.put_client(client)
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