4 #include <boost/filesystem.hpp>
5 #include <boost/program_options.hpp>
7 #include "authenticate.h"
11 #include "garage_tools_version.h"
12 #include "logging/logging.h"
13 #include "ostree_http_repo.h"
15 namespace po = boost::program_options;
17 int main(
int argc,
char **argv) {
20 auto start_time = std::chrono::system_clock::now();
22 std::string ostree_commit;
24 boost::filesystem::path fetch_cred;
25 boost::filesystem::path push_cred;
26 std::string hardwareids;
28 int max_curl_requests;
30 po::options_description desc(
"garage-deploy command line options");
33 (
"help",
"print usage")
34 (
"version",
"Current garage-deploy version")
35 (
"verbose,v",
"Verbose logging (loglevel 1)")
36 (
"quiet,q",
"Quiet mode (loglevel 3)")
37 (
"loglevel", po::value<int>(),
"set log level 0-5 (trace, debug, info, warning, error, fatal)")
38 (
"commit", po::value<std::string>(&ostree_commit)->required(),
"OSTree commit to deploy")
39 (
"name", po::value<std::string>(&name)->required(),
"Name of image")
40 (
"fetch-credentials,f", po::value<boost::filesystem::path>(&fetch_cred)->required(),
"path to source credentials")
41 (
"push-credentials,p", po::value<boost::filesystem::path>(&push_cred)->required(),
"path to destination credentials")
42 (
"hardwareids,h", po::value<std::string>(&hardwareids)->required(),
"list of hardware ids")
43 (
"cacert", po::value<std::string>(&cacerts),
"override path to CA root certificates, in the same format as curl --cacert")
44 (
"jobs", po::value<int>(&max_curl_requests)->default_value(30),
"maximum number of parallel requests")
45 (
"dry-run,n",
"check arguments and authenticate but don't upload");
51 po::store(po::parse_command_line(argc,
reinterpret_cast<const char *
const *
>(argv), desc), vm);
53 if (vm.count(
"help") != 0U) {
57 if (vm.count(
"version") != 0) {
58 LOG_INFO <<
"Current garage-deploy version is: " << garage_tools_version();
62 }
catch (
const po::error &o) {
63 LOG_ERROR << o.what();
70 if (vm.count(
"loglevel") != 0) {
71 const int loglevel = vm[
"loglevel"].as<
int>();
72 logger_set_threshold(
static_cast<boost::log::trivial::severity_level
>(loglevel));
73 LOG_INFO <<
"Loglevel set to " << loglevel;
74 }
else if (
static_cast<int>(vm.count(
"verbose")) != 0) {
75 logger_set_threshold(boost::log::trivial::debug);
76 LOG_DEBUG <<
"Debug level debugging enabled";
77 }
else if (
static_cast<int>(vm.count(
"quiet")) != 0) {
78 logger_set_threshold(boost::log::trivial::warning);
80 logger_set_threshold(boost::log::trivial::info);
82 }
catch (std::exception &e) {
83 LOG_FATAL << e.what();
87 Utils::setUserAgent(std::string(
"garage-deploy/") + garage_tools_version());
89 if (vm.count(
"dry-run") != 0U) {
93 if (max_curl_requests < 1) {
94 LOG_FATAL <<
"--jobs must be greater than 0";
100 if (authenticate(cacerts, fetch_credentials, fetch_server) != EXIT_SUCCESS) {
101 LOG_FATAL <<
"Authentication with fetch server failed";
107 if (authenticate(cacerts, push_credentials, push_server) != EXIT_SUCCESS) {
108 LOG_FATAL <<
"Authentication with push server failed";
112 OSTreeRepo::ptr src_repo = std::make_shared<OSTreeHttpRepo>(&fetch_server);
118 if (!UploadToTreehub(src_repo, push_server, commit, mode, max_curl_requests)) {
119 LOG_FATAL <<
"Upload to treehub failed";
124 if (!push_credentials.CanSignOffline()) {
125 LOG_FATAL <<
"Provided push credentials are missing required components to sign Targets metadata.";
128 if (!OfflineSignRepo(
ServerCredentials(push_credentials.GetPathOnDisk()), name, commit, hardwareids)) {
132 if (CheckRefValid(push_server, ostree_commit, mode, max_curl_requests) != EXIT_SUCCESS) {
133 LOG_FATAL <<
"Check if the ref is present on the server or in targets.json failed";
137 LOG_INFO <<
"Dry run. Not attempting offline signing.";
140 LOG_FATAL << e.what();
144 auto end_time = std::chrono::system_clock::now();
145 std::chrono::duration<double> diff_time = end_time - start_time;
146 LOG_INFO <<
"Total runtime: " << diff_time.count() <<
" seconds.";