Back to index

nordugrid-arc-nox  1.1.0~rc6
arcsync.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 <fstream>
00008 #include <iostream>
00009 #include <list>
00010 #include <string>
00011 #include <sys/types.h>
00012 #include <sys/stat.h>
00013 #include <unistd.h>
00014 
00015 #include <arc/ArcLocation.h>
00016 #include <arc/DateTime.h>
00017 #include <arc/FileLock.h>
00018 #include <arc/IString.h>
00019 #include <arc/Logger.h>
00020 #include <arc/OptionParser.h>
00021 #include <arc/StringConv.h>
00022 #include <arc/Utils.h>
00023 #include <arc/XMLNode.h>
00024 #include <arc/client/TargetGenerator.h>
00025 #include <arc/UserConfig.h>
00026 
00027 int main(int argc, char **argv) {
00028 
00029   setlocale(LC_ALL, "");
00030 
00031   Arc::Logger logger(Arc::Logger::getRootLogger(), "arcsync");
00032   Arc::LogStream logcerr(std::cerr);
00033   logcerr.setFormat(Arc::ShortFormat);
00034   Arc::Logger::getRootLogger().addDestination(logcerr);
00035   Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);
00036 
00037   Arc::ArcLocation::Init(argv[0]);
00038 
00039   Arc::OptionParser options(istring(" "),
00040                             istring("The command synchronized your local job"
00041                                     "list with the information at a given "
00042                                     "cluster or index server"),
00043                             istring("Argument to -i has the format "
00044                                     "Flavour:URL e.g.\n"
00045                                     "ARC0:ldap://grid.tsl.uu.se:2135/"
00046                                     "mds-vo-name=sweden,O=grid\n"
00047                                     "CREAM:ldap://cream.grid.upjs.sk:2170/"
00048                                     "o=grid\n"
00049                                     "\n"
00050                                     "Argument to -c has the format "
00051                                     "Flavour:URL e.g.\n"
00052                                     "ARC0:ldap://grid.tsl.uu.se:2135/"
00053                                     "nordugrid-cluster-name=grid.tsl.uu.se,"
00054                                     "Mds-Vo-name=local,o=grid"));
00055 
00056   std::list<std::string> clusters;
00057   options.AddOption('c', "cluster",
00058                     istring("explicity select or reject a specific cluster"),
00059                     istring("[-]name"),
00060                     clusters);
00061 
00062   std::list<std::string> indexurls;
00063   options.AddOption('i', "index",
00064                     istring("explicity select or reject an index server"),
00065                     istring("[-]name"),
00066                     indexurls);
00067 
00068   std::string joblist;
00069   options.AddOption('j', "joblist",
00070                     istring("file where the jobs will be stored"),
00071                     istring("filename"),
00072                     joblist);
00073 
00074   bool force = false;
00075   options.AddOption('f', "force",
00076                     istring("do not ask for verification"),
00077                     force);
00078 
00079   bool truncate = false;
00080   options.AddOption('T', "truncate",
00081                     istring("truncate the joblist before sync"),
00082                     truncate);
00083 
00084   int timeout = -1;
00085   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00086                     istring("seconds"), timeout);
00087 
00088   std::string conffile;
00089   options.AddOption('z', "conffile",
00090                     istring("configuration file (default ~/.arc/client.conf)"),
00091                     istring("filename"), conffile);
00092 
00093   std::string debug;
00094   options.AddOption('d', "debug",
00095                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00096                     istring("debuglevel"), debug);
00097 
00098   bool version = false;
00099   options.AddOption('v', "version", istring("print version information"),
00100                     version);
00101 
00102   std::list<std::string> params = options.Parse(argc, argv);
00103 
00104   // If debug is specified as argument, it should be set before loading the configuration.
00105   if (!debug.empty())
00106     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00107 
00108   Arc::UserConfig usercfg(conffile, joblist);
00109   if (!usercfg) {
00110     logger.msg(Arc::ERROR, "Failed configuration initialization");
00111     return 1;
00112   }
00113 
00114   if (debug.empty() && !usercfg.Verbosity().empty())
00115     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00116 
00117   if (version) {
00118     std::cout << Arc::IString("%s version %s", "arcsync", VERSION)
00119               << std::endl;
00120     return 0;
00121   }
00122 
00123   //sanity check
00124   if (!force) {
00125     std::cout << Arc::IString("Synchronizing the local list of active jobs with the information in the MDS\n"
00126                               "can result in some inconsistencies. Very recently submitted jobs might not\n"
00127                               "yet be present in the MDS information, whereas jobs very recently scheduled\n"
00128                               "for deletion can still be present."
00129                               ) << std::endl;
00130     std::cout << Arc::IString("Are you sure you want to synchronize your local job list?") << " ["
00131               << Arc::IString("y") << "/" << Arc::IString("n") << "] ";
00132     std::string response;
00133     std::cin >> response;
00134     if (Arc::lower(response) != std::string(Arc::FindTrans("y"))) {
00135       std::cout << Arc::IString("Cancelling synchronization request") << std::endl;
00136       return 0;
00137     }
00138   }
00139 
00140   if (!clusters.empty() || !indexurls.empty())
00141     usercfg.ClearSelectedServices();
00142 
00143   if (!clusters.empty())
00144     usercfg.AddServices(clusters, Arc::COMPUTING);
00145 
00146   if (!indexurls.empty())
00147     usercfg.AddServices(indexurls, Arc::INDEX);
00148 
00149   //Find all jobs
00150   Arc::TargetGenerator targen(usercfg);
00151   targen.GetTargets(1, 1);
00152 
00153   //Some dummy output
00154   std::cout << "Found number of jobs: " << targen.FoundJobs().size() << std::endl;
00155 
00156   //Write extracted job info to joblist (overwrite the file)
00157   { //start of file lock
00158     Arc::FileLock lock(usercfg.JobListFile());
00159     Arc::NS ns;
00160     Arc::Config jobs(ns);
00161 
00162     if (!truncate)
00163       jobs.ReadFromFile(usercfg.JobListFile());
00164     for (std::list<Arc::XMLNode*>::const_iterator itSyncedJob = targen.FoundJobs().begin();
00165          itSyncedJob != targen.FoundJobs().end(); itSyncedJob++) {
00166       if (!truncate)
00167         for (Arc::XMLNode j = jobs["Job"]; j; ++j)
00168           if ((std::string)j["JobID"] == (std::string)(**itSyncedJob)["JobID"]) {
00169             j.Destroy();
00170             break;
00171           }
00172 
00173       jobs.NewChild(**itSyncedJob);
00174     }
00175     jobs.SaveToFile(usercfg.JobListFile());
00176   } //end of file lock
00177 
00178   return 0;
00179 
00180 }