ATLAS Offline Software
Loading...
Searching...
No Matches
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>
25
26//
27// method implementations
28//
29
31
32namespace EL
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;
56 switch (data.step)
57 {
59 {
60 data.batchSkipReleaseSetup = true;
61 }
62 break;
63
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
ClassImp(EL::KubernetesDriver) namespace EL
std::string PathResolverFindDataFile(const std::string &logical_file_name)
virtual::StatusCode doManagerStep(Detail::ManagerData &data) const override
static const std::string optDockerOptions
any extra options we may want to pass to docker
Definition Job.h:523
static const std::string optBatchConfigFile
the job submission configuration file (used by some drivers that need more complex configuration)
Definition Job.h:527
static const std::string optDockerImage
this is the name of the docker image, when using docker with a supported batch driver
Definition Job.h:520
static const std::string optBatchSetupFile
the job submission setup file.
Definition Job.h:532
a Driver to run on Kubernetes
@ doResubmit
call the actual doResubmit method
@ submitJob
do the actual job submission
Definition ManagerStep.h:92
@ batchScriptVar
create the variables needed for the batch-run script
Definition ManagerStep.h:83
This module defines the arguments passed from the BATCH driver to the BATCH worker.
::StatusCode StatusCode
StatusCode definition for legacy code.
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