~curtin-dev/curtin/trunk

« back to all changes in this revision

Viewing changes to tools/curtin-from-container

  • Committer: Scott Moser
  • Date: 2017-12-20 17:33:03 UTC
  • Revision ID: smoser@ubuntu.com-20171220173303-29gha5qb8wpqrd40
README: Mention move of revision control to git.

curtin development has moved its revision control to git.
It is available at
  https://code.launchpad.net/curtin

Clone with
  git clone https://git.launchpad.net/curtin
or
  git clone git+ssh://git.launchpad.net/curtin

For more information see
  http://curtin.readthedocs.io/en/latest/topics/development.html

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
 
3
 
VERBOSITY=0
4
 
 
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; }
9
 
 
10
 
debug() {
11
 
    local level=${1}; shift;
12
 
    [ "${level}" -gt "${VERBOSITY}" ] && return
13
 
    error "${@}"
14
 
}
15
 
 
16
 
Usage() {
17
 
    cat <<EOF
18
 
Usage: ${0##*/} [ options ] container [command [args]]
19
 
 
20
 
   run command in container with provided args.
21
 
 
22
 
   This is most useful as a program provided to vmtest
23
 
     CURTIN_VMTEST_CURTIN_EXE="${0} container curtin" ./tools/jenkins-runner
24
 
 
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.
28
 
 
29
 
   also can be called by
30
 
     CURTIN_PACK_CONTAINER=container ${0##*/} [command [args]]
31
 
EOF
32
 
}
33
 
 
34
 
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
35
 
 
36
 
inside() {
37
 
    local n="$1" close_in=true
38
 
    shift
39
 
    [ "$1" = "-" ] && { close_in=false; shift; }
40
 
    set -- lxc exec --mode=non-interactive "$n" -- "$@"
41
 
    debug 1 "$@"
42
 
    if ${close_in}; then
43
 
        "$@" </dev/null
44
 
    else
45
 
        "$@"
46
 
    fi
47
 
}
48
 
 
49
 
send() {
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
55
 
    afile=${arg%%:*}
56
 
    fpath=${arg#*:}
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'"
62
 
        return
63
 
    }
64
 
 
65
 
    lxc file push "$fpath" "$name/$prefix/$afile" || {
66
 
        rerror "failed: lxc file push '$fpath' '$name/$prefix/$afile'"
67
 
        return
68
 
    }
69
 
    _RET="$afile:$prefix/$afile"
70
 
}
71
 
 
72
 
packrun() {
73
 
    local name="$1" tmpd="" ret=$?
74
 
    shift
75
 
    cmd=()
76
 
    while [ $# -ne 0 ]; do
77
 
        case "$1" in
78
 
            --) break;;
79
 
            -a|--add)
80
 
                from="$1 $2"
81
 
                arg="$2"
82
 
                shift 2 || { rerror "failed shift 2 on $1"; return; }
83
 
                ;;
84
 
            --add=*) from="$1"; arg=${1#*=}; shift;;
85
 
            *) cmd[${#cmd[@]}]="$1"; shift; continue;;
86
 
        esac
87
 
        if [ -z "$tmpd" ]; then
88
 
            tmpd=$(inside "$name" mktemp -d) ||
89
 
                { rerror "failed to make tmpdir in $name"; return; }
90
 
        fi
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}"
95
 
    done
96
 
    cmd=( "${cmd[@]}" "$@" )
97
 
    inside "$name" "${cmd[@]}"
98
 
    ret=$?
99
 
    inside "$name" rm -Rf "$tmpd" || {
100
 
        rerror "failed removing tmpdir in $name";
101
 
        return;
102
 
    }
103
 
    return $ret
104
 
}
105
 
 
106
 
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
107
 
 
108
 
if [ -n "${CURTIN_PACK_CONTAINER}" ]; then
109
 
    container="${CURTIN_PACK_CONTAINER}"
110
 
    [ $# -gt 0 ] ||
111
 
        fail "must give command (CURTIN_PACK_CONTAINER=${container})"
112
 
else
113
 
    [ $# -gt 1 ] ||
114
 
        fail "must give container and command (or set CURTIN_PACK_CONTAINER)"
115
 
    container="$1"
116
 
    shift
117
 
fi
118
 
out=$(lxc info "$container") ||
119
 
    fail "failed 'lxc info $container'. container '$container' does not exist?"
120
 
 
121
 
if [ "${1##*/}" = "curtin" -a "$2" = "pack" ]; then
122
 
    packrun "$container" "$@"
123
 
else
124
 
    inside "$container" "$@"
125
 
fi
126
 
 
127
 
# vi: ts=4 expandtab