Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
main.cc
1 #include <boost/filesystem.hpp>
2 #include <boost/program_options.hpp>
3 #include <iostream>
4 #include <memory>
5 #include <thread>
6 
7 #include "aktualizr_secondary.h"
8 #include "aktualizr_secondary_config.h"
9 #include "utilities/aktualizr_version.h"
10 #include "utilities/utils.h"
11 
12 #include "aktualizr_secondary_file.h"
13 #include "logging/logging.h"
14 #include "secondary_tcp_server.h"
15 #ifdef BUILD_OSTREE
16 #include "aktualizr_secondary_ostree.h"
17 #endif
18 
19 namespace bpo = boost::program_options;
20 
21 void check_secondary_options(const bpo::options_description &description, const bpo::variables_map &vm) {
22  if (vm.count("help") != 0) {
23  std::cout << description << '\n';
24  exit(EXIT_SUCCESS);
25  }
26  if (vm.count("version") != 0) {
27  std::cout << "Current aktualizr-secondary version is: " << aktualizr_version() << "\n";
28  exit(EXIT_SUCCESS);
29  }
30 }
31 
32 bpo::variables_map parse_options(int argc, char **argv) {
33  bpo::options_description description("aktualizr-secondary command line options");
34  // clang-format off
35  description.add_options()
36  ("help,h", "print usage")
37  ("version,v", "Current aktualizr-secondary version")
38  ("loglevel", bpo::value<int>(), "set log level 0-5 (trace, debug, info, warning, error, fatal)")
39  ("config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(), "configuration file or directory")
40  ("server-port,p", bpo::value<int>(), "command server listening port")
41  ("ecu-serial", bpo::value<std::string>(), "serial number of Secondary ECU")
42  ("ecu-hardware-id", bpo::value<std::string>(), "hardware ID of Secondary ECU");
43  // clang-format on
44 
45  bpo::variables_map vm;
46  std::vector<std::string> unregistered_options;
47  try {
48  bpo::basic_parsed_options<char> parsed_options =
49  bpo::command_line_parser(argc, argv).options(description).allow_unregistered().run();
50  bpo::store(parsed_options, vm);
51  check_secondary_options(description, vm);
52  bpo::notify(vm);
53  unregistered_options = bpo::collect_unrecognized(parsed_options.options, bpo::include_positional);
54  if (vm.count("help") == 0 && !unregistered_options.empty()) {
55  std::cout << description << "\n";
56  exit(EXIT_FAILURE);
57  }
58  } catch (const bpo::required_option &ex) {
59  // print the error and append the default commandline option description
60  std::cout << ex.what() << std::endl << description;
61  exit(EXIT_FAILURE);
62  } catch (const bpo::error &ex) {
63  check_secondary_options(description, vm);
64 
65  // log boost error
66  LOG_WARNING << "boost command line option error: " << ex.what();
67 
68  // print the error message to the standard output too, as the user provided
69  // a non-supported commandline option
70  std::cout << ex.what() << '\n';
71 
72  // set the returnValue, thereby ctest will recognize
73  // that something went wrong
74  exit(EXIT_FAILURE);
75  }
76 
77  return vm;
78 }
79 
80 /*****************************************************************************/
81 int main(int argc, char *argv[]) {
82  logger_init();
83  logger_set_threshold(boost::log::trivial::info);
84  LOG_INFO << "aktualizr-secondary version " << aktualizr_version() << " starting";
85 
86  bpo::variables_map commandline_map = parse_options(argc, argv);
87 
88  int ret = EXIT_SUCCESS;
89  try {
90  AktualizrSecondaryConfig config(commandline_map);
91  AktualizrSecondary::Ptr secondary;
92 
93  if (config.pacman.type != PACKAGE_MANAGER_OSTREE) {
94  secondary = std::make_shared<AktualizrSecondaryFile>(config);
95  }
96 #ifdef BUILD_OSTREE
97  else {
98  secondary = std::make_shared<AktualizrSecondaryOstree>(config);
99  }
100 #else
101  else {
102  LOG_ERROR << "Unsupported type of Secondary: " << config.pacman.type;
103  }
104 #endif // BUILD_OSTREE
105 
106  if (!secondary) {
107  throw std::runtime_error("Failed to create IP Secondary of the specified type: " + config.pacman.type);
108  }
109  secondary->initialize();
110 
111  SecondaryTcpServer tcp_server(*secondary, config.network.primary_ip, config.network.primary_port,
112  config.network.port, config.uptane.force_install_completion);
113 
114  tcp_server.run();
115 
116  if (tcp_server.exit_reason() == SecondaryTcpServer::ExitReason::kRebootNeeded) {
117  secondary->completeInstall();
118  }
119 
120  } catch (std::exception &exc) {
121  LOG_ERROR << "Error: " << exc.what();
122  ret = EXIT_FAILURE;
123  }
124  return ret;
125 }
AktualizrSecondaryConfig
Definition: aktualizr_secondary_config.h:35
SecondaryTcpServer
Listens on a socket, decodes calls (ASN.1) and forwards them to an Uptane Secondary implementation.
Definition: secondary_tcp_server.h:16