xref: /cloud-hypervisor/scripts/dev_cli.sh (revision a5b053f81d24e323f73c7197383dda266d58aaa9)
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_TARGET="${CTR_CLH_ROOT_DIR}/build/cargo_target"
27CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads"
28
29# Cargo paths
30# Full path to the cargo registry dir on the host. This appears on the host
31# because we want to persist the cargo registry across container invocations.
32# Otherwise, any rust crates from crates.io would be downloaded again each time
33# we build or test.
34CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry"
35
36# Full path to the cargo git registry on the host. This serves the same purpose
37# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of
38# crates.io.
39CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry"
40
41# Full path to the cargo target dir on the host.
42CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target"
43
44# Send a decorated message to stdout, followed by a new line
45#
46say() {
47    [ -t 1 ] && [ -n "$TERM" ] \
48        && echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
49        || echo "[$CLI_NAME] $*"
50}
51
52# Send a decorated message to stdout, without a trailing new line
53#
54say_noln() {
55    [ -t 1 ] && [ -n "$TERM" ] \
56        && echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
57        || echo "[$CLI_NAME] $*"
58}
59
60# Send a text message to stderr
61#
62say_err() {
63    [ -t 2 ] && [ -n "$TERM" ] \
64        && echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \
65        || echo "[$CLI_NAME] $*" 1>&2
66}
67
68# Send a warning-highlighted text to stdout
69say_warn() {
70    [ -t 1 ] && [ -n "$TERM" ] \
71        && echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \
72        || echo "[$CLI_NAME] $*"
73}
74
75# Exit with an error message and (optional) code
76# Usage: die [-c <error code>] <error message>
77#
78die() {
79    code=1
80    [[ "$1" = "-c" ]] && {
81        code="$2"
82        shift 2
83    }
84    say_err "$@"
85    exit $code
86}
87
88# Exit with an error message if the last exit code is not 0
89#
90ok_or_die() {
91    code=$?
92    [[ $code -eq 0 ]] || die -c $code "$@"
93}
94
95# Make sure the build/ dirs are available. Exit if we can't create them.
96# Upon returning from this call, the caller can be certain the build/ dirs exist.
97#
98ensure_build_dir() {
99    for dir in "$CLH_BUILD_DIR" \
100		   "$CLH_INTEGRATION_WORKLOADS" \
101		   "$CLH_CTR_BUILD_DIR" \
102		   "$CARGO_TARGET_DIR" \
103		   "$CARGO_REGISTRY_DIR" \
104		   "$CARGO_GIT_REGISTRY_DIR"; do
105        mkdir -p "$dir" || die "Error: cannot create dir $dir"
106        [ -x "$dir" ] && [ -w "$dir" ] || \
107            {
108                say "Wrong permissions for $dir. Attempting to fix them ..."
109                chmod +x+w "$dir"
110            } || \
111            die "Error: wrong permissions for $dir. Should be +x+w"
112    done
113}
114
115cmd_help() {
116    echo ""
117    echo "Cloud Hypervisor $(basename $0)"
118    echo "Usage: $(basename $0) <command> [<command args>]"
119    echo ""
120    echo "Available commands:"
121    echo ""
122    echo "    build [--debug|--release] [-- [<cargo args>]]"
123    echo "        Build the Cloud Hypervisor binaries."
124    echo "        --debug               Build the debug binaries. This is the default."
125    echo "        --release             Build the release binaries."
126    echo ""
127    echo "    tests [--unit|--cargo|--all] [-- [<cargo test args>]]"
128    echo "        Run the Cloud Hypervisor tests."
129    echo "        --unit               Run the unit tests."
130    echo "        --cargo              Run the cargo tests."
131    echo "        --integration        Run the integration tests."
132    echo "        --all                Run all tests."
133    echo ""
134    echo "    build-container [--type]"
135    echo "        Build the Cloud Hypervisor container."
136    echo "        --dev                Build dev container. This is the default."
137    echo ""
138    echo "    clean [<cargo args>]]"
139    echo "        Remove the Cloud Hypervisor artifacts."
140    echo ""
141    echo "    help"
142    echo "        Display this help message."
143    echo ""
144}
145
146cmd_build() {
147    build="debug"
148
149    while [ $# -gt 0 ]; do
150	case "$1" in
151            "-h"|"--help")  { cmd_help; exit 1;     } ;;
152            "--debug")      { build="debug";      } ;;
153            "--release")    { build="release";    } ;;
154            "--")           { shift; break;         } ;;
155            *)
156		die "Unknown build argument: $1. Please use --help for help."
157		;;
158	esac
159	shift
160    done
161
162    cargo_args=("$@")
163    [ $build = "release" ] && cargo_args+=("--release")
164
165    $DOCKER_RUNTIME run \
166	   -ti \
167	   --workdir "$CTR_CLH_ROOT_DIR" \
168	   --rm \
169	   --volume /dev:/dev \
170	   --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
171	   "$CTR_IMAGE" \
172	   cargo build \
173	         --target-dir "$CTR_CLH_CARGO_TARGET" \
174	         "${cargo_args[@]}"
175
176    ret=$?
177
178    # If `cargo build` was successful, let's copy the binaries to a more
179    # accessible location.
180    [ $ret -eq 0 ] && {
181        cargo_bin_dir="$CLH_CARGO_TARGET/$build"
182        say "Binaries placed under $cargo_bin_dir"
183    }
184}
185
186cmd_clean() {
187    cargo_args=("$@")
188
189    $DOCKER_RUNTIME run \
190	   -ti \
191	   --workdir "$CTR_CLH_ROOT_DIR" \
192	   --rm \
193	   --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
194	   "$CTR_IMAGE" \
195	   cargo clean \
196	         --target-dir "$CTR_CLH_CARGO_TARGET" \
197	         "${cargo_args[@]}"
198    }
199
200cmd_tests() {
201    unit=false
202    cargo=false
203    integration=false
204
205    while [ $# -gt 0 ]; do
206	case "$1" in
207            "-h"|"--help")    { cmd_help; exit 1;     } ;;
208            "--unit")         { unit=true;      } ;;
209            "--cargo")        { cargo=true;    } ;;
210	    "--integration")  { integration=true;    } ;;
211	    "--all")          { cargo=true; unit=true; integration=true;  } ;;
212            "--")             { shift; break;         } ;;
213            *)
214		die "Unknown tests argument: $1. Please use --help for help."
215		;;
216	esac
217	shift
218    done
219
220    if [ "$unit" = true ] ;  then
221	say "Running unit tests..."
222	$DOCKER_RUNTIME run \
223	       -ti \
224	       --workdir "$CTR_CLH_ROOT_DIR" \
225	       --rm \
226	       --privileged \
227	       --volume /dev:/dev \
228	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
229	       "$CTR_IMAGE" \
230	       ./scripts/run_unit_tests.sh "$@"
231    fi
232
233    if [ "$cargo" = true ] ;  then
234	say "Running cargo tests..."
235	$DOCKER_RUNTIME run \
236	       -ti \
237	       --workdir "$CTR_CLH_ROOT_DIR" \
238	       --rm \
239	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
240	       "$CTR_IMAGE" \
241	       ./scripts/run_cargo_tests.sh
242    fi
243
244    if [ "$integration" = true ] ;  then
245	say "Running integration tests..."
246	$DOCKER_RUNTIME run \
247	       -ti \
248	       --workdir "$CTR_CLH_ROOT_DIR" \
249	       --rm \
250	       --privileged \
251	       --mount type=tmpfs,destination=/tmp \
252	       --volume /dev:/dev \
253	       --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
254	       --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
255	       "$CTR_IMAGE" \
256	       ./scripts/run_integration_tests.sh "$@"
257    fi
258}
259
260cmd_build-container() {
261    container_type="dev"
262
263    while [ $# -gt 0 ]; do
264	case "$1" in
265            "-h"|"--help")  { cmd_help; exit 1;     } ;;
266            "--dev")        { container_type="dev"; } ;;
267            "--")           { shift; break;         } ;;
268            *)
269		die "Unknown build-container argument: $1. Please use --help for help."
270		;;
271	esac
272	shift
273    done
274
275    BUILD_DIR=/tmp/cloud-hypervisor/container/
276
277    mkdir -p $BUILD_DIR
278    cp $CLH_DOCKERFILE $BUILD_DIR
279
280    $DOCKER_RUNTIME build \
281	   --target $container_type \
282	   -t $CTR_IMAGE \
283	   -f $BUILD_DIR/Dockerfile \
284	   $BUILD_DIR
285}
286
287# Parse main command line args.
288#
289while [ $# -gt 0 ]; do
290    case "$1" in
291        -h|--help)              { cmd_help; exit 1;     } ;;
292        -y|--unattended)        { OPT_UNATTENDED=true;  } ;;
293        -*)
294            die "Unknown arg: $1. Please use \`$0 help\` for help."
295            ;;
296        *)
297            break
298            ;;
299    esac
300    shift
301done
302
303# $1 is now a command name. Check if it is a valid command and, if so,
304# run it.
305#
306declare -f "cmd_$1" > /dev/null
307ok_or_die "Unknown command: $1. Please use \`$0 help\` for help."
308
309cmd=cmd_$1
310shift
311
312ensure_build_dir
313
314$cmd "$@"
315