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