本文整理汇总了Python中cm.models.vm.VM类的典型用法代码示例。如果您正苦于以下问题:Python VM类的具体用法?Python VM怎么用?Python VM使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了VM类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_and_shutdown
def save_and_shutdown(farm, name, description):
"""
"""
from cm.models.vm import VM
if farm.state == farm_states["failed"]:
raise CMException("farm_wrong_state")
head_vm = farm.head
try:
VM.save_and_shutdown(head_vm.user_id, head_vm, name, description)
except Exception:
CMException("farm_save")
node_vms = []
if farm.state == farm_states["init_head"]:
for vm in farm.vms.all():
if vm.is_head():
continue
vm.release_resources()
vm.state = vm_states["closed"]
else:
for vm in farm.vms.all():
if not vm.is_head():
node_vms.append(vm)
VM.destroy(node_vms)
try:
farm.state = farm_states["closed"]
farm.save()
except:
CMException("farm_save")
示例2: destroy
def destroy(caller_id, vm_ids):
"""
This function only destroys VM. All the cleanup (removing disk, saving,
rescuing resources, ...) is done by hook through
\c contextualization.update_vm method (yeah, intuitive).
Simple sequence diagram:
@code
CLM CM CTX Node (HOOK)
.
Destroy -->destroy
| | (LV.destroy)
| |------------------------->HookScript
. . |
. . ctx.update_vm<--|
. . | |
. . |------------->cp
. . |------------->rm
. . update_resources
@endcode
@cmview_user
@param_post{vm_ids,list} list of virtual machines' ids
@response{list(dict)} VM.destroy() retval
"""
vms = []
for vm_id in vm_ids:
vms.append(VM.get(caller_id, vm_id))
return VM.destroy(vms)
示例3: destroy
def destroy(farms):
"""
Destroyes farms' VMs (Head and Worker Nodes of each farm) without saving them.
@parameter{farms,list} list of farms to destroy
@response{list(dict)} list of statuses returned by destroyed VMs
@raises{farm_wrong_state,CMException}
@raises{farm_destroy,CMException}
"""
from cm.models.vm import VM
vm_resp = []
for farm in farms:
# those are states in which farm can not be destroyed
if farm.state in (farm_states["init"], farm_states["closing"], farm_states["closed"]):
raise CMException("farm_wrong_state")
for farm in farms:
# stop all threads
if farm.state == farm_states["init_head"]:
for vm in farm.vms.all():
if vm.is_head():
continue
vm.release_resources()
vm.state = vm_states["closed"]
vm.stop_time = datetime.now()
vm.save()
log.debug(vm.user.id, "vm state %s" % vm.state)
r = VM.destroy([farm.head])
else:
for vm in farm.vms.all():
if vm.state == vm_states["init"]:
raise CMException("farm_wrong_state")
log.debug(farm.user_id, "killing wn: %s" % farm.vms)
r = VM.destroy(farm.vms.all())
if True in [x["status"] != "ok" for x in r]:
farm.state = farm_states["failed"]
try:
farm.save()
except Exception:
raise CMException("farm_destroy")
vm_resp.append(r)
farm.state = farm_states["closed"]
try:
farm.save()
except Exception:
raise CMException("farm_destroy")
log.debug(farm.user_id, "session commited")
for vm in farm.vms.all():
log.debug(vm.user.id, "vm state %s" % vm.state)
return vm_resp
示例4: erase
def erase(caller_id, vm_id_list):
"""
Cleans up after each of the specified VMs. Erase should be called for
failed machines after the inspection of the failure.
@cmview_admin_cm
@param_post{vm_id_list,list} list of VM id's
"""
for vm_id in vm_id_list:
vm = VM.admin_get(vm_id)
VM.erase(vm)
示例5: erase
def erase(caller_id, vm_id_list):
"""
Method cleans up after each of VM, which id is in \c vm_ids. Should be
called for failed machines.
@cmview_admin_cm
@parameter{vm_id_list,list} list of vm id's
@noresponse
"""
for vm_id in vm_id_list:
vm = VM.admin_get(vm_id)
VM.erase(vm)
示例6: destroy
def destroy(caller_id, vm_id_list):
"""
Method destroyes VMs with ids listed in \c vm_ids.
@cmview_admin_cm
@parameter{vm_ids,list} list of vm id's
@response{src.cm.views.utils.image.destroy()}
"""
vms = []
for vm_id in vm_id_list:
vms.append(VM.admin_get(vm_id))
return VM.destroy(vms)
示例7: vm_stats
def vm_stats(caller_id, vm_id, stat_name, time, stat_range, resolution):
"""
Function returns requested statistics for specific VM. Stats are returned
in format required by CC1 Web Interface's monitoring javascript.
@cmview_admin_cm
@param_post{vm_id}
@param_post{stat_name,string|list(string)} type of required statistics
@param_post{time,int} time of last row
@param_post{stat_range,string} time interval between first and last stats
@param_post{resolution,string} statistics resolution
@response{list} usage of VM resources (format required by CC1 Web Interface)
"""
vm_u = VM.admin_get(vm_id).long_dict['user_id']
if type(stat_name) is int:
stat_name = [stat_name]
n = []
for stat in stat_name:
n.append(stat_names.keys()[stat_names.values().index(stat)])
p = int(stat_ranges.keys()[stat_ranges.values().index(int(stat_range))])
r = int(stat_resolutions.keys()[stat_resolutions.values().index(resolution)])
return RrdHandler().get_vm_stats('vm-%d-%d' % (vm_id, vm_u), n, int(time) - p, time, r)
示例8: user_data
def user_data(request):
vm_ip = request.META.get('REMOTE_ADDR')
vm = VM.get_by_ip( vm_ip )
user_data = vm.long_dict['user_data']
return base64.b64decode( user_data )
示例9: vm_stats
def vm_stats(caller_id, vm_id, stat_name, time, stat_range, resolution):
"""
Function returns statistics for specific \c vmid.
@decoratedby{src.cm.utils.decorators.user_log}
@parameter{vmid}
@parameter{stat_name,string} type of required statistics
@parameter{time,string} time of last row
@parameter{range,string} period of time from time to past
@parameter{resolution,string} statistics resolution
@response{dict} list of the total usage of VM resources from start VM
"""
vm_u = VM.admin_get(vm_id).long_dict['user_id']
if type(stat_name) is int:
stat_name = [stat_name]
n = []
for stat in stat_name:
n.append(stat_names.keys()[stat_names.values().index(stat)])
p = int(stat_ranges.keys()[stat_ranges.values().index(int(stat_range))])
r = int(stat_resolutions.keys()[stat_resolutions.values().index(resolution)])
return RrdHandler().get_vm_stats('vm-%d-%d' % (vm_id, vm_u), n, int(time) - p, time, r)
示例10: attach
def attach(caller_id, iso_image_id, vm_id):
# vm_id, img_id, destination='usb', check=True/False
"""
Attaches specified IsoImage to specified VM. It makes possible booting
any operating system on created VM.
@cmview_user
@param_post{iso_image_id,int} id of block device (should be IsoImage type)
@param_post{vm_id,int} id of the VM which IsoImage should be attached to
@response{None}
"""
vm = VM.get(caller_id, vm_id)
disk = IsoImage.get(caller_id, iso_image_id)
# Check if disk is already attached to a vm
if disk.vm:
raise CMException('image_attached')
disk.attach(vm)
try:
disk.save()
except:
raise CMException('iso_image_attach')
示例11: get_command
def get_command(remote_ip, **kw):
"""
@param_post{remote_ip,string}
@param_post{kw,dict} keyword params
@returns{Command} next command from the que to the asking VM
"""
vm = VM.get_by_ip(remote_ip)
log.debug(0, "Get first command for %s" % vm.id)
command = vm.command_set.filter(state=command_states['pending']).order_by('id')
if len(command) == 0:
return response('ctx_no_command')
command = command[0]
log.debug(0, "First command is %s" % command.id)
command.state = command_states['executing']
command.save()
d = command.dict()
r = response('ok', d)
if int(kw.get('version', 0)) < VERSION:
f = file(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'actions.py'), 'r')
r['actions_file'] = f.read()
f.close()
return r
示例12: reservation_id
def reservation_id(request):
vm_ip = request.META.get('REMOTE_ADDR')
vm = VM.get_by_ip( vm_ip )
reservation_id = vm.long_dict['reservation_id']
return 'r-' + str(reservation_id)
示例13: hello
def hello(vm_ip, **args):
"""
First function which must be called by VMs ctx module. It registers VM with status 'running ctx',
also serves a special role when creating farms (tracking head, and worker nodes)
@parameter{vm_ip,string}
@parameter{args}
"""
vm = VM.get_by_ip(vm_ip)
log.debug(vm.user_id, "Hello from vm %d ip: %s" % (vm.id, vm_ip))
vm.ctx_api_version = args.get('version', None)
vm.state = vm_states['running ctx']
if vm.ssh_username and vm.ssh_key:
Command.execute('add_ssh_key', vm.user_id, vm.id, user=vm.ssh_username, ssh_key=vm.ssh_key)
if vm.is_head():
Command.register_head(vm)
elif vm.is_farm():
Command.register_node(vm)
try:
vm.save(update_fields=['state', 'ctx_api_version'])
except Exception, e:
log.error(vm.user_id, "Cannot update database for vm %d: %s" % (vm.id, e.message))
return response('ctx_error', "Cannot update database: %s" % e.message)
示例14: create
def create(caller_id, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, count=1, user_data=None,
ssh_key=None, ssh_username=None):
"""
Creates virtual machines.
@cmview_user
@parameter{name,string}
@parameter{description,string}
@parameter{image_id,int}
@parameter{template_id,int}
@parameter{ip_id,int}
@parameter{iso_list,list(int)} ISOs' ids
@parameter{vnc}
@parameter{groups}
@parameter{user_data} data accessible via ec2ctx
@parameter{ssh_key}
@parameter{ssh_username}
@returns @asreturned{src.cm.views.utils.vm.create()}
"""
user = User.get(caller_id)
try:
user.check_points()
except:
message.warn(caller_id, 'point_limit', {'used_points': user.used_points, 'point_limit': user.points})
vms = VM.create(user, name=name, description=description, image_id=image_id,
template_id=template_id, public_ip_id=public_ip_id, iso_list=iso_list, disk_list=disk_list,
vnc=vnc, groups=groups, count=count, user_data=user_data, ssh_key=ssh_key, ssh_username=ssh_username)
for vm in vms:
thread = VMThread(vm, 'create')
thread.start()
return [vm.dict for vm in vms]
示例15: reset
def reset(caller_id, vm_ids):
"""
Safely restarts selected callers VMs
@cmview_user
@param_post{vm_ids,list(int)} ids of the VMs to restart
@response{src.cm.views.utils.image.restart()}
"""
# get to check permissions on vms
vms = []
for vm_id in vm_ids:
vms.append(VM.get(caller_id, vm_id))
return VM.reset(vms)