4 #include <boost/filesystem.hpp>
5 #include <boost/program_options.hpp>
7 #include "accumulator.h"
8 #include "authenticate.h"
12 #include "garage_tools_version.h"
13 #include "logging/logging.h"
14 #include "ostree_http_repo.h"
16 namespace po = boost::program_options;
18 int main(
int argc,
char **argv) {
21 auto start_time = std::chrono::system_clock::now();
24 std::string ostree_commit;
26 boost::filesystem::path fetch_cred;
27 boost::filesystem::path push_cred;
28 std::string hardwareids;
30 int max_curl_requests;
32 po::options_description desc(
"garage-deploy command line options");
35 (
"help",
"print usage")
36 (
"version",
"Current garage-deploy version")
37 (
"verbose,v", accumulator<int>(&verbosity),
"Verbose logging (use twice for more information)")
38 (
"quiet,q",
"Quiet mode")
39 (
"commit", po::value<std::string>(&ostree_commit)->required(),
"OSTree commit to deploy")
40 (
"name", po::value<std::string>(&name)->required(),
"Name of image")
41 (
"fetch-credentials,f", po::value<boost::filesystem::path>(&fetch_cred)->required(),
"path to source credentials")
42 (
"push-credentials,p", po::value<boost::filesystem::path>(&push_cred)->required(),
"path to destination credentials")
43 (
"hardwareids,h", po::value<std::string>(&hardwareids)->required(),
"list of hardware ids")
44 (
"cacert", po::value<std::string>(&cacerts),
"override path to CA root certificates, in the same format as curl --cacert")
45 (
"jobs", po::value<int>(&max_curl_requests)->default_value(30),
"maximum number of parallel requests")
46 (
"dry-run,n",
"check arguments and authenticate but don't upload");
52 po::store(po::parse_command_line(argc, reinterpret_cast<const char *const *>(argv), desc), vm);
54 if (vm.count(
"help") != 0U) {
58 if (vm.count(
"version") != 0) {
59 LOG_INFO <<
"Current garage-deploy version is: " << garage_tools_version();
63 }
catch (
const po::error &o) {
64 LOG_ERROR << o.what();
72 if (static_cast<int>(vm.count(
"quiet")) != 0) {
73 logger_set_threshold(boost::log::trivial::warning);
75 logger_set_threshold(boost::log::trivial::info);
77 }
else if (verbosity == 1) {
78 logger_set_threshold(boost::log::trivial::debug);
79 LOG_DEBUG <<
"Debug level debugging enabled";
80 }
else if (verbosity > 1) {
81 logger_set_threshold(boost::log::trivial::trace);
82 LOG_TRACE <<
"Trace level debugging enabled";
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.";