9224fb85f148ce1fe6e799f321852acae55da643
[gp-arc-client-c.git] / ext / arc_submit.cpp
1 /*
2  *  submit.cpp
3  *  arcclientc
4  *
5  *  Created by Tamas Jung on 4/7/10.
6  *
7  */
8
9 #include "arc_submit.h"
10 #include "logger.h"
11 // -*- indent-tabs-mode: nil -*-
12
13
14 #include "logger.h"
15 #include <fstream>
16 #include <iostream>
17 #include <list>
18 #include <string>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include "arc_libs.h"
24
25
26 char* arc_submit(int argc, char **argv) {
27         
28         
29   char* job_id = NULL;
30         
31         
32
33         //TODO: what necessary here?
34         Arc::OptionParser options(istring("[filename ...]"),
35                                                           istring("The arcsub command is used for "
36                                                                           "submitting jobs to grid enabled "
37                                                                           "computing\nresources."),
38                                                           istring("Argument to -i has the format "
39                                                                           "Flavour:URL e.g.\n"
40                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
41                                                                           "mds-vo-name=sweden,O=grid\n"
42                                                                           "CREAM:ldap://cream.grid.upjs.sk:2170/"
43                                                                           "o=grid\n"
44                                                                           "\n"
45                                                                           "Argument to -c has the format "
46                                                                           "Flavour:URL e.g.\n"
47                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
48                                                                           "nordugrid-cluster-name=grid.tsl.uu.se,"
49                                                                           "Mds-Vo-name=local,o=grid"));
50         
51         std::list<std::string> clusters;
52         options.AddOption('c', "cluster",
53                                           istring("explicity select or reject a specific cluster"),
54                                           istring("[-]name"),
55                                           clusters);
56         
57         std::list<std::string> indexurls;
58         options.AddOption('i', "index",
59                                           istring("explicity select or reject an index server"),
60                                           istring("[-]name"),
61                                           indexurls);
62         
63         std::string jobdescriptionstring;
64         options.AddOption('e', "jobdescrstring",
65                                           istring("jobdescription string describing the job to "
66                                                           "be submitted"),
67                                           istring("string"),
68                                           jobdescriptionstring);
69         
70
71         
72         
73         //jt: dryrun was commented out in the original file, I try to use it TODO:
74      bool dryrun = false;
75      options.AddOption('D', "dryrun", istring("add dryrun option"),
76          dryrun);
77          
78         std::string joblist;
79         options.AddOption('j', "joblist",
80                                           istring("file where the jobs will be stored"),
81                                           istring("filename"),
82                                           joblist);
83         
84         
85         int timeout = -1;
86         options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
87                                           istring("seconds"), timeout);
88         
89         std::string conffile;
90         options.AddOption('z', "conffile",
91                                           istring("configuration file (default ~/.arc/client.conf)"),
92                                           istring("filename"), conffile);
93                 
94         std::string broker;
95         options.AddOption('b', "broker",
96                                           istring("select broker method (Random (default), FastestQueue, or custom)"),
97                                           istring("broker"), broker);
98         
99         
100         std::list<std::string> params = options.Parse(argc, argv);
101         
102         
103         Arc::UserConfig usercfg(conffile, joblist);
104         if (!usercfg) {
105                 logger.msg(Arc::ERROR, "Failed configuration initialization");
106                 return NULL;    
107         }
108         
109         if (timeout > 0)
110                 usercfg.Timeout(timeout);
111         
112         if (!broker.empty())
113                 usercfg.Broker(broker);
114                 
115         if (!clusters.empty() || !indexurls.empty())
116                 usercfg.ClearSelectedServices();
117         
118         if (!clusters.empty())
119                 usercfg.AddServices(clusters, Arc::COMPUTING);
120         
121         if (!indexurls.empty())
122                 usercfg.AddServices(indexurls, Arc::INDEX);
123         
124         if (jobdescriptionstring.empty()) {
125                 logger.msg(Arc::ERROR, "No job description input specified");
126                 return NULL;
127         }
128                 
129         Arc::JobDescription jobdesc;
130         
131         jobdesc.Parse(jobdescriptionstring);
132         
133         if (!jobdesc){
134                 
135                 logger.msg(Arc::ERROR, "Invalid JobDescription");
136                 return NULL;
137         }
138         
139         Arc::TargetGenerator targen(usercfg);
140         targen.RetrieveExecutionTargets();
141   
142         
143         if (targen.GetExecutionTargets().empty()) {
144                 logger.msg(Arc::ERROR, "Job submission aborted because no clusters returned any information");
145                 return NULL;
146         }
147         
148         
149         
150         Arc::BrokerLoader loader;
151         Arc::Broker *ChosenBroker = loader.load(usercfg.Broker().first, usercfg);
152         if (!ChosenBroker) {
153           logger.msg(Arc::ERROR, "Unable to load broker %s", usercfg.Broker().first);
154                 return NULL;
155         }
156         logger.msg(Arc::INFO, "Broker %s loaded", usercfg.Broker().first);
157         
158   std::list<Arc::Job> submittedJobs;
159   
160   submittedJobs.push_back(Arc::Job());  
161
162   if (ChosenBroker->Submit(targen.GetExecutionTargets(), jobdesc, submittedJobs.back())) {
163     job_id = new_str(submittedJobs.back().JobID.str().c_str());
164     logger.msg(Arc::DEBUG, "Job submitted with ID: %s", job_id);
165   } else {
166     logger.msg(Arc::ERROR, "Job submission failed (ChosenBroker->Submit returned false)");
167     submittedJobs.pop_back();
168   }
169   
170   Arc::Job::WriteJobsToFile(usercfg.JobListFile(), submittedJobs);
171     
172   return job_id;
173 }
174
175