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