blob: 5f67c4b7568053c47a41103571cdd9d5ba24e3ab [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"
Tyler Chatowa79419d2020-08-12 20:12:11 -07009#include "aos/init.h"
10#include "aos/json_to_flatbuffer.h"
milind upadhyaya87957a2021-03-06 20:46:30 -080011#include "aos/time/time.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070012#include "gflags/gflags.h"
13#include "starter_rpc_lib.h"
14
15DEFINE_string(config, "./config.json", "File path of aos configuration");
16
Philipp Schrader08537492021-01-23 16:17:55 -080017namespace {
Tyler Chatowa79419d2020-08-12 20:12:11 -070018
milind upadhyaya87957a2021-03-06 20:46:30 -080019namespace chrono = std::chrono;
20
Philipp Schrader08537492021-01-23 16:17:55 -080021static const std::unordered_map<std::string, aos::starter::Command>
22 kCommandConversions{{"start", aos::starter::Command::START},
23 {"stop", aos::starter::Command::STOP},
24 {"restart", aos::starter::Command::RESTART}};
Tyler Chatowa79419d2020-08-12 20:12:11 -070025
milind upadhyaya87957a2021-03-06 20:46:30 -080026void PrintKey() {
Austin Schuhf4334002021-10-16 14:19:51 -070027 absl::PrintF("%-30s %-8s %-6s %-9s\n", "Name", "State", "PID", "Uptime");
milind upadhyaya87957a2021-03-06 20:46:30 -080028}
29
30void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status,
milind upadhyay4272f382021-04-07 18:03:08 -070031 const aos::monotonic_clock::time_point &time) {
32 const auto last_start_time = aos::monotonic_clock::time_point(
33 chrono::nanoseconds(app_status->last_start_time()));
milind upadhyaya87957a2021-03-06 20:46:30 -080034 const auto time_running =
35 chrono::duration_cast<chrono::seconds>(time - last_start_time);
Austin Schuhf4334002021-10-16 14:19:51 -070036 if (app_status->state() == aos::starter::State::STOPPED) {
37 absl::PrintF("%-30s %-8s\n", app_status->name()->string_view(),
38 aos::starter::EnumNameState(app_status->state()));
39 } else {
40 absl::PrintF("%-30s %-8s %-6d %-9ds\n", app_status->name()->string_view(),
41 aos::starter::EnumNameState(app_status->state()),
42 app_status->pid(), time_running.count());
43 }
milind upadhyaya87957a2021-03-06 20:46:30 -080044}
45
Philipp Schrader08537492021-01-23 16:17:55 -080046bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) {
47 if (argc == 1) {
48 // Print status for all processes.
milind upadhyaya87957a2021-03-06 20:46:30 -080049 const auto optional_status = aos::starter::GetStarterStatus(config);
50 if (optional_status) {
51 auto status = *optional_status;
52 const auto time = aos::monotonic_clock::now();
53 PrintKey();
54 for (const aos::starter::ApplicationStatus *app_status :
55 *status.message().statuses()) {
56 PrintApplicationStatus(app_status, time);
57 }
58 } else {
59 LOG(WARNING) << "No status found";
Philipp Schrader08537492021-01-23 16:17:55 -080060 }
61 } else if (argc == 2) {
62 // Print status for the specified process.
milind upadhyay4272f382021-04-07 18:03:08 -070063 const auto application_name =
64 aos::starter::FindApplication(argv[1], config);
Philipp Schrader08537492021-01-23 16:17:55 -080065 auto status = aos::starter::GetStatus(application_name, config);
milind upadhyaya87957a2021-03-06 20:46:30 -080066 PrintKey();
67 PrintApplicationStatus(&status.message(), aos::monotonic_clock::now());
Philipp Schrader08537492021-01-23 16:17:55 -080068 } else {
69 LOG(ERROR) << "The \"status\" command requires zero or one arguments.";
70 return true;
71 }
72 return false;
73}
Tyler Chatowa79419d2020-08-12 20:12:11 -070074
Philipp Schrader08537492021-01-23 16:17:55 -080075bool InteractWithProgram(int argc, char **argv,
76 const aos::Configuration *config) {
77 const char *command_string = argv[0];
Philipp Schrader08537492021-01-23 16:17:55 -080078 if (argc != 2) {
79 LOG(ERROR) << "The \"" << command_string
80 << "\" command requires an application name as an argument.";
81 return true;
Tyler Chatowa79419d2020-08-12 20:12:11 -070082 }
83
Philipp Schrader08537492021-01-23 16:17:55 -080084 const auto command_search = kCommandConversions.find(command_string);
85 CHECK(command_search != kCommandConversions.end())
86 << "Internal error: \"" << command_string
87 << "\" is not in kCommandConversions.";
Tyler Chatowa79419d2020-08-12 20:12:11 -070088
Philipp Schrader08537492021-01-23 16:17:55 -080089 const aos::starter::Command command = command_search->second;
milind upadhyay4272f382021-04-07 18:03:08 -070090 const auto application_name = aos::starter::FindApplication(argv[1], config);
Philipp Schrader08537492021-01-23 16:17:55 -080091 if (aos::starter::SendCommandBlocking(command, application_name, config,
Austin Schuha07b3ce2021-10-10 12:33:21 -070092 chrono::seconds(5))) {
Tyler Chatowa79419d2020-08-12 20:12:11 -070093 switch (command) {
94 case aos::starter::Command::START:
95 std::cout << "Successfully started " << application_name << '\n';
96 break;
97 case aos::starter::Command::STOP:
98 std::cout << "Successfully stopped " << application_name << '\n';
99 break;
100 case aos::starter::Command::RESTART:
101 std::cout << "Successfully restarted " << application_name << '\n';
102 break;
103 }
104 } else {
Philipp Schrader08537492021-01-23 16:17:55 -0800105 std::cout << "Failed to " << command_string << ' ' << application_name
106 << '\n';
Tyler Chatowa79419d2020-08-12 20:12:11 -0700107 }
Philipp Schrader08537492021-01-23 16:17:55 -0800108 return false;
109}
110
Jacob Ismael6388db92021-06-28 22:51:24 -0700111bool RestartAll(int argc, char **, const aos::Configuration *config) {
112 if (argc == 1) {
113 const auto optional_status = aos::starter::GetStarterStatus(config);
114 if (optional_status) {
115 auto status = *optional_status;
116 for (const aos::starter::ApplicationStatus *app_status :
117 *status.message().statuses()) {
118 const auto application_name = aos::starter::FindApplication(
119 app_status->name()->string_view(), config);
120
121 // Restart each running process
122
123 if (aos::starter::SendCommandBlocking(aos::starter::Command::RESTART,
124 application_name, config,
Austin Schuha07b3ce2021-10-10 12:33:21 -0700125 chrono::seconds(5))) {
Jacob Ismael6388db92021-06-28 22:51:24 -0700126 std::cout << "Successfully restarted " << application_name << '\n';
127 } else {
128 std::cout << "Failed to restart " << application_name << '\n';
129 return true;
130 }
131 }
132 } else {
133 LOG(WARNING) << "No processes found";
134 }
135 } else {
136 LOG(ERROR) << "The \"restart_all\" command requires only zero arguments.";
137 return true;
138 }
139 return false;
140}
141
Philipp Schrader08537492021-01-23 16:17:55 -0800142// This is the set of subcommands we support. Each subcommand accepts argc and
143// argv from its own point of view. So argv[0] is always the name of the
144// subcommand. argv[1] and up are the arguments to the subcommand.
145// The subcommand returns true if there was an error parsing the command line
146// arguments. It returns false when the command line arguments are parsed
147// successfully.
148static const std::unordered_map<
149 std::string, std::function<bool(int argc, char **argv,
150 const aos::Configuration *config)>>
Jacob Ismael6388db92021-06-28 22:51:24 -0700151 kCommands{{"status", GetStarterStatus},
152 {"start", InteractWithProgram},
153 {"stop", InteractWithProgram},
154 {"restart", InteractWithProgram},
155 {"restart_all", RestartAll}};
Philipp Schrader08537492021-01-23 16:17:55 -0800156
157} // namespace
158
159int main(int argc, char **argv) {
160 aos::InitGoogle(&argc, &argv);
161
162 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
163 aos::configuration::ReadConfig(FLAGS_config);
164
165 bool parsing_failed = false;
166
167 if (argc < 2) {
168 parsing_failed = true;
169 } else {
170 const char *command = argv[1];
171 auto it = kCommands.find(command);
172 if (it == kCommands.end()) {
173 parsing_failed = true;
174 } else {
175 parsing_failed = it->second(argc - 1, argv + 1, &config.message());
176 }
177 }
178
179 if (parsing_failed) {
180 LOG(ERROR) << "Parsing failed. Valid commands are:";
milind upadhyay4272f382021-04-07 18:03:08 -0700181 for (auto entry : kCommands) {
Philipp Schrader08537492021-01-23 16:17:55 -0800182 LOG(ERROR) << " - " << entry.first;
183 }
184 return 1;
185 }
186
187 return 0;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700188}