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.
total_requests_made() <<
" requests";
55 LOG_INFO <<
"Dry run. No objects uploaded.";
58 LOG_ERROR <<
"One or more errors while pushing";
61 return root_object->is_on_server() == PresenceOnServer::kObjectPresent;
65 const std::string &hardwareids) {
66 const boost::filesystem::path local_repo{
"./tuf/aktualizr"};
70 if (boost::filesystem::is_directory(local_repo)) {
71 boost::filesystem::remove_all(local_repo);
74 std::string init_cmd(
"garage-sign init --repo aktualizr --credentials ");
75 if (system((init_cmd + push_credentials.
GetPathOnDisk().string()).c_str()) != 0) {
76 LOG_ERROR <<
"Could not initilaize tuf repo for sign";
80 if (system(
"garage-sign targets pull --repo aktualizr") != 0) {
81 LOG_ERROR <<
"Could not pull targets";
85 std::string cmd(
"garage-sign targets add --repo aktualizr --format OSTREE --length 0");
86 cmd +=
" --name " + name +
" --version " + hash.string() +
" --sha256 " + hash.string();
87 cmd +=
" --hardwareids " + hardwareids;
88 if (system(cmd.c_str()) != 0) {
89 LOG_ERROR <<
"Could not add targets";
93 LOG_INFO <<
"Signing...\n";
94 if (system(
"garage-sign targets sign --key-name targets --repo aktualizr") != 0) {
95 LOG_ERROR <<
"Could not sign targets";
98 if (system(
"garage-sign targets push --repo aktualizr") != 0) {
99 LOG_ERROR <<
"Could not push signed repo";
103 boost::filesystem::remove_all(local_repo);
104 LOG_INFO <<
"Success";
110 curlEasySetoptWrapper(easy_handle.get(), CURLOPT_VERBOSE, get_curlopt_verbose());
111 ref.PushRef(push_server, easy_handle.get());
112 CURLcode err = curl_easy_perform(easy_handle.get());
114 LOG_ERROR <<
"Error pushing root ref: " << curl_easy_strerror(err);
118 curl_easy_getinfo(easy_handle.get(), CURLINFO_RESPONSE_CODE, &rescode);
119 if (rescode < 200 || rescode >= 400) {
120 LOG_ERROR <<
"Error pushing root ref, got " << rescode <<
" HTTP response";