softwarecontainer  0.18.0-739e8d7 2017-05-04
containerutilityinterface.cpp
1 /*
2  * Copyright (C) 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 
21 
22 #include "softwarecontainer-common.h"
23 #include "createdir.h"
24 
25 
26 #include <lxc/lxccontainer.h>
27 
28 namespace softwarecontainer {
29 ContainerUtilityInterface::ContainerUtilityInterface(std::shared_ptr<Config> config)
30  : m_config(std::move(config))
31 {
32 }
33 
35 {
36  char **containerNames = nullptr;
37  struct lxc_container **containerList = nullptr;
38 
39  const char *basePath = lxc_get_global_config_item("lxc.lxcpath");
40  auto num = list_all_containers(basePath, &containerNames, &containerList);
41 
42  if (0 == num) {
43  // there are no container residue. No need to run more.
44  delete containerList;
45  delete containerNames;
46  return;
47  } else if (-1 == num) {
48  log_error() << "An error is occurred while trying to get deprecated container list";
49  throw ContainerUtilityInterfaceError("An error is occurred while trying to get container list");
50  }
51 
52  log_warning() << num << " unused deprecated containers found";
53  for (auto i = 0; i < num; i++) {
54  struct lxc_container *container = containerList[i];
55  log_debug() << "Deprecated container named " << containerNames[i] << " will be deleted";
56 
57  if (container->is_running(container)) {
58  bool success = container->stop(container);
59  if (!success) {
60  std::string errorMsg = "Unable to stop deprecated container " +
61  std::string(containerNames[i]);
62  throw ContainerUtilityInterfaceError(errorMsg);
63  }
64  }
65 
66  bool success = container->destroy(container);
67  if (!success) {
68  std::string errorMsg = "Unable to destroy deprecated container " +
69  std::string(containerNames[i]);
70  throw ContainerUtilityInterfaceError(errorMsg);
71  }
72 
73  log_debug() << "Deprecated container " << containerNames[i] << " is successfully destroyed";
74  delete container;
75  delete containerNames[i];
76  }
77  delete containerList;
78  delete containerNames;
79 }
80 
82 {
83  const std::string rootDir = m_config->getStringValue("SoftwareContainer", "shared-mounts-dir");
84  if (!isDirectory(rootDir)) {
85  log_debug() << "Container root " << rootDir << " does not exist, trying to create";
86  std::unique_ptr<CreateDir> createDirInstance(new CreateDir());
87  if(!createDirInstance->createDirectory(rootDir)) {
88  std::string message = "Failed to create container root directory";
89  log_error() << message;
90  throw SoftwareContainerError(message);
91  }
92 
93  m_createDirList.push_back(std::move(createDirInstance));
94  }
95 }
96 
97 
98 } //namespace
void removeOldContainers(void)
This method cleans unused old containers before agent starts up.
The CreateDir class is responsible for creating new directories and removing them when it is necessar...
Definition: createdir.h:33
An error occured in ContainerUtilityInterface.
Contains the softwarecontainer::ContainerUtilityAbstractInterface class.
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
void checkWorkspace(void)
Check that the workspace exists.
Developers guide to adding a config item: