ATLAS Offline Software
Loading...
Searching...
No Matches
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>

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 30 of file KubernetesDriver.cxx.

33{
34 void KubernetesDriver ::
35 testInvariant () const
36 {
37 RCU_INVARIANT (this != 0);
38 }
39
40
41
42 KubernetesDriver ::
43 KubernetesDriver ()
44 {
45 RCU_NEW_INVARIANT (this);
46 }
47
48
49
50 ::StatusCode KubernetesDriver ::
51 doManagerStep (Detail::ManagerData& data) const
52 {
53 RCU_READ_INVARIANT (this);
54 using namespace msgEventLoop;
55 ANA_CHECK (BatchDriver::doManagerStep (data));
56 switch (data.step)
57 {
58 case Detail::ManagerStep::batchScriptVar:
59 {
60 data.batchSkipReleaseSetup = true;
61 }
62 break;
63
64 case Detail::ManagerStep::submitJob:
65 case Detail::ManagerStep::doResubmit:
66 {
67 const std::string dockerImage {
68 data.options.castString(Job::optDockerImage)};
69
70 const std::string dockerOptions {
71 data.options.castString(Job::optDockerOptions)};
72 if (!dockerOptions.empty())
73 {
74 ANA_MSG_WARNING ("you specified docker options for kubernetes driver");
75 ANA_MSG_WARNING ("this is not supported in this way");
76 ANA_MSG_WARNING ("instead you need to provide your own kubernetes config file");
77 }
78
80 const std::string batchSetupFile {
81 data.options.castString(Job::optBatchSetupFile, "EventLoop/kubernetes_setup.yml")};
82
83
85 const std::string batchConfigFile {
86 data.options.castString(Job::optBatchConfigFile, "EventLoop/kubernetes_job.yml")};
87 std::string baseConfig;
88 {
89 std::ifstream file (PathResolverFindDataFile (batchConfigFile).c_str());
90 baseConfig = std::string (std::istreambuf_iterator<char>(file),
91 std::istreambuf_iterator<char>() );
92 }
93 baseConfig = RCU::substitute (baseConfig, "%%DOCKERIMAGE%%", dockerImage);
94 baseConfig = RCU::substitute (baseConfig, "%%SUBMITDIR%%", data.submitDir);
95
96 std::ostringstream basedirName;
97 basedirName << data.submitDir << "/tmp";
98 if (!data.resubmit)
99 {
100 if (gSystem->MakeDirectory (basedirName.str().c_str()) != 0)
101 {
102 ANA_MSG_ERROR ("failed to create directory " << basedirName.str());
103 return StatusCode::FAILURE;
104 }
105 }
106
107 const std::string jobFilePath {data.submitDir + "/job.yml"};
108 {
109 bool first {true};
110 std::ofstream jobFile (jobFilePath.c_str());
111 if (!batchSetupFile.empty())
112 {
113 std::ifstream file (PathResolverFindDataFile (batchSetupFile).c_str());
114 std::string setupConfig {std::istreambuf_iterator<char>(file),
115 std::istreambuf_iterator<char>()};
116 setupConfig = RCU::substitute (setupConfig, "%%DOCKERIMAGE%%", dockerImage);
117 setupConfig = RCU::substitute (setupConfig, "%%SUBMITDIR%%", data.submitDir);
118 jobFile << setupConfig;
119 first = false;
120 }
121
122 for (std::size_t jobIndex : data.batchJobIndices)
123 {
124 std::ostringstream dirName;
125 dirName << basedirName.str() << "/" << jobIndex;
126 if (gSystem->MakeDirectory (dirName.str().c_str()) != 0)
127 {
128 ANA_MSG_ERROR ("failed to create directory " << dirName.str());
129 return StatusCode::FAILURE;
130 }
131
132 if (first)
133 first = false;
134 else
135 jobFile << "---\n";
136
137 std::string myConfig = baseConfig;
138 myConfig = RCU::substitute (myConfig, "%%JOBINDEX%%", std::to_string (jobIndex));
139 std::ostringstream command;
140 command << data.submitDir << "/submit/run " << jobIndex;
141 myConfig = RCU::substitute (myConfig, "%%COMMAND%%", command.str());
142
143 jobFile << myConfig << "\n";
144 }
145 }
146
147 std::ostringstream cmd;
148 cmd << "kubectl create -f " << jobFilePath;
149 RCU::Shell::exec (cmd.str());
150 data.submitted = true;
151 }
152 break;
153
154 default:
155 break;
156 }
157 return ::StatusCode::SUCCESS;
158 }
159}
#define RCU_INVARIANT(x)
Definition Assert.h:189
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
std::string PathResolverFindDataFile(const std::string &logical_file_name)
bool first
Definition DeMoScan.py:534
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition ShellExec.cxx:27
std::string substitute(std::string_view str, std::string_view pattern, std::string_view with)
effects: substitute all occurences of "pattern" with "with" in the string "str" returns: the substitu...
TFile * file