Aktualizr
C++ SOTA Client
garage_check.cc
1 #include <string>
2 
3 #include <curl/curl.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/program_options.hpp>
6 
7 #include "authenticate.h"
8 #include "check.h"
9 #include "garage_common.h"
10 #include "garage_tools_version.h"
11 #include "libaktualizr/types.h"
12 #include "logging/logging.h"
13 #include "ostree_http_repo.h"
14 #include "ostree_object.h"
15 #include "request_pool.h"
16 #include "treehub_server.h"
17 #include "utilities/utils.h"
18 
19 namespace po = boost::program_options;
20 
21 int main(int argc, char **argv) {
22  logger_init();
23 
24  std::string ref;
25  boost::filesystem::path credentials_path;
26  std::string cacerts;
27  int max_curl_requests;
29  boost::filesystem::path tree_dir;
30  po::options_description desc("garage-check command line options");
31  // clang-format off
32  desc.add_options()
33  ("help", "print usage")
34  ("version", "Current garage-check 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  ("ref,r", po::value<std::string>(&ref)->required(), "refhash to check")
39  ("credentials,j", po::value<boost::filesystem::path>(&credentials_path)->required(), "credentials (json or zip containing json)")
40  ("cacert", po::value<std::string>(&cacerts), "override path to CA root certificates, in the same format as curl --cacert")
41  ("jobs", po::value<int>(&max_curl_requests)->default_value(30), "maximum number of parallel requests (only relevant with --walk-tree)")
42  ("walk-tree,w", "walk entire tree and check presence of all objects")
43  ("tree-dir,t", po::value<boost::filesystem::path>(&tree_dir), "directory to which to write the tree (only used with --walk-tree)");
44  // clang-format on
45 
46  po::variables_map vm;
47 
48  try {
49  po::store(po::parse_command_line(argc, reinterpret_cast<const char *const *>(argv), desc), vm);
50 
51  if (vm.count("help") != 0U) {
52  LOG_INFO << desc;
53  return EXIT_SUCCESS;
54  }
55  if (vm.count("version") != 0) {
56  LOG_INFO << "Current garage-check version is: " << garage_tools_version();
57  exit(EXIT_SUCCESS);
58  }
59  po::notify(vm);
60  } catch (const po::error &o) {
61  LOG_ERROR << o.what();
62  LOG_ERROR << desc;
63  return EXIT_FAILURE;
64  }
65 
66  try {
67  // Configure logging. Try loglevel first, then verbose, then quiet.
68  if (vm.count("loglevel") != 0) {
69  const int loglevel = vm["loglevel"].as<int>();
70  logger_set_threshold(static_cast<boost::log::trivial::severity_level>(loglevel));
71  LOG_INFO << "Loglevel set to " << loglevel;
72  } else if (static_cast<int>(vm.count("verbose")) != 0) {
73  logger_set_threshold(boost::log::trivial::debug);
74  LOG_DEBUG << "Debug level debugging enabled";
75  } else if (static_cast<int>(vm.count("quiet")) != 0) {
76  logger_set_threshold(boost::log::trivial::warning);
77  } else {
78  logger_set_threshold(boost::log::trivial::info);
79  }
80 
81  Utils::setUserAgent(std::string("garage-check/") + garage_tools_version());
82 
83  if (vm.count("walk-tree") != 0U) {
84  mode = RunMode::kWalkTree;
85  }
86 
87  if (max_curl_requests < 1) {
88  LOG_FATAL << "--jobs must be greater than 0";
89  return EXIT_FAILURE;
90  }
91 
92  TreehubServer treehub;
93  if (authenticate(cacerts, ServerCredentials(credentials_path), treehub) != EXIT_SUCCESS) {
94  LOG_FATAL << "Authentication failed";
95  return EXIT_FAILURE;
96  }
97 
98  if (CheckRefValid(treehub, ref, mode, max_curl_requests, tree_dir) != EXIT_SUCCESS) {
99  LOG_FATAL << "Check if the ref is present on the server or in targets.json failed";
100  return EXIT_FAILURE;
101  }
102  } catch (std::exception &ex) {
103  LOG_ERROR << "Exception: " << ex.what();
104  return EXIT_FAILURE;
105  } catch (...) {
106  LOG_ERROR << "Unknown exception";
107  return EXIT_FAILURE;
108  }
109 }
types.h
ServerCredentials
Definition: server_credentials.h:25
RunMode::kWalkTree
@ kWalkTree
Walk the entire tree (without uploading).
TreehubServer
Definition: treehub_server.h:11
garage_common.h
RunMode::kDefault
@ kDefault
Default operation.
RunMode
RunMode
Execution mode to run garage tools in.
Definition: garage_common.h:6