Brian J Griglak | 6605beb | 2022-05-23 17:31:57 -0600 | [diff] [blame] | 1 | #include <algorithm> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 2 | #include <chrono> |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 3 | #include <functional> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 4 | #include <iostream> |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 5 | #include <optional> |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 6 | #include <string_view> |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 7 | #include <unordered_map> |
| 8 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 9 | #include "absl/strings/str_format.h" |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 10 | #include "absl/strings/str_join.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 11 | #include "aos/init.h" |
| 12 | #include "aos/json_to_flatbuffer.h" |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 13 | #include "aos/time/time.h" |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 14 | #include "gflags/gflags.h" |
| 15 | #include "starter_rpc_lib.h" |
| 16 | |
Austin Schuh | 8e2dfc6 | 2022-08-17 16:36:00 -0700 | [diff] [blame] | 17 | DEFINE_string(config, "aos_config.json", "File path of aos configuration"); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 18 | // TODO(james): Bash autocompletion for node names. |
| 19 | DEFINE_string( |
| 20 | node, "", |
| 21 | "Node to interact with. If empty, just interact with local node."); |
| 22 | DEFINE_bool(all_nodes, false, "Interact with all nodes."); |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 23 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 24 | DEFINE_bool(_bash_autocomplete, false, |
| 25 | "Internal use: Outputs commands or applications for use with " |
| 26 | "autocomplete script."); |
| 27 | DEFINE_string(_bash_autocomplete_word, "", |
| 28 | "Internal use: Current word being autocompleted"); |
Brian J Griglak | 6605beb | 2022-05-23 17:31:57 -0600 | [diff] [blame] | 29 | DEFINE_string(sort, "name", |
| 30 | "The name of the column to sort processes by. " |
| 31 | "Can be \"name\", \"state\", \"pid\", or \"uptime\"."); |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 32 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 33 | namespace { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 34 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 35 | namespace chrono = std::chrono; |
| 36 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 37 | static const std::unordered_map<std::string, aos::starter::Command> |
| 38 | kCommandConversions{{"start", aos::starter::Command::START}, |
| 39 | {"stop", aos::starter::Command::STOP}, |
| 40 | {"restart", aos::starter::Command::RESTART}}; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 41 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 42 | std::vector<const aos::Node *> InteractNodes( |
| 43 | const aos::Configuration *configuration) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 44 | if (!configuration->has_nodes()) { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 45 | return {nullptr}; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 46 | } |
| 47 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 48 | if (!FLAGS_node.empty()) { |
| 49 | CHECK(!FLAGS_all_nodes) << "Can't specify both --node and --all_nodes."; |
| 50 | return {aos::configuration::GetNode(configuration, FLAGS_node)}; |
| 51 | } |
| 52 | |
| 53 | if (FLAGS_all_nodes) { |
| 54 | return aos::configuration::GetNodes(configuration); |
| 55 | } |
| 56 | |
| 57 | return {aos::configuration::GetMyNode(configuration)}; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 60 | std::vector<const aos::Node *> InteractNodesForApplication( |
| 61 | const aos::Configuration *config, std::string_view application_name) { |
| 62 | const std::vector<const aos::Node *> interact_nodes = InteractNodes(config); |
| 63 | std::vector<const aos::Node *> application_nodes; |
| 64 | std::vector<std::string> debug_node_names; |
| 65 | for (const aos::Node *node : interact_nodes) { |
| 66 | if (aos::configuration::GetApplication(config, node, application_name) != |
| 67 | nullptr) { |
| 68 | application_nodes.push_back(node); |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 69 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 70 | if (node != nullptr) { |
| 71 | debug_node_names.push_back(node->name()->str()); |
| 72 | } |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 73 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 74 | |
| 75 | if (application_nodes.empty()) { |
| 76 | if (interact_nodes.size() == 1 && interact_nodes[0] == nullptr) { |
| 77 | std::cout << "Unknown application " << application_name << std::endl; |
| 78 | } else { |
| 79 | std::cout << "Unknown application " << application_name |
| 80 | << " on any of node(s) " |
| 81 | << absl::StrJoin(debug_node_names, ", ") << std::endl; |
| 82 | } |
| 83 | } |
| 84 | return application_nodes; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 85 | } |
| 86 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 87 | void PrintKey() { |
James Kuszmaul | ce3497e | 2023-02-26 12:40:26 -0800 | [diff] [blame^] | 88 | absl::PrintF("%-30s %-10s %-8s %-6s %-9s %-13s\n", "Name", "Node", "State", |
| 89 | "PID", "Uptime", "Last Exit Code"); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Brian J Griglak | 6605beb | 2022-05-23 17:31:57 -0600 | [diff] [blame] | 92 | std::vector<const aos::starter::ApplicationStatus *> SortApplications( |
| 93 | const aos::FlatbufferVector<aos::starter::Status> &status) { |
| 94 | std::vector<const aos::starter::ApplicationStatus *> sorted_statuses; |
| 95 | for (const aos::starter::ApplicationStatus *app_status : |
| 96 | *status.message().statuses()) { |
| 97 | sorted_statuses.push_back(app_status); |
| 98 | } |
| 99 | // If --sort flag not set, then return this unsorted vector as is. |
| 100 | if (FLAGS_sort.empty()) { |
| 101 | return sorted_statuses; |
| 102 | } |
| 103 | |
| 104 | // Convert --sort flag to lowercase for testing below. |
| 105 | std::transform(FLAGS_sort.begin(), FLAGS_sort.end(), FLAGS_sort.begin(), |
| 106 | tolower); |
| 107 | |
| 108 | // This function is called once for each node being reported upon, so there is |
| 109 | // no need to sort on node, it happens implicitly. |
| 110 | |
| 111 | if (FLAGS_sort == "name") { |
| 112 | // Sort on name using std::string_view::operator< for lexicographic order. |
| 113 | std::sort(sorted_statuses.begin(), sorted_statuses.end(), |
| 114 | [](const aos::starter::ApplicationStatus *lhs, |
| 115 | const aos::starter::ApplicationStatus *rhs) { |
| 116 | return lhs->name()->string_view() < rhs->name()->string_view(); |
| 117 | }); |
| 118 | } else if (FLAGS_sort == "state") { |
| 119 | // Sort on state first, and then name for apps in same state. |
| 120 | // ApplicationStatus::state is an enum, so need to call EnumNameState() |
| 121 | // convenience wrapper to convert enum to char*, and then wrap in |
| 122 | // std::string_view for lexicographic ordering. |
| 123 | std::sort(sorted_statuses.begin(), sorted_statuses.end(), |
| 124 | [](const aos::starter::ApplicationStatus *lhs, |
| 125 | const aos::starter::ApplicationStatus *rhs) { |
| 126 | return (lhs->state() != rhs->state()) |
| 127 | ? (std::string_view( |
| 128 | aos::starter::EnumNameState(lhs->state())) < |
| 129 | std::string_view( |
| 130 | aos::starter::EnumNameState(rhs->state()))) |
| 131 | : (lhs->name()->string_view() < |
| 132 | rhs->name()->string_view()); |
| 133 | }); |
| 134 | } else if (FLAGS_sort == "pid") { |
| 135 | // Sort on pid first, and then name for when both apps are not running. |
| 136 | // If the app state is STOPPED, then it will not have a pid, so need to test |
| 137 | // that first. If only one app is STOPPED, then return Boolean state to put |
| 138 | // running apps before stopped. |
| 139 | std::sort(sorted_statuses.begin(), sorted_statuses.end(), |
| 140 | [](const aos::starter::ApplicationStatus *lhs, |
| 141 | const aos::starter::ApplicationStatus *rhs) { |
| 142 | if (lhs->state() == aos::starter::State::STOPPED) { |
| 143 | if (rhs->state() == aos::starter::State::STOPPED) { |
| 144 | return lhs->name()->string_view() < |
| 145 | rhs->name()->string_view(); |
| 146 | } else { |
| 147 | return false; |
| 148 | } |
| 149 | } else { |
| 150 | if (rhs->state() == aos::starter::State::STOPPED) { |
| 151 | return true; |
| 152 | } else { |
| 153 | return lhs->pid() < rhs->pid(); |
| 154 | } |
| 155 | } |
| 156 | }); |
| 157 | } else if (FLAGS_sort == "uptime") { |
| 158 | // Sort on last_start_time first, and then name for when both apps are not |
| 159 | // running, or have exact same start time. Only use last_start_time when app |
| 160 | // is not STOPPED. If only one app is STOPPED, then return Boolean state to |
| 161 | // put running apps before stopped. |
| 162 | std::sort( |
| 163 | sorted_statuses.begin(), sorted_statuses.end(), |
| 164 | [](const aos::starter::ApplicationStatus *lhs, |
| 165 | const aos::starter::ApplicationStatus *rhs) { |
| 166 | if (lhs->state() == aos::starter::State::STOPPED) { |
| 167 | if (rhs->state() == aos::starter::State::STOPPED) { |
| 168 | return lhs->name()->string_view() < rhs->name()->string_view(); |
| 169 | } else { |
| 170 | return false; |
| 171 | } |
| 172 | } else { |
| 173 | if (rhs->state() == aos::starter::State::STOPPED) { |
| 174 | return true; |
| 175 | } else { |
| 176 | return (lhs->last_start_time() == rhs->last_start_time()) |
| 177 | ? (lhs->name()->string_view() < |
| 178 | rhs->name()->string_view()) |
| 179 | : (lhs->last_start_time() < rhs->last_start_time()); |
| 180 | } |
| 181 | } |
| 182 | }); |
| 183 | } else { |
| 184 | std::cerr << "Unknown sort criteria \"" << FLAGS_sort << "\"" << std::endl; |
| 185 | exit(1); |
| 186 | } |
| 187 | |
| 188 | return sorted_statuses; |
| 189 | } |
| 190 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 191 | void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status, |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 192 | const aos::monotonic_clock::time_point &time, |
| 193 | const aos::Node *node) { |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 194 | const auto last_start_time = aos::monotonic_clock::time_point( |
| 195 | chrono::nanoseconds(app_status->last_start_time())); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 196 | const auto time_running = |
| 197 | chrono::duration_cast<chrono::seconds>(time - last_start_time); |
James Kuszmaul | ce3497e | 2023-02-26 12:40:26 -0800 | [diff] [blame^] | 198 | const std::string last_exit_code = |
| 199 | app_status->has_last_exit_code() |
| 200 | ? std::to_string(app_status->last_exit_code()) |
| 201 | : "-"; |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 202 | if (app_status->state() == aos::starter::State::STOPPED) { |
James Kuszmaul | ce3497e | 2023-02-26 12:40:26 -0800 | [diff] [blame^] | 203 | absl::PrintF("%-30s %-10s %-8s %-6s %-9s %-13s\n", |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 204 | app_status->name()->string_view(), |
| 205 | (node == nullptr) ? "none" : node->name()->string_view(), |
James Kuszmaul | ce3497e | 2023-02-26 12:40:26 -0800 | [diff] [blame^] | 206 | aos::starter::EnumNameState(app_status->state()), "", "", |
| 207 | last_exit_code); |
| 208 | } else { |
| 209 | absl::PrintF( |
| 210 | "%-30s %-10s %-8s %-6d %-9s %-13s\n", app_status->name()->string_view(), |
| 211 | (node == nullptr) ? "none" : node->name()->string_view(), |
| 212 | aos::starter::EnumNameState(app_status->state()), app_status->pid(), |
| 213 | std::to_string(time_running.count()) + 's', last_exit_code); |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame] | 214 | } |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 217 | // Prints the status for all applications. |
| 218 | void GetAllStarterStatus(const aos::Configuration *config) { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 219 | PrintKey(); |
| 220 | std::vector<const aos::Node *> missing_nodes; |
| 221 | for (const aos::Node *node : InteractNodes(config)) { |
| 222 | // Print status for all processes. |
| 223 | const auto optional_status = aos::starter::GetStarterStatus(config, node); |
| 224 | if (optional_status) { |
| 225 | const aos::FlatbufferVector<aos::starter::Status> &status = |
| 226 | optional_status->second; |
| 227 | const aos::monotonic_clock::time_point time = optional_status->first; |
Brian J Griglak | 6605beb | 2022-05-23 17:31:57 -0600 | [diff] [blame] | 228 | const auto &sorted_statuses = SortApplications(status); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 229 | for (const aos::starter::ApplicationStatus *app_status : |
Brian J Griglak | 6605beb | 2022-05-23 17:31:57 -0600 | [diff] [blame] | 230 | sorted_statuses) { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 231 | PrintApplicationStatus(app_status, time, node); |
| 232 | } |
| 233 | } else { |
| 234 | missing_nodes.push_back(node); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 235 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 236 | } |
| 237 | for (const aos::Node *node : missing_nodes) { |
| 238 | if (node == nullptr) { |
| 239 | LOG(WARNING) << "No status found."; |
| 240 | } else { |
| 241 | LOG(WARNING) << "No status found for node " |
| 242 | << node->name()->string_view(); |
| 243 | } |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 244 | } |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Handles the "status" command. Returns true if the help message should be |
| 248 | // printed. |
| 249 | bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) { |
| 250 | if (argc == 1) { |
| 251 | GetAllStarterStatus(config); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 252 | } else if (argc == 2) { |
| 253 | // Print status for the specified process. |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 254 | const auto application_name = |
| 255 | aos::starter::FindApplication(argv[1], config); |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 256 | if (application_name == "all") { |
| 257 | GetAllStarterStatus(config); |
| 258 | return false; |
| 259 | } |
| 260 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 261 | const std::vector<const aos::Node *> application_nodes = |
| 262 | InteractNodesForApplication(config, application_name); |
| 263 | if (application_nodes.empty()) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 266 | PrintKey(); |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 267 | for (const aos::Node *node : application_nodes) { |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame] | 268 | auto optional_status = |
| 269 | aos::starter::GetStatus(application_name, config, node); |
| 270 | if (optional_status.has_value()) { |
James Kuszmaul | 2ca441b | 2022-01-07 18:16:23 -0800 | [diff] [blame] | 271 | PrintApplicationStatus(&optional_status.value().second.message(), |
| 272 | optional_status.value().first, node); |
James Kuszmaul | e4bb0a2 | 2022-01-07 18:14:43 -0800 | [diff] [blame] | 273 | } else { |
| 274 | if (node != nullptr) { |
| 275 | LOG(ERROR) << "No status available yet for \"" << application_name |
| 276 | << "\" on node \"" << node->name()->string_view() << "\"."; |
| 277 | } else { |
| 278 | LOG(ERROR) << "No status available yet for \"" << application_name |
| 279 | << "\"."; |
| 280 | } |
| 281 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 282 | } |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 283 | } else { |
| 284 | LOG(ERROR) << "The \"status\" command requires zero or one arguments."; |
| 285 | return true; |
| 286 | } |
| 287 | return false; |
| 288 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 289 | |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 290 | // Sends the provided command to all applications. Prints the success text on |
| 291 | // success, and failure text on failure. |
| 292 | void InteractWithAll(const aos::Configuration *config, |
| 293 | const aos::starter::Command command, |
| 294 | std::string_view success_text, |
| 295 | std::string_view failure_text) { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 296 | std::map<const aos::Node *, |
| 297 | std::unique_ptr<aos::FlatbufferVector<aos::starter::Status>>> |
| 298 | statuses; |
| 299 | |
| 300 | for (const aos::Node *node : InteractNodes(config)) { |
| 301 | std::optional<std::pair<aos::monotonic_clock::time_point, |
| 302 | const aos::FlatbufferVector<aos::starter::Status>>> |
| 303 | optional_status = aos::starter::GetStarterStatus(config, node); |
| 304 | if (optional_status.has_value()) { |
| 305 | statuses[node] = |
| 306 | std::make_unique<aos::FlatbufferVector<aos::starter::Status>>( |
| 307 | optional_status.value().second); |
| 308 | } else { |
| 309 | if (node == nullptr) { |
| 310 | LOG(WARNING) << "Starter not running"; |
| 311 | } else { |
| 312 | LOG(WARNING) << "Starter not running on node " |
| 313 | << node->name()->string_view(); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (!statuses.empty()) { |
| 319 | std::vector<aos::starter::ApplicationCommand> commands; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 320 | |
| 321 | for (const aos::Application *application : *config->applications()) { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 322 | const std::string_view application_name = |
| 323 | application->name()->string_view(); |
| 324 | const std::vector<const aos::Node *> application_nodes = |
| 325 | InteractNodesForApplication(config, application_name); |
| 326 | // Ignore any applications which aren't supposed to be started. |
| 327 | if (application_nodes.empty()) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 328 | continue; |
| 329 | } |
| 330 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 331 | std::vector<const aos::Node *> running_nodes; |
| 332 | if (application->autostart()) { |
| 333 | running_nodes = application_nodes; |
| 334 | } else { |
| 335 | for (const aos::Node *node : application_nodes) { |
| 336 | const aos::starter::ApplicationStatus *application_status = |
| 337 | aos::starter::FindApplicationStatus(statuses[node]->message(), |
| 338 | application_name); |
| 339 | if (application_status->state() == aos::starter::State::STOPPED) { |
| 340 | if (node == nullptr) { |
| 341 | std::cout << "Skipping " << application_name |
| 342 | << " because it is STOPPED\n"; |
| 343 | } else { |
| 344 | std::cout << "Skipping " << application_name << " on " |
| 345 | << node->name()->string_view() |
| 346 | << " because it is STOPPED\n"; |
| 347 | } |
| 348 | continue; |
| 349 | } else { |
| 350 | running_nodes.push_back(node); |
| 351 | } |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 355 | if (!running_nodes.empty()) { |
| 356 | commands.emplace_back(aos::starter::ApplicationCommand{ |
| 357 | command, application_name, running_nodes}); |
| 358 | } |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Restart each running process |
| 362 | if (aos::starter::SendCommandBlocking(commands, config, |
| 363 | chrono::seconds(5))) { |
| 364 | std::cout << success_text << "all \n"; |
| 365 | } else { |
| 366 | std::cout << failure_text << "all \n"; |
| 367 | } |
| 368 | } else { |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 369 | LOG(WARNING) << "None of the starters we care about are running."; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | // Handles the "start", "stop", and "restart" commands. Returns true if the |
| 374 | // help message should be printed. |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 375 | bool InteractWithProgram(int argc, char **argv, |
| 376 | const aos::Configuration *config) { |
| 377 | const char *command_string = argv[0]; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 378 | if (argc != 2) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 379 | LOG(ERROR) |
| 380 | << "The \"" << command_string |
| 381 | << "\" command requires an application name or 'all' as an argument."; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 382 | return true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 385 | const auto command_search = kCommandConversions.find(command_string); |
| 386 | CHECK(command_search != kCommandConversions.end()) |
| 387 | << "Internal error: \"" << command_string |
| 388 | << "\" is not in kCommandConversions."; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 389 | const aos::starter::Command command = command_search->second; |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 390 | |
| 391 | std::string_view success_text; |
| 392 | const std::string failure_text = |
| 393 | std::string("Failed to ") + std::string(command_string) + " "; |
| 394 | switch (command) { |
| 395 | case aos::starter::Command::START: |
| 396 | success_text = "Successfully started "; |
| 397 | break; |
| 398 | case aos::starter::Command::STOP: |
| 399 | success_text = "Successfully stopped "; |
| 400 | break; |
| 401 | case aos::starter::Command::RESTART: |
| 402 | success_text = "Successfully restarted "; |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | const std::string_view application_name = |
| 407 | aos::starter::FindApplication(argv[1], config); |
| 408 | if (application_name == "all") { |
| 409 | InteractWithAll(config, command, success_text, failure_text); |
| 410 | return false; |
| 411 | } |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 412 | |
| 413 | const std::vector<const aos::Node *> application_nodes = |
| 414 | InteractNodesForApplication(config, application_name); |
| 415 | if (application_nodes.empty()) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 419 | if (aos::starter::SendCommandBlocking(command, application_name, config, |
James Kuszmaul | 293b217 | 2021-11-10 16:20:48 -0800 | [diff] [blame] | 420 | chrono::seconds(5), |
| 421 | application_nodes)) { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 422 | std::cout << success_text << application_name << '\n'; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 423 | } else { |
Austin Schuh | 43fceaf | 2021-10-16 14:20:22 -0700 | [diff] [blame] | 424 | std::cout << failure_text << application_name << '\n'; |
Jacob Ismael | 6388db9 | 2021-06-28 22:51:24 -0700 | [diff] [blame] | 425 | } |
| 426 | return false; |
| 427 | } |
| 428 | |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 429 | bool Help(int /*argc*/, char ** /*argv*/, |
| 430 | const aos::Configuration * /*config*/); |
| 431 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 432 | // This is the set of subcommands we support. Each subcommand accepts argc and |
| 433 | // argv from its own point of view. So argv[0] is always the name of the |
| 434 | // subcommand. argv[1] and up are the arguments to the subcommand. |
| 435 | // The subcommand returns true if there was an error parsing the command line |
| 436 | // arguments. It returns false when the command line arguments are parsed |
| 437 | // successfully. |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 438 | static const std::vector< |
| 439 | std::tuple<std::string, |
| 440 | std::function<bool(int argc, char **argv, |
| 441 | const aos::Configuration *config)>, |
| 442 | std::string_view>> |
| 443 | kCommands{ |
| 444 | {"help", Help, ""}, |
| 445 | {"status", GetStarterStatus, |
| 446 | " [application], Returns the status of the provided application, " |
| 447 | "or all applications by default"}, |
| 448 | {"start", InteractWithProgram, |
| 449 | " application, Starts the provided application, " |
| 450 | "or all applications if all is provided"}, |
| 451 | {"stop", InteractWithProgram, |
| 452 | " application, Stops the provided application, " |
| 453 | "or all applications if all is provided"}, |
| 454 | {"restart", InteractWithProgram, |
| 455 | " application, Restarts the provided application, " |
| 456 | "or all applications if all is provided"}}; |
| 457 | |
| 458 | bool Help(int /*argc*/, char ** /*argv*/, |
| 459 | const aos::Configuration * /*config*/) { |
| 460 | std::cout << "Valid commands are:" << std::endl; |
| 461 | for (auto entry : kCommands) { |
| 462 | std::cout << " - " << std::get<0>(entry) << std::get<2>(entry) << std::endl; |
| 463 | } |
| 464 | return false; |
| 465 | } |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 466 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 467 | void Autocomplete(int argc, char **argv, const aos::Configuration *config) { |
| 468 | const std::string_view command = (argc >= 2 ? argv[1] : ""); |
| 469 | const std::string_view app_name = (argc >= 3 ? argv[2] : ""); |
| 470 | |
| 471 | std::cout << "COMPREPLY=("; |
| 472 | if (FLAGS__bash_autocomplete_word == command) { |
| 473 | // Autocomplete the starter command |
| 474 | for (const auto &entry : kCommands) { |
| 475 | if (std::get<0>(entry).find(command) == 0) { |
| 476 | std::cout << '\'' << std::get<0>(entry) << "' "; |
| 477 | } |
| 478 | } |
| 479 | } else { |
| 480 | // Autocomplete the app name |
| 481 | for (const auto *app : *config->applications()) { |
| 482 | if (app->has_name() && app->name()->string_view().find(app_name) == 0) { |
| 483 | std::cout << '\'' << app->name()->string_view() << "' "; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // Autocomplete with "all" |
milind-u | 95296dd | 2021-10-19 07:42:17 -0700 | [diff] [blame] | 488 | if (std::string_view("all").find(app_name) == 0) { |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 489 | std::cout << "'all'"; |
| 490 | } |
| 491 | } |
| 492 | std::cout << ')'; |
| 493 | } |
| 494 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 495 | } // namespace |
| 496 | |
| 497 | int main(int argc, char **argv) { |
| 498 | aos::InitGoogle(&argc, &argv); |
| 499 | |
| 500 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 501 | aos::configuration::ReadConfig(FLAGS_config); |
| 502 | |
milind-u | 08dab88 | 2021-10-17 16:24:08 -0700 | [diff] [blame] | 503 | if (FLAGS__bash_autocomplete) { |
| 504 | Autocomplete(argc, argv, &config.message()); |
| 505 | return 0; |
| 506 | } |
| 507 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 508 | bool parsing_failed = false; |
| 509 | |
| 510 | if (argc < 2) { |
| 511 | parsing_failed = true; |
| 512 | } else { |
| 513 | const char *command = argv[1]; |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 514 | auto it = std::find_if( |
| 515 | kCommands.begin(), kCommands.end(), |
| 516 | [command](const std::tuple< |
| 517 | std::string, |
| 518 | std::function<bool(int argc, char **argv, |
| 519 | const aos::Configuration *config)>, |
| 520 | std::string_view> &t) { return std::get<0>(t) == command; }); |
| 521 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 522 | if (it == kCommands.end()) { |
| 523 | parsing_failed = true; |
| 524 | } else { |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 525 | parsing_failed = std::get<1>(*it)(argc - 1, argv + 1, &config.message()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | if (parsing_failed) { |
Austin Schuh | 33cc416 | 2021-10-16 14:20:28 -0700 | [diff] [blame] | 530 | Help(argc - 1, argv + 1, &config.message()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | return 0; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 535 | } |