blob: aee59156cc0d2059c72e0a87c577d994c5f73766 [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 {
29 /// grpc port number
30 #[arg(long)]
31 #[arg(alias = "grpc_port")]
32 grpc_port: String,
33}
34
35#[tokio::main]
36async fn main() -> Result<(), Box<dyn std::error::Error>> {
37 let args = Args::parse();
38 let gateway_ip_addr = netdev::get_default_gateway()?.ipv4[0];
39
40 let server_addr = format!("http://{}:{}", gateway_ip_addr.to_string(), args.grpc_port);
41
42 debug!("connect to grpc server {}", server_addr);
43
44 let mut client = DebianServiceClient::connect(server_addr).await.map_err(|e| e.to_string())?;
45
46 let mut res_stream = client
47 .open_shutdown_request_queue(tonic::Request::new(ShutdownQueueOpeningRequest {}))
48 .await?
49 .into_inner();
50
51 while let Some(_response) = res_stream.message().await? {
52 let status = Command::new("poweroff").status().expect("power off");
53 if !status.success() {
54 return Err(anyhow!("Failed to power off: {status}").into());
55 }
56 debug!("poweroff");
57 break;
58 }
59 Ok(())
60}