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