1 #include <boost/filesystem.hpp>
2 #include <boost/program_options.hpp>
7 #include "aktualizr_secondary.h"
8 #include "aktualizr_secondary_config.h"
9 #include "aktualizr_secondary_factory.h"
10 #include "utilities/aktualizr_version.h"
11 #include "utilities/utils.h"
13 #include "logging/logging.h"
14 #include "secondary_tcp_server.h"
16 namespace bpo = boost::program_options;
18 void check_secondary_options(
const bpo::options_description &description,
const bpo::variables_map &vm) {
19 if (vm.count(
"help") != 0) {
20 std::cout << description <<
'\n';
23 if (vm.count(
"version") != 0) {
24 std::cout <<
"Current aktualizr-secondary version is: " << aktualizr_version() <<
"\n";
29 bpo::variables_map parse_options(
int argc,
char *argv[]) {
30 bpo::options_description description(
"aktualizr-secondary command line options");
32 description.add_options()
33 (
"help,h",
"print usage")
34 (
"version,v",
"Current aktualizr-secondary version")
35 (
"loglevel", bpo::value<int>(),
"set log level 0-5 (trace, debug, info, warning, error, fatal)")
36 (
"config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(),
"configuration file or directory")
37 (
"server-port,p", bpo::value<int>(),
"command server listening port")
38 (
"ecu-serial", bpo::value<std::string>(),
"serial number of Secondary ECU")
39 (
"ecu-hardware-id", bpo::value<std::string>(),
"hardware ID of Secondary ECU");
42 bpo::variables_map vm;
43 std::vector<std::string> unregistered_options;
45 bpo::basic_parsed_options<char> parsed_options =
46 bpo::command_line_parser(argc, argv).options(description).allow_unregistered().run();
47 bpo::store(parsed_options, vm);
48 check_secondary_options(description, vm);
50 unregistered_options = bpo::collect_unrecognized(parsed_options.options, bpo::include_positional);
51 if (vm.count(
"help") == 0 && !unregistered_options.empty()) {
52 std::cout << description <<
"\n";
55 }
catch (
const bpo::required_option &ex) {
57 std::cout << ex.what() << std::endl << description;
59 }
catch (
const bpo::error &ex) {
60 check_secondary_options(description, vm);
63 LOG_WARNING <<
"boost command line option error: " << ex.what();
67 std::cout << ex.what() <<
'\n';
78 int main(
int argc,
char *argv[]) {
80 logger_set_threshold(boost::log::trivial::info);
81 LOG_INFO <<
"aktualizr-secondary version " << aktualizr_version() <<
" starting";
83 bpo::variables_map commandline_map = parse_options(argc, argv);
85 int ret = EXIT_SUCCESS;
88 LOG_DEBUG <<
"Current directory: " << boost::filesystem::current_path().string();
90 auto secondary = AktualizrSecondaryFactory::create(config);
91 SecondaryTcpServer tcp_server(secondary->getDispatcher(), config.network.primary_ip, config.network.primary_port,
92 config.network.port, config.uptane.force_install_completion);
96 if (tcp_server.exit_reason() == SecondaryTcpServer::ExitReason::kRebootNeeded) {
97 secondary->completeInstall();
100 }
catch (std::runtime_error &exc) {
101 LOG_ERROR <<
"Error: " << exc.what();