softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontaineragent.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
26 #include <glib-unix.h>
27 #include <glibmm.h>
28 
29 #include <ivi-profiling.h>
30 
31 #include "filetoolkitwithundo.h"
32 #include "softwarecontainer.h"
33 #include "softwarecontainer-common.h"
34 #include "softwarecontainererror.h"
35 
36 #include "capability/filteredconfigstore.h"
37 #include "capability/defaultconfigstore.h"
38 #include "capability/servicemanifestloader.h"
39 #include "config/config.h"
40 #include "containeroptions/containeroptionparser.h"
43 
44 #include <jsonparser.h>
45 #include "commandjob.h"
46 #include <queue>
47 
48 namespace softwarecontainer {
49 
50 static constexpr ContainerID INVALID_CONTAINER_ID = -1;
51 
60 {
61 public:
63  m_message("SoftwareContainer error")
64  {
65  }
66 
67  SoftwareContainerAgentError(const std::string &message):
68  m_message(message)
69  {
70  }
71 
72  virtual const char *what() const throw()
73  {
74  return m_message.c_str();
75  }
76 
77 protected:
78  std::string m_message;
79 };
80 
81 
83 {
84  LOG_DECLARE_CLASS_CONTEXT("SCA", "SoftwareContainerAgent");
85  typedef std::shared_ptr<SoftwareContainerAbstractInterface> SoftwareContainerPtr;
86 
87 public:
103  SoftwareContainerAgent(Glib::RefPtr<Glib::MainContext> mainLoopContext,
104  std::shared_ptr<Config> config,
105  std::shared_ptr<SoftwareContainerFactory> factory,
106  std::shared_ptr<ContainerUtilityInterface> utility);
107 
109 
115  std::vector<ContainerID> listContainers();
116 
126  std::vector<std::string> listCapabilities();
127 
132  void deleteContainer(ContainerID containerID);
133 
141  SoftwareContainerPtr getContainer(ContainerID containerID);
142 
152  ContainerID createContainer(const std::string &config);
153 
165  pid_t execute(ContainerID containerID,
166  const std::string &cmdLine,
167  const std::string &workingDirectory,
168  const std::string &outputFile,
169  const EnvironmentVariables &env,
170  std::function<void (pid_t, int)> listener);
171 
178  void shutdownContainer(ContainerID containerID);
179 
186  void suspendContainer(ContainerID containerID);
187 
194  void resumeContainer(ContainerID containerID);
195 
207  void bindMount(const ContainerID containerID,
208  const std::string &pathInHost,
209  const std::string &pathInContainer,
210  bool readOnly);
211 
224  void setCapabilities(const ContainerID &containerID,
225  const std::vector<std::string> &capabilities);
226 private:
235  bool updateGatewayConfigs(const ContainerID &containerID,
236  const GatewayConfiguration &configs);
237 
238  // Find a job given a pid
239  std::shared_ptr<CommandJob> getJob(pid_t pid);
240  // Check if a given containerID is valid
241  void assertContainerExists(ContainerID containerID);
242  // Return a suitable container id
243  ContainerID findSuitableId();
244 
245  // List of containers in use
246  std::map<ContainerID, SoftwareContainerPtr> m_containers;
247 
248  Glib::RefPtr<Glib::MainContext> m_mainLoopContext;
249  SignalConnectionsHandler m_connections;
250  std::vector<ContainerID> m_containerIdPool;
251 
252  std::shared_ptr<FilteredConfigStore> m_filteredConfigStore;
253  std::shared_ptr<DefaultConfigStore> m_defaultConfigStore;
254 
255  std::shared_ptr<Config> m_config;
256 
257  /*
258  * Holds all configs to use for each SoftwareContainer instance,
259  * both the static configs from Config, as well as dynamic values
260  * set by the client when creating a container.
261  *
262  * Each SoftwareContainer instance needs its own copy of these configs
263  * so this reference should be used to create a copy from, which should
264  * be passed to SoftwareContainer as a unique_ptr.
265  */
266  SoftwareContainerConfig m_containerConfig;
267 
268  /*
269  * Responsible for parsing any dynamic values given when creating a container
270  */
271  ContainerOptionParser m_optionParser;
272 
273  std::shared_ptr<SoftwareContainerFactory> m_factory;
274  std::shared_ptr<ContainerUtilityInterface> m_containerUtility;
275 };
276 
277 } // namespace softwarecontainer
Contains all values that should be passed to SoftwareContainer on creation.
Contains the softwarecontainer::ContainerUtilityAbstractInterface class.
Contains the softwarecontainer::SoftwareContainerFactory class.
The SignalConnectionsHandler class contains references to sigc++ connections and automatically discon...
Developers guide to adding a config item:
An error occured in SoftwareContainerAgent.