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