blob: c51c1109f49c14887198922fd22ee6c23d63427c [file] [log] [blame]
Tomáš Pecka625b7a12021-01-23 20:55:51 +01001#include <docopt.h>
Tomáš Peckae566fd42024-01-02 17:00:10 +01002#include <sdbus-c++/IProxy.h>
Tomáš Pecka625b7a12021-01-23 20:55:51 +01003#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áš Pecka7acf3922021-08-10 11:16:57 +02008#include "system_vars.h"
Václav Kubernátbabbab92021-01-27 09:25:05 +01009#include "system/Authentication.h"
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020010#include "system/Firmware.h"
Tomáš Pecka271d5602021-04-13 21:35:34 +020011#include "system/IETFInterfaces.h"
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020012#include "system/IETFInterfacesConfig.h"
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010013#include "system/IETFSystem.h"
Tomáš Peckae566fd42024-01-02 17:00:10 +010014#include "system/JournalUpload.h"
Tomáš Pecka7226bcf2021-04-26 21:12:26 +020015#include "system/LED.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +020016#include "system/LLDP.h"
17#include "system/LLDPCallback.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010018#include "utils/exceptions.h"
Tomáš Pecka5c19e722021-02-15 20:41:29 +010019#include "utils/exec.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010020#include "utils/journal.h"
21#include "utils/log-init.h"
Tomáš Pecka41784702021-05-26 13:57:32 +020022#include "utils/sysrepo.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010023
Tomáš Peckae566fd42024-01-02 17:00:10 +010024using namespace std::literals;
25
Tomáš Pecka625b7a12021-01-23 20:55:51 +010026static const char usage[] =
27 R"(Sysrepo-powered system management.
28
29Usage:
30 veliad-system
Jan Kundrátad8169c2023-05-29 10:49:13 +020031 [--main-log-level=<Level>]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010032 [--sysrepo-log-level=<Level>]
33 [--system-log-level=<Level>]
34 veliad-system (-h | --help)
35 veliad-system --version
36
37Options:
38 -h --help Show this screen.
39 --version Show version.
Jan Kundrátad8169c2023-05-29 10:49:13 +020040 --main-log-level=<N> Log level for other messages [default: 2]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010041 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
42 4 -> debug, 5 -> trace)
Jan Kundrátad8169c2023-05-29 10:49:13 +020043 --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áš Pecka625b7a12021-01-23 20:55:51 +010045)";
46
47DBUS_EVENTLOOP_INIT
48
49int 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áš Pecka41784702021-05-26 13:57:32 +020061 velia::utils::initLogsSysrepo();
Tomáš Pecka625b7a12021-01-23 20:55:51 +010062 spdlog::set_level(spdlog::level::info);
63
64 try {
Jan Kundrátad8169c2023-05-29 10:49:13 +020065 spdlog::get("main")->set_level(parseLogLevel("other messages", args["--main-log-level"]));
Tomáš Pecka625b7a12021-01-23 20:55:51 +010066 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
Tomáš Pecka1935f192021-03-08 19:40:06 +010067 spdlog::get("system")->set_level(parseLogLevel("System logging", args["--system-log-level"]));
Tomáš Pecka625b7a12021-01-23 20:55:51 +010068
Václav Kubernát7efd6d52021-11-09 01:31:11 +010069 auto srConn = sysrepo::Connection{};
70 auto srSess = srConn.sessionStart();
Tomáš Pecka625b7a12021-01-23 20:55:51 +010071
72 DBUS_EVENTLOOP_START
73
Tomáš Peckae566fd42024-01-02 17:00:10 +010074 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áš Pecka625b7a12021-01-23 20:55:51 +010081 // initialize ietf-system
Tomáš Pecka79344c82021-09-16 18:25:59 +020082 auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release", *g_dbusConnection, "org.freedesktop.resolve1");
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010083
84 auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods).
85 dbusConnection->enterEventLoopAsync();
Tomáš Pecka5c19e722021-02-15 20:41:29 +010086
Tomáš Pecka271d5602021-04-13 21:35:34 +020087 // implements ietf-interfaces and ietf-routing
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020088 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át7efd6d52021-11-09 01:31:11 +010091 auto srSessStartup = srConn.sessionStart(sysrepo::Datastore::Startup);
Tomáš Peckac8523852023-08-30 16:59:32 +020092 std::vector<std::string> managedLinks = {"br0", "eth0", "eth1", "eth2", "osc", "oscE", "oscW"};
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020093
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áš Pecka271d5602021-04-13 21:35:34 +0200115
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100116 auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection);
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100117
Václav Kubernát7efd6d52021-11-09 01:31:11 +0100118 auto srSess2 = srConn.sessionStart();
Václav Kubernátbabbab92021-01-27 09:25:05 +0100119 auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword);
120
Tomáš Pecka7226bcf2021-04-26 21:12:26 +0200121 auto leds = velia::system::LED(srConn, "/sys/class/leds");
122
Tomáš Pecka070f60d2021-10-13 21:48:14 +0200123 auto lldp = std::make_shared<velia::system::LLDPDataProvider>([]() { return velia::utils::execAndWait(spdlog::get("system"), NETWORKCTL_EXECUTABLE, {"lldp", "--json=short"}, ""); });
Václav Kubernát7efd6d52021-11-09 01:31:11 +0100124 auto srSubs = srSess.onOperGet("czechlight-lldp", velia::system::LLDPCallback(lldp), "/czechlight-lldp:nbr-list");
Tomáš Pecka7acf3922021-08-10 11:16:57 +0200125
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100126 DBUS_EVENTLOOP_END
127 return 0;
128 } catch (std::exception& e) {
129 velia::utils::fatalException(spdlog::get("main"), e, "main");
130 }
131}