Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 1 | #include <docopt.h> |
Tomáš Pecka | e566fd4 | 2024-01-02 17:00:10 +0100 | [diff] [blame] | 2 | #include <sdbus-c++/IProxy.h> |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 3 | #include <spdlog/sinks/ansicolor_sink.h> |
| 4 | #include <spdlog/spdlog.h> |
| 5 | #include <sysrepo-cpp/Session.hpp> |
| 6 | #include "VELIA_VERSION.h" |
| 7 | #include "main.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 8 | #include "system_vars.h" |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 9 | #include "system/Authentication.h" |
Tomáš Pecka | 08d59cd | 2021-05-24 15:33:07 +0200 | [diff] [blame] | 10 | #include "system/Firmware.h" |
Tomáš Pecka | 271d560 | 2021-04-13 21:35:34 +0200 | [diff] [blame] | 11 | #include "system/IETFInterfaces.h" |
Tomáš Pecka | 08d59cd | 2021-05-24 15:33:07 +0200 | [diff] [blame] | 12 | #include "system/IETFInterfacesConfig.h" |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame] | 13 | #include "system/IETFSystem.h" |
Tomáš Pecka | e566fd4 | 2024-01-02 17:00:10 +0100 | [diff] [blame] | 14 | #include "system/JournalUpload.h" |
Tomáš Pecka | 7226bcf | 2021-04-26 21:12:26 +0200 | [diff] [blame] | 15 | #include "system/LED.h" |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 16 | #include "system/LLDP.h" |
| 17 | #include "system/LLDPCallback.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 18 | #include "utils/exceptions.h" |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 19 | #include "utils/exec.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 20 | #include "utils/journal.h" |
| 21 | #include "utils/log-init.h" |
Tomáš Pecka | 4178470 | 2021-05-26 13:57:32 +0200 | [diff] [blame] | 22 | #include "utils/sysrepo.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 23 | |
Tomáš Pecka | e566fd4 | 2024-01-02 17:00:10 +0100 | [diff] [blame] | 24 | using namespace std::literals; |
| 25 | |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 26 | static const char usage[] = |
| 27 | R"(Sysrepo-powered system management. |
| 28 | |
| 29 | Usage: |
| 30 | veliad-system |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame] | 31 | [--main-log-level=<Level>] |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 32 | [--sysrepo-log-level=<Level>] |
| 33 | [--system-log-level=<Level>] |
| 34 | veliad-system (-h | --help) |
| 35 | veliad-system --version |
| 36 | |
| 37 | Options: |
| 38 | -h --help Show this screen. |
| 39 | --version Show version. |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame] | 40 | --main-log-level=<N> Log level for other messages [default: 2] |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 41 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 42 | 4 -> debug, 5 -> trace) |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame] | 43 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2] |
| 44 | --system-log-level=<N> Log level for the system stuff [default: 3] |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 45 | )"; |
| 46 | |
| 47 | DBUS_EVENTLOOP_INIT |
| 48 | |
| 49 | int main(int argc, char* argv[]) |
| 50 | { |
| 51 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 52 | if (velia::utils::isJournaldActive()) { |
| 53 | loggingSink = velia::utils::create_journald_sink(); |
| 54 | } else { |
| 55 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 56 | } |
| 57 | |
| 58 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true); |
| 59 | |
| 60 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 4178470 | 2021-05-26 13:57:32 +0200 | [diff] [blame] | 61 | velia::utils::initLogsSysrepo(); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 62 | spdlog::set_level(spdlog::level::info); |
| 63 | |
| 64 | try { |
Jan Kundrát | ad8169c | 2023-05-29 10:49:13 +0200 | [diff] [blame] | 65 | spdlog::get("main")->set_level(parseLogLevel("other messages", args["--main-log-level"])); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 66 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
Tomáš Pecka | 1935f19 | 2021-03-08 19:40:06 +0100 | [diff] [blame] | 67 | spdlog::get("system")->set_level(parseLogLevel("System logging", args["--system-log-level"])); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 68 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 69 | auto srConn = sysrepo::Connection{}; |
| 70 | auto srSess = srConn.sessionStart(); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 71 | |
| 72 | DBUS_EVENTLOOP_START |
| 73 | |
Tomáš Pecka | e566fd4 | 2024-01-02 17:00:10 +0100 | [diff] [blame] | 74 | auto journalUploadStartup = velia::system::JournalUpload(srConn.sessionStart(sysrepo::Datastore::Startup), "/cfg/journald-remote", [](auto) {}); |
| 75 | auto journalUploadRunning = velia::system::JournalUpload(srConn.sessionStart(sysrepo::Datastore::Running), "/run/journald-remote", [dbusConn = g_dbusConnection.get()](auto log) { |
| 76 | log->debug("Restarting systemd-journal-upload.service"); |
| 77 | auto sdManager = sdbus::createProxy(*dbusConn, "org.freedesktop.systemd1", "/org/freedesktop/systemd1"); |
| 78 | sdManager->callMethod("RestartUnit").onInterface("org.freedesktop.systemd1.Manager").withArguments("systemd-journal-upload.service"s, "replace"s); |
| 79 | }); |
| 80 | |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 81 | // initialize ietf-system |
Tomáš Pecka | 79344c8 | 2021-09-16 18:25:59 +0200 | [diff] [blame] | 82 | auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release", *g_dbusConnection, "org.freedesktop.resolve1"); |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 83 | |
| 84 | auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods). |
| 85 | dbusConnection->enterEventLoopAsync(); |
Tomáš Pecka | 5c19e72 | 2021-02-15 20:41:29 +0100 | [diff] [blame] | 86 | |
Tomáš Pecka | 271d560 | 2021-04-13 21:35:34 +0200 | [diff] [blame] | 87 | // implements ietf-interfaces and ietf-routing |
Tomáš Pecka | 08d59cd | 2021-05-24 15:33:07 +0200 | [diff] [blame] | 88 | const std::filesystem::path runtimeNetworkDirectory("/run/systemd/network"), persistentNetworkDirectory("/cfg/network/"); |
| 89 | std::filesystem::create_directories(runtimeNetworkDirectory); |
| 90 | std::filesystem::create_directories(persistentNetworkDirectory); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 91 | auto srSessStartup = srConn.sessionStart(sysrepo::Datastore::Startup); |
Tomáš Pecka | c852385 | 2023-08-30 16:59:32 +0200 | [diff] [blame] | 92 | std::vector<std::string> managedLinks = {"br0", "eth0", "eth1", "eth2", "osc", "oscE", "oscW"}; |
Tomáš Pecka | 08d59cd | 2021-05-24 15:33:07 +0200 | [diff] [blame] | 93 | |
| 94 | auto sysrepoIETFInterfacesOperational = std::make_shared<velia::system::IETFInterfaces>(srSess); |
| 95 | auto sysrepoIETFInterfacesStartup = velia::system::IETFInterfacesConfig(srSessStartup, persistentNetworkDirectory, managedLinks, [](const auto&) {}); |
| 96 | auto sysrepoIETFInterfacesRunning = velia::system::IETFInterfacesConfig(srSess, runtimeNetworkDirectory, managedLinks, [](const auto& reconfiguredInterfaces) { |
| 97 | auto log = spdlog::get("system"); |
| 98 | |
| 99 | /* Bring all the updated interfaces down (they will later be brought up by executing `networkctl reload`). |
| 100 | * |
| 101 | * This is required when transitioning from bridge to DHCP configuration. systemd-networkd apparently does not reset many |
| 102 | * interface properties when reconfiguring the interface into new "bridge-less" configuration (the interface stays in the |
| 103 | * bridge and it also does not obtain link local address). |
| 104 | * |
| 105 | * This doesn't seem to be required when transitioning from DHCP to bridge configuration. It's just a "precaution" because |
| 106 | * there might be hidden some caveats that I am unable to see now (some leftover setting). Bringing the interface |
| 107 | * down seems to reset the interface (and it is something we can afford in the interface reconfiguration process). |
| 108 | */ |
| 109 | for (const auto& interfaceName : reconfiguredInterfaces) { |
| 110 | velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"down", interfaceName}, ""); |
| 111 | } |
| 112 | |
| 113 | velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"reload"}, ""); |
| 114 | }); |
Tomáš Pecka | 271d560 | 2021-04-13 21:35:34 +0200 | [diff] [blame] | 115 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 116 | auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 117 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 118 | auto srSess2 = srConn.sessionStart(); |
Václav Kubernát | babbab9 | 2021-01-27 09:25:05 +0100 | [diff] [blame] | 119 | auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword); |
| 120 | |
Tomáš Pecka | 7226bcf | 2021-04-26 21:12:26 +0200 | [diff] [blame] | 121 | auto leds = velia::system::LED(srConn, "/sys/class/leds"); |
| 122 | |
Tomáš Pecka | 070f60d | 2021-10-13 21:48:14 +0200 | [diff] [blame] | 123 | auto lldp = std::make_shared<velia::system::LLDPDataProvider>([]() { return velia::utils::execAndWait(spdlog::get("system"), NETWORKCTL_EXECUTABLE, {"lldp", "--json=short"}, ""); }); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 124 | auto srSubs = srSess.onOperGet("czechlight-lldp", velia::system::LLDPCallback(lldp), "/czechlight-lldp:nbr-list"); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 125 | |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 126 | DBUS_EVENTLOOP_END |
| 127 | return 0; |
| 128 | } catch (std::exception& e) { |
| 129 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 130 | } |
| 131 | } |