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