megoldas a loggerre, egyeb javitasok
[gp-arc-client-c.git] / src / submit.cpp
1 /*
2  *  submit.cpp
3  *  arcclientc
4  *
5  *  Created by Tamas Jung on 4/7/10.
6  *  Copyright 2010 Interface Kft. All rights reserved.
7  *
8  */
9
10 #include "submit.h"
11 #include "logger.h"
12 #include "config.h" //TBD: Kell ez?
13 // -*- indent-tabs-mode: nil -*-
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <fstream>
20 #include <iostream>
21 #include <list>
22 #include <string>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "arc_libs.h"
28
29
30 submit_result_t* submit(int argc, char **argv) {
31         
32         
33         
34         
35         
36         
37         
38         
39         submit_result_t* result = (submit_result_t*)malloc(sizeof(submit_result_t));
40         if (!result) {
41                 return NULL;//TBD: throw sth?
42         }
43         result->message = NULL;
44         result->job_id = NULL;
45         
46         
47
48         //TODO: what necessary here?
49         Arc::OptionParser options(istring("[filename ...]"),
50                                                           istring("The arcsub command is used for "
51                                                                           "submitting jobs to grid enabled "
52                                                                           "computing\nresources."),
53                                                           istring("Argument to -i has the format "
54                                                                           "Flavour:URL e.g.\n"
55                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
56                                                                           "mds-vo-name=sweden,O=grid\n"
57                                                                           "CREAM:ldap://cream.grid.upjs.sk:2170/"
58                                                                           "o=grid\n"
59                                                                           "\n"
60                                                                           "Argument to -c has the format "
61                                                                           "Flavour:URL e.g.\n"
62                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
63                                                                           "nordugrid-cluster-name=grid.tsl.uu.se,"
64                                                                           "Mds-Vo-name=local,o=grid"));
65         
66         std::list<std::string> clusters;
67         options.AddOption('c', "cluster",
68                                           istring("explicity select or reject a specific cluster"),
69                                           istring("[-]name"),
70                                           clusters);
71         
72         std::list<std::string> indexurls;
73         options.AddOption('i', "index",
74                                           istring("explicity select or reject an index server"),
75                                           istring("[-]name"),
76                                           indexurls);
77         
78         std::string jobdescriptionstring;
79         options.AddOption('e', "jobdescrstring",
80                                           istring("jobdescription string describing the job to "
81                                                           "be submitted"),
82                                           istring("string"),
83                                           jobdescriptionstring);
84         
85
86         
87         
88         //jt: dryrun was commented out in the original file, I try to use it TODO:
89      bool dryrun = false;
90      options.AddOption('D', "dryrun", istring("add dryrun option"),
91          dryrun);
92          
93         int timeout = -1;
94         options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
95                                           istring("seconds"), timeout);
96         
97         std::string conffile;
98         options.AddOption('z', "conffile",
99                                           istring("configuration file (default ~/.arc/client.conf)"),
100                                           istring("filename"), conffile);
101                 
102         std::string broker;
103         options.AddOption('b', "broker",
104                                           istring("select broker method (Random (default), FastestQueue, or custom)"),
105                                           istring("broker"), broker);
106         
107         
108         std::list<std::string> params = options.Parse(argc, argv);
109         
110         
111         Arc::UserConfig usercfg(conffile);
112         if (!usercfg) {
113                 result->message = new_str("Failed configuration initialization");
114                 return result;  
115         }
116         
117         if (timeout > 0)
118                 usercfg.Timeout(timeout);
119         
120         if (!broker.empty())
121                 usercfg.Broker(broker);
122                 
123         if (!clusters.empty() || !indexurls.empty())
124                 usercfg.ClearSelectedServices();
125         
126         if (!clusters.empty())
127                 usercfg.AddServices(clusters, Arc::COMPUTING);
128         
129         if (!indexurls.empty())
130                 usercfg.AddServices(indexurls, Arc::INDEX);
131         
132         if (jobdescriptionstring.empty()) {
133                 result->message = new_str("No job description input specified");
134                 return result;
135         }
136         
137         
138
139                 
140         Arc::JobDescription jobdesc;
141         
142         jobdesc.Parse(jobdescriptionstring);
143         
144         if (!jobdesc){
145                 std::ostringstream sstr;
146                 sstr << "Invalid JobDescription:";
147                 sstr << jobdescriptionstring;
148                 result->message = ostringstream2cstring(sstr);
149                 return result;
150         }
151         
152         Arc::TargetGenerator targen(usercfg);
153         targen.GetTargets(0, 1);
154         
155         if (targen.FoundTargets().empty()) {
156                 
157                 result->message = new_str("Job submission aborted because no clusters returned any information");
158                 return result;
159         }
160         
161         
162         
163         Arc::BrokerLoader loader;
164         Arc::Broker *ChosenBroker = loader.load(usercfg.Broker().first, usercfg);
165         if (!ChosenBroker) {
166                 std::ostringstream sstream;
167                 sstream << "Unable to load broker " << usercfg.Broker().first;
168                 result->message = ostringstream2cstring(sstream);
169                 return result;
170         }
171         logger.msg(Arc::INFO, "Broker %s loaded", usercfg.Broker().first);
172         
173                 
174         ChosenBroker->PreFilterTargets(targen.ModifyFoundTargets(), jobdesc);
175         
176         while (true) {
177                 const Arc::ExecutionTarget* target = ChosenBroker->GetBestTarget();
178                 
179                 if (!target) {
180                         result->message = new_str("Job submission failed, no more possible targets");
181                         break;
182                 }
183                 
184                 Arc::Submitter *submitter = target->GetSubmitter(usercfg);
185                                 
186                 if (dryrun) {
187                         result->message = new_str("Dry run, job submission is skipped");
188                         break;
189                 }
190                 //submit the job
191                 Arc::URL jobid = submitter->Submit(jobdesc, *target);
192                 if (!jobid) {
193                         logger.msg(Arc::INFO, "Submission to %s failed, trying next target", target->url.str());
194                         continue;
195                 }
196                 
197                 ChosenBroker->RegisterJobsubmission();
198                 result->job_id = new_str(jobid.str().c_str());
199                 logger.msg(Arc::DEBUG, "Job submitted with jobid: %s", jobid.str());
200                 
201                 break;
202         } //end loop over all possible targets
203         
204         return result;
205 }
206
207