delete false licenses2
[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.GetTargets(0, 1);
141         
142         if (targen.FoundTargets().empty()) {            
143                 logger.msg(Arc::ERROR, "Job submission aborted because no clusters returned any information");
144                 return NULL;
145         }
146         
147         
148         
149         Arc::BrokerLoader loader;
150         Arc::Broker *ChosenBroker = loader.load(usercfg.Broker().first, usercfg);
151         if (!ChosenBroker) {
152           logger.msg(Arc::ERROR, "Unable to load broker %s", usercfg.Broker().first);
153                 return NULL;
154         }
155         logger.msg(Arc::INFO, "Broker %s loaded", usercfg.Broker().first);
156         
157                 
158         ChosenBroker->PreFilterTargets(targen.ModifyFoundTargets(), jobdesc);
159         
160         while (true) {
161                 const Arc::ExecutionTarget* target = ChosenBroker->GetBestTarget();
162                 
163                 if (!target) {
164                         logger.msg(Arc::ERROR, "Job submission failed, no more possible targets");
165                         break;
166                 }
167                 
168                 Arc::Submitter *submitter = target->GetSubmitter(usercfg);
169                                 
170                 if (dryrun) {
171                         logger.msg(Arc::ERROR, "Dry run, job submission is skipped");
172                         break;
173                 }
174                 //submit the job
175                 Arc::URL jobid = submitter->Submit(jobdesc, *target);
176                 if (!jobid) {
177                         logger.msg(Arc::INFO, "Submission to %s failed, trying next target", target->url.str());
178                         continue;
179                 }
180                 
181                 ChosenBroker->RegisterJobsubmission();
182                 job_id = new_str(jobid.str().c_str());
183                 logger.msg(Arc::DEBUG, "Job submitted with jobid: %s", jobid.str());
184                 
185                 break;
186         } //end loop over all possible targets
187         
188         return job_id;
189 }
190
191