blob: 77f5e4f54548ec3d8a2c1e5a7715bf72fe878209 [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"
9#include "system_vars.h"
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010010#include "system/IETFSystem.h"
Tomáš Pecka625b7a12021-01-23 20:55:51 +010011#include "utils/exceptions.h"
12#include "utils/journal.h"
13#include "utils/log-init.h"
14
15/** @short Extract log level from a CLI option */
16spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
17{
18 long x;
19 try {
20 x = option.asLong();
21 } catch (std::invalid_argument&) {
22 throw std::runtime_error(name + " log level: expecting integer");
23 }
24 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
25 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
26 if (x < 0 || x > 5)
27 throw std::runtime_error(name + " log level invalid or out-of-range");
28
29 return static_cast<spdlog::level::level_enum>(5 - x);
30}
31
32static const char usage[] =
33 R"(Sysrepo-powered system management.
34
35Usage:
36 veliad-system
37 [--log-level=<Level>]
38 [--sysrepo-log-level=<Level>]
39 [--system-log-level=<Level>]
40 veliad-system (-h | --help)
41 veliad-system --version
42
43Options:
44 -h --help Show this screen.
45 --version Show version.
46 --log-level=<N> Log level for everything [default: 3]
47 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
48 4 -> debug, 5 -> trace)
49 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3]
50 --system-log-level=<N> Log level for the system stuff [default: 3]
51)";
52
53DBUS_EVENTLOOP_INIT
54
55int main(int argc, char* argv[])
56{
57 std::shared_ptr<spdlog::sinks::sink> loggingSink;
58 if (velia::utils::isJournaldActive()) {
59 loggingSink = velia::utils::create_journald_sink();
60 } else {
61 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
62 }
63
64 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true);
65
66 velia::utils::initLogs(loggingSink);
67 spdlog::set_level(spdlog::level::info);
68
69 try {
70 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
71 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
72
73 spdlog::get("main")->debug("Opening Sysrepo connection");
74 auto srConn = std::make_shared<sysrepo::Connection>();
75 auto srSess = std::make_shared<sysrepo::Session>(srConn);
76
77 DBUS_EVENTLOOP_START
78
79 // initialize ietf-system
80 spdlog::get("main")->debug("Initializing Sysrepo for system models");
Tomáš Peckaf976c5b2021-01-23 21:19:52 +010081 auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release");
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010082
83 auto dbusConnection = sdbus::createConnection(); // second connection for RAUC (for calling methods).
84 dbusConnection->enterEventLoopAsync();
85 auto sysrepoFirmware = velia::system::Firmware(srConn, *g_dbusConnection, *dbusConnection);
Tomáš Pecka625b7a12021-01-23 20:55:51 +010086
Václav Kubernátbabbab92021-01-27 09:25:05 +010087 auto srSess2 = std::make_shared<sysrepo::Session>(srConn);
88 auto authentication = velia::system::Authentication(srSess2, REAL_ETC_PASSWD_FILE, REAL_ETC_SHADOW_FILE, AUTHORIZED_KEYS_FORMAT, velia::system::impl::changePassword);
89
Tomáš Pecka625b7a12021-01-23 20:55:51 +010090 DBUS_EVENTLOOP_END
91 return 0;
92 } catch (std::exception& e) {
93 velia::utils::fatalException(spdlog::get("main"), e, "main");
94 }
95}