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