xref: /cloud-hypervisor/scripts/dev_cli.sh (revision b8e1cf2d4eaa4bd28c77e2c9941ceb2616e51b59)
1#!/bin/bash
2
3# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4# Copyright © 2020 Intel Corporation
5# SPDX-License-Identifier: Apache-2.0
6
7CLI_NAME="Cloud Hypervisor"
8
9CTR_IMAGE_TAG="cloudhypervisor/dev"
10CTR_IMAGE_VERSION="v1"
11CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}"
12
13DOCKER_RUNTIME="docker"
14
15# Host paths
16CLH_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd)
17CLH_ROOT_DIR=$(cd "${CLH_SCRIPTS_DIR}/.." && pwd)
18CLH_BUILD_DIR="${CLH_ROOT_DIR}/build"
19CLH_CARGO_TARGET="${CLH_BUILD_DIR}/cargo_target"
20CLH_DOCKERFILE="${CLH_SCRIPTS_DIR}/../resources/Dockerfile"
21CLH_CTR_BUILD_DIR="/tmp/cloud-hypervisor/ctr-build"
22CLH_INTEGRATION_WORKLOADS="${HOME}/workloads"
23
24# Container paths
25CTR_CLH_ROOT_DIR="/cloud-hypervisor"
26CTR_CLH_CARGO_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build"
27CTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target"
28CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads"
29
30# Cargo paths
31# Full path to the cargo registry dir on the host. This appears on the host
32# because we want to persist the cargo registry across container invocations.
33# Otherwise, any rust crates from crates.io would be downloaded again each time
34# we build or test.
35CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry"
36
37# Full path to the cargo git registry on the host. This serves the same purpose
38# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of
39# crates.io.
40CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry"
41
42# Full path to the cargo target dir on the host.
43CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target"
44
45# Send a decorated message to stdout, followed by a new line
46#
47say() {
48    [ -t 1 ] && [ -n "$TERM" ] \
49        && echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
50        || echo "[$CLI_NAME] $*"
51}
52
53# Send a decorated message to stdout, without a trailing new line
54#
55say_noln() {
56    [ -t 1 ] && [ -n "$TERM" ] \
57        && echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
58        || echo "[$CLI_NAME] $*"
59}
60
61# Send a text message to stderr
62#
63say_err() {
64    [ -t 2 ] && [ -n "$TERM" ] \
65        && echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \
66        || echo "[$CLI_NAME] $*" 1>&2
67}
68
69# Send a warning-highlighted text to stdout
70say_warn() {
71    [ -t 1 ] && [ -n "$TERM" ] \
72        && echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \
73        || echo "[$CLI_NAME] $*"
74}
75
76# Exit with an error message and (optional) code
77# Usage: die [-c <error code>] <error message>
78#
79die() {
80    code=1
81    [[ "$1" = "-c" ]] && {
82        code="$2"
83        shift 2
84    }
85    say_err "$@"
86    exit $code
87}
88
89# Exit with an error message if the last exit code is not 0
90#
91ok_or_die() {
92    code=$?
93    [[ $code -eq 0 ]] || die -c $code "$@"
94}
95
96# Make sure the build/ dirs are available. Exit if we can't create them.
97# Upon returning from this call, the caller can be certain the build/ dirs exist.
98#
99ensure_build_dir() {
100    for dir in "$CLH_BUILD_DIR" \
101		   "$CLH_INTEGRATION_WORKLOADS" \
102		   "$CLH_CTR_BUILD_DIR" \
103		   "$CARGO_TARGET_DIR" \
104		   "$CARGO_REGISTRY_DIR" \
105		   "$CARGO_GIT_REGISTRY_DIR"; do
106        mkdir -p "$dir" || die "Error: cannot create dir $dir"
107        [ -x "$dir" ] && [ -w "$dir" ] || \
108            {
109                say "Wrong permissions for $dir. Attempting to fix them ..."
110                chmod +x+w "$dir"
111            } || \
112            die "Error: wrong permissions for $dir. Should be +x+w"
113    done
114}
115
116# Fix main directory permissions after a container ran as root.
117# Since the container ran as root, any files it creates will be owned by root.
118# This fixes that by recursively changing the ownership of /cloud-hypervisor to the
119# current user.
120#
121fix_dir_perms() {
122    # Yes, running Docker to get elevated privileges, just to chown some files
123    # is a dirty hack.
124    $DOCKER_RUNTIME run \
125	--workdir "$CTR_CLH_ROOT_DIR" \
126	   --rm \
127	   --volume /dev:/dev \
128	   --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
129	   "$CTR_IMAGE" \
130           chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR"
131
132    return $1
133}
134
135cmd_help() {
136    echo ""
137    echo "Cloud Hypervisor $(basename $0)"
138    echo "Usage: $(basename $0) <command> [<command args>]"
139    echo ""
140    echo "Available commands:"
141    echo ""
142    echo "    build [--debug|--release] [-- [<cargo args>]]"
143    echo "        Build the Cloud Hypervisor binaries."
144    echo "        --debug               Build the debug binaries. This is the default."
145    echo "        --release             Build the release binaries."
146    echo ""
147    echo "    tests [--unit|--cargo|--all] [-- [<cargo test args>]]"
148    echo "        Run the Cloud Hypervisor tests."
149    echo "        --unit               Run the unit tests."
150    echo "        --cargo              Run the cargo tests."
151    echo "        --integration        Run the integration tests."
152    echo "        --all                Run all tests."
153    echo ""
154    echo "    build-container [--type]"
155    echo "        Build the Cloud Hypervisor container."
156    echo "        --dev                Build dev container. This is the default."
157    echo ""
158    echo "    clean [<cargo args>]]"
159    echo "        Remove the Cloud Hypervisor artifacts."
160    echo ""
161    echo "    help"
162    echo "        Display this help message."
163    echo ""
164}
165
166cmd_build() {
167    build="debug"
168
169    while [ $# -gt 0 ]; do
170	case "$1" in
171            "-h"|"--help")  { cmd_help; exit 1;     } ;;
172            "--debug")      { build="debug";      } ;;
173            "--release")    { build="release";    } ;;
174            "--")           { shift; break;         } ;;
175            *)
176		die "Unknown build argument: $1. Please use --help for help."
177		;;
178	esac
179	shift
180    done
181
182    cargo_args=("$@")
183    [ $build = "release" ] && cargo_args+=("--release")
184
185    $DOCKER_RUNTIME run \
186	   --user "$(id -u):$(id -g)" \
187	   --workdir "$CTR_CLH_ROOT_DIR" \
188	   --rm \
189	   --volume /dev:/dev \
190	   --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
191	   "$CTR_IMAGE" \
192	   cargo build \
193	         --target-dir "$CTR_CLH_CARGO_TARGET" \
194	         "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$build"
195}
196
197cmd_clean() {
198    cargo_args=("$@")
199
200    $DOCKER_RUNTIME run \
201	   --user "$(id -u):$(id -g)" \
202	   --workdir "$CTR_CLH_ROOT_DIR" \
203	   --rm \
204	   --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
205	   "$CTR_IMAGE" \
206	   cargo clean \
207	         --target-dir "$CTR_CLH_CARGO_TARGET" \
208	         "${cargo_args[@]}"
209    }
210
211cmd_tests() {
212    unit=false
213    cargo=false
214    integration=false
215
216    while [ $# -gt 0 ]; do
217	case "$1" in
218            "-h"|"--help")    { cmd_help; exit 1;     } ;;
219            "--unit")         { unit=true;      } ;;
220            "--cargo")        { cargo=true;    } ;;
221	    "--integration")  { integration=true;    } ;;
222	    "--all")          { cargo=true; unit=true; integration=true;  } ;;
223            "--")             { shift; break;         } ;;
224            *)
225		die "Unknown tests argument: $1. Please use --help for help."
226		;;
227	esac
228	shift
229    done
230
231    if [ "$unit" = true ] ;  then
232	say "Running unit tests..."
233	$DOCKER_RUNTIME run \
234	       --workdir "$CTR_CLH_ROOT_DIR" \
235	       --rm \
236	       --device /dev/kvm \
237	       --device /dev/net/tun \
238	       --cap-add net_admin \
239	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
240	       "$CTR_IMAGE" \
241	       ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $?
242    fi
243
244    if [ "$cargo" = true ] ;  then
245	say "Running cargo tests..."
246	$DOCKER_RUNTIME run \
247	       --workdir "$CTR_CLH_ROOT_DIR" \
248	       --rm \
249	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
250	       "$CTR_IMAGE" \
251	       ./scripts/run_cargo_tests.sh || fix_dir_perms $? || exit $?
252    fi
253
254    if [ "$integration" = true ] ;  then
255	say "Running integration tests..."
256	$DOCKER_RUNTIME run \
257	       --workdir "$CTR_CLH_ROOT_DIR" \
258	       --rm \
259	       --privileged \
260	       --security-opt seccomp=unconfined \
261	       --ipc=host \
262	       --net=host \
263	       --mount type=tmpfs,destination=/tmp \
264	       --volume /dev:/dev \
265	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
266	       --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
267	       --env USER="root" \
268	       "$CTR_IMAGE" \
269	       ./scripts/run_integration_tests.sh "$@" || fix_dir_perms $? || exit $?
270    fi
271
272    fix_dir_perms $?
273}
274
275cmd_build-container() {
276    container_type="dev"
277
278    while [ $# -gt 0 ]; do
279	case "$1" in
280            "-h"|"--help")  { cmd_help; exit 1;     } ;;
281            "--dev")        { container_type="dev"; } ;;
282            "--")           { shift; break;         } ;;
283            *)
284		die "Unknown build-container argument: $1. Please use --help for help."
285		;;
286	esac
287	shift
288    done
289
290    BUILD_DIR=/tmp/cloud-hypervisor/container/
291
292    mkdir -p $BUILD_DIR
293    cp $CLH_DOCKERFILE $BUILD_DIR
294
295    $DOCKER_RUNTIME build \
296	   --target $container_type \
297	   -t $CTR_IMAGE \
298	   -f $BUILD_DIR/Dockerfile \
299	   $BUILD_DIR
300}
301
302# Parse main command line args.
303#
304while [ $# -gt 0 ]; do
305    case "$1" in
306        -h|--help)              { cmd_help; exit 1;     } ;;
307        -y|--unattended)        { OPT_UNATTENDED=true;  } ;;
308        -*)
309            die "Unknown arg: $1. Please use \`$0 help\` for help."
310            ;;
311        *)
312            break
313            ;;
314    esac
315    shift
316done
317
318# $1 is now a command name. Check if it is a valid command and, if so,
319# run it.
320#
321declare -f "cmd_$1" > /dev/null
322ok_or_die "Unknown command: $1. Please use \`$0 help\` for help."
323
324cmd=cmd_$1
325shift
326
327ensure_build_dir
328
329$cmd "$@"
330