blob: 40430025a746c6a3dd593a4321dbafad205c6cad [file] [log] [blame]
Keiichi Watanabec6959812025-03-07 11:05:13 +00001// 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 Cha3cea4812024-12-05 23:04:30 +090015use api::debian_service_client::DebianServiceClient;
16use api::ShutdownQueueOpeningRequest;
17use std::process::Command;
18
19use anyhow::anyhow;
20use clap::Parser;
21use log::debug;
22pub mod api {
23 tonic::include_proto!("com.android.virtualization.terminal.proto");
24}
25
26#[derive(Parser)]
27/// Flags for running command
28pub struct Args {
Keiichi Watanabe9aec0922025-03-07 10:41:06 +000029 /// Path to a file where grpc port number is written
Jeongik Cha3cea4812024-12-05 23:04:30 +090030 #[arg(long)]
Keiichi Watanabe9aec0922025-03-07 10:41:06 +000031 grpc_port_file: String,
Jeongik Cha3cea4812024-12-05 23:04:30 +090032}
33
34#[tokio::main]
35async fn main() -> Result<(), Box<dyn std::error::Error>> {
Keiichi Watanabe744343b2025-03-07 10:34:14 +000036 env_logger::builder().filter_level(log::LevelFilter::Debug).init();
Jeongik Cha3cea4812024-12-05 23:04:30 +090037 let args = Args::parse();
38 let gateway_ip_addr = netdev::get_default_gateway()?.ipv4[0];
39
Keiichi Watanabe9aec0922025-03-07 10:41:06 +000040 // 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 Cha3cea4812024-12-05 23:04:30 +090051
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}