Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 1 | #include <docopt.h> |
| 2 | #include <spdlog/sinks/ansicolor_sink.h> |
| 3 | #include <spdlog/spdlog.h> |
| 4 | #include <sysrepo-cpp/Session.hpp> |
| 5 | #include "VELIA_VERSION.h" |
| 6 | #include "main.h" |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame] | 7 | #include "system/Firmware.h" |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 8 | #include "system/Authentication.h" |
| 9 | #include "system_vars.h" |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 10 | #include "system/IETFSystem.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 11 | #include "utils/exceptions.h" |
| 12 | #include "utils/journal.h" |
| 13 | #include "utils/log-init.h" |
| 14 | |
| 15 | /** @short Extract log level from a CLI option */ |
| 16 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 17 | { |
| 18 | long x; |
| 19 | try { |
| 20 | x = option.asLong(); |
| 21 | } catch (std::invalid_argument&) { |
| 22 | throw std::runtime_error(name + " log level: expecting integer"); |
| 23 | } |
| 24 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 25 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 26 | if (x < 0 || x > 5) |
| 27 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 28 | |
| 29 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 30 | } |
| 31 | |
| 32 | static const char usage[] = |
| 33 | R"(Sysrepo-powered system management. |
| 34 | |
| 35 | Usage: |
| 36 | veliad-system |
| 37 | [--log-level=<Level>] |
| 38 | [--sysrepo-log-level=<Level>] |
| 39 | [--system-log-level=<Level>] |
| 40 | veliad-system (-h | --help) |
| 41 | veliad-system --version |
| 42 | |
| 43 | Options: |
| 44 | -h --help Show this screen. |
| 45 | --version Show version. |
| 46 | --log-level=<N> Log level for everything [default: 3] |
| 47 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 48 | 4 -> debug, 5 -> trace) |
| 49 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3] |
| 50 | --system-log-level=<N> Log level for the system stuff [default: 3] |
| 51 | )"; |
| 52 | |
| 53 | DBUS_EVENTLOOP_INIT |
| 54 | |
| 55 | int main(int argc, char* argv[]) |
| 56 | { |
| 57 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 58 | if (velia::utils::isJournaldActive()) { |
| 59 | loggingSink = velia::utils::create_journald_sink(); |
| 60 | } else { |
| 61 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 62 | } |
| 63 | |
| 64 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true); |
| 65 | |
| 66 | velia::utils::initLogs(loggingSink); |
| 67 | spdlog::set_level(spdlog::level::info); |
| 68 | |
| 69 | try { |
| 70 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
| 71 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 72 | |
| 73 | spdlog::get("main")->debug("Opening Sysrepo connection"); |
| 74 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 75 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 76 | |
| 77 | DBUS_EVENTLOOP_START |
| 78 | |
| 79 | // initialize ietf-system |
| 80 | spdlog::get("main")->debug("Initializing Sysrepo for system models"); |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 81 | auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release"); |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 82 | |
| 83 | auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods). |
| 84 | dbusConnection->enterEventLoopAsync(); |
| 85 | auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 86 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 87 | auto srSess2 = std::make_shared<sysrepo::Session>(srConn); |
| 88 | auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword); |
| 89 | |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 90 | DBUS_EVENTLOOP_END |
| 91 | return 0; |
| 92 | } catch (std::exception& e) { |
| 93 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 94 | } |
| 95 | } |