View Javadoc

1   package fr.in2p3.jsaga.command;
2   
3   import org.apache.commons.cli.*;
4   import org.ogf.saga.job.JobFactory;
5   import org.ogf.saga.job.JobService;
6   import org.ogf.saga.resource.ResourceFactory;
7   import org.ogf.saga.resource.Type;
8   import org.ogf.saga.resource.description.ComputeDescription;
9   import org.ogf.saga.resource.instance.Compute;
10  import org.ogf.saga.resource.manager.ResourceManager;
11  import org.ogf.saga.resource.task.State;
12  import org.ogf.saga.session.Session;
13  import org.ogf.saga.session.SessionFactory;
14  import org.ogf.saga.url.URL;
15  import org.ogf.saga.url.URLFactory;
16  
17  /* ***************************************************
18   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19   * ***             http://cc.in2p3.fr/             ***
20   * ***************************************************
21   * File:   ResourceAcquireCompute
22   * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
23   * Date:   24 feb 2016
24   * ***************************************************
25   * Description:                                      */
26  /**
27   *
28   */
29  public class ResourceAcquireCompute extends AbstractCommand {
30      private static final String OPT_HELP = "h", LONGOPT_HELP = "help";
31      // required arguments
32      private static final String OPT_RESOURCE = "r", LONGOPT_RESOURCE = "resource";
33      // optional arguments
34      private static final String OPT_TEMPLATE = "t", LONGOPT_TEMPLATE = "template";
35  
36      protected ResourceAcquireCompute() {
37          super("jsaga-resource-acquire-compute", null, null, new GnuParser());
38      }
39  
40      public static void main(String[] args) throws Exception {
41          ResourceAcquireCompute command = new ResourceAcquireCompute();
42          CommandLine line = command.parse(args);
43          if (line.hasOption(OPT_HELP))
44          {
45              command.printHelpAndExit(null);
46          }
47          else
48          {
49              // get arguments
50              URL serviceURL = URLFactory.createURL(line.getOptionValue(OPT_RESOURCE));
51              String templateName = line.getOptionValue(OPT_TEMPLATE);
52              Session session = SessionFactory.createSession(true);
53              ResourceManager rm = ResourceFactory.createResourceManager(session, serviceURL);
54              // Create compute description
55              ComputeDescription desc = (ComputeDescription) ResourceFactory.createResourceDescription(Type.COMPUTE);
56              desc.setVectorAttribute(ComputeDescription.TEMPLATE, new String[]{templateName});
57              // Acquire resource
58              Compute computeNode = rm.acquireCompute(desc);
59              // Wait until ACTIVE
60              computeNode.waitFor(State.ACTIVE);
61              // Now we can submit a job
62              String computeNodeAccess = computeNode.getAccess()[0];
63              URL jobServiceURL = URLFactory.createURL(computeNodeAccess);
64              JobService jobService = JobFactory.createJobService(session, jobServiceURL);
65              // ...
66              // Release the resource
67              computeNode.release();
68              System.exit(0);
69          }
70      }
71      @SuppressWarnings("static-access")
72      protected Options createOptions() {
73          Options opt = new Options();
74  
75          // command
76          opt.addOption(OptionBuilder.withDescription("Display this help and exit")
77                  .withLongOpt(LONGOPT_HELP)
78                  .create(OPT_HELP));
79  
80          // required arguments
81          opt.addOption(OptionBuilder.withDescription("the URL of the job service")
82                  .isRequired(true)
83                  .hasArg()
84                  .withArgName("URL")
85                  .withLongOpt(LONGOPT_RESOURCE)
86                  .create(OPT_RESOURCE));
87  
88          opt.addOption(OptionBuilder.withDescription("The image to use ")
89                  .isRequired(true)
90                  .hasArg()
91                  .withArgName("TEMPLATE")
92                  .withLongOpt(LONGOPT_TEMPLATE)
93                  .create(OPT_TEMPLATE));
94  
95          // returns
96          return opt;
97      }
98  }