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  po::options_description desc("garage-check command line options");
33  // clang-format off
34  desc.add_options()
35  ("help", "print usage")
36  ("version", "Current garage-check version")
37  ("verbose,v", accumulator<int>(&verbosity), "Verbose logging (use twice for more information)")
38  ("quiet,q", "Quiet mode")
39  ("ref,r", po::value<std::string>(&ref)->required(), "refhash to check")
40  ("credentials,j", po::value<boost::filesystem::path>(&credentials_path)->required(), "credentials (json or zip containing json)")
41  ("cacert", po::value<std::string>(&cacerts), "override path to CA root certificates, in the same format as curl --cacert")
42  ("jobs", po::value<int>(&max_curl_requests)->default_value(30), "maximum number of parallel requests (only relevant with --walk-tree)")
43  ("walk-tree,w", "walk entire tree and check presence of all objects");
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
68  if (verbosity == 0) {
69  // 'verbose' trumps 'quiet'
70  if (static_cast<int>(vm.count("quiet")) != 0) {
71  logger_set_threshold(boost::log::trivial::warning);
72  } else {
73  logger_set_threshold(boost::log::trivial::info);
74  }
75  } else if (verbosity == 1) {
76  logger_set_threshold(boost::log::trivial::debug);
77  LOG_DEBUG << "Debug level debugging enabled";
78  } else if (verbosity > 1) {
79  logger_set_threshold(boost::log::trivial::trace);
80  LOG_TRACE << "Trace level debugging enabled";
81  } else {
82  assert(0);
83  }
84 
85  Utils::setUserAgent(std::string("garage-check/") + garage_tools_version());
86 
87  if (vm.count("walk-tree") != 0u) {
88  mode = RunMode::kWalkTree;
89  }
90 
91  if (max_curl_requests < 1) {
92  LOG_FATAL << "--jobs must be greater than 0";
93  return EXIT_FAILURE;
94  }
95 
96  TreehubServer treehub;
97  if (authenticate(cacerts, ServerCredentials(credentials_path), treehub) != EXIT_SUCCESS) {
98  LOG_FATAL << "Authentication failed";
99  return EXIT_FAILURE;
100  }
101 
102  if (CheckRefValid(treehub, ref, mode, max_curl_requests) != EXIT_SUCCESS) {
103  LOG_FATAL << "Check if the ref is present on the server or in targets.json failed";
104  return EXIT_FAILURE;
105  }
106  } catch (std::exception &ex) {
107  LOG_ERROR << "Exception: " << ex.what();
108  return EXIT_FAILURE;
109  } catch (...) {
110  LOG_ERROR << "Unknown exception";
111  return EXIT_FAILURE;
112  }
113 }
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