Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
dockerapp_standalone.cc
1 #include "dockerappmanager.h"
2 #include "packagemanagerfactory.h"
3 
4 #include <sstream>
5 
6 /**
7  * @brief This package manager compliments the OSTreePackageManager by also including optional Docker Apps.
8  *
9  * A full description of the Docker App project can be found here:
10  * https://github.com/docker/app/
11  *
12  * NOTE: This implementation is based on the <= 0.8 versions of Docker App that
13  * included a "standalone" command mode.
14  *
15  * Docker Apps are very analogous to docker-compose. In fact, this module
16  * currently "renders" the docker-app file into a docker-compose file. Each
17  * Docker App appears as a Target in the TUF targets list. Each OStree target
18  * can then reference these docker apps in its custom data section. eg:
19  *
20  * "targets": {
21  * httpd.dockerapp-1 : {
22  * "custom" : {"hardwareIds" : ["all"], "name" : "httpd.dockerapp", "version" : "1"},
23  * "hashes" : {"sha256" : "f0ad4e3ce6a5e9cb70c9d747e977fddfacd08419deec0714622029b12dde8338"},
24  * "length" : 889
25  * },
26  * "raspberrypi3-64-lmp-144" : {
27  * "custom" : {
28  * "docker_apps" : {
29  * "httpd" : {
30  * "filename" : "httpd.dockerapp-1"
31  * }
32  * },
33  * "hardwareIds" : ["raspberrypi3-64"],
34  * "name" : "raspberrypi3-64-lmp",
35  * "targetFormat" : "OSTREE",
36  * "version" : "144"
37  * },
38  * "hashes" : {"sha256" : "20ac4f7cd50cda6bfed0caa1f8231cc9a7e40bec60026c66df5f7e143af96942"},
39  * "length" : 0
40  * }
41  * }
42  */
43 
44 struct DockerApp {
45  DockerApp(std::string app_name, const DockerAppManagerConfig &config)
46  : name(std::move(app_name)),
47  app_root(config.docker_apps_root / name),
48  app_params(config.docker_app_params),
49  app_bin(config.docker_app_bin),
50  compose_bin(config.docker_compose_bin) {}
51 
52  bool render(const std::string &app_content, bool persist) {
53  auto bin = boost::filesystem::canonical(app_bin).string();
54  Utils::writeFile(app_root / (name + ".dockerapp"), app_content);
55  std::string cmd("cd " + app_root.string() + " && " + bin + " render " + name);
56  if (!app_params.empty()) {
57  cmd += " --parameters-file " + app_params.string();
58  }
59  std::string yaml;
60  if (Utils::shell(cmd, &yaml, true) != 0) {
61  LOG_ERROR << "Unable to run " << cmd << " output:\n" << yaml;
62  return false;
63  }
64  if (persist) {
65  Utils::writeFile(app_root / "docker-compose.yml", yaml);
66  }
67  return true;
68  }
69 
70  bool fetch() {
71  auto bin = boost::filesystem::canonical(compose_bin).string();
72  std::string cmd("cd " + app_root.string() + " && " + bin + " pull");
73  return std::system(cmd.c_str()) == 0;
74  }
75 
76  bool start() {
77  // Depending on the number and size of the containers in the docker-app,
78  // this command can take a bit of time to complete. Rather than using,
79  // Utils::shell which isn't interactive, we'll use std::system so that
80  // stdout/stderr is streamed while docker sets things up.
81  auto bin = boost::filesystem::canonical(compose_bin).string();
82  std::string cmd("cd " + app_root.string() + " && " + bin + " up --remove-orphans -d");
83  return std::system(cmd.c_str()) == 0;
84  }
85 
86  void remove() {
87  auto bin = boost::filesystem::canonical(compose_bin).string();
88  std::string cmd("cd " + app_root.string() + " && " + bin + " down");
89  if (std::system(cmd.c_str()) == 0) {
90  boost::filesystem::remove_all(app_root);
91  } else {
92  LOG_ERROR << "docker-compose was unable to bring down: " << app_root;
93  }
94  }
95 
96  std::string name;
97  boost::filesystem::path app_root;
98  boost::filesystem::path app_params;
99  boost::filesystem::path app_bin;
100  boost::filesystem::path compose_bin;
101 };
102 
103 bool DockerAppStandalone::iterate_apps(const Uptane::Target &target, const DockerAppCb &cb) const {
104  auto apps = target.custom_data()["docker_apps"];
105  bool res = true;
107  // checkMetaOffline pulls in data from INvStorage to properly initialize
108  // the targets member of the instance so that we can use the LazyTargetList
109  repo.checkMetaOffline(*storage_);
110 
111  if (!apps) {
112  LOG_DEBUG << "Detected an update target from Director with no docker-apps data";
113  for (const auto &t : Uptane::LazyTargetsList(repo, storage_, fake_fetcher_)) {
114  if (t.MatchTarget(target)) {
115  LOG_DEBUG << "Found the match " << t;
116  apps = t.custom_data()["docker_apps"];
117  break;
118  }
119  }
120  }
121 
122  for (const auto &t : Uptane::LazyTargetsList(repo, storage_, fake_fetcher_)) {
123  for (Json::ValueIterator i = apps.begin(); i != apps.end(); ++i) {
124  if ((*i).isObject() && (*i).isMember("filename")) {
125  for (const auto &app : dappcfg_.docker_apps) {
126  if (i.key().asString() == app && (*i)["filename"].asString() == t.filename()) {
127  if (!cb(app, t)) {
128  res = false;
129  }
130  }
131  }
132  } else if ((*i).isObject() && (*i).isMember("uri")) {
133  LOG_TRACE << "Skipping docker app bundle format for " << i.key().asString() << " -> " << *i;
134  } else {
135  LOG_ERROR << "Invalid custom data for docker-app: " << i.key().asString() << " -> " << *i;
136  }
137  }
138  }
139  return res;
140 }
141 
142 bool DockerAppStandalone::fetchTarget(const Uptane::Target &target, Uptane::Fetcher &fetcher, const KeyManager &keys,
143  FetcherProgressCb progress_cb, const api::FlowControlToken *token) {
144  if (!OstreeManager::fetchTarget(target, fetcher, keys, progress_cb, token)) {
145  return false;
146  }
147 
148  LOG_INFO << "Looking for DockerApps to fetch";
149  auto cb = [this, &fetcher, &keys, progress_cb, token](const std::string &app, const Uptane::Target &app_target) {
150  LOG_INFO << "Fetching " << app << " -> " << app_target;
151  if (!PackageManagerInterface::fetchTarget(app_target, fetcher, keys, progress_cb, token)) {
152  return false;
153  }
154  std::stringstream ss;
155  ss << *storage_->openTargetFile(app_target);
156  DockerApp dapp(app, config);
157  return dapp.render(ss.str(), true) && dapp.fetch();
158  };
159  return iterate_apps(target, cb);
160 }
161 
162 data::InstallationResult DockerAppStandalone::install(const Uptane::Target &target) const {
163  auto res = OstreeManager::install(target);
164  handleRemovedApps(target);
165  auto cb = [this](const std::string &app, const Uptane::Target &app_target) {
166  LOG_INFO << "Installing " << app << " -> " << app_target;
167  return DockerApp(app, config).start();
168  };
169  if (!iterate_apps(target, cb)) {
170  res = data::InstallationResult(data::ResultCode::Numeric::kInstallFailed, "Could not render docker app");
171  }
172 
173  if (dappcfg_.docker_prune) {
174  LOG_INFO << "Pruning unused docker images";
175  // Utils::shell which isn't interactive, we'll use std::system so that
176  // stdout/stderr is streamed while docker sets things up.
177  if (std::system("docker image prune -a -f") != 0) {
178  LOG_WARNING << "Unable to prune unused docker images";
179  }
180  }
181  return res;
182 }
183 
184 // Handle the case like:
185 // 1) sota.toml is configured with 2 docker apps: "app1, app2"
186 // 2) update is applied, so we are now running both app1 and app2
187 // 3) sota.toml is updated with 1 docker app: "app1"
188 // At this point we should stop app2 and remove it.
189 void DockerAppStandalone::handleRemovedApps(const Uptane::Target &target) const {
190  if (!boost::filesystem::is_directory(dappcfg_.docker_apps_root)) {
191  LOG_DEBUG << "dappcfg_.docker_apps_root does not exist";
192  return;
193  }
194 
195  std::vector<std::string> target_apps = target.custom_data()["docker_apps"].getMemberNames();
196 
197  for (auto &entry : boost::make_iterator_range(boost::filesystem::directory_iterator(dappcfg_.docker_apps_root), {})) {
198  if (boost::filesystem::is_directory(entry)) {
199  std::string name = entry.path().filename().native();
200  if (std::find(dappcfg_.docker_apps.begin(), dappcfg_.docker_apps.end(), name) == dappcfg_.docker_apps.end()) {
201  LOG_WARNING << "Docker App(" << name
202  << ") installed, but is now removed from configuration. Removing from system";
203  DockerApp(name, dappcfg_).remove();
204  }
205  if (std::find(target_apps.begin(), target_apps.end(), name) == target_apps.end()) {
206  LOG_WARNING << "Docker App(" << name
207  << ") configured, but not defined in installation target. Removing from system";
208  DockerApp(name, dappcfg_).remove();
209  }
210  }
211  }
212 }
Uptane::Fetcher
Definition: fetcher.h:33
DockerAppManagerConfig
Definition: dockerappmanager.h:9
KeyManager
Definition: keymanager.h:13
DockerApp
This package manager compliments the OSTreePackageManager by also including optional Docker Apps.
Definition: dockerapp_standalone.cc:44
data::InstallationResult
Definition: types.h:182
Uptane::LazyTargetsList
Definition: iterator.h:12
api::FlowControlToken
Provides a thread-safe way to pause and terminate task execution.
Definition: apiqueue.h:19
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric::kInstallFailed
Package installation failed.
Uptane::ImagesRepository
Definition: imagesrepository.h:13