summaryrefslogtreecommitdiff
path: root/testing/docker/dmgr.sh
blob: 1330867ef31b11621a21f042b7c016bc981867d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash

# Run from the directory containing this script
cd `dirname $0`

msg()
{
    echo "INFO  :: $*" >&2
}

error()
{
    echo "ERROR :: $*" >&2
    exit 1
}

usage()
{
    cat >&2 <<EOF
${1:+ERROR :: $1

}Usage: $0 [action] [containter] [args...]

Action is one of:

   build     Build or rebuild the specified containers.
   install   Install crosstool-NG in specified containers.
   sample    Build a sample or if no sample name specified, all.
   enter     Spawn a shell in the specified container.
   root      Spawn a root shell in the specified container.
   clean     Clean up in the specified container.

If a special container name 'all' is used, the action is performed
on all the containers.
EOF
    exit 1
}

do_cleanup()
{
    local d

    for d in "$@"; do
        [ -d "$d" ] || continue
        chmod -R a+w "$d"
        rm -rf "$d"
    done
}

# Build a docker container, store its ID.
action_build()
{
    local cntr=$1

    msg "Building Docker container for ${cntr}"
set -x
    docker build --no-cache -t "ctng-${cntr}" --build-arg CTNG_GID=`id -g` --build-arg CTNG_UID=`id -u` "${cntr}"
}

# Common backend for enter/test
_dckr()
{
    local topdir=`cd ../.. && pwd`
    local cntr=$1
    local scmd prefix
    shift

    mkdir -p ${cntr}/{build,install,xtools}
    prefix="docker run --rm -i -t \
        -v `pwd`/common-scripts:/common-scripts:ro \
        -v ${topdir}:/crosstool-ng:ro \
        -v `pwd`/${cntr}/build:/home/ctng/work \
        -v `pwd`/${cntr}/install:/opt/ctng \
        -v `pwd`/${cntr}/xtools:/home/ctng/x-tools \
        -v $HOME/src:/home/ctng/src:ro \
        ctng-${cntr}"
    if [ -n "${AS_ROOT}" ]; then
        $prefix "$@"
    elif [ -n "$*" ]; then
        $prefix su -l ctng -c "$*"
    else
        $prefix su -l ctng
    fi
}

# Run the test
action_install()
{
    local cntr=$1

    # The test assumes the top directory is bootstrapped, but clean.
    msg "Setting up crosstool-NG in ${cntr}"
    do_cleanup ${cntr}/build
    _dckr "${cntr}" /common-scripts/ctng-install && \
        _dckr "${cntr}" /common-scripts/ctng-test-basic
}

# Run the test
action_sample()
{
    local cntr=$1
    shift

    msg "Building samples in ${cntr} [$@]"
    do_cleanup ${cntr}/build
    _dckr "${cntr}" /common-scripts/ctng-build-sample "$@"
}

# Enter the container using the same user account/environment as for testing.
action_enter()
{
    local cntr=$1
    shift

    msg "Entering ${cntr}"
    _dckr "${cntr}" "$@"
}

# Enter the container using the same user account/environment as for testing.
action_root()
{
    local cntr=$1

    msg "Entering ${cntr} as root"
    AS_ROOT=y _dckr "${cntr}" /bin/bash
}

# Clean up after test suite run
action_clean()
{
    local cntr=$1

    msg "Cleaning up after ${cntr}"
    do_cleanup ${cntr}/build
}

# Clean up after test suite run
action_distclean()
{
    local cntr=$1

    msg "Dist cleaning ${cntr}"
    do_cleanup ${cntr}/{build,install,xtools}
}

all_containers=`ls */Dockerfile | sed 's,/Dockerfile,,'`
action=$1
selected_containers=$2
shift 2
if [ "${selected_containers}" = "all" ]; then
    selected_containers="${all_containers}"
fi

case "${action}" in
    build|install|sample|enter|root|clean|distclean)
        for c in ${selected_containers}; do
            eval "action_${action} ${c} \"$@\""
        done
        ;;
    "")
        usage "No action specified."
        ;;
    *)
        usage "Unknown action ${action}."
        ;;
esac