Search in sources :

Example 6 with Disk

use of org.ovirt.engine.sdk4.types.Disk in project ovirt-engine-sdk-java by oVirt.

the class AddLunDiskToVm method main.

public static void main(String[] args) throws Exception {
    // Create the connection to the server:
    Connection connection = connection().url("https://engine/ovirt-engine/api").user("admin@internal").password("123456").trustStoreFile("truststore.jks").build();
    // Locate the virtual machines service and use it to find the virtual machine:
    VmsService vmsService = connection.systemService().vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Locate the service that manages the disk attachments of the virtual machine:
    DiskAttachmentsService diskAttachmentsService = vmsService.vmService(vm.id()).diskAttachmentsService();
    // Use the "add" method of the disk attachments service to add the LUN disk.
    diskAttachmentsService.add().attachment(diskAttachment().disk(disk().name("myiscsidisk").lunStorage(hostStorage().type(StorageType.ISCSI).logicalUnits(logicalUnit().address("192.168.200.3").port(3260).target("iqn.2014-07.org.ovirt:storage").id("36001405e793bf9c57a840f58c9a8a220").username("username").password("password")))).interface_(DiskInterface.VIRTIO).bootable(false).active(true)).send().attachment();
    // Close the connection to the server:
    connection.close();
}
Also used : Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService) DiskAttachmentsService(org.ovirt.engine.sdk4.services.DiskAttachmentsService)

Example 7 with Disk

use of org.ovirt.engine.sdk4.types.Disk in project ovirt-engine-sdk-java by oVirt.

the class VmBackup method run.

/**
 * This is the method that performs the actual work.
 *
 * @param systemService the reference to the root of services
 * @throws Exception if something fails while doing the work
 */
private void run(SystemService systemService) throws Exception {
    // Get the reference to the service that we will use to send events to the audit log:
    EventsService eventsService = systemService.eventsService();
    // 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.
    int eventId = (int) (System.currentTimeMillis() / 1000);
    // Get the reference to the service that manages the virtual machines:
    VmsService vmsService = systemService.vmsService();
    // 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:
    Vm dataVm = vmsService.list().search(String.format("name=%s", DATA_VM_NAME)).allContent(true).send().vms().get(0);
    log.info("Found data virtual machine '{}', the id is '{}'.", dataVm.name(), dataVm.id());
    // Find the virtual machine were we will attach the disks in order to do the backup:
    Vm agentVm = vmsService.list().search(String.format("name=%s", AGENT_VM_NAME)).send().vms().get(0);
    log.info("Found agent virtual machine '{}', the id is '{}'.", agentVm.name(), agentVm.id());
    // Find the services that manage the data and agent virtual machines:
    VmService dataVmService = vmsService.vmService(dataVm.id());
    VmService agentVmService = vmsService.vmService(agentVm.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:
    String snapDescription = String.format("%s-backup-%s", dataVm.name(), UUID.randomUUID());
    // 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.
    eventsService.add().event(event().vm(vm().id(dataVm.id())).origin(APPLICATION_NAME).severity(LogSeverity.NORMAL).customId(eventId++).description(String.format("Backup of virtual machine '%s' using snapshot '%s' is starting.", dataVm.name(), snapDescription))).send();
    // 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:
    String ovfData = dataVm.initialization().configuration().data();
    File ovfFile = new File(String.format("%s-%s.ovf", dataVm.name(), dataVm.id()));
    try (OutputStream ovfStream = new FileOutputStream(ovfFile)) {
        ovfStream.write(ovfData.getBytes(StandardCharsets.UTF_8));
    }
    log.info("Wrote OVF to file '{}'", ovfFile.getAbsolutePath());
    // 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.
    SnapshotsService snapsService = dataVmService.snapshotsService();
    Snapshot snap = snapsService.add().snapshot(snapshot().description(snapDescription)).send().snapshot();
    log.info("Sent request to create snapshot '{}', the id is '{}'.", snap.description(), snap.id());
    // Poll and wait till the status of the snapshot is 'ok', which means that it is completely created:
    SnapshotService snapService = snapsService.snapshotService(snap.id());
    while (snap.snapshotStatus() != SnapshotStatus.OK) {
        log.info("Waiting till the snapshot is created, the status is now '{}'.", snap.snapshotStatus());
        Thread.sleep(1 * 1000);
        snap = snapService.get().send().snapshot();
    }
    log.info("The snapshot is now complete.");
    // Retrieve the descriptions of the disks of the snapshot:
    SnapshotDisksService snapDisksService = snapService.disksService();
    List<Disk> snapDisks = snapDisksService.list().send().disks();
    // 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:
    DiskAttachmentsService attachmentsService = agentVmService.diskAttachmentsService();
    List<DiskAttachment> attachments = new ArrayList<>();
    for (Disk snapDisk : snapDisks) {
        DiskAttachment attachment = attachmentsService.add().attachment(diskAttachment().disk(disk().id(snapDisk.id()).snapshot(snapshot().id(snap.id()))).active(true).bootable(false).interface_(DiskInterface.VIRTIO)).send().attachment();
        attachments.add(attachment);
        log.info("Attached disk '{}' to the agent virtual machine.", attachment.disk().id());
    }
    // been attached.
    for (DiskAttachment attachment : attachments) {
        if (attachment.logicalNamePresent()) {
            log.info("Logical name for disk '{}' is '{}'.", attachment.disk().id(), attachment.logicalName());
        } else {
            log.info("The logical name for disk '{}' isn't available. Is the guest agent installed?", attachment.disk().id());
        }
    }
    // Insert here the code to contact the backup agent and do the actual backup ...
    log.info("Doing the actual backup ...");
    // Detach the disks from the agent virtual machine:
    for (DiskAttachment attachment : attachments) {
        DiskAttachmentService attachmentService = attachmentsService.attachmentService(attachment.id());
        attachmentService.remove().send();
        log.info("Detached disk '{}' to from the agent virtual machine.", attachment.disk().id());
    }
    // Remove the snapshot:
    snapService.remove().send();
    log.info("Removed the snapshot '{}'.", snap.description());
    // Send an external event to indicate to the administrator that the backup of the virtual machine is completed:
    eventsService.add().event(event().vm(vm().id(dataVm.id())).origin(APPLICATION_NAME).severity(LogSeverity.NORMAL).customId(eventId++).description(String.format("Backup of virtual machine '%s' using snapshot '%s' is completed.", dataVm.name(), snapDescription))).send();
    // Bye:
    log.info("Bye!");
}
Also used : SnapshotService(org.ovirt.engine.sdk4.services.SnapshotService) SnapshotsService(org.ovirt.engine.sdk4.services.SnapshotsService) DiskAttachmentService(org.ovirt.engine.sdk4.services.DiskAttachmentService) VmService(org.ovirt.engine.sdk4.services.VmService) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) VmsService(org.ovirt.engine.sdk4.services.VmsService) Snapshot(org.ovirt.engine.sdk4.types.Snapshot) SnapshotDisksService(org.ovirt.engine.sdk4.services.SnapshotDisksService) DiskAttachment(org.ovirt.engine.sdk4.types.DiskAttachment) Vm(org.ovirt.engine.sdk4.types.Vm) FileOutputStream(java.io.FileOutputStream) EventsService(org.ovirt.engine.sdk4.services.EventsService) File(java.io.File) Disk(org.ovirt.engine.sdk4.types.Disk) DiskAttachmentsService(org.ovirt.engine.sdk4.services.DiskAttachmentsService)

Example 8 with Disk

use of org.ovirt.engine.sdk4.types.Disk in project ovirt-engine-sdk-java by oVirt.

the class AddFloatingDisk method main.

public static void main(String[] args) throws Exception {
    // Create the connection to the server:
    Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
    // Get the reference to the disks service:
    DisksService disksService = connection.systemService().disksService();
    // Add the disk. Note that the size of the disk, the `provisionedSize` attribute, is specified in bytes,
    // so to create a disk of 10 GiB the value should be 10 * 2^30.
    Disk disk = disksService.add().disk(disk().name("mydisk").description("My disk").format(DiskFormat.COW).provisionedSize(BigInteger.valueOf(10).multiply(BigInteger.valueOf(2).pow(30))).storageDomains(storageDomain().name("mydata"))).send().disk();
    // Wait till the disk is completely created:
    DiskService diskService = disksService.diskService(disk.id());
    for (; ; ) {
        Thread.sleep(5 * 1000);
        disk = diskService.get().send().disk();
        if (disk.status() == DiskStatus.OK) {
            break;
        }
    }
    // Close the connection to the server:
    connection.close();
}
Also used : DisksService(org.ovirt.engine.sdk4.services.DisksService) Connection(org.ovirt.engine.sdk4.Connection) Disk(org.ovirt.engine.sdk4.types.Disk) DiskService(org.ovirt.engine.sdk4.services.DiskService)

Example 9 with Disk

use of org.ovirt.engine.sdk4.types.Disk in project ovirt-engine-sdk-java by oVirt.

the class ListVmSnapshots method main.

public static void main(String[] args) throws Exception {
    // Create the connection to the server:
    Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
    // Get the reference to the root service:
    SystemService systemService = connection.systemService();
    // Find all the virtual machines and store the id and name in a
    // map, so that looking them up later will be faster:
    VmsService vmsService = systemService.vmsService();
    Map<String, String> vmsMap = new HashMap<>();
    for (Vm vm : vmsService.list().send().vms()) {
        vmsMap.put(vm.id(), vm.name());
    }
    // Same for storage domains:
    StorageDomainsService storageDomainsService = systemService.storageDomainsService();
    Map<String, String> storageDomainsMap = new HashMap<>();
    for (StorageDomain sd : storageDomainsService.list().send().storageDomains()) {
        storageDomainsMap.put(sd.id(), sd.name());
    }
    // find its disks:
    for (Map.Entry<String, String> vm : vmsMap.entrySet()) {
        VmService vmService = vmsService.vmService(vm.getKey());
        SnapshotsService snapshotsService = vmService.snapshotsService();
        Map<String, String> snapshotsMap = new HashMap<>();
        for (Snapshot snapshot : snapshotsService.list().send().snapshots()) {
            snapshotsMap.put(snapshot.id(), snapshot.description());
        }
        for (Map.Entry<String, String> snapshot : snapshotsMap.entrySet()) {
            SnapshotService snapshotService = snapshotsService.snapshotService(snapshot.getKey());
            SnapshotDisksService snapshotDisksService = snapshotService.disksService();
            for (Disk disk : snapshotDisksService.list().send().disks()) {
                if (disk.storageDomains().size() > 0) {
                    String storageDomainId = disk.storageDomains().get(0).id();
                    String storageDomainName = storageDomainsMap.get(storageDomainId);
                    System.out.printf("%s:%s:%s:%s\n", vm.getValue(), snapshot.getValue(), disk.alias(), storageDomainName);
                }
            }
        }
    }
    // Close the connection to the server:
    connection.close();
}
Also used : SnapshotService(org.ovirt.engine.sdk4.services.SnapshotService) SnapshotsService(org.ovirt.engine.sdk4.services.SnapshotsService) HashMap(java.util.HashMap) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService) StorageDomainsService(org.ovirt.engine.sdk4.services.StorageDomainsService) Snapshot(org.ovirt.engine.sdk4.types.Snapshot) SnapshotDisksService(org.ovirt.engine.sdk4.services.SnapshotDisksService) StorageDomain(org.ovirt.engine.sdk4.types.StorageDomain) SystemService(org.ovirt.engine.sdk4.services.SystemService) Vm(org.ovirt.engine.sdk4.types.Vm) HashMap(java.util.HashMap) Map(java.util.Map) Disk(org.ovirt.engine.sdk4.types.Disk)

Example 10 with Disk

use of org.ovirt.engine.sdk4.types.Disk in project ovirt-engine-sdk-java by oVirt.

the class AddTemplate method main.

public static void main(String[] args) throws Exception {
    // Create the connection to the server:
    Connection connection = connection().url("https://engine40.example.com/ovirt-engine/api").user("admin@internal").password("redhat123").trustStoreFile("truststore.jks").build();
    // Get the reference to the root of the services tree:
    SystemService systemService = connection.systemService();
    // Find the original virtual machine:
    VmsService vmsService = systemService.vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Get the identifiers of the disks attached to the virtual machine. We # need this because we want to tell the
    // server to create the disks of the template using a format different to the format used by the original disks.
    List<String> diskIds = new ArrayList<>();
    for (DiskAttachment attachment : connection.followLink(vm.diskAttachments())) {
        diskIds.add(attachment.disk().id());
    }
    // Create a customized list of disk attachments, explicitly indicating that we want COW disks, regardless of
    // format the original disks had:
    List<DiskAttachment> attachments = new ArrayList<>();
    for (String diskId : diskIds) {
        attachments.add(diskAttachment().disk(disk().id(diskId).format(DiskFormat.COW)).build());
    }
    // Send the request to create the template. Note that the way to specify the original virtual machine, and the
    // customizations, is to use the 'vm' attribute of the 'Template' type.
    TemplatesService templatesService = systemService.templatesService();
    Template template = templatesService.add().template(template().name("mytemplate").vm(vm().id(vm.id()).diskAttachments(attachments))).send().template();
    // Wait till the status of the template is OK, as that means that it is completely created and ready to use:
    TemplateService templateService = templatesService.templateService(template.id());
    for (; ; ) {
        Thread.sleep(5 * 1000);
        template = templateService.get().send().template();
        if (template.status() == TemplateStatus.OK) {
            break;
        }
    }
    // Close the connection to the server:
    connection.close();
}
Also used : SystemService(org.ovirt.engine.sdk4.services.SystemService) DiskAttachment(org.ovirt.engine.sdk4.types.DiskAttachment) Vm(org.ovirt.engine.sdk4.types.Vm) Connection(org.ovirt.engine.sdk4.Connection) ArrayList(java.util.ArrayList) VmsService(org.ovirt.engine.sdk4.services.VmsService) TemplateService(org.ovirt.engine.sdk4.services.TemplateService) TemplatesService(org.ovirt.engine.sdk4.services.TemplatesService) Template(org.ovirt.engine.sdk4.types.Template)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)9 VmsService (org.ovirt.engine.sdk4.services.VmsService)9 Vm (org.ovirt.engine.sdk4.types.Vm)8 Disk (org.ovirt.engine.sdk4.types.Disk)6 DiskAttachment (org.ovirt.engine.sdk4.types.DiskAttachment)5 DiskAttachmentsService (org.ovirt.engine.sdk4.services.DiskAttachmentsService)4 VmService (org.ovirt.engine.sdk4.services.VmService)4 SystemService (org.ovirt.engine.sdk4.services.SystemService)3 ArrayList (java.util.ArrayList)2 DiskService (org.ovirt.engine.sdk4.services.DiskService)2 DisksService (org.ovirt.engine.sdk4.services.DisksService)2 SnapshotDisksService (org.ovirt.engine.sdk4.services.SnapshotDisksService)2 SnapshotService (org.ovirt.engine.sdk4.services.SnapshotService)2 SnapshotsService (org.ovirt.engine.sdk4.services.SnapshotsService)2 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)2 TemplateService (org.ovirt.engine.sdk4.services.TemplateService)2 TemplatesService (org.ovirt.engine.sdk4.services.TemplatesService)2 Snapshot (org.ovirt.engine.sdk4.types.Snapshot)2 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)2 Template (org.ovirt.engine.sdk4.types.Template)2