~andreserl/maas/update-maas-images-v3

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
#!/bin/bash

_APT_UPDATED=false
MEPH2_BRANCH="lp:maas-images"
SSTREAM_BRANCH="lp:simplestreams"
CROSS_DEPS=( qemu-user-static u-boot-tools )
MEPH2_D=""
# MEPH2_SIMPLESTREAMS can be:
# 'package': install the package
# 'system': use whatever 'import' provides
# 'branch': use the MEPH2_BRANCH
# 'auto': use system if available or distro (> trusty), or branch
MEPH2_SIMPLESTREAMS=${MEPH2_SIMPLESTREAMS:-"auto"}
MEPH2_UTIL_ONLY=${MEPH2_UTIL_ONLY:-"auto"}

error() { echo "$@" 1>&2; }
fail() { [ $# -eq 0 ] || error "$@"; exit 2; }

rel="$(lsb_release -sc)"
DEPS_UTIL_ONLY=(
    bzr
    python3
    python3-yaml
)

DEPS_FULL=(
    bzr
    cloud-image-utils      # for mount-image-callback
    gpgv
    python3-distro-info
    python3-requests
    python3-yaml
    squashfs-tools
    zerofree
)

apt_get() {
    local ret=""
    if [ "$1" != "update" ] && ! $_APT_UPDATED; then
        error "updating apt"
        apt_get update >/dev/null || {
            ret=$?;
            error "failed to update apt [$ret]";
            return $ret;
        }
        _APT_UPDATED=true
    fi
    sudo DEBIAN_FRONTEND=noninteractive apt-get --quiet \
        --assume-yes "$@" </dev/null
}

filter_installed_packages() {
    # write to stdout, a list of packages not installed locally
    local fmt='${Package} ${Version}\n'
    LC_ALL=C dpkg-query --show "--showformat=${fmt}" "$@" 2>&1 | awk '
        $0 ~ /[Nn]o packages/ {
            sub("[.]$","",$NF);
            pkgs[n]=$NF;
            n=n+1;
        }
        $2 == "" {
                pkgs[n]=$1;
                n=n+1;
        };
        END { for(p in pkgs) {printf("%s ",pkgs[p])}; printf("\n"); }' n=0
}

apt_install() {
    local needed
    needed=$(filter_installed_packages "$@")
    [ -z "$needed" ] && return 0
    error "installing: $needed"
    apt_get install "$@"
}


streams_package=true
if [[ "$rel" < trusty ]]; then
    error "# WARN: building not supported prior to trusty" 1>&2
    error "# WARN:  but meph2-util (index operations) supported" 1>&2
    [ "${MEPH2_SIMPLESTREAMS}" = "package" ] &&
        fail "no distro simplestreams < trusty"
    MEPH2_SIMPLESTREAMS="auto"
    streams_package=false
    def_meph2_only=true
else
    def_meph2_only=false
fi

case "$MEPH2_UTIL_ONLY" in
    false|0) meph2_only=false;;
    auto) meph2_only=$def_meph2_only;;
    *) meph2_only=true;;
esac

if $meph2_only; then
    DEPS=( "${DEPS_UTIL_ONLY[@]}" )
else    
    DEPS=( "${DEPS_FULL[@]}" )
fi

if [ "$MEPH2_SIMPLESTREAMS" = "auto" ]; then
    if python3 -c 'import simplestreams' >/dev/null 2>&1; then
       MEPH2_SIMPLESTREAMS="system"
    elif $streams_package; then
       MEPH2_SIMPLESTREAMS="package"
    else
       MEPH2_SIMPLESTREAMS="branch"
    fi
fi
[ "$MEPH2_SIMPLESTREAMS" = "package" ] &&
    DEPS=( "${DEPS[@]}" python3-simplestreams )

me=$(readlink -f "$0")
myd=$(dirname "$me")
if [ -f "$myd/bin/meph2-cloudimg-sync" ]; then
    MEPH2_D="$myd"
elif [ -f "$PWD/maas-images/bin/meph2-cloudimg-sync" ]; then
    MEPH2_D="$PWD/maas-images"
fi

error "MEPH2_SIMPLESTREAMS=$MEPH2_SIMPLESTREAMS MEPH2_UTIL_ONLY=$meph2_only"

arch=$(dpkg --print-architecture)
deps=( "${DEPS[@]}" )
if [ "$arch" = "amd64" ] && ! $meph2_only; then
    deps=( "${deps[@]}" "${CROSS_DEPS[@]}" )
fi

apt_install "${deps[@]}"

if [ -z "$MEPH2_D" ]; then
    error "getting $MEPH2_BRANCH to $PWD/maas-images"
    bzr branch "$MEPH2_BRANCH" maas-images ||
        fail "failed branch $MEPH2_BRANCH"
    MEPH2_D="$PWD/maas-images"
fi

if [ "$MEPH2_SIMPLESTREAMS" = "branch" ]; then
    sstream_d="$MEPH2_D/simplestreams.bzr"
    if [ ! -d "$sstream_d" ]; then
        bzr branch "$SSTREAM_BRANCH" "$sstream_d" &&
            ln -snf simplestreams.bzr/simplestreams "$MEPH2_D/simplestreams" ||
            fail "failed to get $SSTREAM_BRANCH"
        out=$(cd "$MEPH2_D" && python3 -c 'import simplestreams' 2>&1) ||
            fail "error getting simplestreams: $out"
    fi
fi

# vi: ts=4 expandtab