Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
DobbyProxy.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 * DobbyProxy.h
21 * Author:
22 *
23 */
24#ifndef DOBBYPROXY_H
25#define DOBBYPROXY_H
26
27
28#if defined(DOBBY_BUILD)
29#include <Dobby/IDobbyProxy.h>
30#include <IIpcService.h>
31#include <IDGenerator.h>
32#else
33#include <Dobby/Public/Dobby/IDobbyProxy.h>
34#include <Dobby/IpcService/IIpcService.h>
35#include <Dobby/IDGenerator.h>
36#endif
37
38#include <string>
39#include <memory>
40#include <thread>
41#include <mutex>
42#include <condition_variable>
43#include <deque>
44
45
46// -----------------------------------------------------------------------------
56class DobbyProxy : public IDobbyProxy
57{
58public:
59 DobbyProxy(const std::shared_ptr<AI_IPC::IIpcService>& ipcService,
60 const std::string& serviceName,
61 const std::string& objectName);
62 ~DobbyProxy() final;
63
64public:
65 // Admin interface
66 bool shutdown() const override;
67
68 bool ping() const override;
69
70 bool isAlive(const std::chrono::milliseconds& timeout) const override;
71
72 bool setLogMethod(uint32_t method, int pipeFd) const override;
73
74 bool setLogLevel(int level) const override;
75
76 bool setAIDbusAddress(bool privateBus,
77 const std::string& address) const override;
78
79public:
80 // Control interface
81 int32_t startContainerFromSpec(const std::string& id,
82 const std::string& jsonSpec,
83 const std::list<int>& files,
84 const std::string& command = "",
85 const std::string& displaySocket = "",
86 const std::vector<std::string>& envVars = std::vector<std::string>()) const override;
87
88
89 int32_t startContainerFromBundle(const std::string& id,
90 const std::string& bundlePath,
91 const std::list<int>& files,
92 const std::string& command = "",
93 const std::string& displaySocket = "",
94 const std::vector<std::string>& envVars = std::vector<std::string>()) const override;
95
96 bool stopContainer(int32_t cd, bool withPrejudice) const override;
97
98 bool pauseContainer(int32_t cd) const override;
99
100 bool resumeContainer(int32_t cd) const override;
101
102 bool hibernateContainer(int32_t descriptor, const std::string& options) const override;
103
104 bool wakeupContainer(int32_t descriptor) const override;
105
106 bool addContainerMount(int32_t descriptor,
107 const std::string& source,
108 const std::string& destination,
109 const std::vector<std::string>& mountFlags,
110 const std::string& mountData) const override;
111
112 bool addAnnotation(int32_t cd,
113 const std::string& key,
114 const std::string& value) const override;
115
116 bool removeAnnotation(int32_t cd,
117 const std::string& key) const override;
118
119 bool removeContainerMount(int32_t descriptor, const std::string& source) const override;
120
121 bool execInContainer(int32_t cd,
122 const std::string& options,
123 const std::string& command) const override;
124
125 int getContainerState(int32_t cd) const override;
126
127 int registerListener(const StateChangeListener &listener, const void* cbParams) override;
128
129 void unregisterListener(int tag) override;
130
131 std::string getContainerInfo(int32_t descriptor) const override;
132
133 std::list<std::pair<int32_t, std::string>> listContainers() const override;
134
135#if (AI_BUILD_TYPE == AI_DEBUG)
136
137public:
138 // Debug interface
139 bool createBundle(const std::string& id,
140 const std::string& jsonSpec) const override;
141
142 std::string getSpec(int32_t descriptor) const override;
143
144 std::string getOCIConfig(int32_t descriptor) const override;
145
146#if (AI_ENABLE_TRACING)
147 bool startInProcessTracing(int traceFileFd,
148 const std::string &categoryFilter) const override;
149
150 bool stopInProcessTracing() const override;
151#endif // (AI_ENABLE_TRACING)
152
153#endif // (AI_BUILD_TYPE == AI_DEBUG)
154
155private:
156 void onContainerStartedEvent(const AI_IPC::VariantList& args);
157 void onContainerStoppedEvent(const AI_IPC::VariantList& args);
158 void onContainerHibernatedEvent(const AI_IPC::VariantList& args);
159 void onContainerAwokenEvent(const AI_IPC::VariantList& args);
160
161private:
162 bool invokeMethod(const char *interface_, const char *method_,
163 const AI_IPC::VariantList& params_,
164 AI_IPC::VariantList& returns_) const;
165
167
168private:
169 const std::shared_ptr<AI_IPC::IIpcService> mIpcService;
170 const std::string mServiceName;
171 const std::string mObjectName;
172
173private:
174 std::string mContainerStartedSignal;
175 std::string mContainerStoppedSignal;
176
177private:
178 std::thread mStateChangeThread;
179 std::mutex mStateChangeLock;
180 std::condition_variable mStateChangeCond;
181
183 {
184 enum Type { Terminate, ContainerStarted, ContainerStopped, ContainerHibernated, ContainerAwoken };
185
186 explicit StateChangeEvent(Type type_)
187 : type(type_), descriptor(-1)
188 { }
189
190 StateChangeEvent(Type type_, int32_t descriptor_, const std::string& name_)
191 : type(type_), descriptor(descriptor_), name(name_)
192 { }
193
194 Type type;
195 int32_t descriptor;
196 std::string name;
197 };
198
199 std::deque<StateChangeEvent> mStateChangeQueue;
200
201 std::mutex mListenersLock;
202 AICommon::IDGenerator<8> mListenerIdGen;
203 std::map<int, std::pair<StateChangeListener, const void*>> mListeners;
204
205};
206
207
208
209#endif // !defined(DOBBYPROXY_H)
Class used to generate unique numbers.
Definition IDGenerator.h:62
Wrapper around an IpcService object that provides simpler method calls to the dobby daemon.
Definition DobbyProxy.h:57
void unregisterListener(int tag) override
Unregisters a listener previously registered.
Definition DobbyProxy.cpp:184
bool addAnnotation(int32_t cd, const std::string &key, const std::string &value) const override
adds a key value pair to the container annotation
Definition DobbyProxy.cpp:940
bool execInContainer(int32_t cd, const std::string &options, const std::string &command) const override
Executes a command in the given container.
Definition DobbyProxy.cpp:1039
int registerListener(const StateChangeListener &listener, const void *cbParams) override
Installs a callback 'listener' to be notified of changes to the state of the containers.
Definition DobbyProxy.cpp:159
void containerStateChangeThread()
Thread function that receives notifications on container state changes and then calls the install han...
Definition DobbyProxy.cpp:1448
void onContainerStartedEvent(const AI_IPC::VariantList &args)
Called when a org.rdk.dobby.ctrl1.Started event is received from the Dobby 'hypervisor' daemon.
Definition DobbyProxy.cpp:209
bool removeContainerMount(int32_t descriptor, const std::string &source) const override
unmounts a directory/device inside the container
Definition DobbyProxy.cpp:1004
int getContainerState(int32_t cd) const override
Returns the current state of a container.
Definition DobbyProxy.cpp:1079
std::string getContainerInfo(int32_t descriptor) const override
Gets the stats / info for the given container.
Definition DobbyProxy.cpp:1175
bool createBundle(const std::string &id, const std::string &jsonSpec) const override
Debugging utility that can be used to create a bundle based on a dobby spec file.
Definition DobbyProxy.cpp:1269
int32_t startContainerFromBundle(const std::string &id, const std::string &bundlePath, const std::list< int > &files, const std::string &command="", const std::string &displaySocket="", const std::vector< std::string > &envVars=std::vector< std::string >()) const override
Starts a container with the given id, bundle path and the list of files.
Definition DobbyProxy.cpp:677
bool addContainerMount(int32_t descriptor, const std::string &source, const std::string &destination, const std::vector< std::string > &mountFlags, const std::string &mountData) const override
mounts a new host directory/device inside container
Definition DobbyProxy.cpp:907
std::string getSpec(int32_t descriptor) const override
Debugging utility to retrieve the original spec file for a running container (i.e....
Definition DobbyProxy.cpp:1307
bool isAlive(const std::chrono::milliseconds &timeout) const override
Checks if the daemon is alive.
Definition DobbyProxy.cpp:381
bool hibernateContainer(int32_t descriptor, const std::string &options) const override
Checkpoints the container with the descriptor (container integer id)
Definition DobbyProxy.cpp:834
bool ping() const override
Asks the daemon to send back a pong message.
Definition DobbyProxy.cpp:444
bool pauseContainer(int32_t cd) const override
Pauses the container with the descriptor (container integer id)
Definition DobbyProxy.cpp:768
~DobbyProxy() final
Unregisters the signal listeners and flushes the ipc connection.
Definition DobbyProxy.cpp:119
std::list< std::pair< int32_t, std::string > > listContainers() const override
Returns a list of containers.
Definition DobbyProxy.cpp:1213
bool invokeMethod(const char *interface_, const char *method_, const AI_IPC::VariantList &params_, AI_IPC::VariantList &returns_) const
Invokes a dbus method on the daemon.
Definition DobbyProxy.cpp:351
bool wakeupContainer(int32_t descriptor) const override
Restores previously checkpointed container with the given id.
Definition DobbyProxy.cpp:869
bool stopContainer(int32_t cd, bool withPrejudice) const override
Stops the container with the descriptor (container integer id)
Definition DobbyProxy.cpp:730
std::string getOCIConfig(int32_t descriptor) const override
Debugging utility to retrieve the config.json file for a running container (i.e. like the 'virsh dump...
Definition DobbyProxy.cpp:1344
bool setLogLevel(int level) const override
Simply sets the log level in the daemon.
Definition DobbyProxy.cpp:577
void onContainerAwokenEvent(const AI_IPC::VariantList &args)
Called when a org.rdk.dobby.ctrl1.Awoken event is received from the Dobby 'hypervisor' daemon.
Definition DobbyProxy.cpp:311
bool setAIDbusAddress(bool privateBus, const std::string &address) const override
Sets the AI dbus address for use by the containeriser.
Definition DobbyProxy.cpp:470
bool resumeContainer(int32_t cd) const override
Resumes the container with the descriptor (container integer id)
Definition DobbyProxy.cpp:801
void onContainerHibernatedEvent(const AI_IPC::VariantList &args)
Called when a org.rdk.dobby.ctrl1.Hibernated event is received from the Dobby 'hypervisor' daemon.
Definition DobbyProxy.cpp:277
int32_t startContainerFromSpec(const std::string &id, const std::string &jsonSpec, const std::list< int > &files, const std::string &command="", const std::string &displaySocket="", const std::vector< std::string > &envVars=std::vector< std::string >()) const override
Starts a container with the given id, json spec file and the list of files.
Definition DobbyProxy.cpp:621
bool shutdown() const override
Asks the daemon to shut itself down.
Definition DobbyProxy.cpp:423
bool removeAnnotation(int32_t cd, const std::string &key) const override
removes a key value pair from the container annotation
Definition DobbyProxy.cpp:972
void onContainerStoppedEvent(const AI_IPC::VariantList &args)
Called when a org.rdk.dobby.ctrl1.Stopped event is received from the Dobby 'hypervisor' daemon.
Definition DobbyProxy.cpp:243
bool setLogMethod(uint32_t method, int pipeFd) const override
Sets the logging method used by the daemon.
Definition DobbyProxy.cpp:516
Wrapper around an IpcService object that provides simpler method calls to the Dobby 'hypervisor' daem...
Definition IDobbyProxy.h:92
Definition DobbyProxy.h:183