def revert_and_resume(vm_id, vm_identity): try: sys_snapshot = current.db.snapshot(vm_id=vm_id, type=SNAPSHOT_SYSTEM) if sys_snapshot: revert({'vm_id': vm_id, 'snapshot_id': sys_snapshot.id}) logger.debug('Snapshot of %s reverted from %s successfully' % (vm_identity, sys_snapshot.snapshot_name)) delete_snapshot({'vm_id': vm_id, 'snapshot_id': sys_snapshot.id}) logger.debug('Snapshot %s deleted successfully' % (sys_snapshot.snapshot_name)) event_data = current.db.vm_event_log(vm_id=vm_id, new_value='System Shutdown') # event_data = current.db((current.db.vm_event_log.vm_id == vm_id) & (current.db.vm_event_log.new_value == 'System Shutdown')).select(current.db.vm_event_log.ALL) print event_data if event_data: start({'vm_id': vm_id}) if int(event_data.old_value) == current.VM_STATUS_SUSPENDED: suspend({'vm_id': vm_id}) del current.db.vm_event_log[event_data.id] current.db.commit() except: log_exception() pass
def respawn_dangling_vms(host_id): vms = current.db(current.db.vm_data.host_id == host_id).select(current.db.vm_data.ALL) vm_image_location = get_constant('vmfiles_path') + get_constant('vms') + '/%s/%s.qcow2' for vm_data in vms: logger.debug('Re-spawning VM ' + vm_data.vm_identity) #Create a copy of existing image and rename it with '_old' suffix storage_type = config.get("GENERAL_CONF","storage_type") copy_command = 'ndmpcopy ' if storage_type == current.STORAGE_NETAPP_NFS else 'cp ' ds_image_location = vm_data.datastore_id.path + get_constant('vms') + '/%s/%s.qcow2' command_to_execute = copy_command + ds_image_location%(vm_data.vm_identity, vm_data.vm_identity) + \ ' ' + ds_image_location%(vm_data.vm_identity, vm_data.vm_identity+'_old') execute_remote_cmd(vm_data.datastore_id.ds_ip, vm_data.datastore_id.username, command_to_execute, vm_data.datastore_id.password) logger.debug('Backup copy of the VM image cretaed successfully.') vm_properties = {} vm_properties['host'] = find_new_host(vm_data.RAM, vm_data.vCPU) vm_properties['ram'] = vm_data.RAM vm_properties['vcpus'] = vm_data.vCPU vm_properties['mac_addr'] = vm_data.private_ip.mac_addr vm_properties['vnc_port'] = vm_data.vnc_port vm_properties['template'] = current.db.template[vm_data.template_id] vm_properties['vlan_name'] = current.db(current.db.private_ip_pool.private_ip == vm_data.private_ip).select()[0].vlan.name # Re-spawn the VM on new host launch_vm_on_host(vm_data, vm_image_location%(vm_data.vm_identity, vm_data.vm_identity), vm_properties) vm_data.update_record(host_id = vm_properties['host']) #Find the most recent snapshot of the given VM; revert to the snapshot recent_snapshot = current.db(current.db.snapshot.vm_id == vm_data.id).select(orderby = ~current.db.snapshot.timestamp)[0] logger.debug('Reverting VM %s to snapshot %s' %(vm_data.vm_identity, recent_snapshot.snapshot_name)) revert(dict(vm_id = vm_data.id, snapshot_id = recent_snapshot.id))
def revert_and_resume(vm_id, vm_identity): try: sys_snapshot = current.db.snapshot(vm_id=vm_id, type=SNAPSHOT_SYSTEM) if sys_snapshot: revert({'vm_id' : vm_id, 'snapshot_id' : sys_snapshot.id}) logger.debug('Snapshot of %s reverted from %s successfully' %(vm_identity, sys_snapshot.snapshot_name)) delete_snapshot({'vm_id':vm_id, 'snapshot_id':sys_snapshot.id}) logger.debug('Snapshot %s deleted successfully' %(sys_snapshot.snapshot_name)) event_data = current.db.vm_event_log(vm_id = vm_id, new_value = 'System Shutdown') # event_data = current.db((current.db.vm_event_log.vm_id == vm_id) & (current.db.vm_event_log.new_value == 'System Shutdown')).select(current.db.vm_event_log.ALL) print event_data if event_data: start({'vm_id':vm_id}) if int(event_data.old_value) == current.VM_STATUS_SUSPENDED: suspend({'vm_id':vm_id}) del current.db.vm_event_log[event_data.id] current.db.commit() except: log_exception() pass