1#!/bin/bash
2# Copyright 2023 Google Inc. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16color_cyan="\033[0;36m"
17color_plain="\033[0m"
18color_yellow="\033[0;33m"
19
20# validate number of arguments to between 2 and 4
21if [ "$#" -lt 2 ] || [ "$#" -gt 4 ]; then
22  echo "This script requires 2 mandatory and 2 optional parameters:"
23  echo "Mandatory: server address, base instance number."
24  echo "Optional: the number of instances to invoke, the path to a vendor debug ramdisk image."
25  echo "For example: ./launch_cvd_arm64_server.sh user@<ip> 10 1 ./vendor_boot-debug.img"
26  exit 1
27fi
28
29# map arguments to variables
30server=$1
31base_instance_num=$2
32if [ "$#" -gt 2 ]; then
33  num_instances=$3
34else
35  num_instances=1
36fi
37if [ "$#" -eq 4 ]; then
38  vendor_boot_debug_image=$4
39  vendor_boot_debug_flag="--vendor_boot_image=$(basename $4)"
40else
41  vendor_boot_debug_image=""
42  vendor_boot_debug_flag=""
43fi
44
45# set img_dir and cvd_host_tool_dir
46img_dir=${ANDROID_PRODUCT_OUT:-$PWD}
47cvd_host_tool_dir=${ANDROID_HOST_OUT:+"$ANDROID_HOST_OUT/../linux_musl-arm64"}
48cvd_host_tool_dir=${cvd_host_tool_dir:-$PWD}
49
50# create a temp directory to store the artifacts
51temp_dir=/tmp/cvd_dist
52rm -rf $temp_dir
53mkdir -p $temp_dir
54
55# copy and compress the artifacts to the temp directory
56cvd_home_dir=cvd_home
57ssh $server -t "mkdir -p ~/.cvd_artifact; mkdir -p ~/$cvd_home_dir"
58if [ -f $img_dir/required_images ]; then
59  rsync -aSvch --recursive $img_dir --files-from=$img_dir/required_images $server:~/$cvd_home_dir --info=progress2
60else
61  rsync -aSvch --recursive $img_dir/bootloader $img_dir/*.img $server:~/$cvd_home_dir --info=progress2
62fi
63if [ ! -z "$vendor_boot_debug_image" ]; then
64  echo "use the debug ramdisk image: $vendor_boot_debug_image"
65  rsync -Svch $vendor_boot_debug_image $server:~/$cvd_home_dir --info=progress2
66fi
67
68# copy the cvd host package
69if [ -d $cvd_host_tool_dir/cvd-host_package ]; then
70  echo "Use contents in cvd-host_package dir"
71  rsync -avch $cvd_host_tool_dir/cvd-host_package/* $server:~/$cvd_home_dir --info=progress2
72elif [ -f $cvd_host_tool_dir/cvd-host_package.tar.gz ]; then
73  echo "Use contents in cvd-host_package.tar.gz"
74  # re-compress with rsyncable option
75  # TODO(b/275312073): remove this if toxbox supports rsyncable
76  cd $cvd_host_tool_dir; pigz -d -c cvd-host_package.tar.gz | pigz -R > $temp_dir/cvd-host_package.tar.gz
77  rsync -avh $temp_dir/* $server:.cvd_artifact --info=progress2
78  ssh $server -t "cd .cvd_artifact; tar -zxvf cvd-host_package.tar.gz -C ~/$cvd_home_dir/"
79else
80  echo "There is neither cvd-host_package dir nor cvd-host_package.tar.gz"
81  exit 1
82fi
83
84trap cleanup SIGINT
85cleanup() {
86  echo -e "${color_yellow}SIGINT: stopping the launch instances${color_plain}"
87  ssh $server -t "cd ~/$cvd_home_dir && HOME=~/$cvd_home_dir bin/stop_cvd"
88}
89
90# TODO(kwstephenkim): remove the flag at once if cuttlefish removes the flag
91daemon_flag="--daemon=true"
92instance_ids_flag="--base_instance_num=$base_instance_num \
93  --num_instances=$num_instances"
94echo -e "${color_cyan}Booting the cuttlefish instances${color_plain}"
95ssh $server \
96  -t "cd ~/$cvd_home_dir && HOME=~/$cvd_home_dir bin/launch_cvd $instance_ids_flag $daemon_flag $vendor_boot_debug_flag"
97
98# Web UI port is 2443 instead 1443 because there could be a running operator in this machine as well.
99web_ui_port=2443
100echo -e "Web UI port: $web_ui_port. ${color_cyan}Please point your browser to https://localhost:$web_ui_port for the UI${color_plain}"
101
102# sets up SSH port forwarding to the remote server for various ports and launch cvd instance
103adb_port_forwarding=""
104print_launcher_logs=""
105for instance_num in $(seq $base_instance_num $(($base_instance_num+$num_instances-1))); do
106  device_name="cvd_$base_instance_num-$instance_num"
107  adb_port=$((6520+$instance_num-1))
108  echo -e "$device_name is using adb port $adb_port. Try ${color_cyan}adb connect 127.0.0.1:${adb_port}${color_plain} if you want to connect to this device"
109  adb_port_forwarding+="-L $adb_port:127.0.0.1:$adb_port "
110  print_launcher_logs+="tail -f ~/$cvd_home_dir/cuttlefish/instances/cvd-$instance_num/logs/launcher.log | sed 's/^/[$device_name] /' &"
111done
112
113ports_forwarding="-L $web_ui_port:127.0.0.1:1443 \
114  -L 15550:127.0.0.1:15550 -L 15551:127.0.0.1:15551 -L 15552:127.0.0.1:15552 \
115  -L 15553:127.0.0.1:15553 -L 15554:127.0.0.1:15554 -L 15555:127.0.0.1:15555 \
116  -L 15556:127.0.0.1:15556 -L 15557:127.0.0.1:15557 -L 15558:127.0.0.1:15558 \
117  $adb_port_forwarding"
118echo "Set up ssh ports forwarding: $ports_forwarding"
119echo -e "${color_yellow}Please stop the running instances by ctrl+c${color_plain}"
120ssh $server $ports_forwarding $print_launcher_logs
121