3 #include <boost/filesystem.hpp>
4 #include <boost/intrusive_ptr.hpp>
6 #include "authenticate.h"
7 #include "logging/logging.h"
8 #include "ostree_object.h"
9 #include "rate_controller.h"
10 #include "request_pool.h"
11 #include "treehub_server.h"
12 #include "utilities/utils.h"
14 bool CheckPoolState(
const OSTreeObject::ptr &root_object,
const RequestPool &request_pool) {
15 switch (request_pool.run_mode()) {
18 return !request_pool.is_stopped() && !request_pool.is_idle();
22 return root_object->is_on_server() != PresenceOnServer::kObjectPresent && !request_pool.is_stopped();
26 bool UploadToTreehub(
const OSTreeRepo::ptr &src_repo,
TreehubServer &push_server,
const OSTreeHash &ostree_commit,
27 const RunMode mode,
const int max_curl_requests) {
28 assert(max_curl_requests > 0);
30 OSTreeObject::ptr root_object;
32 root_object = src_repo->GetObject(ostree_commit, OstreeObjectType::OSTREE_OBJECT_TYPE_COMMIT);
34 LOG_FATAL <<
"OSTree commit " << ostree_commit <<
" was not found in src repository";
38 RequestPool request_pool(push_server, max_curl_requests, mode);
41 request_pool.AddQuery(root_object);
49 }
while (CheckPoolState(root_object, request_pool));
51 if (root_object->is_on_server() == PresenceOnServer::kObjectPresent) {
53 LOG_INFO <<
"Upload to Treehub complete after " << request_pool.head_requests_made() <<
" HEAD requests and "
55 LOG_INFO <<
"Total size of uploaded objects: " << request_pool.total_object_size() <<
" bytes.";
57 LOG_INFO <<
"Dry run. No objects uploaded.";
60 LOG_ERROR <<
"One or more errors while pushing";
63 return root_object->is_on_server() == PresenceOnServer::kObjectPresent;
67 const std::string &hardwareids) {
68 const boost::filesystem::path local_repo{
"./tuf/aktualizr"};
72 if (boost::filesystem::is_directory(local_repo)) {
73 boost::filesystem::remove_all(local_repo);
76 std::string init_cmd(
"garage-sign init --repo aktualizr --credentials ");
77 if (system((init_cmd + push_credentials.
GetPathOnDisk().string()).c_str()) != 0) {
78 LOG_ERROR <<
"Could not initilaize tuf repo for sign";
82 if (system(
"garage-sign targets pull --repo aktualizr") != 0) {
83 LOG_ERROR <<
"Could not pull targets";
87 std::string cmd(
"garage-sign targets add --repo aktualizr --format OSTREE --length 0");
88 cmd +=
" --name " + name +
" --version " + hash.string() +
" --sha256 " + hash.string();
89 cmd +=
" --hardwareids " + hardwareids;
90 if (system(cmd.c_str()) != 0) {
91 LOG_ERROR <<
"Could not add targets";
95 LOG_INFO <<
"Signing...\n";
96 if (system(
"garage-sign targets sign --key-name targets --repo aktualizr") != 0) {
97 LOG_ERROR <<
"Could not sign targets";
100 if (system(
"garage-sign targets push --repo aktualizr") != 0) {
101 LOG_ERROR <<
"Could not push signed repo";
105 boost::filesystem::remove_all(local_repo);
106 LOG_INFO <<
"Success";
112 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
113 ref.PushRef(push_server, easy_handle.get());
114 CURLcode err = curl_easy_perform(easy_handle.get());
116 LOG_ERROR <<
"Error pushing root ref: " << curl_easy_strerror(err);
120 curl_easy_getinfo(easy_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
121 if (rescode < 200 || rescode >= 400) {
122 LOG_ERROR <<
"Error pushing root ref, got " << rescode <<
" HTTP response";