Merge "launch_cvd_arm64_server for docker" into main am: be5db40c2d am: e28652bd4b
Original change: https://android-review.googlesource.com/c/device/google/cuttlefish/+/2910604
Change-Id: Ia7a3e2a314f4344f9e1f637a940aa727e91bc46f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/tools/launch_cvd_arm64_server_docker.sh b/tools/launch_cvd_arm64_server_docker.sh
new file mode 100755
index 0000000..4c7d792
--- /dev/null
+++ b/tools/launch_cvd_arm64_server_docker.sh
@@ -0,0 +1,114 @@
+#!/bin/bash
+# Copyright 2024 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+color_cyan="\033[0;36m"
+color_plain="\033[0m"
+color_yellow="\033[0;33m"
+
+# validate number of arguments
+if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
+ echo "This script requires 1 mandatory and 1 optional parameters, server address and optionally number of instances to invoke"
+ exit 1
+fi
+
+# map arguments to variables
+# $1: ARM server address
+# $2: Instance number (Optional, default is 1)
+server=$1
+if [ "$#" -eq 2 ]; then
+ num_instances=$2
+else
+ num_instances=1
+fi
+
+# set img_dir and cvd_host_tool_dir
+img_dir=${ANDROID_PRODUCT_OUT:-$PWD}
+cvd_host_tool_dir=${ANDROID_HOST_OUT:+"$ANDROID_HOST_OUT/../linux_musl-arm64"}
+cvd_host_tool_dir=${cvd_host_tool_dir:-$PWD}
+
+# create a temp directory to store the artifacts
+temp_dir=/tmp/cvd_dist
+rm -rf $temp_dir
+mkdir -p $temp_dir
+
+# copy and compress the artifacts to the temp directory
+cvd_home_dir=cvd_home
+ssh $server -t "mkdir -p ~/.cvd_artifact; mkdir -p ~/$cvd_home_dir"
+if [ -f $img_dir/required_images ]; then
+ rsync -aSvch --recursive $img_dir --files-from=$img_dir/required_images $server:~/$cvd_home_dir --info=progress2
+else
+ rsync -aSvch --recursive $img_dir/bootloader $img_dir/*.img $server:~/$cvd_home_dir --info=progress2
+fi
+
+# copy the cvd host package
+if [ -d $cvd_host_tool_dir/cvd-host_package ]; then
+ echo "Use contents in cvd-host_package dir"
+ rsync -avch $cvd_host_tool_dir/cvd-host_package/* $server:~/$cvd_home_dir --info=progress2
+elif [ -f $cvd_host_tool_dir/cvd-host_package.tar.gz ]; then
+ echo "Use contents in cvd-host_package.tar.gz"
+ # re-compress with rsyncable option
+ # TODO(b/275312073): remove this if toxbox supports rsyncable
+ cd $cvd_host_tool_dir; pigz -d -c cvd-host_package.tar.gz | pigz -R > $temp_dir/cvd-host_package.tar.gz
+ rsync -avh $temp_dir/* $server:.cvd_artifact --info=progress2
+ ssh $server -t "cd .cvd_artifact; tar -zxvf cvd-host_package.tar.gz -C ~/$cvd_home_dir/"
+else
+ echo "There is neither cvd-host_package dir nor cvd-host_package.tar.gz"
+ exit 1
+fi
+
+container_id=$(ssh $server -t "docker run --privileged -P -v ~/$cvd_home_dir:/root/cvd_home -d cuttlefish --num-instances=$num_instances")
+# to remove trailing ^M
+container_id=${container_id//$'\r'}
+echo -e "${color_cyan}Booting the cuttlefish instances in container $container_id${color_plain}"
+
+trap cleanup SIGINT
+cleanup() {
+ echo -e "${color_yellow}SIGINT: stopping the launch instances${color_plain}"
+ ssh $server "docker kill $container_id"
+}
+
+# Web UI port is 2443 instead 1443 because there could be a running operator in this machine as well.
+web_ui_port=2443
+echo -e "Web UI port: $web_ui_port. ${color_cyan}Please point your browser to https://localhost:$web_ui_port for the UI${color_plain}"
+
+# sets up SSH port forwarding to the remote server for various ports and launch cvd instance
+for instance_num in $(seq 1 $num_instances); do
+ device_name="cvd_$instance_num"
+ adb_port=$((6520+$instance_num-1))
+ 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"
+done
+docker_inspect=$(ssh $server "docker inspect --format='{{json .NetworkSettings.Ports }}' $container_id")
+docker_port_parser_script='
+import sys, json;
+web_ui_port = int(sys.argv[1])
+max_instances = 40
+num_instance = int(sys.argv[2])
+json_raw=input()
+data = json.loads(json_raw)
+for k in data:
+ original_port = int(k.split("/")[0])
+ assigned_port = int(data[k][0]["HostPort"])
+ if original_port == 1443:
+ original_port = web_ui_port
+ if original_port >= 6520 and original_port <= 6520 + max_instances:
+ if original_port - 6520 >= num_instance:
+ continue
+ print(f"-L {original_port}:127.0.0.1:{assigned_port}", end=" ")
+'
+ports_forwarding=$(echo $docker_inspect | python -c "$docker_port_parser_script" $web_ui_port $num_instances)
+echo -e $ports_forwarding
+echo "Set up ssh ports forwarding: $ports_forwarding"
+echo -e "${color_yellow}Please stop the running instances by ctrl+c${color_plain}"
+ssh $server $ports_forwarding "docker logs -f $container_id"