ATLAS Offline Software
KubernetesDriver.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 //
9 // includes
10 //
11 
13 
14 #include <fstream>
15 #include <sstream>
16 #include <TSystem.h>
18 #include <EventLoop/Job.h>
19 #include <EventLoop/ManagerData.h>
20 #include <EventLoop/MessageCheck.h>
22 #include <RootCoreUtils/Assert.h>
25 #include <RootCoreUtils/ThrowMsg.h>
26 
27 //
28 // method implementations
29 //
30 
32 
33 namespace EL
34 {
36  testInvariant () const
37  {
38  RCU_INVARIANT (this != 0);
39  }
40 
41 
42 
45  {
46  RCU_NEW_INVARIANT (this);
47  }
48 
49 
50 
52  doManagerStep (Detail::ManagerData& data) const
53  {
54  RCU_READ_INVARIANT (this);
55  using namespace msgEventLoop;
57  switch (data.step)
58  {
60  {
61  data.batchSkipReleaseSetup = true;
62  }
63  break;
64 
67  {
68  const std::string dockerImage {
69  data.options.castString(Job::optDockerImage)};
70 
71  const std::string dockerOptions {
72  data.options.castString(Job::optDockerOptions)};
73  if (!dockerOptions.empty())
74  {
75  ANA_MSG_WARNING ("you specified docker options for kubernetes driver");
76  ANA_MSG_WARNING ("this is not supported in this way");
77  ANA_MSG_WARNING ("instead you need to provide your own kubernetes config file");
78  }
79 
81  const std::string batchSetupFile {
82  data.options.castString(Job::optBatchSetupFile, "EventLoop/kubernetes_setup.yml")};
83 
84 
86  const std::string batchConfigFile {
87  data.options.castString(Job::optBatchConfigFile, "EventLoop/kubernetes_job.yml")};
88  std::string baseConfig;
89  {
90  std::ifstream file (PathResolverFindDataFile (batchConfigFile).c_str());
91  baseConfig = std::string (std::istreambuf_iterator<char>(file),
92  std::istreambuf_iterator<char>() );
93  }
94  baseConfig = RCU::substitute (baseConfig, "%%DOCKERIMAGE%%", dockerImage);
95  baseConfig = RCU::substitute (baseConfig, "%%SUBMITDIR%%", data.submitDir);
96 
97  std::ostringstream basedirName;
98  basedirName << data.submitDir << "/tmp";
99  if (!data.resubmit)
100  {
101  if (gSystem->MakeDirectory (basedirName.str().c_str()) != 0)
102  RCU_THROW_MSG ("failed to create directory " + basedirName.str());
103  }
104 
105  const std::string jobFilePath {data.submitDir + "/job.yml"};
106  {
107  bool first {true};
108  std::ofstream jobFile (jobFilePath.c_str());
109  if (!batchSetupFile.empty())
110  {
111  std::ifstream file (PathResolverFindDataFile (batchSetupFile).c_str());
112  std::string setupConfig {std::istreambuf_iterator<char>(file),
113  std::istreambuf_iterator<char>()};
114  setupConfig = RCU::substitute (setupConfig, "%%DOCKERIMAGE%%", dockerImage);
115  setupConfig = RCU::substitute (setupConfig, "%%SUBMITDIR%%", data.submitDir);
116  jobFile << setupConfig;
117  first = false;
118  }
119 
120  for (std::size_t jobIndex : data.batchJobIndices)
121  {
122  std::ostringstream dirName;
123  dirName << basedirName.str() << "/" << jobIndex;
124  if (gSystem->MakeDirectory (dirName.str().c_str()) != 0)
125  RCU_THROW_MSG ("failed to create directory " + dirName.str());
126 
127  if (first)
128  first = false;
129  else
130  jobFile << "---\n";
131 
132  std::string myConfig = baseConfig;
133  myConfig = RCU::substitute (myConfig, "%%JOBINDEX%%", std::to_string (jobIndex));
134  std::ostringstream command;
135  command << data.submitDir << "/submit/run " << jobIndex;
136  myConfig = RCU::substitute (myConfig, "%%COMMAND%%", command.str());
137 
138  jobFile << myConfig << "\n";
139  }
140  }
141 
142  std::ostringstream cmd;
143  cmd << "kubectl create -f " << jobFilePath;
144  RCU::Shell::exec (cmd.str());
145  data.submitted = true;
146  }
147  break;
148 
149  default:
150  break;
151  }
152  return ::StatusCode::SUCCESS;
153  }
154 }
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
EL::Detail::ManagerStep::batchScriptVar
@ batchScriptVar
create the variables needed for the batch-run script
EL::KubernetesDriver
a Driver to run on Kubernetes
Definition: KubernetesDriver.h:22
EL::Job::optDockerImage
static const std::string optDockerImage
this is the name of the docker image, when using docker with a supported batch driver
Definition: Job.h:525
EL::BatchDriver::doManagerStep
virtual ::StatusCode doManagerStep(Detail::ManagerData &data) const override
rerun_display.cmd
string cmd
Definition: rerun_display.py:67
Job.h
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
ShellExec.h
Assert.h
MessageCheck.h
StringUtil.h
KubernetesDriver.h
EL::KubernetesDriver::KubernetesDriver
KubernetesDriver()
effects: standard default constructor guarantee: strong failures: low level errors I
EL::Detail::ManagerStep::doResubmit
@ doResubmit
call the actual doResubmit method
ClassImp
ClassImp(EL::KubernetesDriver) namespace EL
Definition: KubernetesDriver.cxx:31
EL::KubernetesDriver::testInvariant
void testInvariant() const
effects: test the invariant of this object guarantee: no-fail
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ManagerData.h
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
file
TFile * file
Definition: tile_monitor.h:29
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
EL::Job::optDockerOptions
static const std::string optDockerOptions
any extra options we may want to pass to docker
Definition: Job.h:528
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
StatusCode.h
PathResolver.h
EL::Job::optBatchConfigFile
static const std::string optBatchConfigFile
the job submission configuration file (used by some drivers that need more complex configuration)
Definition: Job.h:532
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
ThrowMsg.h
PathResolverFindDataFile
std::string PathResolverFindDataFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:379
RCU::substitute
std::string substitute(const std::string &str, const std::string &pattern, const std::string &with)
effects: substitute all occurences of "pattern" with "with" in the string "str" returns: the substitu...
Definition: StringUtil.cxx:24
EL::KubernetesDriver::doManagerStep
virtual ::StatusCode doManagerStep(Detail::ManagerData &data) const override
RCU::Shell::exec
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition: ShellExec.cxx:29
DeMoScan.first
bool first
Definition: DeMoScan.py:536
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
EL::Detail::ManagerStep::submitJob
@ submitJob
do the actual job submission
EL::Job::optBatchSetupFile
static const std::string optBatchSetupFile
the job submission setup file.
Definition: Job.h:537
get_generator_info.command
string command
Definition: get_generator_info.py:38
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233