Esempio n. 1
0
int RUNMAIN(arccat)(int argc, char **argv) {

  setlocale(LC_ALL, "");

  Arc::Logger logger(Arc::Logger::getRootLogger(), "arccat");
  Arc::LogStream logcerr(std::cerr);
  logcerr.setFormat(Arc::ShortFormat);
  Arc::Logger::getRootLogger().addDestination(logcerr);
  Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);

  Arc::ArcLocation::Init(argv[0]);

  ClientOptions opt(ClientOptions::CO_CAT,
                    istring("[job ...]"),
                    istring("The arccat command performs the cat "
                            "command on the stdout, stderr or grid\n"
                            "manager's error log of the job."));

  std::list<std::string> jobidentifiers = opt.Parse(argc, argv);

  if (opt.showversion) {
    std::cout << Arc::IString("%s version %s", "arccat", VERSION)
              << std::endl;
    return 0;
  }

  // If debug is specified as argument, it should be set before loading the configuration.
  if (!opt.debug.empty())
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(opt.debug));

  if (opt.show_plugins) {
    std::list<std::string> types;
    types.push_back("HED:JobControllerPlugin");
    showplugins("arccat", types, logger);
    return 0;
  }

  Arc::UserConfig usercfg(opt.conffile, opt.joblist);
  if (!usercfg) {
    logger.msg(Arc::ERROR, "Failed configuration initialization");
    return 1;
  }

  if (!checkproxy(usercfg)) {
    return 1;
  }

  if (opt.debug.empty() && !usercfg.Verbosity().empty())
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));

  for (std::list<std::string>::const_iterator it = opt.jobidinfiles.begin(); it != opt.jobidinfiles.end(); it++) {
    if (!Arc::Job::ReadJobIDsFromFile(*it, jobidentifiers)) {
      logger.msg(Arc::WARNING, "Cannot read specified jobid file: %s", *it);
    }
  }

  if (opt.timeout > 0)
    usercfg.Timeout(opt.timeout);

  if ((!opt.joblist.empty() || !opt.status.empty()) && jobidentifiers.empty() && opt.clusters.empty())
    opt.all = true;

  if (jobidentifiers.empty() && opt.clusters.empty() && !opt.all) {
    logger.msg(Arc::ERROR, "No jobs given");
    return 1;
  }

  std::list<std::string> selectedURLs;
  if (!opt.clusters.empty()) {
    selectedURLs = getSelectedURLsFromUserConfigAndCommandLine(usercfg, opt.clusters);
  }
  std::list<std::string> rejectManagementURLs = getRejectManagementURLsFromUserConfigAndCommandLine(usercfg, opt.rejectmanagement);

  std::list<Arc::Job> jobs;
  Arc::JobInformationStorageXML jobList(usercfg.JobListFile());
  if (( opt.all && !jobList.ReadAll(jobs, rejectManagementURLs)) ||
      (!opt.all && !jobList.Read(jobs, jobidentifiers, selectedURLs, rejectManagementURLs))) {
    logger.msg(Arc::ERROR, "Unable to read job information from file (%s)", usercfg.JobListFile());
    return 1;
  }

  if (!opt.all) {
    for (std::list<std::string>::const_iterator itJIDAndName = jobidentifiers.begin();
         itJIDAndName != jobidentifiers.end(); ++itJIDAndName) {
      std::cout << Arc::IString("Warning: Job not found in job list: %s", *itJIDAndName) << std::endl;
    }
  }

  Arc::JobSupervisor jobmaster(usercfg, jobs);
  jobmaster.Update();
  jobmaster.SelectValid();
  if (!opt.status.empty()) {
    jobmaster.SelectByStatus(opt.status);
  }

  jobs = jobmaster.GetSelectedJobs();
  if (jobs.empty()) {
    std::cout << Arc::IString("No jobs") << std::endl;
    return 1;
  }

  std::string resourceName;
  Arc::Job::ResourceType resource;
  if (opt.show_joblog)      { resource = Arc::Job::JOBLOG; resourceName = "joblog"; }
  else if (opt.show_stderr) { resource = Arc::Job::STDERR; resourceName = "stderr"; }
  else                      { resource = Arc::Job::STDOUT; resourceName = "stdout"; }

  // saving to a temp file is necessary because chunks from server
  // may arrive out of order
  std::string filename = Glib::build_filename(Glib::get_tmp_dir(), "arccat.XXXXXX");
  int tmp_h = Glib::mkstemp(filename);
  if (tmp_h == -1) {
    logger.msg(Arc::INFO, "Could not create temporary file \"%s\"", filename);
    logger.msg(Arc::ERROR, "Cannot create output of %s for any jobs", resourceName);
    return 1;
  }

  Arc::URL dst("stdio:///"+Arc::tostring(tmp_h));
  if (!dst) {
    logger.msg(Arc::ERROR, "Cannot create output of %s for any jobs", resourceName);
    logger.msg(Arc::INFO, "Invalid destination URL %s", dst.str());
    close(tmp_h);
    unlink(filename.c_str());
    return 1;
  }

  int retval = 0;
  for (std::list<Arc::Job>::const_iterator it = jobs.begin();
       it != jobs.end(); it++) {
    if (!it->State || (!opt.status.empty() &&
        std::find(opt.status.begin(), opt.status.end(), it->State()) == opt.status.end() &&
        std::find(opt.status.begin(), opt.status.end(), it->State.GetGeneralState()) == opt.status.end())) {
      continue;
    }

    if (it->State == Arc::JobState::DELETED) {
      logger.msg(Arc::WARNING, "Job deleted: %s", it->JobID);
      retval = 1;
      continue;
    }

    // The job-log might be available before the job has started (middleware dependent).
    if (!opt.show_joblog &&
        !it->State.IsFinished() &&
        it->State != Arc::JobState::RUNNING &&
        it->State != Arc::JobState::FINISHING) {
      logger.msg(Arc::WARNING, "Job has not started yet: %s", it->JobID);
      retval = 1;
      continue;
    }

    if ((opt.show_joblog && it->LogDir.empty()) ||
        (!opt.show_joblog && opt.show_stderr && it->StdErr.empty()) ||
        (!opt.show_joblog && !opt.show_stderr && it->StdOut.empty())) {
      logger.msg(Arc::ERROR, "Cannot determine the %s location: %s", resourceName, it->JobID);
      retval = 1;
      continue;
    }

    Arc::URL src;
    it->GetURLToResource(resource, src);
    if (!src) {
      logger.msg(Arc::ERROR, "Cannot create output of %s for job (%s): Invalid source %s", resourceName, it->JobID, src.str());
      retval = 1;
      continue;
    }

    if (!it->CopyJobFile(usercfg, src, dst)) {
      retval = 1;
      continue;
    }

    logger.msg(Arc::VERBOSE, "Catting %s for job %s", resourceName, it->JobID);

    std::ifstream is(filename.c_str());
    char c;
    while (is.get(c)) {
      std::cout.put(c);
    }
    is.close();
  }

  close(tmp_h);
  unlink(filename.c_str());

  return retval;
}
Esempio n. 2
0
int RUNMAIN(arcinfo)(int argc, char **argv) {

  setlocale(LC_ALL, "");

  Arc::Logger logger(Arc::Logger::getRootLogger(), "arcinfo");
  Arc::LogStream logcerr(std::cerr);
  logcerr.setFormat(Arc::ShortFormat);
  Arc::Logger::getRootLogger().addDestination(logcerr);
  Arc::Logger::getRootLogger().setThreshold(Arc::WARNING);

  Arc::ArcLocation::Init(argv[0]);

  ClientOptions opt(ClientOptions::CO_INFO,
                    istring("[resource ...]"),
                    istring("The arcinfo command is used for "
                            "obtaining the status of computing "
                            "resources on the Grid."));

  {
    std::list<std::string> clusterstmp = opt.Parse(argc, argv);
    opt.clusters.insert(opt.clusters.end(), clusterstmp.begin(), clusterstmp.end());
  }

  if (opt.showversion) {
    std::cout << Arc::IString("%s version %s", "arcinfo", VERSION) << std::endl;
    return 0;
  }

  // If debug is specified as argument, it should be set before loading the configuration.
  if (!opt.debug.empty())
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(opt.debug));

  if (opt.show_plugins) {
    std::list<std::string> types;
    types.push_back("HED:ServiceEndpointRetrieverPlugin");
    types.push_back("HED:TargetInformationRetrieverPlugin");
    showplugins("arcinfo", types, logger);
    return 0;
  }
  
  Arc::UserConfig usercfg(opt.conffile);
  if (!usercfg) {
    logger.msg(Arc::ERROR, "Failed configuration initialization");
    return 1;
  }
  
  if (opt.list_configured_services) {
    std::map<std::string, Arc::ConfigEndpoint> allServices = usercfg.GetAllConfiguredServices();
    std::cout << "Configured registries:" << std::endl;
    for (std::map<std::string, Arc::ConfigEndpoint>::const_iterator it = allServices.begin(); it != allServices.end(); it++) {
      if (it->second.type == Arc::ConfigEndpoint::REGISTRY) {
        std::cout << "  " << it->first << ": " << it->second.URLString;
        if (!it->second.InterfaceName.empty()) {
          std::cout << " (" << it->second.InterfaceName << ")";
        }
        std::cout << std::endl;
      }
    }
    std::cout << "Configured computing elements:" << std::endl;
    for (std::map<std::string, Arc::ConfigEndpoint>::const_iterator it = allServices.begin(); it != allServices.end(); it++) {
      if (it->second.type == Arc::ConfigEndpoint::COMPUTINGINFO) {
        std::cout << "  " << it->first << ": " << it->second.URLString;
        if (!it->second.InterfaceName.empty() || !it->second.RequestedSubmissionInterfaceName.empty()) {
          std::cout << " (" << it->second.InterfaceName;
          if (!it->second.InterfaceName.empty() && !it->second.RequestedSubmissionInterfaceName.empty()) {
            std::cout << " / ";
          }
          std::cout << it->second.RequestedSubmissionInterfaceName + ")";
        }
        std::cout << std::endl;
      }
    }
    return 0;
  }

  if (opt.debug.empty() && !usercfg.Verbosity().empty())
    Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));

  if (opt.timeout > 0)
    usercfg.Timeout(opt.timeout);

  std::list<Arc::Endpoint> endpoints = getServicesFromUserConfigAndCommandLine(usercfg, opt.indexurls, opt.clusters, opt.requestedSubmissionInterfaceName, opt.infointerface);

  std::set<std::string> preferredInterfaceNames;
  if (usercfg.InfoInterface().empty()) {
    preferredInterfaceNames.insert("org.nordugrid.ldapglue2");
  } else {
    preferredInterfaceNames.insert(usercfg.InfoInterface());
  }

  std::list<std::string> rejectDiscoveryURLs = getRejectDiscoveryURLsFromUserConfigAndCommandLine(usercfg, opt.rejectdiscovery);

  Arc::ComputingServiceUniq csu;
  Arc::ComputingServiceRetriever csr(usercfg, std::list<Arc::Endpoint>(), rejectDiscoveryURLs, preferredInterfaceNames);
  csr.addConsumer(csu);
  for (std::list<Arc::Endpoint>::const_iterator it = endpoints.begin(); it != endpoints.end(); ++it) {
    csr.addEndpoint(*it);
  }
  csr.wait();

  std::list<Arc::ComputingServiceType> services = csu.getServices();
  for (std::list<Arc::ComputingServiceType>::const_iterator it = services.begin();
       it != services.end(); ++it) {
    if (opt.longlist) {
      if (it != services.begin()) std::cout << std::endl;
      std::cout << *it;
      std::cout << std::flush;
    }
    else {
      std::cout << "Computing service: " << (**it).Name;
      if (!(**it).QualityLevel.empty()) {
        std::cout << " (" << (**it).QualityLevel << ")";
      }
      std::cout << std::endl;
      
      std::stringstream infostream, submissionstream;
      for (std::map<int, Arc::ComputingEndpointType>::const_iterator itCE = it->ComputingEndpoint.begin();
           itCE != it->ComputingEndpoint.end(); ++itCE) {
        if (itCE->second->Capability.count(Arc::Endpoint::GetStringForCapability(Arc::Endpoint::COMPUTINGINFO))) {
          infostream << "  " << Arc::IString("Information endpoint") << ": " << itCE->second->URLString << std::endl;
        }
        if (itCE->second->Capability.empty() ||
            itCE->second->Capability.count(Arc::Endpoint::GetStringForCapability(Arc::Endpoint::JOBSUBMIT)) ||
            itCE->second->Capability.count(Arc::Endpoint::GetStringForCapability(Arc::Endpoint::JOBCREATION)))
        {
          submissionstream << "  ";
          submissionstream << Arc::IString("Submission endpoint") << ": ";
          submissionstream << itCE->second->URLString;
          submissionstream << " (" << Arc::IString("status") << ": ";
          submissionstream << itCE->second->HealthState << ", ";
          submissionstream << Arc::IString("interface") << ": ";
          submissionstream << itCE->second->InterfaceName << ")" << std::endl;           
        }
      }
      
      std::cout << infostream.str() << submissionstream.str();
    }
  }

  bool anEndpointFailed = false;
  // Check if querying endpoint succeeded.
  Arc::EndpointStatusMap statusMap = csr.getAllStatuses();
  for (std::list<Arc::Endpoint>::const_iterator it = endpoints.begin(); it != endpoints.end(); ++it) {
    Arc::EndpointStatusMap::const_iterator itStatus = statusMap.find(*it);
    if (itStatus != statusMap.end() &&
        itStatus->second != Arc::EndpointQueryingStatus::SUCCESSFUL &&
        itStatus->second != Arc::EndpointQueryingStatus::SUSPENDED_NOTREQUIRED) {
      if (!anEndpointFailed) {
        anEndpointFailed = true;
        std::cerr << Arc::IString("ERROR: Failed to retrieve information from the following endpoints:") << std::endl;
      }
      
      std::cerr << "  " << it->URLString;
      if (!itStatus->second.getDescription().empty()) {
        std::cerr << " (" << itStatus->second.getDescription() << ")";
      }
      std::cerr << std::endl;
    }
  }
  if (anEndpointFailed) return 1;
  
  if (services.empty()) {
    std::cerr << Arc::IString("ERROR: Failed to retrieve information");
    if (!endpoints.empty()) {
      std::cerr << " " << Arc::IString("from the following endpoints:") << std::endl;
      for (std::list<Arc::Endpoint>::const_iterator it = endpoints.begin(); it != endpoints.end(); ++it) {
        std::cerr << "  " << it->URLString << std::endl;
      }
    } else {
      std::cerr << std::endl;
    }
    return 1;
  }

  return 0;
}