blob: ae65ff06a63cc349134bcc062c1b76f0d8315d50 [file] [log] [blame]
Tyler Chatowa79419d2020-08-12 20:12:11 -07001#include <chrono>
Philipp Schrader08537492021-01-23 16:17:55 -08002#include <functional>
Tyler Chatowa79419d2020-08-12 20:12:11 -07003#include <iostream>
milind upadhyaya87957a2021-03-06 20:46:30 -08004#include <optional>
milind upadhyay4272f382021-04-07 18:03:08 -07005#include <string_view>
Tyler Chatowa79419d2020-08-12 20:12:11 -07006#include <unordered_map>
7
Philipp Schrader08537492021-01-23 16:17:55 -08008#include "absl/strings/str_format.h"
James Kuszmaul293b2172021-11-10 16:20:48 -08009#include "absl/strings/str_join.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070010#include "aos/init.h"
11#include "aos/json_to_flatbuffer.h"
milind upadhyaya87957a2021-03-06 20:46:30 -080012#include "aos/time/time.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070013#include "gflags/gflags.h"
14#include "starter_rpc_lib.h"
15
16DEFINE_string(config, "./config.json", "File path of aos configuration");
James Kuszmaul293b2172021-11-10 16:20:48 -080017// TODO(james): Bash autocompletion for node names.
18DEFINE_string(
19 node, "",
20 "Node to interact with. If empty, just interact with local node.");
21DEFINE_bool(all_nodes, false, "Interact with all nodes.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070022
milind-u08dab882021-10-17 16:24:08 -070023DEFINE_bool(_bash_autocomplete, false,
24 "Internal use: Outputs commands or applications for use with "
25 "autocomplete script.");
26DEFINE_string(_bash_autocomplete_word, "",
27 "Internal use: Current word being autocompleted");
28
Philipp Schrader08537492021-01-23 16:17:55 -080029namespace {
Tyler Chatowa79419d2020-08-12 20:12:11 -070030
milind upadhyaya87957a2021-03-06 20:46:30 -080031namespace chrono = std::chrono;
32
Philipp Schrader08537492021-01-23 16:17:55 -080033static const std::unordered_map<std::string, aos::starter::Command>
34 kCommandConversions{{"start", aos::starter::Command::START},
35 {"stop", aos::starter::Command::STOP},
36 {"restart", aos::starter::Command::RESTART}};
Tyler Chatowa79419d2020-08-12 20:12:11 -070037
James Kuszmaul293b2172021-11-10 16:20:48 -080038std::vector<const aos::Node *> InteractNodes(
39 const aos::Configuration *configuration) {
Austin Schuh43fceaf2021-10-16 14:20:22 -070040 if (!configuration->has_nodes()) {
James Kuszmaul293b2172021-11-10 16:20:48 -080041 return {nullptr};
Austin Schuh43fceaf2021-10-16 14:20:22 -070042 }
43
James Kuszmaul293b2172021-11-10 16:20:48 -080044 if (!FLAGS_node.empty()) {
45 CHECK(!FLAGS_all_nodes) << "Can't specify both --node and --all_nodes.";
46 return {aos::configuration::GetNode(configuration, FLAGS_node)};
47 }
48
49 if (FLAGS_all_nodes) {
50 return aos::configuration::GetNodes(configuration);
51 }
52
53 return {aos::configuration::GetMyNode(configuration)};
Austin Schuh43fceaf2021-10-16 14:20:22 -070054}
55
James Kuszmaul293b2172021-11-10 16:20:48 -080056std::vector<const aos::Node *> InteractNodesForApplication(
57 const aos::Configuration *config, std::string_view application_name) {
58 const std::vector<const aos::Node *> interact_nodes = InteractNodes(config);
59 std::vector<const aos::Node *> application_nodes;
60 std::vector<std::string> debug_node_names;
61 for (const aos::Node *node : interact_nodes) {
62 if (aos::configuration::GetApplication(config, node, application_name) !=
63 nullptr) {
64 application_nodes.push_back(node);
Austin Schuh43fceaf2021-10-16 14:20:22 -070065 }
James Kuszmaul293b2172021-11-10 16:20:48 -080066 if (node != nullptr) {
67 debug_node_names.push_back(node->name()->str());
68 }
Austin Schuh43fceaf2021-10-16 14:20:22 -070069 }
James Kuszmaul293b2172021-11-10 16:20:48 -080070
71 if (application_nodes.empty()) {
72 if (interact_nodes.size() == 1 && interact_nodes[0] == nullptr) {
73 std::cout << "Unknown application " << application_name << std::endl;
74 } else {
75 std::cout << "Unknown application " << application_name
76 << " on any of node(s) "
77 << absl::StrJoin(debug_node_names, ", ") << std::endl;
78 }
79 }
80 return application_nodes;
Austin Schuh43fceaf2021-10-16 14:20:22 -070081}
82
milind upadhyaya87957a2021-03-06 20:46:30 -080083void PrintKey() {
James Kuszmaul293b2172021-11-10 16:20:48 -080084 absl::PrintF("%-30s %-10s %-8s %-6s %-9s\n", "Name", "Node", "State", "PID",
85 "Uptime");
milind upadhyaya87957a2021-03-06 20:46:30 -080086}
87
88void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status,
James Kuszmaul293b2172021-11-10 16:20:48 -080089 const aos::monotonic_clock::time_point &time,
90 const aos::Node *node) {
milind upadhyay4272f382021-04-07 18:03:08 -070091 const auto last_start_time = aos::monotonic_clock::time_point(
92 chrono::nanoseconds(app_status->last_start_time()));
milind upadhyaya87957a2021-03-06 20:46:30 -080093 const auto time_running =
94 chrono::duration_cast<chrono::seconds>(time - last_start_time);
Austin Schuhf4334002021-10-16 14:19:51 -070095 if (app_status->state() == aos::starter::State::STOPPED) {
James Kuszmaul293b2172021-11-10 16:20:48 -080096 absl::PrintF("%-30s %-10s %-8s\n", app_status->name()->string_view(),
97 (node == nullptr) ? "none" : node->name()->string_view(),
Austin Schuhf4334002021-10-16 14:19:51 -070098 aos::starter::EnumNameState(app_status->state()));
99 } else {
James Kuszmaul293b2172021-11-10 16:20:48 -0800100 absl::PrintF("%-30s %-10s %-8s %-6d %-9s\n",
101 app_status->name()->string_view(),
102 (node == nullptr) ? "none" : node->name()->string_view(),
Austin Schuhf4334002021-10-16 14:19:51 -0700103 aos::starter::EnumNameState(app_status->state()),
Austin Schuh31bbdea2021-10-16 15:51:37 -0700104 app_status->pid(), std::to_string(time_running.count()) + 's');
Austin Schuhf4334002021-10-16 14:19:51 -0700105 }
milind upadhyaya87957a2021-03-06 20:46:30 -0800106}
107
Austin Schuh43fceaf2021-10-16 14:20:22 -0700108// Prints the status for all applications.
109void GetAllStarterStatus(const aos::Configuration *config) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800110 PrintKey();
111 std::vector<const aos::Node *> missing_nodes;
112 for (const aos::Node *node : InteractNodes(config)) {
113 // Print status for all processes.
114 const auto optional_status = aos::starter::GetStarterStatus(config, node);
115 if (optional_status) {
116 const aos::FlatbufferVector<aos::starter::Status> &status =
117 optional_status->second;
118 const aos::monotonic_clock::time_point time = optional_status->first;
119 for (const aos::starter::ApplicationStatus *app_status :
120 *status.message().statuses()) {
121 PrintApplicationStatus(app_status, time, node);
122 }
123 } else {
124 missing_nodes.push_back(node);
Philipp Schrader08537492021-01-23 16:17:55 -0800125 }
James Kuszmaul293b2172021-11-10 16:20:48 -0800126 }
127 for (const aos::Node *node : missing_nodes) {
128 if (node == nullptr) {
129 LOG(WARNING) << "No status found.";
130 } else {
131 LOG(WARNING) << "No status found for node "
132 << node->name()->string_view();
133 }
milind-u08dab882021-10-17 16:24:08 -0700134 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700135}
136
137// Handles the "status" command. Returns true if the help message should be
138// printed.
139bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) {
140 if (argc == 1) {
141 GetAllStarterStatus(config);
Philipp Schrader08537492021-01-23 16:17:55 -0800142 } else if (argc == 2) {
143 // Print status for the specified process.
milind upadhyay4272f382021-04-07 18:03:08 -0700144 const auto application_name =
145 aos::starter::FindApplication(argv[1], config);
Austin Schuh43fceaf2021-10-16 14:20:22 -0700146 if (application_name == "all") {
147 GetAllStarterStatus(config);
148 return false;
149 }
150
James Kuszmaul293b2172021-11-10 16:20:48 -0800151 const std::vector<const aos::Node *> application_nodes =
152 InteractNodesForApplication(config, application_name);
153 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700154 return false;
155 }
milind upadhyaya87957a2021-03-06 20:46:30 -0800156 PrintKey();
James Kuszmaul293b2172021-11-10 16:20:48 -0800157 for (const aos::Node *node : application_nodes) {
158 auto status = aos::starter::GetStatus(application_name, config, node);
159 PrintApplicationStatus(&status.message(), aos::monotonic_clock::now(),
160 node);
161 }
Philipp Schrader08537492021-01-23 16:17:55 -0800162 } else {
163 LOG(ERROR) << "The \"status\" command requires zero or one arguments.";
164 return true;
165 }
166 return false;
167}
Tyler Chatowa79419d2020-08-12 20:12:11 -0700168
Austin Schuh43fceaf2021-10-16 14:20:22 -0700169// Sends the provided command to all applications. Prints the success text on
170// success, and failure text on failure.
171void InteractWithAll(const aos::Configuration *config,
172 const aos::starter::Command command,
173 std::string_view success_text,
174 std::string_view failure_text) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800175 std::map<const aos::Node *,
176 std::unique_ptr<aos::FlatbufferVector<aos::starter::Status>>>
177 statuses;
178
179 for (const aos::Node *node : InteractNodes(config)) {
180 std::optional<std::pair<aos::monotonic_clock::time_point,
181 const aos::FlatbufferVector<aos::starter::Status>>>
182 optional_status = aos::starter::GetStarterStatus(config, node);
183 if (optional_status.has_value()) {
184 statuses[node] =
185 std::make_unique<aos::FlatbufferVector<aos::starter::Status>>(
186 optional_status.value().second);
187 } else {
188 if (node == nullptr) {
189 LOG(WARNING) << "Starter not running";
190 } else {
191 LOG(WARNING) << "Starter not running on node "
192 << node->name()->string_view();
193 }
194 }
195 }
196
197 if (!statuses.empty()) {
198 std::vector<aos::starter::ApplicationCommand> commands;
Austin Schuh43fceaf2021-10-16 14:20:22 -0700199
200 for (const aos::Application *application : *config->applications()) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800201 const std::string_view application_name =
202 application->name()->string_view();
203 const std::vector<const aos::Node *> application_nodes =
204 InteractNodesForApplication(config, application_name);
205 // Ignore any applications which aren't supposed to be started.
206 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700207 continue;
208 }
209
James Kuszmaul293b2172021-11-10 16:20:48 -0800210 std::vector<const aos::Node *> running_nodes;
211 if (application->autostart()) {
212 running_nodes = application_nodes;
213 } else {
214 for (const aos::Node *node : application_nodes) {
215 const aos::starter::ApplicationStatus *application_status =
216 aos::starter::FindApplicationStatus(statuses[node]->message(),
217 application_name);
218 if (application_status->state() == aos::starter::State::STOPPED) {
219 if (node == nullptr) {
220 std::cout << "Skipping " << application_name
221 << " because it is STOPPED\n";
222 } else {
223 std::cout << "Skipping " << application_name << " on "
224 << node->name()->string_view()
225 << " because it is STOPPED\n";
226 }
227 continue;
228 } else {
229 running_nodes.push_back(node);
230 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700231 }
232 }
233
James Kuszmaul293b2172021-11-10 16:20:48 -0800234 if (!running_nodes.empty()) {
235 commands.emplace_back(aos::starter::ApplicationCommand{
236 command, application_name, running_nodes});
237 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700238 }
239
240 // Restart each running process
241 if (aos::starter::SendCommandBlocking(commands, config,
242 chrono::seconds(5))) {
243 std::cout << success_text << "all \n";
244 } else {
245 std::cout << failure_text << "all \n";
246 }
247 } else {
James Kuszmaul293b2172021-11-10 16:20:48 -0800248 LOG(WARNING) << "None of the starters we care about are running.";
Austin Schuh43fceaf2021-10-16 14:20:22 -0700249 }
250}
251
252// Handles the "start", "stop", and "restart" commands. Returns true if the
253// help message should be printed.
Philipp Schrader08537492021-01-23 16:17:55 -0800254bool InteractWithProgram(int argc, char **argv,
255 const aos::Configuration *config) {
256 const char *command_string = argv[0];
Philipp Schrader08537492021-01-23 16:17:55 -0800257 if (argc != 2) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700258 LOG(ERROR)
259 << "The \"" << command_string
260 << "\" command requires an application name or 'all' as an argument.";
Philipp Schrader08537492021-01-23 16:17:55 -0800261 return true;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700262 }
263
Philipp Schrader08537492021-01-23 16:17:55 -0800264 const auto command_search = kCommandConversions.find(command_string);
265 CHECK(command_search != kCommandConversions.end())
266 << "Internal error: \"" << command_string
267 << "\" is not in kCommandConversions.";
Philipp Schrader08537492021-01-23 16:17:55 -0800268 const aos::starter::Command command = command_search->second;
Austin Schuh43fceaf2021-10-16 14:20:22 -0700269
270 std::string_view success_text;
271 const std::string failure_text =
272 std::string("Failed to ") + std::string(command_string) + " ";
273 switch (command) {
274 case aos::starter::Command::START:
275 success_text = "Successfully started ";
276 break;
277 case aos::starter::Command::STOP:
278 success_text = "Successfully stopped ";
279 break;
280 case aos::starter::Command::RESTART:
281 success_text = "Successfully restarted ";
282 break;
283 }
284
285 const std::string_view application_name =
286 aos::starter::FindApplication(argv[1], config);
287 if (application_name == "all") {
288 InteractWithAll(config, command, success_text, failure_text);
289 return false;
290 }
James Kuszmaul293b2172021-11-10 16:20:48 -0800291
292 const std::vector<const aos::Node *> application_nodes =
293 InteractNodesForApplication(config, application_name);
294 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700295 return false;
296 }
297
Philipp Schrader08537492021-01-23 16:17:55 -0800298 if (aos::starter::SendCommandBlocking(command, application_name, config,
James Kuszmaul293b2172021-11-10 16:20:48 -0800299 chrono::seconds(5),
300 application_nodes)) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700301 std::cout << success_text << application_name << '\n';
Tyler Chatowa79419d2020-08-12 20:12:11 -0700302 } else {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700303 std::cout << failure_text << application_name << '\n';
Jacob Ismael6388db92021-06-28 22:51:24 -0700304 }
305 return false;
306}
307
Austin Schuh33cc4162021-10-16 14:20:28 -0700308bool Help(int /*argc*/, char ** /*argv*/,
309 const aos::Configuration * /*config*/);
310
Philipp Schrader08537492021-01-23 16:17:55 -0800311// This is the set of subcommands we support. Each subcommand accepts argc and
312// argv from its own point of view. So argv[0] is always the name of the
313// subcommand. argv[1] and up are the arguments to the subcommand.
314// The subcommand returns true if there was an error parsing the command line
315// arguments. It returns false when the command line arguments are parsed
316// successfully.
Austin Schuh33cc4162021-10-16 14:20:28 -0700317static const std::vector<
318 std::tuple<std::string,
319 std::function<bool(int argc, char **argv,
320 const aos::Configuration *config)>,
321 std::string_view>>
322 kCommands{
323 {"help", Help, ""},
324 {"status", GetStarterStatus,
325 " [application], Returns the status of the provided application, "
326 "or all applications by default"},
327 {"start", InteractWithProgram,
328 " application, Starts the provided application, "
329 "or all applications if all is provided"},
330 {"stop", InteractWithProgram,
331 " application, Stops the provided application, "
332 "or all applications if all is provided"},
333 {"restart", InteractWithProgram,
334 " application, Restarts the provided application, "
335 "or all applications if all is provided"}};
336
337bool Help(int /*argc*/, char ** /*argv*/,
338 const aos::Configuration * /*config*/) {
339 std::cout << "Valid commands are:" << std::endl;
340 for (auto entry : kCommands) {
341 std::cout << " - " << std::get<0>(entry) << std::get<2>(entry) << std::endl;
342 }
343 return false;
344}
Philipp Schrader08537492021-01-23 16:17:55 -0800345
milind-u08dab882021-10-17 16:24:08 -0700346void Autocomplete(int argc, char **argv, const aos::Configuration *config) {
347 const std::string_view command = (argc >= 2 ? argv[1] : "");
348 const std::string_view app_name = (argc >= 3 ? argv[2] : "");
349
350 std::cout << "COMPREPLY=(";
351 if (FLAGS__bash_autocomplete_word == command) {
352 // Autocomplete the starter command
353 for (const auto &entry : kCommands) {
354 if (std::get<0>(entry).find(command) == 0) {
355 std::cout << '\'' << std::get<0>(entry) << "' ";
356 }
357 }
358 } else {
359 // Autocomplete the app name
360 for (const auto *app : *config->applications()) {
361 if (app->has_name() && app->name()->string_view().find(app_name) == 0) {
362 std::cout << '\'' << app->name()->string_view() << "' ";
363 }
364 }
365
366 // Autocomplete with "all"
milind-u95296dd2021-10-19 07:42:17 -0700367 if (std::string_view("all").find(app_name) == 0) {
milind-u08dab882021-10-17 16:24:08 -0700368 std::cout << "'all'";
369 }
370 }
371 std::cout << ')';
372}
373
Philipp Schrader08537492021-01-23 16:17:55 -0800374} // namespace
375
376int main(int argc, char **argv) {
377 aos::InitGoogle(&argc, &argv);
378
379 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
380 aos::configuration::ReadConfig(FLAGS_config);
381
milind-u08dab882021-10-17 16:24:08 -0700382 if (FLAGS__bash_autocomplete) {
383 Autocomplete(argc, argv, &config.message());
384 return 0;
385 }
386
Philipp Schrader08537492021-01-23 16:17:55 -0800387 bool parsing_failed = false;
388
389 if (argc < 2) {
390 parsing_failed = true;
391 } else {
392 const char *command = argv[1];
Austin Schuh33cc4162021-10-16 14:20:28 -0700393 auto it = std::find_if(
394 kCommands.begin(), kCommands.end(),
395 [command](const std::tuple<
396 std::string,
397 std::function<bool(int argc, char **argv,
398 const aos::Configuration *config)>,
399 std::string_view> &t) { return std::get<0>(t) == command; });
400
Philipp Schrader08537492021-01-23 16:17:55 -0800401 if (it == kCommands.end()) {
402 parsing_failed = true;
403 } else {
Austin Schuh33cc4162021-10-16 14:20:28 -0700404 parsing_failed = std::get<1>(*it)(argc - 1, argv + 1, &config.message());
Philipp Schrader08537492021-01-23 16:17:55 -0800405 }
406 }
407
408 if (parsing_failed) {
Austin Schuh33cc4162021-10-16 14:20:28 -0700409 Help(argc - 1, argv + 1, &config.message());
Philipp Schrader08537492021-01-23 16:17:55 -0800410 return 1;
411 }
412
413 return 0;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700414}