Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyManager.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: DobbyManager.h
21  *
22  */
23 #ifndef DOBBYMANAGER_H
24 #define DOBBYMANAGER_H
25 
26 #include "IDobbyUtils.h"
27 #include "IDobbyIPCUtils.h"
28 #include "DobbyConfig.h"
29 #include "IDobbyRdkPlugin.h"
30 #include "IDobbyRdkLoggingPlugin.h"
31 #include "ContainerId.h"
32 #include "DobbyLogger.h"
33 #include "DobbyRunC.h"
34 #include <IIpcService.h>
35 
36 #include <pthread.h>
37 
38 #include <map>
39 #include <list>
40 #include <cstdint>
41 #include <mutex>
42 #include <thread>
43 #include <string>
44 #include <memory>
45 #include <future>
46 #include <functional>
47 #include <netinet/in.h>
48 #include <semaphore.h>
49 
50 #if defined(RDK)
51 # include <json/json.h>
52 #else
53 # include <jsoncpp/json.h>
54 #endif
55 
56 class IDobbyEnv;
57 class IDobbySettings;
58 class DobbyStartState;
60 class DobbyConfig;
61 
62 class DobbyContainer;
63 
64 
65 // -----------------------------------------------------------------------------
77 {
78 public:
79  typedef std::function<void(int32_t cd, const ContainerId& id)> ContainerStartedFunc;
80  typedef std::function<void(int32_t cd, const ContainerId& id, int32_t status)> ContainerStoppedFunc;
81  typedef std::function<void(int32_t cd, const ContainerId& id)> ContainerHibernatedFunc;
82 
83 public:
84  DobbyManager(const std::shared_ptr<IDobbyEnv>& env,
85  const std::shared_ptr<IDobbyUtils>& utils,
86  const std::shared_ptr<IDobbyIPCUtils>& ipcUtils,
87  const std::shared_ptr<const IDobbySettings>& settings,
88  const ContainerStartedFunc& containerStartedCb,
89  const ContainerStoppedFunc& containerStoppedCb,
90  const ContainerHibernatedFunc& containerHibernatedCb,
91  const ContainerHibernatedFunc& containerAwokenCb);
92  ~DobbyManager();
93 
94 private:
95  void setupSystem();
96  void setupWorkspace(const std::shared_ptr<IDobbyEnv>& env);
97 
98  void cleanupContainers();
99  bool cleanupContainer(const DobbyRunC::ContainerListItem& container);
101 public:
102 #if defined(LEGACY_COMPONENTS)
103  int32_t startContainerFromSpec(const ContainerId& id,
104  const std::string& jsonSpec,
105  const std::list<int>& files,
106  const std::string& command,
107  const std::string& displaySocket,
108  const std::vector<std::string>& envVars);
109 #endif //defined(LEGACY_COMPONENTS)
110 
111  int32_t startContainerFromBundle(const ContainerId& id,
112  const std::string& bundlePath,
113  const std::list<int>& files,
114  const std::string& command,
115  const std::string& displaySocket,
116  const std::vector<std::string>& envVars);
117 
118  bool stopContainer(int32_t cd, bool withPrejudice);
119 
120  bool pauseContainer(int32_t cd);
121  bool resumeContainer(int32_t cd);
122  bool hibernateContainer(int32_t cd, const std::string& options);
123  bool wakeupContainer(int32_t cd);
124 
125  bool addMount(int32_t cd,
126  const std::string& source,
127  const std::string& destination,
128  const std::vector<std::string>& mountFlags,
129  const std::string& mountData);
130 
131  bool removeMount(int32_t cd, const std::string& source);
132 
133  bool execInContainer(int32_t cd,
134  const std::string& options,
135  const std::string& command);
136 
137  std::list<std::pair<int32_t, ContainerId>> listContainers() const;
138 
139  int32_t stateOfContainer(int32_t cd) const;
140 
141  std::string statsOfContainer(int32_t cd) const;
142 
143 public:
144  std::string ociConfigOfContainer(int32_t cd) const;
145 
146 #if defined(LEGACY_COMPONENTS)
147 public:
148  std::string specOfContainer(int32_t cd) const;
149  bool createBundle(const ContainerId& id, const std::string& jsonSpec);
150 #endif //defined(LEGACY_COMPONENTS)
151 
152 private:
153  void handleContainerTerminate(const ContainerId &id, const std::unique_ptr<DobbyContainer>& container, const int status);
154  void onChildExit();
155 
156 private:
157  std::shared_ptr<IDobbyRdkLoggingPlugin>
158  GetContainerLogger(const std::unique_ptr<DobbyContainer> &container);
159 
160  bool createAndStart(const ContainerId &id,
161  const std::unique_ptr<DobbyContainer> &container,
162  const std::list<int> &files);
163 
164  bool customiseConfig(const std::shared_ptr<DobbyConfig> &config,
165  const std::string &command,
166  const std::string &displaySocket,
167  const std::vector<std::string> &envVars);
168 
169  bool createAndStartContainer(const ContainerId& id,
170  const std::unique_ptr<DobbyContainer>& container,
171  const std::list<int>& files);
172 
173  bool restartContainer(const ContainerId& id,
174  const std::unique_ptr<DobbyContainer>& container);
175 
176 private:
177  ContainerStartedFunc mContainerStartedCb;
178  ContainerStoppedFunc mContainerStoppedCb;
179  ContainerHibernatedFunc mContainerHibernatedCb;
180  ContainerHibernatedFunc mContainerAwokenCb;
181 
182 private:
183  mutable std::mutex mLock;
184  std::map<ContainerId, std::unique_ptr<DobbyContainer>> mContainers;
185  std::multimap<ContainerId, pid_t> mContainerExecPids;
186 
187 private:
188  bool onPostInstallationHook(const std::unique_ptr<DobbyContainer> &container);
189  bool onPreCreationHook(const std::unique_ptr<DobbyContainer> &container);
190  bool onPostHaltHook(const std::unique_ptr<DobbyContainer> &container);
191 
192 #if defined(LEGACY_COMPONENTS)
193 private:
194  bool onPostConstructionHook(const ContainerId& id,
195  const std::shared_ptr<DobbyStartState>& startState,
196  const std::unique_ptr<DobbyContainer>& container);
197  bool onPreStartHook(const ContainerId& id,
198  const std::unique_ptr<DobbyContainer>& container);
199  bool onPostStartHook(const ContainerId& id,
200  const std::unique_ptr<DobbyContainer>& container);
201  bool onPostStopHook(const ContainerId& id,
202  const std::unique_ptr<DobbyContainer>& container);
203  bool onPreDestructionHook(const ContainerId& id,
204  const std::unique_ptr<DobbyContainer>& container);
205 
206 private:
207  enum class HookType {
208  PostConstruction, PreStart, PostStart, PostStop, PreDestruction };
209 #endif //defined(LEGACY_COMPONENTS)
210 
211 private:
212  void startRuncMonitorThread();
213  void stopRuncMonitorThread();
214  void runcMonitorThread();
215 
217 
218  bool shouldEnableSTrace(const std::shared_ptr<DobbyConfig> &config) const;
219 private:
220  const std::shared_ptr<IDobbyEnv> mEnvironment;
221  const std::shared_ptr<IDobbyUtils> mUtilities;
222  const std::shared_ptr<IDobbyIPCUtils> mIPCUtilities;
223  const std::shared_ptr<const IDobbySettings> mSettings;
224 
225 private:
226  std::unique_ptr<DobbyLogger> mLogger;
227  std::unique_ptr<DobbyRunC> mRunc;
228 
229 private:
230  sem_t mRuncMonitorThreadStartedSem;
231  std::thread mRuncMonitorThread;
232  std::atomic<bool> mRuncMonitorTerminate;
233  int mCleanupTaskTimerId;
234 
235 #if defined(LEGACY_COMPONENTS)
236 private:
237  std::unique_ptr<DobbyLegacyPluginManager> mLegacyPlugins;
238 #endif // defined(LEGACY_COMPONENTS)
239 
240 };
241 
242 
243 #endif // !defined(DOBBYMANAGER_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
Interface that configuration file parser classes have to implement.
Definition: DobbyConfig.h:66
Wrapper object used to store container resources.
Definition: DobbyContainer.h:59
Class that manages all the plugin hook libraries.
Definition: DobbyLegacyPluginManager.h:65
The main object which starts / stops / manages the containers.
Definition: DobbyManager.h:77
bool restartContainer(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container)
Attempts to restart the container.
Definition: DobbyManager.cpp:1243
void cleanupContainersShutdown()
Gracefully stops and cleans up any running containers. Will emit the container stop event when a cont...
Definition: DobbyManager.cpp:476
bool execInContainer(int32_t cd, const std::string &options, const std::string &command)
Executes a command in a running container.
Definition: DobbyManager.cpp:2144
bool resumeContainer(int32_t cd)
Thaws a frozen container.
Definition: DobbyManager.cpp:1491
void stopRuncMonitorThread()
Stops the monitor thread and cleans up it's resources.
Definition: DobbyManager.cpp:3125
void cleanupContainers()
Gets a list of running containers and tries to kill and delete them.
Definition: DobbyManager.cpp:375
std::string statsOfContainer(int32_t cd) const
Gets the stats for the container.
Definition: DobbyManager.cpp:2337
bool addMount(int32_t cd, const std::string &source, const std::string &destination, const std::vector< std::string > &mountFlags, const std::string &mountData)
adds a mount to a running container
Definition: DobbyManager.cpp:1761
void runcMonitorThread()
Thread function that monitors for any SIGCHILD signals and if detected loops through the running cont...
Definition: DobbyManager.cpp:3164
bool wakeupContainer(int32_t cd)
Wakeup a checkpointed container from existing dump.
Definition: DobbyManager.cpp:1664
int32_t startContainerFromBundle(const ContainerId &id, const std::string &bundlePath, const std::list< int > &files, const std::string &command, const std::string &displaySocket, const std::vector< std::string > &envVars)
Where the magic begins ... attempts to create a container from an OCI bundle*.
Definition: DobbyManager.cpp:993
bool cleanupContainer(const DobbyRunC::ContainerListItem &container)
Cleans up a container that is in an unknown state - used at Dobby startup to ensure the box is in a c...
Definition: DobbyManager.cpp:249
bool removeMount(int32_t cd, const std::string &source)
removes a mount from a running container
Definition: DobbyManager.cpp:2043
std::string ociConfigOfContainer(int32_t cd) const
Debugging method to allow you to retrieve the OCI config.json spec used to create the container.
Definition: DobbyManager.cpp:2408
bool createAndStartContainer(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const std::list< int > &files)
Creates and attempts to start the container.
Definition: DobbyManager.cpp:694
bool onPostHaltHook(const std::unique_ptr< DobbyContainer > &container)
Called at the post-halt stage of container startup.
Definition: DobbyManager.cpp:2624
void setupSystem()
Configures the linux system for enabling features needed for runc.
Definition: DobbyManager.cpp:153
bool createAndStart(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const std::list< int > &files)
Create and start a container. Set up and capture logs from all container hooks if an RDK logging plug...
Definition: DobbyManager.cpp:549
std::list< std::pair< int32_t, ContainerId > > listContainers() const
Returns a list of all the containers.
Definition: DobbyManager.cpp:2222
void setupWorkspace(const std::shared_ptr< IDobbyEnv > &env)
Configures the workspace directory.
Definition: DobbyManager.cpp:208
bool customiseConfig(const std::shared_ptr< DobbyConfig > &config, const std::string &command, const std::string &displaySocket, const std::vector< std::string > &envVars)
Updates the container config with custom options provided by the start command.
Definition: DobbyManager.cpp:634
bool onPreCreationHook(const std::unique_ptr< DobbyContainer > &container)
Called at the pre-create stage of container startup.
Definition: DobbyManager.cpp:2587
std::shared_ptr< IDobbyRdkLoggingPlugin > GetContainerLogger(const std::unique_ptr< DobbyContainer > &container)
Get the instance of the logging plugin for the current container (if one is loaded)
Definition: DobbyManager.cpp:522
void onChildExit()
Called when we detect a child process has terminated.
Definition: DobbyManager.cpp:2961
bool stopContainer(int32_t cd, bool withPrejudice)
Stops a running container.
Definition: DobbyManager.cpp:1310
int32_t stateOfContainer(int32_t cd) const
Returns the state of a given container.
Definition: DobbyManager.cpp:2247
bool onPostInstallationHook(const std::unique_ptr< DobbyContainer > &container)
Called at the post-installation stage of container startup.
Definition: DobbyManager.cpp:2540
bool hibernateContainer(int32_t cd, const std::string &options)
Dumps a running container's processes.
Definition: DobbyManager.cpp:1545
void handleContainerTerminate(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const int status)
Perform all the necessary cleanup and run plugins required when a container has terminated.
Definition: DobbyManager.cpp:2864
void startRuncMonitorThread()
Starts a thread that monitors for SIGCHILD signals.
Definition: DobbyManager.cpp:3103
bool pauseContainer(int32_t cd)
Freezes a running container.
Definition: DobbyManager.cpp:1430
bool invalidContainerCleanupTask()
Task that will try and cleanup invalid/unknown state containers periodically - if the container can b...
Definition: DobbyManager.cpp:3220
Stores the start state of the container.
Definition: DobbyStartState.h:49
Interface that exports the environment of the daemon to plugins.
Definition: IDobbyEnv.h:46
Interface provided to the library at startup, contains the configuration options for Dobby.
Definition: IDobbySettings.h:50
Definition: DobbyRunC.h:67