本文整理汇总了Python中instance.models.Instance类的典型用法代码示例。如果您正苦于以下问题:Python Instance类的具体用法?Python Instance怎么用?Python Instance使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Instance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: instances
def instances(request, host_id):
"""
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
errors = []
instances = []
compute = Compute.objects.get(id=host_id)
try:
conn = wvmInstances(compute.hostname,
compute.login,
compute.password,
compute.type)
get_instances = conn.get_instances()
except libvirtError as msg_error:
errors.append(msg_error.message)
for instance in get_instances:
try:
inst = Instance.objects.get(compute_id=host_id, name=instance)
uuid = inst.uuid
except Instance.DoesNotExist:
uuid = conn.get_uuid(instance)
inst = Instance(compute_id=host_id, name=instance, uuid=uuid)
inst.save()
instances.append({'name': instance,
'status': conn.get_instance_status(instance),
'uuid': uuid,
'memory': conn.get_instance_memory(instance),
'vcpu': conn.get_instance_vcpu(instance)})
try:
if request.method == 'POST':
name = request.POST.get('name', '')
if 'start' in request.POST:
conn.start(name)
return HttpResponseRedirect(request.get_full_path())
if 'shutdown' in request.POST:
conn.shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'destroy' in request.POST:
conn.force_shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend(name)
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume(name)
return HttpResponseRedirect(request.get_full_path())
conn.close()
except libvirtError as msg_error:
errors.append(msg_error.message)
return render_to_response('instances.html', locals(), context_instance=RequestContext(request))
示例2: instance
def instance(request, host_id, vname):
"""
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
def show_clone_disk(disks):
clone_disk = []
for disk in disks:
if disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk['image'] + "-clone"
clone_disk.append({'dev': disk['dev'], 'storage': disk['storage'], 'image': image})
return clone_disk
errors = []
messages = []
time_refresh = TIME_JS_REFRESH
compute = Compute.objects.get(id=host_id)
computes = Compute.objects.all()
computes_count = len(computes)
keymaps = QEMU_KEYMAPS
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
vname)
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
cur_vcpu = conn.get_cur_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
cur_memory = conn.get_cur_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384]
memory_host = conn.get_max_memory()
vcpu_host = len(vcpu_range)
telnet_port = conn.get_telnet_port()
vnc_port = conn.get_vnc()
vnc_keymap = conn.get_vnc_keymap
snapshots = sorted(conn.get_snapshot(), reverse=True)
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks)
except libvirtError as msg_error:
errors.append(msg_error.message)
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
if instance.uuid != uuid:
instance.uuid = uuid
instance.save()
except Instance.DoesNotExist:
instance = Instance(compute_id=host_id, name=vname, uuid=uuid)
instance.save()
try:
if request.method == 'POST':
if 'start' in request.POST:
conn.start()
return HttpResponseRedirect(request.get_full_path())
if 'power' in request.POST:
if 'shutdown' == request.POST.get('power', ''):
conn.shutdown()
return HttpResponseRedirect(request.get_full_path())
if 'destroy' == request.POST.get('power', ''):
conn.force_shutdown()
return HttpResponseRedirect(request.get_full_path())
if 'managedsave' == request.POST.get('power', ''):
conn.managedsave()
return HttpResponseRedirect(request.get_full_path())
if 'deletesaveimage' in request.POST:
conn.managed_save_remove()
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend()
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume()
return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST:
if conn.get_status() == 1:
conn.force_shutdown()
if request.POST.get('delete_disk', ''):
conn.delete_disk()
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
#.........这里部分代码省略.........
示例3: create
#.........这里部分代码省略.........
networks = sorted(conn.get_networks())
instances = conn.get_instances()
get_images = sorted(conn.get_storages_images())
mac_auto = util.randomMAC()
except libvirtError as err:
errors.append(err)
if conn:
if not storages:
msg = _("You haven't defined have any storage pools")
errors.append(msg)
if not networks:
msg = _("You haven't defined have any network pools")
errors.append(msg)
if request.method == 'POST':
if 'create_flavor' in request.POST:
form = FlavorAddForm(request.POST)
if form.is_valid():
data = form.cleaned_data
create_flavor = Flavor(label=data['label'],
vcpu=data['vcpu'],
memory=data['memory'],
disk=data['disk'])
create_flavor.save()
return HttpResponseRedirect(request.get_full_path())
if 'delete_flavor' in request.POST:
flavor_id = request.POST.get('flavor', '')
delete_flavor = Flavor.objects.get(id=flavor_id)
delete_flavor.delete()
return HttpResponseRedirect(request.get_full_path())
if 'create_xml' in request.POST:
xml = request.POST.get('from_xml', '')
try:
name = util.get_xml_path(xml, '/domain/name')
except util.libxml2.parserError:
name = None
if name in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
else:
try:
conn._defineXML(xml)
return HttpResponseRedirect(reverse('instance', args=[host_id, name]))
except libvirtError as err:
errors.append(err.message)
if 'create' in request.POST:
volumes = {}
form = NewVMForm(request.POST)
if form.is_valid():
data = form.cleaned_data
if data['meta_prealloc']:
meta_prealloc = True
if instances:
if data['name'] in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
if not errors:
if data['hdd_size']:
if not data['mac']:
msg = _("No Virtual Machine MAC has been entered")
errors.append(msg)
else:
try:
path = conn.create_volume(data['storage'], data['name'], data['hdd_size'],
metadata=meta_prealloc)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
elif data['template']:
templ_path = conn.get_volume_path(data['template'])
clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc)
volumes[clone_path] = conn.get_volume_type(clone_path)
else:
if not data['images']:
msg = _("First you need to create or select an image")
errors.append(msg)
else:
for vol in data['images'].split(','):
try:
path = conn.get_volume_path(vol)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
if not errors:
uuid = util.randomUUID()
try:
conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'],
uuid, volumes, data['networks'], data['virtio'], data['mac'])
create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid)
create_instance.save()
return HttpResponseRedirect(reverse('instance', args=[host_id, data['name']]))
except libvirtError as err:
if data['hdd_size']:
conn.delete_volume(volumes.keys()[0])
errors.append(err)
conn.close()
return render_to_response('create.html', locals(), context_instance=RequestContext(request))
示例4: instance
def instance(request, host_id, vname):
"""
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect('/login')
errors = []
messages = []
time_refresh = TIME_JS_REFRESH
compute = Compute.objects.get(id=host_id)
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
vname)
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
memory_range = [256, 512, 1024, 2048, 4096, 8192, 16384]
vnc_port = conn.get_vnc()
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
except libvirtError as msg_error:
errors.append(msg_error.message)
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
if instance.uuid != uuid:
instance.uuid = uuid
instance.save()
except Instance.DoesNotExist:
instance = Instance(compute_id=host_id, name=vname, uuid=uuid)
instance.save()
try:
if request.method == 'POST':
if 'start' in request.POST:
conn.start()
return HttpResponseRedirect(request.get_full_path())
if 'power' in request.POST:
if 'shutdown' == request.POST.get('power', ''):
conn.shutdown()
return HttpResponseRedirect(request.get_full_path())
if 'destroy' == request.POST.get('power', ''):
conn.force_shutdown()
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend()
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume()
return HttpResponseRedirect(request.get_full_path())
if 'delete' in request.POST:
if conn.get_status() == 1:
conn.force_shutdown()
if request.POST.get('delete_disk', ''):
conn.delete_disk()
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
instance.delete()
finally:
conn.delete()
return HttpResponseRedirect('/instances/%s/' % host_id)
if 'snapshot' in request.POST:
conn.create_snapshot()
msg = _("Create snapshot for instance successful")
messages.append(msg)
if 'umount_iso' in request.POST:
image = request.POST.get('iso_media', '')
conn.umount_iso(image)
return HttpResponseRedirect(request.get_full_path())
if 'mount_iso' in request.POST:
image = request.POST.get('iso_media', '')
conn.mount_iso(image)
return HttpResponseRedirect(request.get_full_path())
if 'set_autostart' in request.POST:
conn.set_autostart(1)
return HttpResponseRedirect(request.get_full_path())
if 'unset_autostart' in request.POST:
conn.set_autostart(0)
return HttpResponseRedirect(request.get_full_path())
if 'change_settings' in request.POST:
description = request.POST.get('description', '')
vcpu = request.POST.get('vcpu', '')
memory = request.POST.get('memory', '')
conn.change_settings(description, memory, vcpu)
return HttpResponseRedirect(request.get_full_path())
if 'change_xml' in request.POST:
xml = request.POST.get('inst_xml', '')
#.........这里部分代码省略.........
示例5: create
#.........这里部分代码省略.........
if request.method == 'POST':
if 'create_flavor' in request.POST:
form = FlavorAddForm(request.POST)
if form.is_valid():
data = form.cleaned_data
flavor_name_list = Flavor.objects.filter(label=data['label'])
if flavor_name_list:
msg = _("A virtual machine template with this name already exists")
errors.append(msg)
else:
create_flavor = Flavor(label=data['label'],
vcpu=data['vcpu'],
memory=data['memory'],
disk=data['disk'])
create_flavor.save()
return HttpResponseRedirect(request.get_full_path())
if 'delete_flavor' in request.POST:
flavor_id = request.POST.get('flavor', '')
delete_flavor = Flavor.objects.get(id=flavor_id)
delete_flavor.delete()
return HttpResponseRedirect(request.get_full_path())
if 'create_xml' in request.POST:
xml = request.POST.get('from_xml', '')
try:
name = util.get_xml_path(xml, '/domain/name')
except util.libxml2.parserError:
name = None
if name in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
else:
try:
conn._defineXML(xml)
return HttpResponseRedirect(reverse('instance', args=[host_id, name]))
except libvirtError as err:
errors.append(err.message)
if 'create' in request.POST:
volumes = {}
interfaces = []
vm_vfs_info = {}
form = NewVMForm(request.POST)
if form.is_valid():
data = form.cleaned_data
if instances:
if data['name'] in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
if not errors:
if data['hdd_size']:
try:
path = conn.create_volume(data['storage'], data['name'], data['hdd_size'],
metadata=meta_prealloc)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
elif data['template']:
templ_path = conn.get_volume_path(data['template'])
clone_path = conn.clone_from_template(data['name'], templ_path, metadata=meta_prealloc)
volumes[clone_path] = conn.get_volume_type(clone_path)
ifaces_all = request.POST.getlist('interfaces')
else:
if not data['images']:
msg = _("First you need to create or select an image")
errors.append(msg)
else:
for vol in data['images'].split(','):
try:
path = conn.get_volume_path(vol)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
if not errors:
uuid = util.randomUUID()
try:
template_ver3_flag = True
if data['template'].find("WiseGrid_V3") == -1:
template_ver3_flag = False
conn.create_instance(data['name'], data['cur_memory'], data['cur_vcpu'],
uuid, volumes, ifaces_all, False, True, template_ver3_flag)
create_instance = Instance(compute_id=host_id, name=data['name'], uuid=uuid)
create_instance.save()
vm_vfs_info[data['name']] = ifaces_all
if not errors:
util.update_vfs_fro_vm(vm_vfs_info)
return HttpResponseRedirect(reverse('instance', args=[host_id, data['name']]))
except libvirtError as err:
if data['hdd_size']:
conn.delete_volume(volumes.keys()[0])
errors.append(err)
else:
print form.errors
errors.append(form.errors)
conn.close()
return HttpResponseRedirect(reverse('instances',args=[host_id,errors]))
示例6: instance
def instance(request, host_id, vname):
"""
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('login'))
def show_clone_disk(disks):
clone_disk = []
for disk in disks:
if disk['image'] is None:
continue
if disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk['image'] + "-clone"
clone_disk.append(
{'dev': disk['dev'], 'storage': disk['storage'], 'image': image, 'format': disk['format']})
return clone_disk
errors = []
messages = []
time_refresh = TIME_JS_REFRESH * 3
compute = Compute.objects.get(id=host_id)
computes = Compute.objects.all()
computes_count = len(computes)
keymaps = QEMU_KEYMAPS
console_types = QEMU_CONSOLE_TYPES
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
vname)
conn_interfaces = wvmInterfaces(compute.hostname,
compute.login,
compute.password,
compute.type)
ifaces = conn_interfaces.get_ifaces()
i = 0
ifaces_all = util.get_free_vfs()
ifaces_all = sorted(ifaces_all.iteritems(),key=lambda ax:ax[0])
print ifaces_all
temp_ifaces = []
hd_resources = []
hd_resources_checked = conn.get_hd_resources_device()
if os.path.exists(util.get_hd_resources_conf()):
hd_resources = util.load_hd_resources()
for vf_filter in hd_resources.keys():
if vf_filter == "mem" or vf_filter == "vcpu":
continue
if hd_resources[vf_filter]['used'] == 1:
del hd_resources[vf_filter]
else:
hd_resources = util.create_hd_resources()
vcpu_left = hd_resources["vcpu"]
mem_left = hd_resources["mem"]
del hd_resources["vcpu"]
del hd_resources["mem"]
is_vf = False
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
cur_vcpu = conn.get_cur_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
cur_memory = conn.get_cur_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
hd_resources_checked = conn.get_hd_resources_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
vcpu_max = vcpu_range
vcpu_range = xrange(1, int(vcpu_left) + 1)
memory_range = [2048, 4096, 6144, 8192, 16384]
memory_range = [ memory for memory in memory_range if memory/1024 <= mem_left ]
if memory not in memory_range:
insort(memory_range, memory)
if cur_memory not in memory_range:
insort(memory_range, cur_memory)
memory_host = conn.get_max_memory()
vcpu_host = len(vcpu_max)
telnet_port = conn.get_telnet_port()
console_type = conn.get_console_type()
console_port = conn.get_console_port()
console_keymap = conn.get_console_keymap()
snapshots = sorted(conn.get_snapshot(), reverse=True)
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks)
#.........这里部分代码省略.........
示例7: instances
def instances(request, host_id):
"""
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('login'))
errors = []
instances = []
time_refresh = 8000
get_instances = []
cnt_max = settings.vf_cnt_max
conn = None
meta_prealloc = False
compute = Compute.objects.get(id=host_id)
memory_range = [2048, 4096, 6144, 8192, 16384]
ifaces_all = util.get_free_vfs()
ifaces_all = sorted(ifaces_all.iteritems(),key=lambda ax:ax[0])
try:
conn = wvmInstances(compute.hostname,
compute.login,
compute.password,
compute.type)
conn_create = wvmCreate(compute.hostname,
compute.login,
compute.password,
compute.type)
conn_storage = wvmStorage(compute.hostname,
compute.login,
compute.password,
compute.type,
'Images')
state = conn_storage.is_active()
if state:
conn_storage.refresh()
volumes = conn_storage.update_volumes()
conn_storage.close()
else:
volumes = None
get_images = volumes
get_instances = conn.get_instances()
except libvirtError as err:
pass
# errors.append(err)
for instance in get_instances:
try:
inst = Instance.objects.get(compute_id=host_id, name=instance)
uuid = inst.uuid
except Instance.DoesNotExist:
uuid = conn.get_uuid(instance)
inst = Instance(compute_id=host_id, name=instance, uuid=uuid)
inst.save()
instances.append({'name': instance,
'status': conn.get_instance_status(instance),
'uuid': uuid,
'memory': conn.get_instance_memory(instance),
'vcpu': conn.get_instance_vcpu(instance),
'has_managed_save_image': conn.get_instance_managed_save_image(instance)})
if conn:
try:
if request.method == 'POST':
name = request.POST.get('name', '')
if 'start' in request.POST:
conn.start(name)
return HttpResponseRedirect(request.get_full_path())
if 'shutdown' in request.POST:
conn.shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'destroy' in request.POST:
conn.force_shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'managedsave' in request.POST:
conn.managedsave(name)
return HttpResponseRedirect(request.get_full_path())
if 'deletesaveimage' in request.POST:
conn.managed_save_remove(name)
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend(name)
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume(name)
return HttpResponseRedirect(request.get_full_path())
if 'create' in request.POST:
volumes = {}
interfaces = []
vm_vfs_info = {}
form = NewVMForm(request.POST)
if form.is_valid():
data = form.cleaned_data
if instances:
if data['name'] in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
if not errors:
if data['hdd_size']:
#.........这里部分代码省略.........
示例8: instance
def instance(request, host_id, vname):
"""
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('login'))
def show_clone_disk(disks):
clone_disk = []
for disk in disks:
if disk['image'] is None:
continue
if disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk['image'] + "-clone"
clone_disk.append(
{'dev': disk['dev'], 'storage': disk['storage'], 'image': image, 'format': disk['format']})
return clone_disk
errors = []
messages = []
time_refresh = TIME_JS_REFRESH * 3
compute = Compute.objects.get(id=host_id)
computes = Compute.objects.all()
computes_count = len(computes)
keymaps = QEMU_KEYMAPS
console_types = QEMU_CONSOLE_TYPES
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
vname)
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
cur_vcpu = conn.get_cur_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
cur_memory = conn.get_cur_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384]
if memory not in memory_range:
insort(memory_range, memory)
if cur_memory not in memory_range:
insort(memory_range, cur_memory)
memory_host = conn.get_max_memory()
vcpu_host = len(vcpu_range)
telnet_port = conn.get_telnet_port()
console_type = conn.get_console_type()
console_port = conn.get_console_port()
console_keymap = conn.get_console_keymap()
snapshots = sorted(conn.get_snapshot(), reverse=True)
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks)
console_passwd = conn.get_console_passwd()
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
if instance.uuid != uuid:
instance.uuid = uuid
instance.save()
except Instance.DoesNotExist:
instance = Instance(compute_id=host_id, name=vname, uuid=uuid)
instance.save()
if request.method == 'POST':
if 'start' in request.POST:
conn.start()
return HttpResponseRedirect(request.get_full_path() + '#shutdown')
if 'power' in request.POST:
if 'shutdown' == request.POST.get('power', ''):
conn.shutdown()
return HttpResponseRedirect(request.get_full_path() + '#shutdown')
if 'destroy' == request.POST.get('power', ''):
conn.force_shutdown()
return HttpResponseRedirect(request.get_full_path() + '#forceshutdown')
if 'managedsave' == request.POST.get('power', ''):
conn.managedsave()
return HttpResponseRedirect(request.get_full_path() + '#managedsave')
if 'deletesaveimage' in request.POST:
conn.managed_save_remove()
return HttpResponseRedirect(request.get_full_path() + '#managedsave')
if 'suspend' in request.POST:
conn.suspend()
return HttpResponseRedirect(request.get_full_path() + '#suspend')
if 'pmwakeup' in request.POST:
conn.pMWakeup()
return HttpResponseRedirect(request.get_full_path() + '#pmwakeup')
if 'resume' in request.POST:
#.........这里部分代码省略.........
示例9: instance
def instance(request, host_id, vname):
"""
Instance block
"""
def show_clone_disk(disks):
clone_disk = []
for disk in disks:
if disk['image'].count(".") and len(disk['image'].rsplit(".", 1)[1]) <= 7:
name, suffix = disk['image'].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk['image'] + "-clone"
clone_disk.append(
{'dev': disk['dev'], 'storage': disk['storage'], 'image': image, 'format': disk['format']})
return clone_disk
errors = []
messages = []
time_refresh = TIME_JS_REFRESH
compute = Compute.objects.get(id=host_id)
computes = Compute.objects.all()
computes_count = len(computes)
keymaps = QEMU_KEYMAPS
try:
conn = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
vname)
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
cur_vcpu = conn.get_cur_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
cur_memory = conn.get_cur_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
memory_range = [256, 512, 1024, 2048, 4096, 6144, 8192, 16384]
memory_host = conn.get_max_memory()
vcpu_host = len(vcpu_range)
telnet_port = conn.get_telnet_port()
vnc_port = conn.get_vnc_port()
vnc_keymap = conn.get_vnc_keymap()
vnc_password = conn.get_vnc_passwd()
snapshots = sorted(conn.get_snapshot(), reverse=True)
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks)
cpu_usage = conn.cpu_usage()['cpu']
except libvirtError as err:
errors.append(err)
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
if instance.uuid != uuid:
instance.uuid = uuid
instance.save()
except Instance.DoesNotExist:
instance = Instance(compute_id=host_id, name=vname, uuid=uuid)
instance.save()
try:
if request.method == 'POST':
if 'start' in request.POST:
conn.start()
return HttpResponseRedirect(request.get_full_path() + '#shutdown')
if 'power' in request.POST:
if 'shutdown' == request.POST.get('power', ''):
conn.shutdown()
return HttpResponseRedirect(request.get_full_path() + '#shutdown')
if 'destroy' == request.POST.get('power', ''):
conn.force_shutdown()
return HttpResponseRedirect(request.get_full_path() + '#forceshutdown')
if 'managedsave' == request.POST.get('power', ''):
conn.managedsave()
return HttpResponseRedirect(request.get_full_path() + '#managedsave')
if 'deletesaveimage' in request.POST:
conn.managed_save_remove()
return HttpResponseRedirect(request.get_full_path() + '#managedsave')
if 'suspend' in request.POST:
conn.suspend()
return HttpResponseRedirect(request.get_full_path() + '#suspend')
if 'resume' in request.POST:
conn.resume()
return HttpResponseRedirect(request.get_full_path() + '#suspend')
if 'delete' in request.POST:
if conn.get_status() == 1:
conn.force_shutdown()
if request.POST.get('delete_disk', ''):
print 'yeah'
conn.delete_disk()
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
#.........这里部分代码省略.........
示例10: instances
def instances(request, host_id):
"""
Instances block
"""
errors = []
instances = []
time_refresh = 8000
get_instances = []
conn = None
compute = Compute.objects.get(id=host_id)
try:
conn = wvmInstances(compute.hostname,
compute.login,
compute.password,
compute.type)
get_instances = conn.get_instances()
except libvirtError as err:
errors.append(err)
for instance in get_instances:
try:
inst = Instance.objects.get(compute_id=host_id, name=instance)
uuid = inst.uuid
except Instance.DoesNotExist:
uuid = conn.get_uuid(instance)
inst = Instance(compute_id=host_id, name=instance, uuid=uuid)
inst.save()
conn2 = wvmInstance(compute.hostname,
compute.login,
compute.password,
compute.type,
instance)
instances.append({'name': instance,
'status': conn.get_instance_status(instance),
'uuid': uuid,
'memory': conn.get_instance_memory(instance),
'vcpu': conn.get_instance_vcpu(instance),
'storage': conn2.get_disk_device(),
'has_managed_save_image': conn.get_instance_managed_save_image(instance)})
conn2.close()
if conn:
try:
if request.method == 'POST':
name = request.POST.get('name', '')
if 'start' in request.POST:
conn.start(name)
return HttpResponseRedirect(request.get_full_path())
if 'shutdown' in request.POST:
conn.shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'destroy' in request.POST:
conn.force_shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'managedsave' in request.POST:
conn.managedsave(name)
return HttpResponseRedirect(request.get_full_path())
if 'deletesaveimage' in request.POST:
conn.managed_save_remove(name)
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend(name)
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume(name)
return HttpResponseRedirect(request.get_full_path())
conn.close()
except libvirtError as err:
errors.append(err)
object = {
'response': {
'instances': instances
},
'errors': [str(error) for error in errors]
}
return render(object, 'instances.html', locals(), request)
示例11: instance
def instance(request, host_id, vname):
"""
Instance block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect("/login")
def show_clone_disk(disks):
clone_disk = []
for disk in disks:
if disk["image"].count(".") and len(disk["image"].rsplit(".", 1)[1]) <= 7:
name, suffix = disk["image"].rsplit(".", 1)
image = name + "-clone" + "." + suffix
else:
image = disk["image"] + "-clone"
clone_disk.append(
{"dev": disk["dev"], "storage": disk["storage"], "image": image, "format": disk["format"]}
)
return clone_disk
errors = []
messages = []
time_refresh = TIME_JS_REFRESH
compute = Compute.objects.get(id=host_id)
computes = Compute.objects.all()
computes_count = len(computes)
keymaps = QEMU_KEYMAPS
try:
conn = wvmInstance(compute.hostname, compute.login, compute.password, compute.type, vname)
status = conn.get_status()
autostart = conn.get_autostart()
vcpu = conn.get_vcpu()
cur_vcpu = conn.get_cur_vcpu()
uuid = conn.get_uuid()
memory = conn.get_memory()
cur_memory = conn.get_cur_memory()
description = conn.get_description()
disks = conn.get_disk_device()
media = conn.get_media_device()
networks = conn.get_net_device()
media_iso = sorted(conn.get_iso_media())
vcpu_range = conn.get_max_cpus()
memory_range = [256, 512, 768, 1024, 2048, 4096, 6144, 8192, 16384]
if not memory in memory_range:
insort(memory_range, memory)
if not cur_memory in memory_range:
insort(memory_range, cur_memory)
memory_host = conn.get_max_memory()
vcpu_host = len(vcpu_range)
telnet_port = conn.get_telnet_port()
vnc_port = conn.get_vnc_port()
vnc_keymap = conn.get_vnc_keymap()
snapshots = sorted(conn.get_snapshot(), reverse=True)
inst_xml = conn._XMLDesc(VIR_DOMAIN_XML_SECURE)
has_managed_save_image = conn.get_managed_save_image()
clone_disks = show_clone_disk(disks)
vnc_passwd = conn.get_vnc_passwd()
except libvirtError as err:
errors.append(err)
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
if instance.uuid != uuid:
instance.uuid = uuid
instance.save()
except Instance.DoesNotExist:
instance = Instance(compute_id=host_id, name=vname, uuid=uuid)
instance.save()
try:
if request.method == "POST":
if "start" in request.POST:
conn.start()
return HttpResponseRedirect(request.get_full_path() + "#shutdown")
if "power" in request.POST:
if "shutdown" == request.POST.get("power", ""):
conn.shutdown()
return HttpResponseRedirect(request.get_full_path() + "#shutdown")
if "destroy" == request.POST.get("power", ""):
conn.force_shutdown()
return HttpResponseRedirect(request.get_full_path() + "#forceshutdown")
if "managedsave" == request.POST.get("power", ""):
conn.managedsave()
return HttpResponseRedirect(request.get_full_path() + "#managedsave")
if "deletesaveimage" in request.POST:
conn.managed_save_remove()
return HttpResponseRedirect(request.get_full_path() + "#managedsave")
if "suspend" in request.POST:
conn.suspend()
return HttpResponseRedirect(request.get_full_path() + "#suspend")
if "resume" in request.POST:
conn.resume()
return HttpResponseRedirect(request.get_full_path() + "#suspend")
if "delete" in request.POST:
if conn.get_status() == 1:
conn.force_shutdown()
try:
instance = Instance.objects.get(compute_id=host_id, name=vname)
#.........这里部分代码省略.........
示例12: instances
def instances(request, host_id):
"""
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect("/login")
errors = []
instances = []
time_refresh = 8000
get_instances = []
conn = None
compute = Compute.objects.get(id=host_id)
try:
conn = wvmInstances(compute.hostname, compute.login, compute.password, compute.type)
get_instances = conn.get_instances()
except libvirtError as err:
errors.append(err)
for instance in get_instances:
try:
inst = Instance.objects.get(compute_id=host_id, name=instance)
uuid = inst.uuid
except Instance.DoesNotExist:
uuid = conn.get_uuid(instance)
inst = Instance(compute_id=host_id, name=instance, uuid=uuid)
inst.save()
instances.append(
{
"name": instance,
"status": conn.get_instance_status(instance),
"uuid": uuid,
"memory": conn.get_instance_memory(instance),
"vcpu": conn.get_instance_vcpu(instance),
"has_managed_save_image": conn.get_instance_managed_save_image(instance),
}
)
if conn:
try:
if request.method == "POST":
name = request.POST.get("name", "")
if "start" in request.POST:
conn.start(name)
return HttpResponseRedirect(request.get_full_path())
if "shutdown" in request.POST:
conn.shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if "destroy" in request.POST:
conn.force_shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if "managedsave" in request.POST:
conn.managedsave(name)
return HttpResponseRedirect(request.get_full_path())
if "deletesaveimage" in request.POST:
conn.managed_save_remove(name)
return HttpResponseRedirect(request.get_full_path())
if "suspend" in request.POST:
conn.suspend(name)
return HttpResponseRedirect(request.get_full_path())
if "resume" in request.POST:
conn.resume(name)
return HttpResponseRedirect(request.get_full_path())
conn.close()
except libvirtError as err:
errors.append(err)
return render_to_response("instances.html", locals(), context_instance=RequestContext(request))
示例13: create
#.........这里部分代码省略.........
errors.append(msg)
if request.method == "POST":
if "create_flavor" in request.POST:
form = FlavorAddForm(request.POST)
if form.is_valid():
data = form.cleaned_data
create_flavor = Flavor(
label=data["label"], vcpu=data["vcpu"], memory=data["memory"], disk=data["disk"]
)
create_flavor.save()
return HttpResponseRedirect(request.get_full_path())
if "delete_flavor" in request.POST:
flavor_id = request.POST.get("flavor", "")
delete_flavor = Flavor.objects.get(id=flavor_id)
delete_flavor.delete()
return HttpResponseRedirect(request.get_full_path())
if "create_xml" in request.POST:
xml = request.POST.get("from_xml", "")
try:
name = util.get_xml_path(xml, "/domain/name")
except util.libxml2.parserError:
name = None
if name in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
else:
try:
conn._defineXML(xml)
return HttpResponseRedirect(reverse("instance", args=[host_id, name]))
except libvirtError as err:
errors.append(err.message)
if "create" in request.POST:
volumes = {}
form = NewVMForm(request.POST)
if form.is_valid():
data = form.cleaned_data
if data["meta_prealloc"]:
meta_prealloc = True
if instances:
if data["name"] in instances:
msg = _("A virtual machine with this name already exists")
errors.append(msg)
if not errors:
if data["hdd_size"]:
if not data["mac"]:
msg = _("No Virtual Machine MAC has been entered")
errors.append(msg)
else:
try:
path = conn.create_volume(
data["storage"], data["name"], data["hdd_size"], metadata=meta_prealloc
)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
elif data["template"]:
templ_path = conn.get_volume_path(data["template"])
clone_path = conn.clone_from_template(data["name"], templ_path, metadata=meta_prealloc)
volumes[clone_path] = conn.get_volume_type(clone_path)
else:
if not data["images"]:
msg = _("First you need to create or select an image")
errors.append(msg)
else:
for vol in data["images"].split(","):
try:
path = conn.get_volume_path(vol)
volumes[path] = conn.get_volume_type(path)
except libvirtError as msg_error:
errors.append(msg_error.message)
if data["cache_mode"] not in conn.get_cache_modes():
msg = _("Invalid cache mode")
errors.append(msg)
if not errors:
uuid = util.randomUUID()
try:
conn.create_instance(
data["name"],
data["memory"],
data["vcpu"],
data["host_model"],
uuid,
volumes,
data["cache_mode"],
data["networks"],
data["virtio"],
data["mac"],
)
create_instance = Instance(compute_id=host_id, name=data["name"], uuid=uuid)
create_instance.save()
return HttpResponseRedirect(reverse("instance", args=[host_id, data["name"]]))
except libvirtError as err:
if data["hdd_size"]:
conn.delete_volume(volumes.keys()[0])
errors.append(err)
conn.close()
return render_to_response("create.html", locals(), context_instance=RequestContext(request))
示例14: ErrorException
cte = cteutils.get_obj(cname)
except ErrorException, e:
raise ErrorException(e.code, e.value)
except Exception, e:
logger.error(str(e))
raise ErrorException(500, "")
try:
cte_ips = cteutils.get_all_ip_range(cname, 4)
except ErrorException, e:
raise ErrorException(e.code, e.value)
except Exception, e:
logger.error(str(e))
raise ErrorException(500, "")
ins = Instance()
ins.uid = uid
ins.name = name
ins.desc = desc
ins.vcpu = flv['vcpu']
ins.disk = flv['disk']
ins.memory = flv['memory']
ins.compute = cte
try:
ips_used = get_all_ips()
except:
ips_used = []
示例15: instances
def instances(request, host_id):
"""
Instances block
"""
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('login'))
errors = []
instances = []
time_refresh = 8000
get_instances = []
conn = None
compute = Compute.objects.get(id=host_id)
try:
conn = wvmInstances(compute.hostname,
compute.login,
compute.password,
compute.type,
compute.hypervisor)
get_instances = conn.get_instances()
except libvirtError as err:
errors.append(err)
for instance in get_instances:
try:
inst = Instance.objects.get(compute_id=host_id, name=instance)
uuid = inst.uuid
except Instance.DoesNotExist:
uuid = conn.get_uuid(instance)
inst = Instance(compute_id=host_id, name=instance, uuid=uuid)
inst.save()
acl = Instance.objects.get(compute_id=host_id, name=instance).acl
if request.user in acl.all() or request.user.is_staff:
if compute.hypervisor == 'qemu':
instances.append({'name': instance,
'status': conn.get_instance_status(instance),
'uuid': uuid,
'memory': conn.get_instance_memory(instance),
'vcpu': conn.get_instance_vcpu(instance),
'has_managed_save_image': conn.get_instance_managed_save_image(instance)})
else:
instances.append({'name': instance,
'status': conn.get_instance_status(instance),
'uuid': uuid,
'memory': conn.get_instance_memory(instance),
'vcpu': conn.get_instance_vcpu(instance)})
if conn:
try:
if request.method == 'POST':
name = request.POST.get('name', '')
if 'start' in request.POST:
conn.start(name)
return HttpResponseRedirect(request.get_full_path())
if 'shutdown' in request.POST:
conn.shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'destroy' in request.POST:
conn.force_shutdown(name)
return HttpResponseRedirect(request.get_full_path())
if 'managedsave' in request.POST:
conn.managedsave(name)
return HttpResponseRedirect(request.get_full_path())
if 'deletesaveimage' in request.POST:
conn.managed_save_remove(name)
return HttpResponseRedirect(request.get_full_path())
if 'suspend' in request.POST:
conn.suspend(name)
return HttpResponseRedirect(request.get_full_path())
if 'resume' in request.POST:
conn.resume(name)
return HttpResponseRedirect(request.get_full_path())
conn.close()
except libvirtError as err:
errors.append(err)
return render_to_response('instances.html', locals(), context_instance=RequestContext(request))