1 #include <boost/filesystem.hpp>
2 #include <boost/program_options.hpp>
7 #include "aktualizr_secondary.h"
8 #include "aktualizr_secondary_config.h"
9 #include "utilities/aktualizr_version.h"
10 #include "utilities/utils.h"
12 #include "aktualizr_secondary_file.h"
13 #include "logging/logging.h"
14 #include "secondary_tcp_server.h"
16 #include "aktualizr_secondary_ostree.h"
19 namespace bpo = boost::program_options;
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';
26 if (vm.count(
"version") != 0) {
27 std::cout <<
"Current aktualizr-secondary version is: " << aktualizr_version() <<
"\n";
32 bpo::variables_map parse_options(
int argc,
char **argv) {
33 bpo::options_description description(
"aktualizr-secondary command line options");
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");
45 bpo::variables_map vm;
46 std::vector<std::string> unregistered_options;
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);
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";
58 }
catch (
const bpo::required_option &ex) {
60 std::cout << ex.what() << std::endl << description;
62 }
catch (
const bpo::error &ex) {
63 check_secondary_options(description, vm);
66 LOG_WARNING <<
"boost command line option error: " << ex.what();
70 std::cout << ex.what() <<
'\n';
81 int main(
int argc,
char *argv[]) {
83 logger_set_threshold(boost::log::trivial::info);
84 LOG_INFO <<
"aktualizr-secondary version " << aktualizr_version() <<
" starting";
86 bpo::variables_map commandline_map = parse_options(argc, argv);
88 int ret = EXIT_SUCCESS;
91 AktualizrSecondary::Ptr secondary;
93 if (config.pacman.type != PACKAGE_MANAGER_OSTREE) {
94 secondary = std::make_shared<AktualizrSecondaryFile>(config);
98 secondary = std::make_shared<AktualizrSecondaryOstree>(config);
102 LOG_ERROR <<
"Unsupported type of Secondary: " << config.pacman.type;
104 #endif // BUILD_OSTREE
107 throw std::runtime_error(
"Failed to create IP Secondary of the specified type: " + config.pacman.type);
109 secondary->initialize();
111 SecondaryTcpServer tcp_server(*secondary, config.network.primary_ip, config.network.primary_port,
112 config.network.port, config.uptane.force_install_completion);
116 if (tcp_server.exit_reason() == SecondaryTcpServer::ExitReason::kRebootNeeded) {
117 secondary->completeInstall();
120 }
catch (std::exception &exc) {
121 LOG_ERROR <<
"Error: " << exc.what();