Keiichi Watanabe | c695981 | 2025-03-07 11:05:13 +0000 | [diff] [blame] | 1 | // Copyright 2024 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Jeongik Cha | 3cea481 | 2024-12-05 23:04:30 +0900 | [diff] [blame] | 15 | use api::debian_service_client::DebianServiceClient; |
| 16 | use api::ShutdownQueueOpeningRequest; |
| 17 | use std::process::Command; |
| 18 | |
| 19 | use anyhow::anyhow; |
| 20 | use clap::Parser; |
| 21 | use log::debug; |
| 22 | pub mod api { |
| 23 | tonic::include_proto!("com.android.virtualization.terminal.proto"); |
| 24 | } |
| 25 | |
| 26 | #[derive(Parser)] |
| 27 | /// Flags for running command |
| 28 | pub struct Args { |
Keiichi Watanabe | 9aec092 | 2025-03-07 10:41:06 +0000 | [diff] [blame] | 29 | /// Path to a file where grpc port number is written |
Jeongik Cha | 3cea481 | 2024-12-05 23:04:30 +0900 | [diff] [blame] | 30 | #[arg(long)] |
Keiichi Watanabe | 9aec092 | 2025-03-07 10:41:06 +0000 | [diff] [blame] | 31 | grpc_port_file: String, |
Jeongik Cha | 3cea481 | 2024-12-05 23:04:30 +0900 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | #[tokio::main] |
| 35 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
Keiichi Watanabe | 744343b | 2025-03-07 10:34:14 +0000 | [diff] [blame] | 36 | env_logger::builder().filter_level(log::LevelFilter::Debug).init(); |
Jeongik Cha | 3cea481 | 2024-12-05 23:04:30 +0900 | [diff] [blame] | 37 | let args = Args::parse(); |
| 38 | let gateway_ip_addr = netdev::get_default_gateway()?.ipv4[0]; |
| 39 | |
Keiichi Watanabe | 9aec092 | 2025-03-07 10:41:06 +0000 | [diff] [blame] | 40 | // Wait for `grpc_port_file` becomes available. |
| 41 | const GRPC_PORT_MAX_RETRY_COUNT: u32 = 10; |
| 42 | for _ in 0..GRPC_PORT_MAX_RETRY_COUNT { |
| 43 | if std::path::Path::new(&args.grpc_port_file).exists() { |
| 44 | break; |
| 45 | } |
| 46 | debug!("{} does not exist. Wait 1 second", args.grpc_port_file); |
| 47 | tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 48 | } |
| 49 | let grpc_port = std::fs::read_to_string(&args.grpc_port_file)?.trim().to_string(); |
| 50 | let server_addr = format!("http://{}:{}", gateway_ip_addr.to_string(), grpc_port); |
Jeongik Cha | 3cea481 | 2024-12-05 23:04:30 +0900 | [diff] [blame] | 51 | |
| 52 | debug!("connect to grpc server {}", server_addr); |
| 53 | |
| 54 | let mut client = DebianServiceClient::connect(server_addr).await.map_err(|e| e.to_string())?; |
| 55 | |
| 56 | let mut res_stream = client |
| 57 | .open_shutdown_request_queue(tonic::Request::new(ShutdownQueueOpeningRequest {})) |
| 58 | .await? |
| 59 | .into_inner(); |
| 60 | |
| 61 | while let Some(_response) = res_stream.message().await? { |
| 62 | let status = Command::new("poweroff").status().expect("power off"); |
| 63 | if !status.success() { |
| 64 | return Err(anyhow!("Failed to power off: {status}").into()); |
| 65 | } |
| 66 | debug!("poweroff"); |
| 67 | break; |
| 68 | } |
| 69 | Ok(()) |
| 70 | } |