ATLAS Offline Software
Functions
KubernetesDriver.cxx File Reference
#include <EventLoop/KubernetesDriver.h>
#include <fstream>
#include <sstream>
#include <TSystem.h>
#include <AsgMessaging/StatusCode.h>
#include <EventLoop/Job.h>
#include <EventLoop/ManagerData.h>
#include <EventLoop/MessageCheck.h>
#include <PathResolver/PathResolver.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/ShellExec.h>
#include <RootCoreUtils/StringUtil.h>
#include <RootCoreUtils/ThrowMsg.h>

Go to the source code of this file.

Functions

 ClassImp (EL::KubernetesDriver) namespace EL
 

Function Documentation

◆ ClassImp()

ClassImp ( EL::KubernetesDriver  )
Author
Nils Krumnack

the setup file we use as a template

the config file we use as a template

Definition at line 31 of file KubernetesDriver.cxx.

34 {
35  void KubernetesDriver ::
36  testInvariant () const
37  {
38  RCU_INVARIANT (this != 0);
39  }
40 
41 
42 
43  KubernetesDriver ::
44  KubernetesDriver ()
45  {
46  RCU_NEW_INVARIANT (this);
47  }
48 
49 
50 
51  ::StatusCode KubernetesDriver ::
52  doManagerStep (Detail::ManagerData& data) const
53  {
54  RCU_READ_INVARIANT (this);
55  using namespace msgEventLoop;
56  ANA_CHECK (BatchDriver::doManagerStep (data));
57  switch (data.step)
58  {
59  case Detail::ManagerStep::batchScriptVar:
60  {
61  data.batchSkipReleaseSetup = true;
62  }
63  break;
64 
65  case Detail::ManagerStep::submitJob:
66  case Detail::ManagerStep::doResubmit:
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
rerun_display.cmd
string cmd
Definition: rerun_display.py:67
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
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
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
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
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
get_generator_info.command
string command
Definition: get_generator_info.py:38
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233