softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontaineragentadaptor.cpp
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 
20 #include "softwarecontaineragentadaptor.h"
21 
22 namespace softwarecontainer {
23 
24 SoftwareContainerAgentAdaptor::~SoftwareContainerAgentAdaptor()
25 {
26 }
27 
28 SoftwareContainerAgentAdaptor::SoftwareContainerAgentAdaptor(
29  Glib::RefPtr<Glib::MainLoop> &mainLoop,
31  bool useSessionBus) :
32  com::pelagicore::SoftwareContainerAgent(),
33  m_mainLoop(mainLoop),
34  m_agent(agent)
35 {
36  std::string agentBusName = "com.pelagicore.SoftwareContainerAgent";
37  Gio::DBus::BusType busType = useSessionBus ? Gio::DBus::BUS_TYPE_SESSION
38  : Gio::DBus::BUS_TYPE_SYSTEM;
39 
40  name_lost.connect(sigc::mem_fun(this, &SoftwareContainerAgentAdaptor::onDBusError));
41  name_acquired.connect(sigc::mem_fun(this, &SoftwareContainerAgentAdaptor::onDBusNameAcquired));
42  object_not_registered.connect(
43  sigc::mem_fun(this, &SoftwareContainerAgentAdaptor::onDBusError)
44  );
45  connect(busType, agentBusName);
46 }
47 
48 void SoftwareContainerAgentAdaptor::onDBusError(std::string message)
49 {
50  log_error() << "Fatal D-Bus error: " << message;
51  m_mainLoop->quit();
52 }
53 
54 void SoftwareContainerAgentAdaptor::onDBusNameAcquired(std::string name)
55 {
56  log_info() << "Name " << name << " aquired on D-Bus";
57 }
58 
59 void SoftwareContainerAgentAdaptor::List(SoftwareContainerAgentMessageHelper msg)
60 {
61  std::vector<int> list = m_agent.listContainers();
62  msg.returnValue(list);
63 }
64 
65 void SoftwareContainerAgentAdaptor::ListCapabilities(SoftwareContainerAgentMessageHelper msg)
66 {
67  std::vector<std::string> stdStrVec = m_agent.listCapabilities();
68  std::vector<Glib::ustring> glibStrVec = SoftwareContainerAgentCommon::stdStringVecToGlibStringVec(stdStrVec);
69  msg.returnValue(glibStrVec);
70 }
71 
72 void SoftwareContainerAgentAdaptor::Execute(
73  const gint32 containerID,
74  const std::string commandLine,
75  const std::string workingDirectory,
76  const std::string outputFile,
77  const std::map<std::string, std::string> env,
79 {
80  pid_t pid = m_agent.execute(
81  containerID,
82  commandLine,
83  workingDirectory,
84  outputFile,
85  env,
86  [this, containerID](pid_t pid, int exitCode) {
87  ProcessStateChanged_emitter(containerID, pid, false, exitCode);
88  log_info() << "ProcessStateChanged " << pid << " code " << exitCode;
89  }
90  );
91  msg.returnValue(pid);
92 }
93 
94 void SoftwareContainerAgentAdaptor::Suspend(const gint32 containerID, SoftwareContainerAgentMessageHelper msg)
95 {
96  m_agent.suspendContainer(containerID);
97  msg.returnValue();
98 }
99 
100 void SoftwareContainerAgentAdaptor::Resume(const gint32 containerID, SoftwareContainerAgentMessageHelper msg)
101 {
102  m_agent.resumeContainer(containerID);
103  msg.returnValue();
104 }
105 
106 void SoftwareContainerAgentAdaptor::Destroy(const gint32 containerID, SoftwareContainerAgentMessageHelper msg)
107 {
108  m_agent.shutdownContainer(containerID);
109  msg.returnValue();
110 }
111 
112 void SoftwareContainerAgentAdaptor::BindMount(
113  const gint32 containerID,
114  const std::string pathInHost,
115  const std::string PathInContainer,
116  const bool readOnly,
118 {
119  m_agent.bindMount(containerID, pathInHost, PathInContainer, readOnly);
120  msg.returnValue();
121 }
122 
123 void SoftwareContainerAgentAdaptor::SetCapabilities(
124  const gint32 containerID,
125  const std::vector<std::string> capabilities,
127 {
128  m_agent.setCapabilities(containerID, capabilities);
129  msg.returnValue();
130 }
131 
132 void SoftwareContainerAgentAdaptor::Create(const std::string config,
134 {
135  gint32 containerID = m_agent.createContainer(config);
136  msg.returnValue(containerID);
137 }
138 
139 } // namespace softwarecontainer
Developers guide to adding a config item: