1 #include "update_agent_ostree.h"
3 #include "package_manager/ostreemanager.h"
7 static void extractCredentialsArchive(
const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
8 std::string* treehub_server);
10 bool OstreeUpdateAgent::isTargetSupported(
const Uptane::Target& target)
const {
return target.
IsOstree(); }
15 installed_image_info.len = 0;
16 installed_image_info.hash = ostreePackMan_->getCurrentHash();
21 auto currently_installed_target = ostreePackMan_->getCurrent();
22 if (!currently_installed_target.IsValid()) {
26 installed_image_info.name = targetname_prefix_ +
"-" + installed_image_info.hash;
28 installed_image_info.name = currently_installed_target.filename();
32 }
catch (
const std::exception& exc) {
33 LOG_ERROR <<
"Failed to get the currently installed revision: " << exc.what();
38 bool OstreeUpdateAgent::download(
const Uptane::Target& target,
const std::string&
data) {
39 std::string treehub_server;
40 bool download_result =
false;
43 std::string ca, cert, pkey, server_url;
44 extractCredentialsArchive(
data, &ca, &cert, &pkey, &server_url);
45 keyMngr_->loadKeys(&pkey, &cert, &ca);
46 boost::trim(server_url);
47 treehub_server = server_url;
48 }
catch (std::runtime_error& exc) {
49 LOG_ERROR << exc.what();
53 auto install_res = OstreeManager::pull(sysrootPath_, treehub_server, *keyMngr_, target);
55 switch (install_res.result_code.num_code) {
56 case data::ResultCode::Numeric::kOk: {
57 LOG_INFO <<
"The target revision has been successfully downloaded: " << target.sha256Hash();
58 download_result =
true;
62 LOG_INFO <<
"The target revision is already present on the local ostree repo: " << target.sha256Hash();
63 download_result =
true;
67 LOG_ERROR <<
"Failed to download the target revision: " << target.sha256Hash() <<
" ( "
68 << install_res.result_code.toString() <<
" ): " << install_res.description;
72 return download_result;
76 return (ostreePackMan_->install(target)).result_code.num_code;
79 void OstreeUpdateAgent::completeInstall() { ostreePackMan_->completeInstall(); }
82 return ostreePackMan_->finalizeInstall(target);
85 void extractCredentialsArchive(
const std::string& archive, std::string* ca, std::string* cert, std::string* pkey,
86 std::string* treehub_server) {
88 std::stringstream as(archive);
89 *ca = Utils::readFileFromArchive(as,
"ca.pem");
92 std::stringstream as(archive);
93 *cert = Utils::readFileFromArchive(as,
"client.pem");
96 std::stringstream as(archive);
97 *pkey = Utils::readFileFromArchive(as,
"pkey.pem");
100 std::stringstream as(archive);
101 *treehub_server = Utils::readFileFromArchive(as,
"server.url",
true);