Back to index

nordugrid-arc-nox  1.1.0~rc6
arckill.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <iostream>
00008 #include <list>
00009 #include <string>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/IString.h>
00013 #include <arc/Logger.h>
00014 #include <arc/OptionParser.h>
00015 #include <arc/StringConv.h>
00016 #include <arc/client/JobController.h>
00017 #include <arc/client/JobSupervisor.h>
00018 #include <arc/UserConfig.h>
00019 
00020 int main(int argc, char **argv) {
00021 
00022   setlocale(LC_ALL, "");
00023 
00024   Arc::Logger logger(Arc::Logger::getRootLogger(), "arckill");
00025   Arc::LogStream logcerr(std::cerr);
00026   logcerr.setFormat(Arc::ShortFormat);
00027   Arc::Logger::getRootLogger().addDestination(logcerr);
00028   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00029 
00030   Arc::ArcLocation::Init(argv[0]);
00031 
00032   Arc::OptionParser options(istring("[job ...]"),
00033                             istring("The arckill command is used to kill "
00034                                     "running jobs."),
00035                             istring("Argument to -c has the format "
00036                                     "Flavour:URL e.g.\n"
00037                                     "ARC0:ldap://grid.tsl.uu.se:2135/"
00038                                     "nordugrid-cluster-name=grid.tsl.uu.se,"
00039                                     "Mds-Vo-name=local,o=grid"));
00040 
00041   bool all = false;
00042   options.AddOption('a', "all",
00043                     istring("all jobs"),
00044                     all);
00045 
00046   std::string joblist;
00047   options.AddOption('j', "joblist",
00048                     istring("file containing a list of jobs"),
00049                     istring("filename"),
00050                     joblist);
00051 
00052   std::list<std::string> clusters;
00053   options.AddOption('c', "cluster",
00054                     istring("explicity select or reject a specific cluster"),
00055                     istring("[-]name"),
00056                     clusters);
00057 
00058   std::list<std::string> status;
00059   options.AddOption('s', "status",
00060                     istring("only select jobs whose status is statusstr"),
00061                     istring("statusstr"),
00062                     status);
00063 
00064   bool keep = false;
00065   options.AddOption('k', "keep",
00066                     istring("keep the files on the server (do not clean)"),
00067                     keep);
00068 
00069   int timeout = -1;
00070   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00071                     istring("seconds"), timeout);
00072 
00073   std::string conffile;
00074   options.AddOption('z', "conffile",
00075                     istring("configuration file (default ~/.arc/client.conf)"),
00076                     istring("filename"), conffile);
00077 
00078   std::string debug;
00079   options.AddOption('d', "debug",
00080                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00081                     istring("debuglevel"), debug);
00082 
00083   bool version = false;
00084   options.AddOption('v', "version", istring("print version information"),
00085                     version);
00086 
00087   std::list<std::string> jobs = options.Parse(argc, argv);
00088 
00089   // If debug is specified as argument, it should be set before loading the configuration.
00090   if (!debug.empty())
00091     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00092 
00093   Arc::UserConfig usercfg(conffile, joblist);
00094   if (!usercfg) {
00095     logger.msg(Arc::ERROR, "Failed configuration initialization");
00096     return 1;
00097   }
00098 
00099   if (debug.empty() && !usercfg.Verbosity().empty())
00100     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00101 
00102   if (timeout > 0)
00103     usercfg.Timeout(timeout);
00104 
00105   if (version) {
00106     std::cout << Arc::IString("%s version %s", "arckill", VERSION)
00107               << std::endl;
00108     return 0;
00109   }
00110 
00111   if ((!joblist.empty() || !status.empty()) && jobs.empty() && clusters.empty())
00112     all = true;
00113 
00114   if (jobs.empty() && clusters.empty() && !all) {
00115     logger.msg(Arc::ERROR, "No jobs given");
00116     return 1;
00117   }
00118 
00119   if (!jobs.empty() || all)
00120     usercfg.ClearSelectedServices();
00121 
00122   if (!clusters.empty()) {
00123     usercfg.ClearSelectedServices();
00124     usercfg.AddServices(clusters, Arc::COMPUTING);
00125   }
00126 
00127   // If the user specified a joblist on the command line joblist equals
00128   // usercfg.JobListFile(). If not use the default, ie. usercfg.JobListFile().
00129   Arc::JobSupervisor jobmaster(usercfg, jobs);
00130   if (!jobmaster.JobsFound()) {
00131     std::cout << "No jobs" << std::endl;
00132     return 0;
00133   }
00134   std::list<Arc::JobController*> jobcont = jobmaster.GetJobControllers();
00135 
00136   if (jobcont.empty()) {
00137     logger.msg(Arc::ERROR, "No job controller plugins loaded");
00138     return 1;
00139   }
00140 
00141   int retval = 0;
00142   for (std::list<Arc::JobController*>::iterator it = jobcont.begin();
00143        it != jobcont.end(); it++)
00144     if (!(*it)->Kill(status, keep))
00145       retval = 1;
00146 
00147   return retval;
00148 }