Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 1 | #include <chrono> |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 2 | #include <functional> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 3 | #include <iostream> |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 4 | #include <optional> |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 5 | #include <string_view> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 6 | #include <unordered_map> |
| 7 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 8 | #include "absl/strings/str_format.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 9 | #include "aos/init.h" |
| 10 | #include "aos/json_to_flatbuffer.h" |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 11 | #include "aos/time/time.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 12 | #include "gflags/gflags.h" |
| 13 | #include "starter_rpc_lib.h" |
| 14 | |
| 15 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
| 16 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame^] | 17 | DEFINE_bool(_bash_autocomplete, false, |
| 18 | "Internal use: Outputs commands or applications for use with " |
| 19 | "autocomplete script."); |
| 20 | DEFINE_string(_bash_autocomplete_word, "", |
| 21 | "Internal use: Current word being autocompleted"); |
| 22 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 23 | namespace { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 24 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 25 | namespace chrono = std::chrono; |
| 26 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 27 | static const std::unordered_map<std::string, aos::starter::Command> |
| 28 | kCommandConversions{{"start", aos::starter::Command::START}, |
| 29 | {"stop", aos::starter::Command::STOP}, |
| 30 | {"restart", aos::starter::Command::RESTART}}; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 31 | |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 32 | const aos::Node *MaybeMyNode(const aos::Configuration *configuration) { |
| 33 | if (!configuration->has_nodes()) { |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
| 37 | return aos::configuration::GetMyNode(configuration); |
| 38 | } |
| 39 | |
| 40 | bool ValidApplication(const aos::Configuration *config, |
| 41 | std::string_view application_name) { |
| 42 | const aos::Node *node = MaybeMyNode(config); |
| 43 | const aos::Application *application = |
| 44 | aos::configuration::GetApplication(config, node, application_name); |
| 45 | if (application == nullptr) { |
| 46 | if (node) { |
| 47 | std::cout << "Unknown application '" << application_name << "' on node '" |
| 48 | << node->name()->string_view() << "'" << std::endl; |
| 49 | } else { |
| 50 | std::cout << "Unknown application '" << application_name << "'" |
| 51 | << std::endl; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 58 | void PrintKey() { |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 59 | absl::PrintF("%-30s %-8s %-6s %-9s\n", "Name", "State", "PID", "Uptime"); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status, |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 63 | const aos::monotonic_clock::time_point &time) { |
| 64 | const auto last_start_time = aos::monotonic_clock::time_point( |
| 65 | chrono::nanoseconds(app_status->last_start_time())); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 66 | const auto time_running = |
| 67 | chrono::duration_cast<chrono::seconds>(time - last_start_time); |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 68 | if (app_status->state() == aos::starter::State::STOPPED) { |
| 69 | absl::PrintF("%-30s %-8s\n", app_status->name()->string_view(), |
| 70 | aos::starter::EnumNameState(app_status->state())); |
| 71 | } else { |
Austin Schuh | 31bbdea | 2021-10-16 15:51:37 -0700 | [diff] [blame] | 72 | absl::PrintF("%-30s %-8s %-6d %-9s\n", app_status->name()->string_view(), |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 73 | aos::starter::EnumNameState(app_status->state()), |
Austin Schuh | 31bbdea | 2021-10-16 15:51:37 -0700 | [diff] [blame] | 74 | app_status->pid(), std::to_string(time_running.count()) + 's'); |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 75 | } |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 78 | // Prints the status for all applications. |
| 79 | void GetAllStarterStatus(const aos::Configuration *config) { |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame^] | 80 | // Print status for all processes. |
| 81 | const auto optional_status = aos::starter::GetStarterStatus(config); |
| 82 | if (optional_status) { |
| 83 | auto status = *optional_status; |
| 84 | const auto time = aos::monotonic_clock::now(); |
| 85 | PrintKey(); |
| 86 | for (const aos::starter::ApplicationStatus *app_status : |
| 87 | *status.message().statuses()) { |
| 88 | PrintApplicationStatus(app_status, time); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 89 | } |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame^] | 90 | } else { |
| 91 | LOG(WARNING) << "No status found"; |
| 92 | } |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Handles the "status" command. Returns true if the help message should be |
| 96 | // printed. |
| 97 | bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) { |
| 98 | if (argc == 1) { |
| 99 | GetAllStarterStatus(config); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 100 | } else if (argc == 2) { |
| 101 | // Print status for the specified process. |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 102 | const auto application_name = |
| 103 | aos::starter::FindApplication(argv[1], config); |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 104 | if (application_name == "all") { |
| 105 | GetAllStarterStatus(config); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (!ValidApplication(config, application_name)) { |
| 110 | return false; |
| 111 | } |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 112 | auto status = aos::starter::GetStatus(application_name, config); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 113 | PrintKey(); |
| 114 | PrintApplicationStatus(&status.message(), aos::monotonic_clock::now()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 115 | } else { |
| 116 | LOG(ERROR) << "The \"status\" command requires zero or one arguments."; |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 121 | |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 122 | // Sends the provided command to all applications. Prints the success text on |
| 123 | // success, and failure text on failure. |
| 124 | void InteractWithAll(const aos::Configuration *config, |
| 125 | const aos::starter::Command command, |
| 126 | std::string_view success_text, |
| 127 | std::string_view failure_text) { |
| 128 | const auto optional_status = aos::starter::GetStarterStatus(config); |
| 129 | if (optional_status) { |
| 130 | auto status = *optional_status; |
| 131 | const aos::Node *my_node = MaybeMyNode(config); |
| 132 | std::vector<std::pair<aos::starter::Command, std::string_view>> commands; |
| 133 | |
| 134 | for (const aos::Application *application : *config->applications()) { |
| 135 | // Ignore any applications which aren't supposed to be started on this |
| 136 | // node. |
| 137 | if (!aos::configuration::ApplicationShouldStart(config, my_node, |
| 138 | application)) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | const std::string_view application_name = |
| 143 | application->name()->string_view(); |
| 144 | if (!application->autostart()) { |
| 145 | const aos::starter::ApplicationStatus *application_status = |
| 146 | aos::starter::FindApplicationStatus(status.message(), |
| 147 | application_name); |
| 148 | if (application_status->state() == aos::starter::State::STOPPED) { |
| 149 | std::cout << "Skipping " << application_name |
| 150 | << " because it is STOPPED\n"; |
| 151 | continue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | commands.emplace_back(command, application_name); |
| 156 | } |
| 157 | |
| 158 | // Restart each running process |
| 159 | if (aos::starter::SendCommandBlocking(commands, config, |
| 160 | chrono::seconds(5))) { |
| 161 | std::cout << success_text << "all \n"; |
| 162 | } else { |
| 163 | std::cout << failure_text << "all \n"; |
| 164 | } |
| 165 | } else { |
| 166 | LOG(WARNING) << "Starter not running"; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Handles the "start", "stop", and "restart" commands. Returns true if the |
| 171 | // help message should be printed. |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 172 | bool InteractWithProgram(int argc, char **argv, |
| 173 | const aos::Configuration *config) { |
| 174 | const char *command_string = argv[0]; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 175 | if (argc != 2) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 176 | LOG(ERROR) |
| 177 | << "The \"" << command_string |
| 178 | << "\" command requires an application name or 'all' as an argument."; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 179 | return true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 182 | const auto command_search = kCommandConversions.find(command_string); |
| 183 | CHECK(command_search != kCommandConversions.end()) |
| 184 | << "Internal error: \"" << command_string |
| 185 | << "\" is not in kCommandConversions."; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 186 | const aos::starter::Command command = command_search->second; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 187 | |
| 188 | std::string_view success_text; |
| 189 | const std::string failure_text = |
| 190 | std::string("Failed to ") + std::string(command_string) + " "; |
| 191 | switch (command) { |
| 192 | case aos::starter::Command::START: |
| 193 | success_text = "Successfully started "; |
| 194 | break; |
| 195 | case aos::starter::Command::STOP: |
| 196 | success_text = "Successfully stopped "; |
| 197 | break; |
| 198 | case aos::starter::Command::RESTART: |
| 199 | success_text = "Successfully restarted "; |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | const std::string_view application_name = |
| 204 | aos::starter::FindApplication(argv[1], config); |
| 205 | if (application_name == "all") { |
| 206 | InteractWithAll(config, command, success_text, failure_text); |
| 207 | return false; |
| 208 | } |
| 209 | if (!ValidApplication(config, application_name)) { |
| 210 | return false; |
| 211 | } |
| 212 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 213 | if (aos::starter::SendCommandBlocking(command, application_name, config, |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 214 | chrono::seconds(5))) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 215 | std::cout << success_text << application_name << '\n'; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 216 | } else { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 217 | std::cout << failure_text << application_name << '\n'; |
Jacob Ismael | 6388db9 | 2021-06-28 22:51:24 -0700 | [diff] [blame] | 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 222 | bool Help(int /*argc*/, char ** /*argv*/, |
| 223 | const aos::Configuration * /*config*/); |
| 224 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 225 | // This is the set of subcommands we support. Each subcommand accepts argc and |
| 226 | // argv from its own point of view. So argv[0] is always the name of the |
| 227 | // subcommand. argv[1] and up are the arguments to the subcommand. |
| 228 | // The subcommand returns true if there was an error parsing the command line |
| 229 | // arguments. It returns false when the command line arguments are parsed |
| 230 | // successfully. |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 231 | static const std::vector< |
| 232 | std::tuple<std::string, |
| 233 | std::function<bool(int argc, char **argv, |
| 234 | const aos::Configuration *config)>, |
| 235 | std::string_view>> |
| 236 | kCommands{ |
| 237 | {"help", Help, ""}, |
| 238 | {"status", GetStarterStatus, |
| 239 | " [application], Returns the status of the provided application, " |
| 240 | "or all applications by default"}, |
| 241 | {"start", InteractWithProgram, |
| 242 | " application, Starts the provided application, " |
| 243 | "or all applications if all is provided"}, |
| 244 | {"stop", InteractWithProgram, |
| 245 | " application, Stops the provided application, " |
| 246 | "or all applications if all is provided"}, |
| 247 | {"restart", InteractWithProgram, |
| 248 | " application, Restarts the provided application, " |
| 249 | "or all applications if all is provided"}}; |
| 250 | |
| 251 | bool Help(int /*argc*/, char ** /*argv*/, |
| 252 | const aos::Configuration * /*config*/) { |
| 253 | std::cout << "Valid commands are:" << std::endl; |
| 254 | for (auto entry : kCommands) { |
| 255 | std::cout << " - " << std::get<0>(entry) << std::get<2>(entry) << std::endl; |
| 256 | } |
| 257 | return false; |
| 258 | } |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 259 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame^] | 260 | void Autocomplete(int argc, char **argv, const aos::Configuration *config) { |
| 261 | const std::string_view command = (argc >= 2 ? argv[1] : ""); |
| 262 | const std::string_view app_name = (argc >= 3 ? argv[2] : ""); |
| 263 | |
| 264 | std::cout << "COMPREPLY=("; |
| 265 | if (FLAGS__bash_autocomplete_word == command) { |
| 266 | // Autocomplete the starter command |
| 267 | for (const auto &entry : kCommands) { |
| 268 | if (std::get<0>(entry).find(command) == 0) { |
| 269 | std::cout << '\'' << std::get<0>(entry) << "' "; |
| 270 | } |
| 271 | } |
| 272 | } else { |
| 273 | // Autocomplete the app name |
| 274 | for (const auto *app : *config->applications()) { |
| 275 | if (app->has_name() && app->name()->string_view().find(app_name) == 0) { |
| 276 | std::cout << '\'' << app->name()->string_view() << "' "; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Autocomplete with "all" |
| 281 | if (std::string_view("all").find(FLAGS__bash_autocomplete_word) == 0) { |
| 282 | std::cout << "'all'"; |
| 283 | } |
| 284 | } |
| 285 | std::cout << ')'; |
| 286 | } |
| 287 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 288 | } // namespace |
| 289 | |
| 290 | int main(int argc, char **argv) { |
| 291 | aos::InitGoogle(&argc, &argv); |
| 292 | |
| 293 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 294 | aos::configuration::ReadConfig(FLAGS_config); |
| 295 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame^] | 296 | if (FLAGS__bash_autocomplete) { |
| 297 | Autocomplete(argc, argv, &config.message()); |
| 298 | return 0; |
| 299 | } |
| 300 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 301 | bool parsing_failed = false; |
| 302 | |
| 303 | if (argc < 2) { |
| 304 | parsing_failed = true; |
| 305 | } else { |
| 306 | const char *command = argv[1]; |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 307 | auto it = std::find_if( |
| 308 | kCommands.begin(), kCommands.end(), |
| 309 | [command](const std::tuple< |
| 310 | std::string, |
| 311 | std::function<bool(int argc, char **argv, |
| 312 | const aos::Configuration *config)>, |
| 313 | std::string_view> &t) { return std::get<0>(t) == command; }); |
| 314 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 315 | if (it == kCommands.end()) { |
| 316 | parsing_failed = true; |
| 317 | } else { |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 318 | parsing_failed = std::get<1>(*it)(argc - 1, argv + 1, &config.message()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | if (parsing_failed) { |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 323 | Help(argc - 1, argv + 1, &config.message()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 324 | return 1; |
| 325 | } |
| 326 | |
| 327 | return 0; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 328 | } |