Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
update_agent_ostree.cc
1 #include "update_agent_ostree.h"
2 
3 #include "package_manager/ostreemanager.h"
4 
5 // TODO: consider moving this and SotaUptaneClient::secondaryTreehubCredentials() to encapsulate them in one place that
6 // is shared between IP Secondary's component
7 static void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
8  std::string* treehub_server);
9 
10 bool OstreeUpdateAgent::isTargetSupported(const Uptane::Target& target) const { return target.IsOstree(); }
11 
12 bool OstreeUpdateAgent::getInstalledImageInfo(Uptane::InstalledImageInfo& installed_image_info) const {
13  bool result = false;
14  try {
15  installed_image_info.hash = _ostreePackMan->getCurrentHash();
16  // This is the policy on a target image name in case of ostree
17  // The policy in followed and implied in meta-updater (garage-sign/push) and the backend
18  installed_image_info.name = _targetname_prefix + "-" + installed_image_info.hash;
19  installed_image_info.len = 0;
20  result = true;
21  } catch (const std::exception& exc) {
22  LOG_ERROR << "Failed to get the currently installed revision: " << exc.what();
23  }
24  return result;
25 }
26 
27 bool OstreeUpdateAgent::download(const Uptane::Target& target, const std::string& data) {
28  std::string treehub_server;
29  bool download_result = false;
30 
31  try {
32  std::string ca, cert, pkey, server_url;
33  extractCredentialsArchive(data, &ca, &cert, &pkey, &server_url);
34  // TODO: why are qe loading this credentials at all ?
35  _keyMngr->loadKeys(&pkey, &cert, &ca);
36  boost::trim(server_url);
37  treehub_server = server_url;
38  } catch (std::runtime_error& exc) {
39  LOG_ERROR << exc.what();
40  return false;
41  }
42 
43  auto install_res = OstreeManager::pull(_sysrootPath, treehub_server, *_keyMngr, target);
44 
45  switch (install_res.result_code.num_code) {
46  case data::ResultCode::Numeric::kOk: {
47  LOG_INFO << "The target revision has been successfully downloaded: " << target.sha256Hash();
48  download_result = true;
49  break;
50  }
52  LOG_INFO << "The target revision is already present on the local ostree repo: " << target.sha256Hash();
53  download_result = true;
54  break;
55  }
56  default: {
57  LOG_ERROR << "Failed to download the target revision: " << target.sha256Hash() << " ( "
58  << install_res.result_code.toString() << " ): " << install_res.description;
59  }
60  }
61 
62  return download_result;
63 }
64 
65 data::ResultCode::Numeric OstreeUpdateAgent::install(const Uptane::Target& target) {
66  return (_ostreePackMan->install(target)).result_code.num_code;
67 }
68 
69 data::InstallationResult OstreeUpdateAgent::applyPendingInstall(const Uptane::Target& target) {
70  if (!_ostreePackMan->rebootDetected()) {
71  // it should be removed from here, once we refactor the ostree package manager,
72  // e.g. _ostreePackMan->finalizeInstall() does this check/if
73  return data::InstallationResult(data::ResultCode::Numeric::kNeedCompletion,
74  "Reboot is required for the pending update application");
75  }
76 
77  data::InstallationResult install_result = _ostreePackMan->finalizeInstall(target);
78  // it should be removed from here, once we refactor the ostree package manager,
79  // e.g. the pacman will reset/clear the flag by itself
80  _ostreePackMan->rebootFlagClear();
81  return install_result;
82 }
83 
84 void extractCredentialsArchive(const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
85  std::string* treehub_server) {
86  {
87  std::stringstream as(archive);
88  *ca = Utils::readFileFromArchive(as, "ca.pem");
89  }
90  {
91  std::stringstream as(archive);
92  *cert = Utils::readFileFromArchive(as, "client.pem");
93  }
94  {
95  std::stringstream as(archive);
96  *pkey = Utils::readFileFromArchive(as, "pkey.pem");
97  }
98  {
99  std::stringstream as(archive);
100  *treehub_server = Utils::readFileFromArchive(as, "server.url", true);
101  }
102 }
data::ResultCode::Numeric::kAlreadyProcessed
Operation has already been processed.
data::InstallationResult
Definition: types.h:179
data
General data structures.
Definition: types.cc:44
Uptane::InstalledImageInfo
Definition: tuf.h:132
Uptane::Target::IsOstree
bool IsOstree() const
Is this an OSTree target? OSTree targets need special treatment because the hash doesn't represent th...
Definition: tuf.cc:220
result
Results of libaktualizr API calls.
Definition: results.h:13
Uptane::Target
Definition: tuf.h:238
data::ResultCode::Numeric
Numeric
Definition: types.h:125