Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyRunC.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2016 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: DobbyRunc.h
21  *
22  */
23 #ifndef DOBBYRUNC_H
24 #define DOBBYRUNC_H
25 
26 #include "IDobbyUtils.h"
27 #include "DobbyLogger.h"
28 #include "ContainerId.h"
29 
30 #include <json/json.h>
31 
32 #include <memory>
33 #include <mutex>
34 #include <list>
35 
36 class DobbyBundle;
37 class IDobbyStream;
38 
39 // -----------------------------------------------------------------------------
48 class DobbyRunC
49 {
50 public:
51  explicit DobbyRunC(const std::shared_ptr<IDobbyUtils> &utils,
52  const std::shared_ptr<const IDobbySettings> &settings);
53  ~DobbyRunC();
54 
55 public:
56  enum class ContainerStatus
57  {
58  Unknown,
59  Created,
60  Running,
61  Pausing,
62  Paused,
63  Stopped
64  };
65 
67  {
68  ContainerId id;
69  pid_t pid;
70  std::string bundlePath;
71  ContainerStatus status;
72  };
73 
74 public:
75  std::pair<pid_t, pid_t> create(const ContainerId &id,
76  const std::shared_ptr<const DobbyBundle> &bundle,
77  const std::shared_ptr<const IDobbyStream> &console,
78  const std::list<int> &files = std::list<int>(),
79  const std::string& customConfigPath = "") const;
80 
81  bool destroy(const ContainerId &id, const std::shared_ptr<const IDobbyStream> &console, bool force = false) const;
82  bool start(const ContainerId &id, const std::shared_ptr<const IDobbyStream> &console) const;
83  bool killCont(const ContainerId &id, int signal, bool all = false) const;
84  bool pause(const ContainerId &id) const;
85  bool resume(const ContainerId &id) const;
86  std::pair<pid_t, pid_t> exec(const ContainerId &id,
87  const std::string &options,
88  const std::string &command) const;
89 
90  ContainerStatus state(const ContainerId &id) const;
91  std::list<ContainerListItem> list() const;
92 
93 public:
94  pid_t run(const ContainerId &id,
95  const std::shared_ptr<const DobbyBundle> &bundle,
96  const std::shared_ptr<const IDobbyStream> &console,
97  const std::list<int> &files = std::list<int>()) const;
98 
99 public:
100  const std::string getWorkingDir() const;
101 
102 private:
103  pid_t forkExecRunC(const std::vector<const char *> &args,
104  const std::initializer_list<const char *> &envs,
105  const std::list<int> &files = std::list<int>(),
106  const std::shared_ptr<const IDobbyStream> &stdoutStream = nullptr,
107  const std::shared_ptr<const IDobbyStream> &stderrStream = nullptr) const;
108 
109  pid_t readPidFile(const std::string pidFilePath) const;
110 
111  ContainerStatus getContainerStatusFromJson(const Json::Value &state) const;
112 
113 private:
114  const std::shared_ptr<IDobbyUtils> mUtilities;
115  const std::string mRuncPath;
116 
117  const std::string mWorkingDir;
118  const std::string mLogDir;
119  const std::string mLogFilePath;
120  const std::string mConsoleSocket;
121 };
122 
123 #endif // !defined(DOBBYRUNC_H)
A wrapper around a std::string, used to add some type definition to to an id and also to sanity check...
Definition: ContainerId.h:41
Simple class that just creates a subdir in the bundles directory.
Definition: DobbyBundle.h:46
Wrapper around the runc command line app.
Definition: DobbyRunC.h:49
bool destroy(const ContainerId &id, const std::shared_ptr< const IDobbyStream > &console, bool force=false) const
Runs the runc command line tool with the 'delete' command.
Definition: DobbyRunC.cpp:720
bool killCont(const ContainerId &id, int signal, bool all=false) const
Runs the runc command line tool with the 'kill' command.
Definition: DobbyRunC.cpp:395
std::list< ContainerListItem > list() const
Runs the runc command line tool with the 'list' command.
Definition: DobbyRunC.cpp:930
bool start(const ContainerId &id, const std::shared_ptr< const IDobbyStream > &console) const
Starts a container created with create command.
Definition: DobbyRunC.cpp:343
pid_t readPidFile(const std::string pidFilePath) const
Reads file with pid of the container and converts it into pid_t type variable.
Definition: DobbyRunC.cpp:1295
ContainerStatus state(const ContainerId &id) const
Runs the runc command line tool with the 'state' command.
Definition: DobbyRunC.cpp:839
bool pause(const ContainerId &id) const
Runs the runc command line tool with the 'pause' command.
Definition: DobbyRunC.cpp:504
pid_t forkExecRunC(const std::vector< const char * > &args, const std::initializer_list< const char * > &envs, const std::list< int > &files=std::list< int >(), const std::shared_ptr< const IDobbyStream > &stdoutStream=nullptr, const std::shared_ptr< const IDobbyStream > &stderrStream=nullptr) const
Performs a fork then exec of the runC binary with the supplied args.
Definition: DobbyRunC.cpp:1083
std::pair< pid_t, pid_t > create(const ContainerId &id, const std::shared_ptr< const DobbyBundle > &bundle, const std::shared_ptr< const IDobbyStream > &console, const std::list< int > &files=std::list< int >(), const std::string &customConfigPath="") const
Creates the container but doesn't start the init process.
Definition: DobbyRunC.cpp:140
bool resume(const ContainerId &id) const
Runs the runc command line tool with the 'resume' command.
Definition: DobbyRunC.cpp:549
std::pair< pid_t, pid_t > exec(const ContainerId &id, const std::string &options, const std::string &command) const
Runs the crun command line tool with the 'exec' command.
Definition: DobbyRunC.cpp:604
ContainerStatus getContainerStatusFromJson(const Json::Value &state) const
Gets the container status from the json object.
Definition: DobbyRunC.cpp:798
pid_t run(const ContainerId &id, const std::shared_ptr< const DobbyBundle > &bundle, const std::shared_ptr< const IDobbyStream > &console, const std::list< int > &files=std::list< int >()) const
Runs the runc command line tool with the 'run' command.
Definition: DobbyRunC.cpp:94
Interface for all character streams used in the daemon.
Definition: DobbyStream.h:41
Definition: DobbyRunC.h:67