Search in sources :

Example 56 with Service

use of org.ovirt.engine.sdk4.Service in project webtools.servertools by eclipse.

the class Tomcat70Configuration method getServerPorts.

/**
 * Returns a list of ServerPorts that this configuration uses.
 *
 * @return java.util.List
 */
public List getServerPorts() {
    List<ServerPort> ports = new ArrayList<ServerPort>();
    // first add server port
    try {
        int port = Integer.parseInt(server.getPort());
        ports.add(new ServerPort("server", Messages.portServer, port, "TCPIP"));
    } catch (Exception e) {
    // ignore
    }
    // add connectors
    try {
        String instanceServiceName = serverInstance.getService().getName();
        int size = server.getServiceCount();
        for (int i = 0; i < size; i++) {
            Service service = server.getService(i);
            int size2 = service.getConnectorCount();
            for (int j = 0; j < size2; j++) {
                Connector connector = service.getConnector(j);
                String name = "HTTP/1.1";
                String protocol2 = "HTTP";
                boolean advanced = true;
                String[] contentTypes = null;
                int port = -1;
                try {
                    port = Integer.parseInt(connector.getPort());
                } catch (Exception e) {
                // ignore
                }
                String protocol = connector.getProtocol();
                if (protocol != null && protocol.length() > 0) {
                    if (protocol.startsWith("HTTP")) {
                        name = protocol;
                    } else if (protocol.startsWith("AJP")) {
                        name = protocol;
                        protocol2 = "AJP";
                    } else {
                        // Get Tomcat equivalent name if protocol handler class specified
                        name = protocolHandlerMap.get(protocol);
                        if (name != null) {
                            // Prepare simple protocol string for ServerPort protocol
                            int index = name.indexOf('/');
                            if (index > 0)
                                protocol2 = name.substring(0, index);
                            else
                                protocol2 = name;
                        } else // Specified protocol is unknown, just use as is
                        {
                            name = protocol;
                            protocol2 = protocol;
                        }
                    }
                }
                if (protocol2.toLowerCase().equals("http"))
                    contentTypes = new String[] { "web", "webservices" };
                String secure = connector.getSecure();
                if (secure != null && secure.length() > 0) {
                    name = "SSL";
                    protocol2 = "SSL";
                } else
                    advanced = false;
                String portId;
                if (instanceServiceName != null && instanceServiceName.equals(service.getName()))
                    portId = Integer.toString(j);
                else
                    portId = i + "/" + j;
                ports.add(new ServerPort(portId, name, port, protocol2, contentTypes, advanced));
            }
        }
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Error getting server ports", e);
    }
    return ports;
}
Also used : Connector(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector) ArrayList(java.util.ArrayList) Service(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service) ServerPort(org.eclipse.wst.server.core.ServerPort) CoreException(org.eclipse.core.runtime.CoreException)

Example 57 with Service

use of org.ovirt.engine.sdk4.Service in project webtools.servertools by eclipse.

the class Tomcat70Configuration method modifyServerPort.

/**
 * Modify the port with the given id.
 *
 * @param id java.lang.String
 * @param port int
 */
public void modifyServerPort(String id, int port) {
    try {
        if ("server".equals(id)) {
            server.setPort(port + "");
            isServerDirty = true;
            firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
            return;
        }
        int i = id.indexOf("/");
        // If a connector in the instance Service
        if (i < 0) {
            int connNum = Integer.parseInt(id);
            Connector connector = serverInstance.getConnector(connNum);
            if (connector != null) {
                connector.setPort(port + "");
                isServerDirty = true;
                firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
            }
        } else // Else a connector in another Service
        {
            int servNum = Integer.parseInt(id.substring(0, i));
            int connNum = Integer.parseInt(id.substring(i + 1));
            Service service = server.getService(servNum);
            Connector connector = service.getConnector(connNum);
            connector.setPort(port + "");
            isServerDirty = true;
            firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
        }
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Error modifying server port " + id, e);
    }
}
Also used : Connector(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector) Service(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service) CoreException(org.eclipse.core.runtime.CoreException)

Example 58 with Service

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

the class StartVmWithCloudInit 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();
    // Find the virtual machine:
    VmsService vmsService = connection.systemService().vmsService();
    Vm vm = vmsService.list().search("name=myvm").send().vms().get(0);
    // Find the service that manages the virtual machine:
    VmService vmService = vmsService.vmService(vm.id());
    // Create cloud-init script, which you want to execute in deployed virtual machine, please note that
    // the script must be properly formatted and indented as it's using YAML.
    String myScript = "write_files:\n" + "  - content: |\n" + "      Hello, world!\n" + "    path: /tmp/greeting.txt\n" + "    permissions: '0644'\n";
    // Start the virtual machine enabling cloud-init and providing the password for the `root` user and the network
    // configuration:
    vmService.start().useCloudInit(true).vm(vm().initialization(initialization().userName("root").rootPassword("redhat123").hostName("myvm.example.com").nicConfigurations(nicConfiguration().name("eth0").onBoot(true).bootProtocol(BootProtocol.STATIC).ip(ip().version(IpVersion.V4).address("192.168.0.100").netmask("255.255.255.0").gateway("192.168.0.1"))).dnsServers("192.168.0.1 192.168.0.2 192.168.0.3").dnsSearch("example.com").customScript(myScript))).send();
    // Close the connection to the server:
    connection.close();
}
Also used : Vm(org.ovirt.engine.sdk4.types.Vm) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService)

Example 59 with Service

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

the class UnassignTagToVm 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 "vms" service:
    VmsService vmsService = connection.systemService().vmsService();
    // Find the virtual machine:
    Vm vm = vmsService.list().search("name=myvm0").send().vms().get(0);
    // Find the service that manages the vm:
    VmService vmService = vmsService.vmService(vm.id());
    // Locate the service that manages the tags of the vm:
    AssignedTagsService tagsService = vmService.tagsService();
    // Find the tag id:
    String tagId = null;
    for (Tag tag : tagsService.list().send().tags()) {
        if (tag.name().equals("mytag")) {
            tagId = tag.id();
            break;
        }
    }
    // Locate the service that manages the tag:
    AssignedTagService tagService = tagsService.tagService(tagId);
    // Unassign tag from virtual machine:
    tagService.remove().send();
    // Close the connection to the server:
    connection.close();
}
Also used : AssignedTagService(org.ovirt.engine.sdk4.services.AssignedTagService) Vm(org.ovirt.engine.sdk4.types.Vm) VmService(org.ovirt.engine.sdk4.services.VmService) Connection(org.ovirt.engine.sdk4.Connection) VmsService(org.ovirt.engine.sdk4.services.VmsService) Tag(org.ovirt.engine.sdk4.types.Tag) AssignedTagsService(org.ovirt.engine.sdk4.services.AssignedTagsService)

Example 60 with Service

use of org.ovirt.engine.sdk4.Service 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)

Aggregations

Connection (org.ovirt.engine.sdk4.Connection)55 Vm (org.ovirt.engine.sdk4.types.Vm)26 VmsService (org.ovirt.engine.sdk4.services.VmsService)25 Connector (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector)22 Service (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service)22 CoreException (org.eclipse.core.runtime.CoreException)18 VmService (org.ovirt.engine.sdk4.services.VmService)15 ArrayList (java.util.ArrayList)12 StorageDomain (org.ovirt.engine.sdk4.types.StorageDomain)12 StorageDomainsService (org.ovirt.engine.sdk4.services.StorageDomainsService)11 SystemService (org.ovirt.engine.sdk4.services.SystemService)10 ServerPort (org.eclipse.wst.server.core.ServerPort)9 StorageDomainService (org.ovirt.engine.sdk4.services.StorageDomainService)7 HostService (org.ovirt.engine.sdk4.services.HostService)6 HostsService (org.ovirt.engine.sdk4.services.HostsService)6 Disk (org.ovirt.engine.sdk4.types.Disk)6 Test (org.junit.Test)5 DataCenterService (org.ovirt.engine.sdk4.services.DataCenterService)5 DataCentersService (org.ovirt.engine.sdk4.services.DataCentersService)5 Cluster (org.ovirt.engine.sdk4.types.Cluster)5