blob: 0635c28faeb7ad98c94d55f580dfd56b53ae517a [file] [log] [blame]
Jan Kundrátdc2b0722018-03-02 14:13:37 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Jan Kundráta2740442018-03-22 16:56:43 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Jan Kundrátdc2b0722018-03-02 14:13:37 +01004 *
Václav Kubernát624a8872018-03-02 17:28:47 +01005 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
Jan Kundrátdc2b0722018-03-02 14:13:37 +01006 *
7*/
Václav Kubernátfd261162020-11-12 02:36:29 +01008#include <atomic>
Václav Kubernát2bed9522020-12-01 03:37:41 +01009#include <boost/algorithm/string.hpp>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010010#include <docopt.h>
Václav Kubernát624a8872018-03-02 17:28:47 +010011#include <iostream>
Václav Kubernát435706e2019-02-20 18:05:59 +010012#include <optional>
Václav Kubernáta395d332019-02-13 16:49:20 +010013#include <replxx.hxx>
Václav Kubernát90de9502019-11-20 17:19:44 +010014#include <sstream>
Jan Kundrátdc2b0722018-03-02 14:13:37 +010015#include "NETCONF_CLI_VERSION.h"
Václav Kubernát96344a12018-05-28 16:33:39 +020016#include "interpreter.hpp"
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020017#include "proxy_datastore.hpp"
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010018#include "yang_schema.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010019#if defined(SYSREPO_CLI)
Václav Kubernát6415b822018-08-22 17:40:01 +020020#include "sysrepo_access.hpp"
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010021#define PROGRAM_NAME "sysrepo-cli"
Václav Kubernát715c85c2020-04-14 01:46:08 +020022static const auto usage = R"(CLI interface to sysrepo
23
24Usage:
Václav Kubernátf5d75152020-12-03 03:52:34 +010025 sysrepo-cli [-d <datastore_target>]
Václav Kubernát715c85c2020-04-14 01:46:08 +020026 sysrepo-cli (-h | --help)
27 sysrepo-cli --version
28
29Options:
Václav Kubernátf5d75152020-12-03 03:52:34 +010030 -d <datastore_target> can be "running", "startup" or "operational" [default: operational])";
Václav Kubernát74487df2020-06-04 01:29:28 +020031#elif defined(YANG_CLI)
32#include <boost/spirit/home/x3.hpp>
Václav Kubernát619e6542020-06-29 14:13:43 +020033#include <filesystem>
Václav Kubernát74487df2020-06-04 01:29:28 +020034#include "yang_access.hpp"
35#define PROGRAM_NAME "yang-cli"
36static const auto usage = R"(CLI interface for creating local YANG data instances
37
Václav Kubernát619e6542020-06-29 14:13:43 +020038 The <schema_file_or_module_name> argument is treated as a file name if a file
39 with such a path exists, otherwise it's treated as a module name. Search dirs
40 will be used to find a schema for that module.
41
Václav Kubernát74487df2020-06-04 01:29:28 +020042Usage:
Václav Kubernát28cf3362020-06-29 17:52:51 +020043 yang-cli [--configonly] [-s <search_dir>] [-e enable_features]... [-i data_file]... <schema_file_or_module_name>...
Václav Kubernát74487df2020-06-04 01:29:28 +020044 yang-cli (-h | --help)
45 yang-cli --version
46
47Options:
48 -s <search_dir> Set search for schema lookup
Václav Kubernát548cb192020-06-26 14:00:42 +020049 -e <enable_features> Feature to enable after modules are loaded. This option can be supplied more than once. Format: <module_name>:<feature>
Václav Kubernát28cf3362020-06-29 17:52:51 +020050 -i <data_file> File to import data from
51 --configonly Disable editing of operational data)";
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010052#elif defined(NETCONF_CLI)
53// FIXME: improve usage
54static const auto usage = R"(CLI interface for NETCONF
55
56Usage:
Václav Kubernátf5d75152020-12-03 03:52:34 +010057 netconf-cli [-v] [-d <datastore_target>] [-p <port>] <host>
58 netconf-cli [-v] [-d <datastore_target>] --socket <path>
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010059 netconf-cli (-h | --help)
60 netconf-cli --version
61
62Options:
63 -v enable verbose mode
64 -p <port> port number [default: 830]
Václav Kubernátf5d75152020-12-03 03:52:34 +010065 -d <datastore_target> can be "running", "startup" or "operational" [default: operational])";
Václav Kubernáte2e15ee2020-02-05 17:38:13 +010066#include "cli-netconf.hpp"
Václav Kubernátb4e5b182020-11-16 19:55:09 +010067#include "netconf_access.hpp"
Jan Kundrát9435bc02021-01-04 18:03:01 +010068#define PROGRAM_NAME "netconf-cli"
Václav Kubernátfd261162020-11-12 02:36:29 +010069// FIXME: this should be replaced by C++20 std::jthread at some point
70struct PoorMansJThread {
71 ~PoorMansJThread()
72 {
73 if (thread.joinable()) {
74 thread.join();
75 }
76 }
77 std::thread thread;
78};
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010079#else
80#error "Unknown CLI backend"
81#endif
Jan Kundrátdc2b0722018-03-02 14:13:37 +010082
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010083const auto HISTORY_FILE_NAME = PROGRAM_NAME "_history";
84
Jan Kundrátdc2b0722018-03-02 14:13:37 +010085int main(int argc, char* argv[])
86{
87 auto args = docopt::docopt(usage,
88 {argv + 1, argv + argc},
89 true,
Václav Kubernátb79f3ca2020-02-04 15:56:01 +010090 PROGRAM_NAME " " NETCONF_CLI_VERSION,
Jan Kundrátdc2b0722018-03-02 14:13:37 +010091 true);
Václav Kubernát28cf3362020-06-29 17:52:51 +020092 WritableOps writableOps = WritableOps::No;
Václav Kubernátff2c9f62018-05-16 20:26:31 +020093
Václav Kubernátfd261162020-11-12 02:36:29 +010094 using replxx::Replxx;
95 Replxx lineEditor;
96 std::atomic<int> backendReturnCode = 0;
97
Václav Kubernátf5d75152020-12-03 03:52:34 +010098 // For some reason, GCC10 still needs [[maybe_unused]] because of conditional compilation...
99 [[maybe_unused]] auto datastoreTarget = DatastoreTarget::Operational;
Václav Kubernát715c85c2020-04-14 01:46:08 +0200100 if (const auto& ds = args["-d"]) {
101 if (ds.asString() == "startup") {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100102 datastoreTarget = DatastoreTarget::Startup;
Václav Kubernát715c85c2020-04-14 01:46:08 +0200103 } else if (ds.asString() == "running") {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100104 datastoreTarget = DatastoreTarget::Running;
105 } else if (ds.asString() == "operational") {
106 datastoreTarget = DatastoreTarget::Operational;
Václav Kubernát715c85c2020-04-14 01:46:08 +0200107 } else {
Václav Kubernátf5d75152020-12-03 03:52:34 +0100108 std::cerr << PROGRAM_NAME << ": unknown datastore target: " << ds.asString() << "\n";
Václav Kubernát715c85c2020-04-14 01:46:08 +0200109 return 1;
110 }
111 }
Václav Kubernátf5d75152020-12-03 03:52:34 +0100112
113 auto datastoreTargetString = args["-d"] ? args["-d"].asString() : std::string("operational");
114
115#if defined(SYSREPO_CLI)
116 auto datastore = std::make_shared<SysrepoAccess>();
117 std::cout << "Connected to sysrepo [datastore target: " << datastoreTargetString << "]" << std::endl;
Václav Kubernát74487df2020-06-04 01:29:28 +0200118#elif defined(YANG_CLI)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200119 auto datastore = std::make_shared<YangAccess>();
Václav Kubernát28cf3362020-06-29 17:52:51 +0200120 if (args["--configonly"].asBool()) {
121 writableOps = WritableOps::No;
122 } else {
123 writableOps = WritableOps::Yes;
124 std::cout << "ops is writable" << std::endl;
125 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200126 if (const auto& search_dir = args["-s"]) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200127 datastore->addSchemaDir(search_dir.asString());
Václav Kubernát74487df2020-06-04 01:29:28 +0200128 }
Václav Kubernát619e6542020-06-29 14:13:43 +0200129 for (const auto& schemaFile : args["<schema_file_or_module_name>"].asStringList()) {
130 if (std::filesystem::exists(schemaFile)) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200131 datastore->addSchemaFile(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +0200132 } else if (schemaFile.find('/') == std::string::npos) { // Module names cannot have a slash
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200133 datastore->loadModule(schemaFile);
Václav Kubernát619e6542020-06-29 14:13:43 +0200134 } else {
135 std::cerr << "Cannot load YANG module " << schemaFile << "\n";
136 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200137 }
138 if (const auto& enableFeatures = args["-e"]) {
139 namespace x3 = boost::spirit::x3;
140 auto grammar = +(x3::char_-":") >> ":" >> +(x3::char_-":");
141 for (const auto& enableFeature : enableFeatures.asStringList()) {
142 std::pair<std::string, std::string> parsed;
143 auto it = enableFeature.begin();
144 auto res = x3::parse(it, enableFeature.cend(), grammar, parsed);
145 if (!res || it != enableFeature.cend()) {
146 std::cerr << "Error parsing feature enable flags: " << enableFeature << "\n";
147 return 1;
148 }
149 try {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200150 datastore->enableFeature(parsed.first, parsed.second);
Václav Kubernát74487df2020-06-04 01:29:28 +0200151 } catch (std::runtime_error& ex) {
152 std::cerr << ex.what() << "\n";
153 return 1;
154 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200155 }
156 }
Václav Kubernát548cb192020-06-26 14:00:42 +0200157 if (const auto& dataFiles = args["-i"]) {
158 for (const auto& dataFile : dataFiles.asStringList()) {
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200159 datastore->addDataFile(dataFile);
Václav Kubernát548cb192020-06-26 14:00:42 +0200160 }
161 }
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100162#elif defined(NETCONF_CLI)
163 auto verbose = args.at("-v").asBool();
164 if (verbose) {
165 NetconfAccess::setNcLogLevel(NC_VERB_DEBUG);
166 }
167
168 SshProcess process;
Václav Kubernátfd261162020-11-12 02:36:29 +0100169 PoorMansJThread processWatcher;
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100170 std::shared_ptr<NetconfAccess> datastore;
171
Jan Kundrát1ccf0d42021-02-05 09:09:57 +0100172 if (args.at("--socket").asBool()) {
173 try {
174 datastore = std::make_shared<NetconfAccess>(args.at("<path>").asString());
175 } catch (std::runtime_error& ex) {
176 std::cerr << "UNIX socket connection failed: " << ex.what() << std::endl;
177 return 1;
178 }
179 } else {
180 try {
181 process = sshProcess(args.at("<host>").asString(), args.at("-p").asString());
182 processWatcher.thread = std::thread{std::thread{[&process, &lineEditor, &backendReturnCode] () {
183 process.process.wait();
184 backendReturnCode = process.process.exit_code();
185 // CTRL-U clears from the cursor to the start of the line
186 // CTRL-K clears from the cursor to the end of the line
187 // CTRL-D send EOF
188 lineEditor.emulate_key_press(replxx::Replxx::KEY::control('U'));
189 lineEditor.emulate_key_press(replxx::Replxx::KEY::control('K'));
190 lineEditor.emulate_key_press(replxx::Replxx::KEY::control('D'));
191 }}};
192 datastore = std::make_shared<NetconfAccess>(process.std_out.native_source(), process.std_in.native_sink());
193 } catch (std::runtime_error& ex) {
194 std::cerr << "SSH connection failed: " << ex.what() << std::endl;
195 return 1;
196 }
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100197 }
Václav Kubernátf5d75152020-12-03 03:52:34 +0100198 std::cout << "Connected via NETCONF [datastore target: " << datastoreTargetString << "]" << std::endl;
Václav Kubernátb79f3ca2020-02-04 15:56:01 +0100199#else
200#error "Unknown CLI backend"
201#endif
202
Václav Kubernátf5d75152020-12-03 03:52:34 +0100203 datastore->setTarget(datastoreTarget);
204
Václav Kubernáte2e15ee2020-02-05 17:38:13 +0100205#if defined(SYSREPO_CLI) || defined(NETCONF_CLI)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200206 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
207 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
208 };
209#elif defined(YANG_CLI)
210 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>&) {
211 return nullptr;
212 };
213#endif
214
215 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200216 auto dataQuery = std::make_shared<DataQuery>(*datastore);
217 Parser parser(datastore->schema(), writableOps, dataQuery);
Václav Kubernát395d92c2020-01-24 12:18:18 +0100218
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100219 lineEditor.bind_key(Replxx::KEY::meta(Replxx::KEY::BACKSPACE), [&lineEditor](const auto& code) {
Václav Kubernát48637292020-07-08 16:54:13 +0200220 return lineEditor.invoke(Replxx::ACTION::KILL_TO_BEGINING_OF_WORD, code);
221 });
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100222 lineEditor.bind_key(Replxx::KEY::control('W'), [&lineEditor](const auto& code) {
Václav Kubernát48637292020-07-08 16:54:13 +0200223 return lineEditor.invoke(Replxx::ACTION::KILL_TO_WHITESPACE_ON_LEFT, code);
224 });
Václav Kubernát395d92c2020-01-24 12:18:18 +0100225
226 lineEditor.set_word_break_characters("\t _[]/:'\"=-%");
227
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100228 lineEditor.set_completion_callback([&parser](const std::string& input, int& context) {
Václav Kubernáta395d332019-02-13 16:49:20 +0100229 std::stringstream stream;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100230 auto completions = parser.completeCommand(input, stream);
Václav Kubernáta395d332019-02-13 16:49:20 +0100231
Jan Kundrát8d8efe82019-10-18 10:21:36 +0200232 std::vector<replxx::Replxx::Completion> res;
Václav Kubernát1ed4aa32020-01-23 13:13:28 +0100233 std::copy(completions.m_completions.begin(), completions.m_completions.end(), std::back_inserter(res));
234 context = completions.m_contextLength;
Václav Kubernáta395d332019-02-13 16:49:20 +0100235 return res;
236 });
Václav Kubernát2b684612018-08-09 18:55:24 +0200237
Václav Kubernát435706e2019-02-20 18:05:59 +0100238 std::optional<std::string> historyFile;
239 if (auto xdgHome = getenv("XDG_DATA_HOME")) {
240 historyFile = std::string(xdgHome) + "/" + HISTORY_FILE_NAME;
241 } else if (auto home = getenv("HOME")) {
242 historyFile = std::string(home) + "/.local/share/" + HISTORY_FILE_NAME;
243 }
244
Václav Kubernát3a433232020-07-08 17:52:50 +0200245 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100246 lineEditor.history_load(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200247 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100248
Václav Kubernátfd261162020-11-12 02:36:29 +0100249 while (backendReturnCode == 0) {
Václav Kubernát2bed9522020-12-01 03:37:41 +0100250 auto fullContextPath = parser.currentNode();
251 std::string prompt;
252 if (auto activeRpcPath = proxyDatastore.inputDatastorePath()) {
253 auto rpcPrefixLength = activeRpcPath->size();
254 prompt = "(prepare: " + *activeRpcPath + ") " + fullContextPath.substr(rpcPrefixLength);
255 } else {
256 prompt = fullContextPath;
257 }
258
259 prompt += "> ";
260
261 auto line = lineEditor.input(prompt);
Václav Kubernáta395d332019-02-13 16:49:20 +0100262 if (!line) {
Václav Kubernát82bf1312019-11-05 11:19:26 +0100263 // If user pressed CTRL-C to abort the line, errno gets set to EAGAIN.
264 // If user pressed CTRL-D (for EOF), errno doesn't get set to EAGAIN, so we exit the program.
265 // I have no idea why replxx uses errno for this.
266 if (errno == EAGAIN) {
267 continue;
268 } else {
269 break;
270 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100271 }
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200272
Jan Kundráte3877022018-09-05 15:32:09 +0200273 std::locale C_locale("C");
Václav Kubernáta395d332019-02-13 16:49:20 +0100274 std::string_view view{line};
275 if (std::all_of(view.begin(), view.end(),
Jan Kundráte3877022018-09-05 15:32:09 +0200276 [C_locale](const auto c) { return std::isspace(c, C_locale);})) {
277 continue;
278 }
279
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200280 try {
Václav Kubernát5b80e522019-01-25 12:17:03 +0100281 command_ cmd = parser.parseCommand(line, std::cout);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +0200282 boost::apply_visitor(Interpreter(parser, proxyDatastore), cmd);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200283 } catch (InvalidCommandException& ex) {
284 std::cerr << ex.what() << std::endl;
Václav Kubernátc58e4aa2019-04-03 18:37:32 +0200285 } catch (DatastoreException& ex) {
286 std::cerr << ex.what() << std::endl;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200287 } catch (std::runtime_error& ex) {
288 std::cerr << ex.what() << std::endl;
Václav Kubernát160e5a22020-12-01 01:11:28 +0100289 } catch (std::logic_error& ex) {
290 std::cerr << ex.what() << std::endl;
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200291 }
Václav Kubernát5b80e522019-01-25 12:17:03 +0100292
Václav Kubernáta395d332019-02-13 16:49:20 +0100293 lineEditor.history_add(line);
Václav Kubernátff2c9f62018-05-16 20:26:31 +0200294 }
295
Václav Kubernát3a433232020-07-08 17:52:50 +0200296 if (historyFile) {
Václav Kubernát435706e2019-02-20 18:05:59 +0100297 lineEditor.history_save(historyFile.value());
Václav Kubernát3a433232020-07-08 17:52:50 +0200298 }
Václav Kubernát435706e2019-02-20 18:05:59 +0100299
Václav Kubernátfd261162020-11-12 02:36:29 +0100300 return backendReturnCode;
Jan Kundrátdc2b0722018-03-02 14:13:37 +0100301}