blob: fe367db56bed619cd9179d07a37b7037a65280aa [file] [log] [blame]
Tomáš Pecka625b7a12021-01-23 20:55:51 +01001#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áš Pecka7acf3922021-08-10 11:16:57 +02007#include "system_vars.h"
Václav Kubernátbabbab92021-01-27 09:25:05 +01008#include "system/Authentication.h"
Tomáš Pecka08d59cd2021-05-24 15:33:07 +02009#include "system/Firmware.h"
Tomáš Pecka271d5602021-04-13 21:35:34 +020010#include "system/IETFInterfaces.h"
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020011#include "system/IETFInterfacesConfig.h"
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010012#include "system/IETFSystem.h"
Tomáš Pecka7226bcf2021-04-26 21:12:26 +020013#include "system/LED.h"
Tomáš Pecka7acf3922021-08-10 11:16:57 +020014#include "system/LLDP.h"
15#include "system/LLDPCallback.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010016#include "utils/exceptions.h"
Tomáš Pecka5c19e722021-02-15 20:41:29 +010017#include "utils/exec.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010018#include "utils/journal.h"
19#include "utils/log-init.h"
Tomáš Pecka41784702021-05-26 13:57:32 +020020#include "utils/sysrepo.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010021
Tomáš Pecka625b7a12021-01-23 20:55:51 +010022static const char usage[] =
23 R"(Sysrepo-powered system management.
24
25Usage:
26 veliad-system
Tomáš Pecka625b7a12021-01-23 20:55:51 +010027 [--sysrepo-log-level=<Level>]
28 [--system-log-level=<Level>]
29 veliad-system (-h | --help)
30 veliad-system --version
31
32Options:
33 -h --help Show this screen.
34 --version Show version.
Václav Kubernát5f57dec2021-02-11 02:02:46 +010035 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010036 --system-log-level=<N> Log level for the system stuff [default: 3]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010037 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
38 4 -> debug, 5 -> trace)
Tomáš Pecka625b7a12021-01-23 20:55:51 +010039)";
40
41DBUS_EVENTLOOP_INIT
42
43int main(int argc, char* argv[])
44{
45 std::shared_ptr<spdlog::sinks::sink> loggingSink;
46 if (velia::utils::isJournaldActive()) {
47 loggingSink = velia::utils::create_journald_sink();
48 } else {
49 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
50 }
51
52 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true);
53
54 velia::utils::initLogs(loggingSink);
Tomáš Pecka41784702021-05-26 13:57:32 +020055 velia::utils::initLogsSysrepo();
Tomáš Pecka625b7a12021-01-23 20:55:51 +010056 spdlog::set_level(spdlog::level::info);
57
58 try {
Tomáš Pecka625b7a12021-01-23 20:55:51 +010059 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
Tomáš Pecka1935f192021-03-08 19:40:06 +010060 spdlog::get("system")->set_level(parseLogLevel("System logging", args["--system-log-level"]));
Tomáš Pecka625b7a12021-01-23 20:55:51 +010061
Tomáš Pecka625b7a12021-01-23 20:55:51 +010062 auto srConn = std::make_shared<sysrepo::Connection>();
63 auto srSess = std::make_shared<sysrepo::Session>(srConn);
64
65 DBUS_EVENTLOOP_START
66
67 // initialize ietf-system
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010068 auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release");
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010069
70 auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods).
71 dbusConnection->enterEventLoopAsync();
Tomáš Pecka5c19e722021-02-15 20:41:29 +010072
Tomáš Pecka271d5602021-04-13 21:35:34 +020073 // implements ietf-interfaces and ietf-routing
Tomáš Pecka08d59cd2021-05-24 15:33:07 +020074 const std::filesystem::path runtimeNetworkDirectory("/run/systemd/network"), persistentNetworkDirectory("/cfg/network/");
75 std::filesystem::create_directories(runtimeNetworkDirectory);
76 std::filesystem::create_directories(persistentNetworkDirectory);
77 auto srSessStartup = std::make_shared<sysrepo::Session>(srConn, SR_DS_STARTUP);
78 std::vector<std::string> managedLinks = {"br0", "eth0", "eth1", "osc", "oscE", "oscW"};
79
80 auto sysrepoIETFInterfacesOperational = std::make_shared<velia::system::IETFInterfaces>(srSess);
81 auto sysrepoIETFInterfacesStartup = velia::system::IETFInterfacesConfig(srSessStartup, persistentNetworkDirectory, managedLinks, [](const auto&) {});
82 auto sysrepoIETFInterfacesRunning = velia::system::IETFInterfacesConfig(srSess, runtimeNetworkDirectory, managedLinks, [](const auto& reconfiguredInterfaces) {
83 auto log = spdlog::get("system");
84
85 /* Bring all the updated interfaces down (they will later be brought up by executing `networkctl reload`).
86 *
87 * This is required when transitioning from bridge to DHCP configuration. systemd-networkd apparently does not reset many
88 * interface properties when reconfiguring the interface into new "bridge-less" configuration (the interface stays in the
89 * bridge and it also does not obtain link local address).
90 *
91 * This doesn't seem to be required when transitioning from DHCP to bridge configuration. It's just a "precaution" because
92 * there might be hidden some caveats that I am unable to see now (some leftover setting). Bringing the interface
93 * down seems to reset the interface (and it is something we can afford in the interface reconfiguration process).
94 */
95 for (const auto& interfaceName : reconfiguredInterfaces) {
96 velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"down", interfaceName}, "");
97 }
98
99 velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"reload"}, "");
100 });
Tomáš Pecka271d5602021-04-13 21:35:34 +0200101
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100102 auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection);
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100103
Václav Kubernátbabbab92021-01-27 09:25:05 +0100104 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áš Pecka7226bcf2021-04-26 21:12:26 +0200107 auto leds = velia::system::LED(srConn, "/sys/class/leds");
108
Tomáš Pecka7acf3922021-08-10 11:16:57 +0200109 auto lldp = std::make_shared<velia::system::LLDPDataProvider>("/run/systemd/netif/lldp", *g_dbusConnection, "org.freedesktop.network1");
110 auto srSubs = std::make_shared<sysrepo::Subscribe>(srSess);
111 srSubs->oper_get_items_subscribe("czechlight-lldp", velia::system::LLDPCallback(lldp), "/czechlight-lldp:nbr-list");
112
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100113 DBUS_EVENTLOOP_END
114 return 0;
115 } catch (std::exception& e) {
116 velia::utils::fatalException(spdlog::get("main"), e, "main");
117 }
118}