[Users] [rhevm-api] Need help to Start and Stop VM using Java SDK

Juan Hernandez jhernand at redhat.com
Wed Feb 12 18:34:12 UTC 2014


On 02/12/2014 10:08 AM, Tejesh M wrote:
> Thanks Juan... I'm in process of automating VM creation from Template &
> i want to assign random root password.. can you suggest whether is it
> possible via java SDK?
> 

This is possible with a combination of cloud-init and the Java SDK.

First you will need to create a VM manually, install the cloud-init
package, and then make a template from this VM. Once you have this
template you can use something like the following to create a VM from
that template, and start it so that cloud-init will assign the password
you want:

  // Generate the random password, using whatever mechanism you
  // prefer:
  String password = ...;

  // You need to know the name of the template, the cluster and
  // the VM you are going to create:
  String templateName = "mytemplate";
  String clusterName = "mycluster";
  String vmName = "myvm";

  // Prepare the data to create the VM from the template:
  Template templateData = new Template();
  templateData.setName(templateName);
  Cluster clusterData = new Cluster();
  clusterData.setName(clusterName);
  VM vmDataForCreate = new VM();
  vmDataForCreate.setName(vmName);
  vmDataForCreate.setCluster(clusterData);
  vmDataForCreate.setTemplate(templateData);

  // Send the request to create the VM to the server:
  api.getVMs().add(vmDataForCreate);

  // White till the VM is down (it will be locked for a while):
  for (;;) {
    String state = api.getVMs().get(vmName).getStatus().getState();
    if ("down".equals(state)) {
      break;
    }
    Thread.sleep(1000);
  }

  // Populate parameters for the action to start the VM with cloud-init:
  User userData = new User();
  userData.setUserName("root");
  userData.setPassword(password);
  Users usersData = new Users();
  usersData.getUsers().add(userData);
  CloudInit cloudData = new CloudInit();
  cloudData.setUsers(usersData);
  Initialization initData = new Initialization();
  initData.setCloudInit(cloudData);
  VM vmDataForStart = new VM();
  vmDataForStart.setInitialization(initData);
  Action actionData = new Action();
  actionData.setVm(vmDataForStart);

  // Send the request to start the VM to the server:
  api.getVMs().get(vmName).start(actionData);

> 
>   
> 
> 
> On Tue, Feb 11, 2014 at 9:01 PM, Juan Hernandez <jhernand at redhat.com
> <mailto:jhernand at redhat.com>> wrote:
> 
>     On 02/11/2014 04:16 PM, Itamar Heim wrote:
>     > On 02/11/2014 04:55 PM, Tejesh M wrote:
>     >> Thanks for your fast response.
>     >>
>     >> But intelli sense is not listing vm.*start *function
>     >>
> 
>     This is because you are using the VM class from the
>     org.ovirt.engine.sdk.entities package, instead of from the
>     org.ovirt.engine.sdk.decorators package.
> 
>     The entities package contains classes that plain containers for the data
>     that is sent to and received from the server.
> 
>     The decorators package contains classes that extend those plain
>     containers adding functionality like the action methods.
> 
>     >> I'm using rhevm-sdk-java-1.0.0.29-1.jar
>     >>
>     >> _*code:*_
>     >>
>     >> import org.ovirt.engine.sdk.Api;
>     >> import org.ovirt.engine.sdk.common.*;
>     >> import org.ovirt.engine.sdk.decorators.VMDisk;
>     >> import org.ovirt.engine.sdk.entities.*;
>     >> import org.apache.commons.beanutils.*;
>     >>
>     >>
>     >> public class rhvm {
>     >>      static Api api;
>     >>      public static void callAPI()
>     >>      {
>     >>          try{
>     >>
>     >>          api = new Api("https://rhevm:443/api", "admin at internal",
>     >> "password");
>     >>
>     >>
>     >>          org.ovirt.engine.sdk.entities.VM vm =
>     >> api.getVMs().get("testVM3"); //get VM
> 
>     If you want to be able to use the "start" method you will need something
>     like this:
> 
>     org.ovirt.engine.sdk.decorators.VM vm = api.getVMs().get("testVM3");
> 
>     Then your IDE will show you the additional methods in the decorator,
>     including the "start" method.
> 
>     >>          vm.setDescription("java_sdk");
>     >>
>     >>          System.out.print("VM ID:" + vm.getStatus().getState());
>     >>          System.out.print("VM ID:" + vm.getId());
>     >>
>     >>
>     >>          }
>     >>          catch(Exception e)
>     >>          {
>     >>              System.out.print(e);
>     >>          }
>     >>          finally {
>     >>               api.shutdown();
>     >>           }
>     >>      }
>     >> public static void main(String a[]) throws Exception
>     >> {
>     >>      //rhvm obj = new rhvm();
>     >>      callAPI();
>     >>      System.out.println("\ncompleted");
>     >> }
>     >> }
>     >>
>     >>
>     >>
>     >>
>     >> On Tue, Feb 11, 2014 at 8:08 PM, Ewoud Kohl van Wijngaarden
>     >> <ewoud+rhevm-api at kohlvanwijngaarden.nl
>     <mailto:ewoud%2Brhevm-api at kohlvanwijngaarden.nl>
>     >> <mailto:ewoud+rhevm-api at kohlvanwijngaarden.nl
>     <mailto:ewoud%2Brhevm-api at kohlvanwijngaarden.nl>>> wrote:
>     >>
>     >>     On Tue, Feb 11, 2014 at 08:00:57PM +0530, Tejesh M wrote:
>     >>      > Can anyone share sample on how to Start & Stop VM using
>     Java SDK? I
>     >>      > couldn't find any document on JAVA SDK for RHEVM.
>     >>
>     >>     While this list hasn't been in use for some time, I hope I
>     can still
>     >>     point you
>     >>     in the right direction.
>     http://www.ovirt.org/Java-sdk#Examples has the
>     >>     following examples:
>     >>
>     >>     // -- Create proxy
>     >>
>     >>     // #1 - import
>     >>     import org.ovirt.engine.sdk.Api;
>     >>
>     >>     // #2 - create proxy
>     >>     Api api = new Api("http://localhost:8080/api", "user at domain",
>     >>     "password");
>     >>
>     >>     // -- perform an action on resource
>     >>
>     >>     // #1 - fetch resource
>     >>     VM vm = api.getVMs().get("test");
>     >>
>     >>     // #2 - create params + perform an action
>     >>     Action res = vm.start(new Action() {
>     >>              {
>     >>                      setVm(new org.ovirt.engine.sdk.entities.VM());
>     >>              }
>     >>     });
>     >>
>     >>     I'd guess vm.stop is exactly the same as vm.start.
>     >>     _______________________________________________
>     >>     rhevm-api mailing list
>     >>     rhevm-api at lists.fedorahosted.org
>     <mailto:rhevm-api at lists.fedorahosted.org>
>     >>     <mailto:rhevm-api at lists.fedorahosted.org
>     <mailto:rhevm-api at lists.fedorahosted.org>>
>     >>     https://lists.fedorahosted.org/mailman/listinfo/rhevm-api
>     >>
>     >>
>     >>
>     >>
>     >> --
>     >> Thanks & Regards
>     >> Tejesh
>     >>
>     >>
>     >> _______________________________________________
>     >> rhevm-api mailing list
>     >> rhevm-api at lists.fedorahosted.org
>     <mailto:rhevm-api at lists.fedorahosted.org>
>     >> https://lists.fedorahosted.org/mailman/listinfo/rhevm-api
>     >>
>     >
>     > moving to users at ovirt.org...
>     >
>     > thanks,
>     >     Itamar


-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list