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>
26
27//
28// method implementations
29//
30
32
33namespace EL
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;
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}
#define RCU_INVARIANT(x)
Definition Assert.h:201
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_READ_INVARIANT(x)
Definition Assert.h:229
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
ClassImp(EL::KubernetesDriver) namespace EL
std::string PathResolverFindDataFile(const std::string &logical_file_name)
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
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:544
static const std::string optBatchConfigFile
the job submission configuration file (used by some drivers that need more complex configuration)
Definition Job.h:548
static const std::string optDockerImage
this is the name of the docker image, when using docker with a supported batch driver
Definition Job.h:541
static const std::string optBatchSetupFile
the job submission setup file.
Definition Job.h:553
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.
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
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...
TFile * file