96
98
from nova.volume import volume_types
98
100
FLAGS = flags.FLAGS
99
flags.DECLARE('fixed_range', 'nova.network.manager')
101
flags.DECLARE('flat_network_bridge', 'nova.network.manager')
100
102
flags.DECLARE('num_networks', 'nova.network.manager')
103
flags.DECLARE('multi_host', 'nova.network.manager')
101
104
flags.DECLARE('network_size', 'nova.network.manager')
102
105
flags.DECLARE('vlan_start', 'nova.network.manager')
103
106
flags.DECLARE('vpn_start', 'nova.network.manager')
104
flags.DECLARE('fixed_range_v6', 'nova.network.manager')
105
flags.DECLARE('gateway_v6', 'nova.network.manager')
107
flags.DECLARE('default_floating_pool', 'nova.network.manager')
108
flags.DECLARE('public_interface', 'nova.network.linux_net')
106
109
flags.DECLARE('libvirt_type', 'nova.virt.libvirt.connection')
107
110
flags.DEFINE_flag(flags.HelpFlag())
108
111
flags.DEFINE_flag(flags.HelpshortFlag())
680
687
class FloatingIpCommands(object):
681
688
"""Class for managing floating ip."""
683
@args('--ip_range', dest="range", metavar='<range>', help='IP range')
684
def create(self, range):
690
@args('--ip_range', dest="ip_range", metavar='<range>', help='IP range')
691
@args('--pool', dest="pool", metavar='<pool>', help='Optional pool')
692
@args('--interface', dest="interface", metavar='<interface>',
693
help='Optional interface')
694
def create(self, ip_range, pool=None, interface=None):
685
695
"""Creates floating ips for zone by range"""
686
for address in netaddr.IPNetwork(range):
687
db.floating_ip_create(context.get_admin_context(),
688
{'address': str(address)})
696
addresses = netaddr.IPNetwork(ip_range)
697
admin_context = context.get_admin_context()
699
pool = FLAGS.default_floating_pool
701
interface = FLAGS.public_interface
702
for address in addresses.iter_hosts():
703
db.floating_ip_create(admin_context,
704
{'address': str(address),
706
'interface': interface})
690
708
@args('--ip_range', dest="ip_range", metavar='<range>', help='IP range')
691
709
def delete(self, ip_range):
692
710
"""Deletes floating ips by range"""
693
for address in netaddr.IPNetwork(ip_range):
711
for address in netaddr.IPNetwork(ip_range).iter_hosts():
694
712
db.floating_ip_destroy(context.get_admin_context(),
1003
1025
@args('--ec2_id', dest='ec2_id', metavar='<ec2 id>', help='EC2 ID')
1004
1026
@args('--dest', dest='dest', metavar='<Destanation>',
1005
help='destanation node')
1006
def block_migration(self, ec2_id, dest):
1027
help='destanation node')
1028
@args('--disk_over_commit', dest='disk_over_commit',
1029
metavar='<overcommit flag>',
1030
help='Allow overcommit (default Flase)')
1031
def block_migration(self, ec2_id, dest, disk_over_commit=False):
1007
1032
"""Migrates a running instance to a new machine with storage data."""
1009
self._migration(ec2_id, dest, True)
1034
self._migration(ec2_id, dest, True, disk_over_commit)
1012
1037
class ServiceCommands(object):
1072
1097
@args('--host', dest='host', metavar='<host>', help='Host')
1073
1098
def describe_resource(self, host):
1074
"""Describes cpu/memory/hdd info for host."""
1099
"""Describes cpu/memory/hdd info for host.
1101
:param host: hostname.
1076
1104
result = rpc.call(context.get_admin_context(),
1077
1105
FLAGS.scheduler_topic,
1078
1106
{"method": "show_host_resources",
1079
1107
"args": {"host": host}})
1081
if type(result) != dict:
1109
if not isinstance(result, dict):
1082
1110
print _('An unexpected error has occurred.')
1083
1111
print _('[Result]'), result
1085
cpu = result['resource']['vcpus']
1086
mem = result['resource']['memory_mb']
1087
hdd = result['resource']['local_gb']
1088
cpu_u = result['resource']['vcpus_used']
1089
mem_u = result['resource']['memory_mb_used']
1090
hdd_u = result['resource']['local_gb_used']
1113
# Printing a total and used_now
1114
# (NOTE)The host name width 16 characters
1115
print '%(a)-25s%(b)16s%(c)8s%(d)8s%(e)8s' % {"a": _('HOST'),
1120
print '%(a)-16s(total)%(b)26s%(c)8s%(d)8s' %\
1122
"b": result['resource']['vcpus'],
1123
"c": result['resource']['memory_mb'],
1124
"d": result['resource']['local_gb']}
1126
print '%(a)-16s(used_now)%(b)23s%(c)8s%(d)8s' %\
1128
"b": result['resource']['vcpus_used'],
1129
"c": result['resource']['memory_mb_used'],
1130
"d": result['resource']['local_gb_used']}
1132
# Printing a used_max
1095
print 'HOST\t\t\tPROJECT\t\tcpu\tmem(mb)\tdisk(gb)'
1096
print '%s(total)\t\t\t%s\t%s\t%s' % (host, cpu, mem, hdd)
1097
print '%s(used_now)\t\t\t%s\t%s\t%s' % (host, cpu_u, mem_u, hdd_u)
1098
for p_id, val in result['usage'].items():
1136
ctxt = context.get_admin_context()
1137
instance_refs = db.instance_get_all_by_host(ctxt, host)
1139
project_ids = [i['project_id'] for i in instance_refs]
1140
project_ids = list(set(project_ids))
1142
for project_id in project_ids:
1143
vcpus = [i['vcpus'] for i in instance_refs \
1144
if i['project_id'] == project_id]
1146
mem = [i['memory_mb'] for i in instance_refs \
1147
if i['project_id'] == project_id]
1149
disk = [i['local_gb'] for i in instance_refs \
1150
if i['project_id'] == project_id]
1152
usage[project_id] = {
1153
'vcpus': reduce(lambda x, y: x + y, vcpus),
1154
'memory_mb': reduce(lambda x, y: x + y, mem),
1155
'local_gb': reduce(lambda x, y: x + y, disk)}
1157
for p_id, val in usage.items():
1099
1158
cpu_sum += val['vcpus']
1100
1159
mem_sum += val['memory_mb']
1101
1160
hdd_sum += val['local_gb']
1102
print '%s(used_max)\t\t\t%s\t%s\t%s' % (host, cpu_sum,
1105
for p_id, val in result['usage'].items():
1106
print '%s\t\t%s\t\t%s\t%s\t%s' % (host,
1112
@args('--host', dest='host', metavar='<host>', help='Host')
1113
def update_resource(self, host):
1114
"""Updates available vcpu/memory/disk info for host."""
1116
ctxt = context.get_admin_context()
1117
service_refs = db.service_get_all_by_host(ctxt, host)
1118
if len(service_refs) <= 0:
1119
raise exception.Invalid(_('%s does not exist.') % host)
1121
service_refs = [s for s in service_refs if s['topic'] == 'compute']
1122
if len(service_refs) <= 0:
1123
raise exception.Invalid(_('%s is not compute node.') % host)
1126
db.queue_get_for(ctxt, FLAGS.compute_topic, host),
1127
{"method": "update_available_resource"})
1161
print '%(a)-16s(used_max)%(b)23s%(c)8s%(d)8s' % {"a": host,
1166
for p_id, val in usage.items():
1167
print '%(a)-25s%(b)16s%(c)8s%(d)8s%(e)8s' %\
1171
"d": val['memory_mb'],
1172
"e": val['local_gb']}
1130
1175
class HostCommands(object):