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 | |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 26 | void PrintKey() { |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame^] | 27 | absl::PrintF("%-30s %-8s %-6s %-9s\n", "Name", "State", "PID", "Uptime"); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status, |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 31 | const aos::monotonic_clock::time_point &time) { |
| 32 | const auto last_start_time = aos::monotonic_clock::time_point( |
| 33 | chrono::nanoseconds(app_status->last_start_time())); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 34 | const auto time_running = |
| 35 | chrono::duration_cast<chrono::seconds>(time - last_start_time); |
Austin Schuh | f433400 | 2021-10-16 14:19:51 -0700 | [diff] [blame^] | 36 | if (app_status->state() == aos::starter::State::STOPPED) { |
| 37 | absl::PrintF("%-30s %-8s\n", app_status->name()->string_view(), |
| 38 | aos::starter::EnumNameState(app_status->state())); |
| 39 | } else { |
| 40 | absl::PrintF("%-30s %-8s %-6d %-9ds\n", app_status->name()->string_view(), |
| 41 | aos::starter::EnumNameState(app_status->state()), |
| 42 | app_status->pid(), time_running.count()); |
| 43 | } |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 46 | bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) { |
| 47 | if (argc == 1) { |
| 48 | // Print status for all processes. |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 49 | const auto optional_status = aos::starter::GetStarterStatus(config); |
| 50 | if (optional_status) { |
| 51 | auto status = *optional_status; |
| 52 | const auto time = aos::monotonic_clock::now(); |
| 53 | PrintKey(); |
| 54 | for (const aos::starter::ApplicationStatus *app_status : |
| 55 | *status.message().statuses()) { |
| 56 | PrintApplicationStatus(app_status, time); |
| 57 | } |
| 58 | } else { |
| 59 | LOG(WARNING) << "No status found"; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 60 | } |
| 61 | } else if (argc == 2) { |
| 62 | // Print status for the specified process. |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 63 | const auto application_name = |
| 64 | aos::starter::FindApplication(argv[1], config); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 65 | auto status = aos::starter::GetStatus(application_name, config); |
milind upadhyay | a87957a | 2021-03-06 20:46:30 -0800 | [diff] [blame] | 66 | PrintKey(); |
| 67 | PrintApplicationStatus(&status.message(), aos::monotonic_clock::now()); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 68 | } else { |
| 69 | LOG(ERROR) << "The \"status\" command requires zero or one arguments."; |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 74 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 75 | bool InteractWithProgram(int argc, char **argv, |
| 76 | const aos::Configuration *config) { |
| 77 | const char *command_string = argv[0]; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 78 | if (argc != 2) { |
| 79 | LOG(ERROR) << "The \"" << command_string |
| 80 | << "\" command requires an application name as an argument."; |
| 81 | return true; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 84 | const auto command_search = kCommandConversions.find(command_string); |
| 85 | CHECK(command_search != kCommandConversions.end()) |
| 86 | << "Internal error: \"" << command_string |
| 87 | << "\" is not in kCommandConversions."; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 88 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 89 | const aos::starter::Command command = command_search->second; |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 90 | const auto application_name = aos::starter::FindApplication(argv[1], config); |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 91 | if (aos::starter::SendCommandBlocking(command, application_name, config, |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 92 | chrono::seconds(5))) { |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 93 | switch (command) { |
| 94 | case aos::starter::Command::START: |
| 95 | std::cout << "Successfully started " << application_name << '\n'; |
| 96 | break; |
| 97 | case aos::starter::Command::STOP: |
| 98 | std::cout << "Successfully stopped " << application_name << '\n'; |
| 99 | break; |
| 100 | case aos::starter::Command::RESTART: |
| 101 | std::cout << "Successfully restarted " << application_name << '\n'; |
| 102 | break; |
| 103 | } |
| 104 | } else { |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 105 | std::cout << "Failed to " << command_string << ' ' << application_name |
| 106 | << '\n'; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 107 | } |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
Jacob Ismael | 6388db9 | 2021-06-28 22:51:24 -0700 | [diff] [blame] | 111 | bool RestartAll(int argc, char **, const aos::Configuration *config) { |
| 112 | if (argc == 1) { |
| 113 | const auto optional_status = aos::starter::GetStarterStatus(config); |
| 114 | if (optional_status) { |
| 115 | auto status = *optional_status; |
| 116 | for (const aos::starter::ApplicationStatus *app_status : |
| 117 | *status.message().statuses()) { |
| 118 | const auto application_name = aos::starter::FindApplication( |
| 119 | app_status->name()->string_view(), config); |
| 120 | |
| 121 | // Restart each running process |
| 122 | |
| 123 | if (aos::starter::SendCommandBlocking(aos::starter::Command::RESTART, |
| 124 | application_name, config, |
Austin Schuh | a07b3ce | 2021-10-10 12:33:21 -0700 | [diff] [blame] | 125 | chrono::seconds(5))) { |
Jacob Ismael | 6388db9 | 2021-06-28 22:51:24 -0700 | [diff] [blame] | 126 | std::cout << "Successfully restarted " << application_name << '\n'; |
| 127 | } else { |
| 128 | std::cout << "Failed to restart " << application_name << '\n'; |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | } else { |
| 133 | LOG(WARNING) << "No processes found"; |
| 134 | } |
| 135 | } else { |
| 136 | LOG(ERROR) << "The \"restart_all\" command requires only zero arguments."; |
| 137 | return true; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 142 | // This is the set of subcommands we support. Each subcommand accepts argc and |
| 143 | // argv from its own point of view. So argv[0] is always the name of the |
| 144 | // subcommand. argv[1] and up are the arguments to the subcommand. |
| 145 | // The subcommand returns true if there was an error parsing the command line |
| 146 | // arguments. It returns false when the command line arguments are parsed |
| 147 | // successfully. |
| 148 | static const std::unordered_map< |
| 149 | std::string, std::function<bool(int argc, char **argv, |
| 150 | const aos::Configuration *config)>> |
Jacob Ismael | 6388db9 | 2021-06-28 22:51:24 -0700 | [diff] [blame] | 151 | kCommands{{"status", GetStarterStatus}, |
| 152 | {"start", InteractWithProgram}, |
| 153 | {"stop", InteractWithProgram}, |
| 154 | {"restart", InteractWithProgram}, |
| 155 | {"restart_all", RestartAll}}; |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 156 | |
| 157 | } // namespace |
| 158 | |
| 159 | int main(int argc, char **argv) { |
| 160 | aos::InitGoogle(&argc, &argv); |
| 161 | |
| 162 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 163 | aos::configuration::ReadConfig(FLAGS_config); |
| 164 | |
| 165 | bool parsing_failed = false; |
| 166 | |
| 167 | if (argc < 2) { |
| 168 | parsing_failed = true; |
| 169 | } else { |
| 170 | const char *command = argv[1]; |
| 171 | auto it = kCommands.find(command); |
| 172 | if (it == kCommands.end()) { |
| 173 | parsing_failed = true; |
| 174 | } else { |
| 175 | parsing_failed = it->second(argc - 1, argv + 1, &config.message()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (parsing_failed) { |
| 180 | LOG(ERROR) << "Parsing failed. Valid commands are:"; |
milind upadhyay | 4272f38 | 2021-04-07 18:03:08 -0700 | [diff] [blame] | 181 | for (auto entry : kCommands) { |
Philipp Schrader | 0853749 | 2021-01-23 16:17:55 -0800 | [diff] [blame] | 182 | LOG(ERROR) << " - " << entry.first; |
| 183 | } |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | return 0; |
Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame] | 188 | } |