5
error() { echo "$@" 1>&2; }
6
rerror() { local r=$?; [ $r -eq 0 ] && r=1; error "$@"; return $r; }
7
fail() { local r=$?; [ $r -eq 0 ] && r=1; failrc "$r" "$@"; }
8
failrc() { local r=$1; shift; [ $# -eq 0 ] || error "$@"; exit $r; }
11
local level=${1}; shift;
12
[ "${level}" -gt "${VERBOSITY}" ] && return
18
Usage: ${0##*/} [ options ] container [command [args]]
20
run command in container with provided args.
22
This is most useful as a program provided to vmtest
23
CURTIN_VMTEST_CURTIN_EXE="${0} container curtin" ./tools/jenkins-runner
25
if 'command and arg1' is 'curtin pack', then it will
26
read --add arguments to curtin pack, and copy them into a tmpdir container
27
and adjust the command and args to reference the copied paths.
30
CURTIN_PACK_CONTAINER=container ${0##*/} [command [args]]
34
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
37
local n="$1" close_in=true
39
[ "$1" = "-" ] && { close_in=false; shift; }
40
set -- lxc exec --mode=non-interactive "$n" -- "$@"
50
local name="$1" prefix="$2" arg="$3"
51
local afile="" fpath="" dname=""
52
# take ARCHIVE_PATH:FILE_PATH as 'arg'
53
# push FILE_PATH into $name/prefix/$archive_path
54
# return prefix/archive_path
57
dname=$(dirname "${prefix}/${afile}")
58
# older lxc (2.0.9) do not have --create-dirs option to file push
59
# so create the directory ourselves.
60
inside "$name" mkdir -p "$dname" || {
61
rerror "failed to create '$dname' in container '$name'"
65
lxc file push "$fpath" "$name/$prefix/$afile" || {
66
rerror "failed: lxc file push '$fpath' '$name/$prefix/$afile'"
69
_RET="$afile:$prefix/$afile"
73
local name="$1" tmpd="" ret=$?
76
while [ $# -ne 0 ]; do
82
shift 2 || { rerror "failed shift 2 on $1"; return; }
84
--add=*) from="$1"; arg=${1#*=}; shift;;
85
*) cmd[${#cmd[@]}]="$1"; shift; continue;;
87
if [ -z "$tmpd" ]; then
88
tmpd=$(inside "$name" mktemp -d) ||
89
{ rerror "failed to make tmpdir in $name"; return; }
91
send "$name" "$tmpd" "$arg" ||
92
{ rerror "failed send($name, $tmpd, $arg)"; return; }
93
debug 1 "changed $from => --add=${_RET}"
94
cmd[${#cmd[@]}]="--add=${_RET}"
96
cmd=( "${cmd[@]}" "$@" )
97
inside "$name" "${cmd[@]}"
99
inside "$name" rm -Rf "$tmpd" || {
100
rerror "failed removing tmpdir in $name";
106
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
108
if [ -n "${CURTIN_PACK_CONTAINER}" ]; then
109
container="${CURTIN_PACK_CONTAINER}"
111
fail "must give command (CURTIN_PACK_CONTAINER=${container})"
114
fail "must give container and command (or set CURTIN_PACK_CONTAINER)"
118
out=$(lxc info "$container") ||
119
fail "failed 'lxc info $container'. container '$container' does not exist?"
121
if [ "${1##*/}" = "curtin" -a "$2" = "pack" ]; then
122
packrun "$container" "$@"
124
inside "$container" "$@"