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" |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame^] | 9 | #include "system/Network.h" |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 10 | #include "system_vars.h" |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 11 | #include "system/IETFSystem.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 12 | #include "utils/exceptions.h" |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame^] | 13 | #include "utils/exec.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 14 | #include "utils/journal.h" |
| 15 | #include "utils/log-init.h" |
| 16 | |
| 17 | /** @short Extract log level from a CLI option */ |
| 18 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 19 | { |
| 20 | long x; |
| 21 | try { |
| 22 | x = option.asLong(); |
| 23 | } catch (std::invalid_argument&) { |
| 24 | throw std::runtime_error(name + " log level: expecting integer"); |
| 25 | } |
| 26 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 27 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 28 | if (x < 0 || x > 5) |
| 29 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 30 | |
| 31 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 32 | } |
| 33 | |
| 34 | static const char usage[] = |
| 35 | R"(Sysrepo-powered system management. |
| 36 | |
| 37 | Usage: |
| 38 | veliad-system |
| 39 | [--log-level=<Level>] |
| 40 | [--sysrepo-log-level=<Level>] |
| 41 | [--system-log-level=<Level>] |
| 42 | veliad-system (-h | --help) |
| 43 | veliad-system --version |
| 44 | |
| 45 | Options: |
| 46 | -h --help Show this screen. |
| 47 | --version Show version. |
| 48 | --log-level=<N> Log level for everything [default: 3] |
| 49 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 50 | 4 -> debug, 5 -> trace) |
| 51 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3] |
| 52 | --system-log-level=<N> Log level for the system stuff [default: 3] |
| 53 | )"; |
| 54 | |
| 55 | DBUS_EVENTLOOP_INIT |
| 56 | |
| 57 | int main(int argc, char* argv[]) |
| 58 | { |
| 59 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 60 | if (velia::utils::isJournaldActive()) { |
| 61 | loggingSink = velia::utils::create_journald_sink(); |
| 62 | } else { |
| 63 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 64 | } |
| 65 | |
| 66 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true); |
| 67 | |
| 68 | velia::utils::initLogs(loggingSink); |
| 69 | spdlog::set_level(spdlog::level::info); |
| 70 | |
| 71 | try { |
| 72 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
| 73 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 74 | |
| 75 | spdlog::get("main")->debug("Opening Sysrepo connection"); |
| 76 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 77 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 78 | |
| 79 | DBUS_EVENTLOOP_START |
| 80 | |
| 81 | // initialize ietf-system |
| 82 | spdlog::get("main")->debug("Initializing Sysrepo for system models"); |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 83 | auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release"); |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 84 | |
| 85 | auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods). |
| 86 | dbusConnection->enterEventLoopAsync(); |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame^] | 87 | |
| 88 | // initialize czechlight-system:networking |
| 89 | const std::filesystem::path runtimeNetworkDirectory("/run/systemd/network"); |
| 90 | std::filesystem::create_directories(runtimeNetworkDirectory); |
| 91 | auto sysrepoNetworkRunning = velia::system::Network(srSess, runtimeNetworkDirectory, [](const std::vector<std::string>& reconfiguredInterfaces) { |
| 92 | auto log = spdlog::get("system"); |
| 93 | |
| 94 | /* Bring all the updated interfaces down (they will later be brought up by executing `networkctl reload`). |
| 95 | * |
| 96 | * This is required when transitioning from bridge to DHCP configuration. systemd-networkd apparently does not reset many |
| 97 | * interface properties when reconfiguring the interface into new "bridge-less" configuration (the interface stays in the |
| 98 | * bridge and it also does not obtain link local address). |
| 99 | * |
| 100 | * This doesn't seem to be required when transitioning from DHCP to bridge configuration. It's just a "precaution" because |
| 101 | * there might be hidden some caveats that I am unable to see now (some leftover setting). Bringing the interface |
| 102 | * down seems to reset the interface (and it is something we can afford in the interface reconfiguration process). |
| 103 | */ |
| 104 | for (const auto& interfaceName : reconfiguredInterfaces) { |
| 105 | velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"down", interfaceName}, ""); |
| 106 | } |
| 107 | |
| 108 | velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"reload"}, ""); |
| 109 | }); |
| 110 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 111 | auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 112 | |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 113 | auto srSess2 = std::make_shared<sysrepo::Session>(srConn); |
| 114 | auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword); |
| 115 | |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 116 | DBUS_EVENTLOOP_END |
| 117 | return 0; |
| 118 | } catch (std::exception& e) { |
| 119 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 120 | } |
| 121 | } |