def build_entity(self): return otypes.Event( description=self._module.params['description'], severity=otypes.LogSeverity(self._module.params['severity']), origin=self._module.params['origin'], custom_id=self._module.params['custom_id'], id=self._module.params['id'], cluster=otypes.Cluster( id=self._module.params['cluster'] ) if self._module.params['cluster'] is not None else None, data_center=otypes.DataCenter( id=self._module.params['data_center'] ) if self._module.params['data_center'] is not None else None, host=otypes.Host( id=self._module.params['host'] ) if self._module.params['host'] is not None else None, storage_domain=otypes.StorageDomain( id=self._module.params['storage_domain'] ) if self._module.params['storage_domain'] is not None else None, template=otypes.Template( id=self._module.params['template'] ) if self._module.params['template'] is not None else None, user=otypes.User( id=self._module.params['user'] ) if self._module.params['user'] is not None else None, vm=otypes.Vm( id=self._module.params['vm'] ) if self._module.params['vm'] is not None else None, )
def event(self, vm, severity, description): self.events_service.add(event=types.Event( vm=types.Vm(id=vm.id, ), origin=self.config["application_name"], severity=severity, custom_id=self.event_id, description=description, ), ) self.event_id += 1
def test_invocation_logger(api, test_id): engine = api.system_service() events = engine.events_service() events.add( types.Event( comment='delimiter for test function invocation in engine log', custom_id=random.randrange(1, 2**31), description='OST invoked: ' + test_id, origin='OST-basic-suite', severity=types.LogSeverity(types.LogSeverity.NORMAL)))
def _add_event(self, event, severity=types.LogSeverity.NORMAL): logging.info("Add event: '{}' with severity '{}'.".format( event, severity)) try: self._events_service.add(event=types.Event( origin='image-backup', severity=severity, # use current time as unique event id. custom_id=int(time.time()), # Add the event to the data VM. vm=self._data_vm_service.get(), description='image-backup: {}'.format(event))) except Exception as e: logging.warning("Error creating event: {}".format(e))
def create_snapshot(vms, snapshots_service, events_service): logger.info("Snapshot creation started ...") try: events_service.add(event=types.Event( vm=types.Vm(id=vms.id, ), origin="Backup_of_VM", severity=types.LogSeverity.NORMAL, custom_id=int(time.time()), description=( 'Backup of virtual machine \'%s\' using snapshot \'%s\' is ' 'starting.' % (vms.name, config.get_snapshot_description())), ), ) # Add the new snapshot: snapshot = snapshots_service.add( types.Snapshot( description=config.get_snapshot_description(), persist_memorystate=False, ), ) # 'Waiting for Snapshot creation to finish' snapshot_service = snapshots_service.snapshot_service(snapshot.id) while True: time.sleep(5) snapshot = snapshot_service.get() if snapshot.snapshot_status == types.SnapshotStatus.OK: break logger.info("Snapshot created") except Exception as e: logger.info("Can't create snapshot for VM: %s", vms.name) logger.info("DEBUG: %s", e) return snapshot
agent_vm_service = vms_service.vm_service(agent_vm.id) # Create an unique description for the snapshot, so that it is easier # for the administrator to identify this snapshot as a temporary one # created just for backup purposes: snap_description = '%s-backup-%s' % (data_vm.name, uuid.uuid4()) # Send an external event to indicate to the administrator that the # backup of the virtual machine is starting. Note that the description # of the event contains the name of the virtual machine and the name of # the temporary snapshot, this way, if something fails, the administrator # will know what snapshot was used and remove it manually. events_service.add(event=types.Event( vm=types.Vm(id=data_vm.id, ), origin=APPLICATION_NAME, severity=types.LogSeverity.NORMAL, custom_id=event_id, description=('Backup of virtual machine \'%s\' using snapshot \'%s\' is ' 'starting.' % (data_vm.name, snap_description)), ), ) event_id += 1 # Save the OVF to a file, so that we can use to restore the virtual # machine later. The name of the file is the name of the virtual # machine, followed by a dash and the identifier of the virtual machine, # to make it unique: ovf_data = data_vm.initialization.configuration.data ovf_file = '%s-%s.ovf' % (data_vm.name, data_vm.id) with open(ovf_file, 'w') as ovs_fd: ovs_fd.write(ovf_data.encode('utf-8')) logging.info('Wrote OVF to file \'%s\'.', os.path.abspath(ovf_file))
def restore(connection, AGENT_VM_NAME="obackup", DATA_VM_NAME, APPLICATION_NAME="obackup", DATA_DISKS, CONF, sd, vm_config): # Get the reference to the root of the services tree: system_service = connection.system_service() # Get the reference to the service that we will use to send events to # the audit log: events_service = system_service.events_service() # In order to send events we need to also send unique integer ids. These # should usually come from an external database, but in this example we # will just generate them from the current time in seconds since Jan 1st # 1970. event_id = int(time.time()) # Get the reference to the service that manages the virtual machines: vms_service = system_service.vms_service() # # Find the virtual machine that we want to back up. Note that we need to # # use the 'all_content' parameter to retrieve the retrieve the OVF, as # # it isn't retrieved by default: # data_vm = vms_service.list( # search='name=%s' % DATA_VM_NAME, # all_content=True, # )[0] # logging.info( # 'Found data virtual machine \'%s\', the id is \'%s\'.', # data_vm.name, data_vm.id, # ) # vm_dict = { "name" : data_vm.name, "cluster" : data_vm.cluster.id, "soundcard_enabled" : data_vm.soundcard_enabled, "cpu" : { "architecture" : data_vm.cpu.architecture.name, "sockets" : data_vm.cpu.topology.sockets, "cores" : data_vm.cpu.topology.cores, "threads" : data_vm.cpu.topology.threads }, "memory" : data_vm.memory, "memory_policy" : { "max" : data_vm.memory_policy.max, "ballooning" : data_vm.memory_policy.ballooning, "guaranteed" : data_vm.memory_policy.guaranteed, }, "description" : data_vm.description, "nics" : nics, "disk_attachments" : data_vm_disks, "display" : data_vm.display.type.name, "time_zone" : data_vm.time_zone.name, "os" : data_vm.os.type, "type" : data_vm.type.name, "virtio_scsi" : data_vm.virtio_scsi.enabled, "high_availability" : data_vm.high_availability.enabled, } vms_service.add( types.Vm( name = DATA_VM_NAME + '_restored', cluster = types.Cluster(name=cluster), template = types.Template(name='Blank'), soundcard_enabled = types. ), ) logging.info( 'Creating VM %s_restored', DATA_VM_NAME, ) # Find the virtual machine were we will attach the disks in order to do # the backup: agent_vm = vms_service.list( search='name=%s' % AGENT_VM_NAME, )[0] logging.info( 'Found agent virtual machine \'%s\', the id is \'%s\'.', agent_vm.name, agent_vm.id, ) # Find the services that manage the data and agent virtual machines: data_vm_service = vms_service.vm_service(data_vm.id) agent_vm_service = vms_service.vm_service(agent_vm.id) # Create an unique description for the snapshot, so that it is easier # for the administrator to identify this snapshot as a temporary one # created just for backup purposes: snap_description = '%s_obackup_%s' % (data_vm.name, uuid.uuid4()) # Send an external event to indicate to the administrator that the # backup of the virtual machine is starting. Note that the description # of the event contains the name of the virtual machine and the name of # the temporary snapshot, this way, if something fails, the administrator # will know what snapshot was used and remove it manually. events_service.add( event=types.Event( vm=types.Vm( id=data_vm.id, ), origin=APPLICATION_NAME, severity=types.LogSeverity.NORMAL, custom_id=event_id, description=( 'Backup of virtual machine \'%s\' using snapshot \'%s\' is ' 'starting.' % (data_vm.name, snap_description) ), ), ) event_id += 1 # Save the OVF to a file, so that we can use to restore the virtual # machine later. The name of the file is the name of the virtual # machine, followed by a dash and the identifier of the virtual machine, # to make it unique: ovf_data = data_vm.initialization.configuration.data ovf_file = '%s-%s.ovf' % (data_vm.name, data_vm.id) with open(ovf_file, 'w') as ovs_fd: ovs_fd.write(ovf_data.encode('utf-8')) logging.info('Wrote OVF to file \'%s\'.', os.path.abspath(ovf_file)) # Send the request to create the snapshot. Note that this will return # before the snapshot is completely created, so we will later need to # wait till the snapshot is completely created. # The snapshot will not include memory. Change to True the parameter # persist_memorystate to get it (in that case the VM will be paused for a while). snaps_service = data_vm_service.snapshots_service() snap = snaps_service.add( snapshot=types.Snapshot( description=snap_description, persist_memorystate=False, ), ) logging.info( 'Sent request to create snapshot \'%s\', the id is \'%s\'.', snap.description, snap.id, ) # Poll and wait till the status of the snapshot is 'ok', which means # that it is completely created: snap_service = snaps_service.snapshot_service(snap.id) while snap.snapshot_status != types.SnapshotStatus.OK: logging.info( 'Waiting till the snapshot is created, the satus is now \'%s\'.', snap.snapshot_status, ) time.sleep(1) snap = snap_service.get() logging.info('The snapshot is now complete.') # Retrieve the descriptions of the disks of the snapshot: snap_disks_service = snap_service.disks_service() snap_disks = snap_disks_service.list() # Attach all the disks of the snapshot to the agent virtual machine, and # save the resulting disk attachments in a list so that we can later # detach them easily: attachments_service = agent_vm_service.disk_attachments_service() attachments = [] attachment_map = {} alphabet = map(chr, range(97, 123)) blkid = 0 for snap_disk in snap_disks: if snap_disk.id in DATA_DISKS: blkid += 1 attachment = attachments_service.add( attachment=types.DiskAttachment( disk=types.Disk( id=snap_disk.id, snapshot=types.Snapshot( id=snap.id, ), ), active=True, bootable=False, interface=types.DiskInterface.VIRTIO, ), ) attachments.append(attachment) attachment_map["vd" + alphabet[blkid]] = attachment logging.info( 'Attached disk \'%s\' to the agent virtual machine.', attachment.disk.id, ) # Insert here the code to contact the backup agent and do the actual # backup ... logging.info('Doing the actual backup ...') for vdisk in attachment_map.keys(): logging.info( 'Backing up disk \'%s\'.', attachment_map[vdisk].id ) backup_status = backy2.backup_disk(vdisk, attachment_map[vdisk].id, snap_description, data_vm.name) if backup_status == "Failed": logging.error( 'Failed to back up VM %s Disk %s, check backy2 logs', data_vm.name, attachment_map[vdisk].id ) events_service.add( event=types.Fault( vm=types.Vm( id=data_vm.id, ), origin=APPLICATION_NAME, severity=types.LogSeverity.ERROR, custom_id=event_id, description=( 'Backup of /VM \'%s\' disk %s failed', data_vm.name, attachment_map[vdisk].id ), ), ) event_id += 1 # Detach the disks from the agent virtual machine: for attachment in attachments: attachment_service = attachments_service.attachment_service(attachment.id) attachment_service.remove() logging.info( 'Detached disk \'%s\' from the agent virtual machine.', attachment.disk.id, ) # Remove the snapshot: snap_service.remove() logging.info('Removed the snapshot \'%s\'.', snap.description) # Send an external event to indicate to the administrator that the # backup of the virtual machine is completed: events_service.add( event=types.Event( vm=types.Vm( id=data_vm.id, ), origin=APPLICATION_NAME, severity=types.LogSeverity.NORMAL, custom_id=event_id, description=( 'Backup of virtual machine \'%s\' using snapshot \'%s\' is ' 'completed.' % (data_vm.name, snap_description) ), ), ) event_id += 1
# open a connection to the server connection = sdk.Connection(url=url, username=username, password=password, insecure=True, debug=True) # services that manage data and virtual machines system_service = connection.system_service() events_service = system_service.events_service() # use time as unique event id event_id = int(time.time()) # send an event to the system events_service.add(event=types.Event(origin='test-application', severity=types.LogSeverity.NORMAL, custom_id=event_id, description="Authentication test")) vms_service = system_service.vms_service() vms = vms_service.list(search='name=login*', all_content=True) for vm in vms: print(vm.name, vm.id) # close the connection to the server connection.close()