blob: f725d8e5d094ac8eb56ff4596b079d201936c73f [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áš Pecka594a6762021-01-29 11:06:08 +01007#include "system/Firmware.h"
Václav Kubernátbabbab92021-01-27 09:25:05 +01008#include "system/Authentication.h"
Tomáš Pecka5c19e722021-02-15 20:41:29 +01009#include "system/Network.h"
Václav Kubernátbabbab92021-01-27 09:25:05 +010010#include "system_vars.h"
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010011#include "system/IETFSystem.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010012#include "utils/exceptions.h"
Tomáš Pecka5c19e722021-02-15 20:41:29 +010013#include "utils/exec.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010014#include "utils/journal.h"
Václav Kubernát6018f082021-02-11 01:32:18 +010015#include "utils/log.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010016#include "utils/log-init.h"
17
Tomáš Pecka625b7a12021-01-23 20:55:51 +010018static const char usage[] =
19 R"(Sysrepo-powered system management.
20
21Usage:
22 veliad-system
Tomáš Pecka625b7a12021-01-23 20:55:51 +010023 [--sysrepo-log-level=<Level>]
24 [--system-log-level=<Level>]
25 veliad-system (-h | --help)
26 veliad-system --version
27
28Options:
29 -h --help Show this screen.
30 --version Show version.
Václav Kubernát5f57dec2021-02-11 02:02:46 +010031 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010032 --system-log-level=<N> Log level for the system stuff [default: 3]
Tomáš Pecka625b7a12021-01-23 20:55:51 +010033 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
34 4 -> debug, 5 -> trace)
Tomáš Pecka625b7a12021-01-23 20:55:51 +010035)";
36
37DBUS_EVENTLOOP_INIT
38
39int main(int argc, char* argv[])
40{
41 std::shared_ptr<spdlog::sinks::sink> loggingSink;
42 if (velia::utils::isJournaldActive()) {
43 loggingSink = velia::utils::create_journald_sink();
44 } else {
45 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
46 }
47
48 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true);
49
50 velia::utils::initLogs(loggingSink);
51 spdlog::set_level(spdlog::level::info);
52
53 try {
Tomáš Pecka625b7a12021-01-23 20:55:51 +010054 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
Tomáš Pecka1935f192021-03-08 19:40:06 +010055 spdlog::get("system")->set_level(parseLogLevel("System logging", args["--system-log-level"]));
Tomáš Pecka625b7a12021-01-23 20:55:51 +010056
Tomáš Pecka625b7a12021-01-23 20:55:51 +010057 auto srConn = std::make_shared<sysrepo::Connection>();
58 auto srSess = std::make_shared<sysrepo::Session>(srConn);
59
60 DBUS_EVENTLOOP_START
61
62 // initialize ietf-system
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010063 auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release");
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010064
65 auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods).
66 dbusConnection->enterEventLoopAsync();
Tomáš Pecka5c19e722021-02-15 20:41:29 +010067
68 // initialize czechlight-system:networking
Tomáš Peckaa936ee72021-02-15 21:37:10 +010069 const std::filesystem::path runtimeNetworkDirectory("/run/systemd/network"), persistentNetworkDirectory("/cfg/network/");
Tomáš Pecka5c19e722021-02-15 20:41:29 +010070 std::filesystem::create_directories(runtimeNetworkDirectory);
Tomáš Peckaa936ee72021-02-15 21:37:10 +010071 std::filesystem::create_directories(persistentNetworkDirectory);
72
73 auto srSessStartup = std::make_shared<sysrepo::Session>(srConn, SR_DS_STARTUP);
74
Jan Kundrátef2b3802021-02-18 09:57:05 +010075 auto sysrepoNetworkStartup = velia::system::Network(srSess, persistentNetworkDirectory, [](const auto&) {});
76 auto sysrepoNetworkRunning = velia::system::Network(srSess, runtimeNetworkDirectory, [](const auto& reconfiguredInterfaces) {
Tomáš Pecka5c19e722021-02-15 20:41:29 +010077 auto log = spdlog::get("system");
78
79 /* Bring all the updated interfaces down (they will later be brought up by executing `networkctl reload`).
80 *
81 * This is required when transitioning from bridge to DHCP configuration. systemd-networkd apparently does not reset many
82 * interface properties when reconfiguring the interface into new "bridge-less" configuration (the interface stays in the
83 * bridge and it also does not obtain link local address).
84 *
85 * This doesn't seem to be required when transitioning from DHCP to bridge configuration. It's just a "precaution" because
86 * there might be hidden some caveats that I am unable to see now (some leftover setting). Bringing the interface
87 * down seems to reset the interface (and it is something we can afford in the interface reconfiguration process).
88 */
89 for (const auto& interfaceName : reconfiguredInterfaces) {
90 velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"down", interfaceName}, "");
91 }
92
93 velia::utils::execAndWait(log, NETWORKCTL_EXECUTABLE, {"reload"}, "");
94 });
95
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010096 auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection);
Tomáš Pecka625b7a12021-01-23 20:55:51 +010097
Václav Kubernátbabbab92021-01-27 09:25:05 +010098 auto srSess2 = std::make_shared<sysrepo::Session>(srConn);
99 auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword);
100
Tomáš Pecka625b7a12021-01-23 20:55:51 +0100101 DBUS_EVENTLOOP_END
102 return 0;
103 } catch (std::exception& e) {
104 velia::utils::fatalException(spdlog::get("main"), e, "main");
105 }
106}