~ubuntu-on-ec2/ubuntu-on-ec2/ec2-publishing-scripts

« back to all changes in this revision

Viewing changes to find-kernel-ramdisk

  • Committer: Thomas Bechtold
  • Date: 2021-11-02 09:26:52 UTC
  • mfrom: (674.1.1 ec2-publishing-scripts)
  • Revision ID: thomas.bechtold@canonical.com-20211102092652-egu0lz0uflc5yyj5
Drop all code & update README with the new url

The code moved to git, so drop all code and
mention in the README the new URL and that
the code from this repo is still there,
just in the 2nd oldest revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
# vi: ts=4 noexpandtab
3
 
 
4
 
REGIONS=( )
5
 
TMPD=""
6
 
OWNER_ID="099720109477"
7
 
error() { echo "$@" 1>&2; }
8
 
errorp() { printf "$@" 1>&2; }
9
 
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
10
 
failp() { [ $# -eq 0 ] || errorp "$@"; exit 1; }
11
 
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; exit 1; }
12
 
cleanup() {
13
 
        [ -n "${TMPD}" -a -d "${TMPD}" ] && rm -Rf "${TMPD}"
14
 
        return 0
15
 
}
16
 
 
17
 
prepare_for_sort() {
18
 
        # example basenames:
19
 
        # ubuntu-karmic-i386-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-kernel.img
20
 
        # ubuntu-karmic-i386-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-ramdisk.20091020.img
21
 
        # ubuntu-karmic-x86_64-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-ramdisk.20091020.1.img
22
 
        # ubuntu-karmic-x86_64-linux-image-2.6.31-300-ec2-v-2.6.31-300.5-kernel.img 
23
 
        
24
 
        # read 2 inputs id manifest
25
 
        # write 4 outputs:
26
 
        #   string up to "-kernel" or "-ramdisk"
27
 
    #   string after , or '0' if empty
28
 
        #   id, manifest
29
 
        # this should be sortable on key1, key2
30
 
 
31
 
        local bname id man tmp ser base suf=".img.manifest.xml"
32
 
        while read id man; do
33
 
                bname=${man##*/}
34
 
                case "${bname}" in
35
 
                        *-kernel${suf}|*-ramdisk${suf}) ser=0; base=${bname%-*${suf}};;
36
 
                        *-kernel*${suf})
37
 
                                tmp=${bname%${suf}}
38
 
                                base=${tmp%-kernel*}
39
 
                                ser=${tmp##*-kernel.};;
40
 
                        *-ramdisk.*.img.manifest.xml)
41
 
                                tmp=${bname%${suf}}
42
 
                                base=${tmp%-ramdisk*}
43
 
                                ser=${tmp##*-ramdisk.};;
44
 
            *) ser=0; base=${bname};;
45
 
                esac
46
 
                printf "%s\t%s\t%s\t%s\t\n" \
47
 
                        "${base}" "${ser}" "${id}" "${man}"
48
 
        done
49
 
}
50
 
 
51
 
krd_sort() {
52
 
        local x
53
 
        prepare_for_sort | LANG=C sort --reverse |
54
 
                awk -F '\t' '{ printf("%s\t%s\t\n",$3,$4); }'
55
 
        for x in "${PIPESTATUS[@]}"; do [ "$x" = "0" ] || return 1; done
56
 
        return 0
57
 
}
58
 
 
59
 
checkstatus() {
60
 
        local x="" i=0
61
 
        for x in "$@"; do
62
 
                [ "$x" = "0" ] || i=$(($i+1))
63
 
        done
64
 
        return $i
65
 
}
66
 
 
67
 
get_regions() {
68
 
        local regs=""
69
 
        [ "${#REGIONS[@]}" -eq 0 ] || return 0
70
 
        REGIONS=( $(ec2-list-all-regions) ) || return 1
71
 
}
72
 
 
73
 
Usage() {
74
 
        cat <<EOF
75
 
Usage: ${0##*/} [ options ] suite [arch]
76
 
 
77
 
   find kernels and ramdisks on ec2 for suite release-type
78
 
   output format is the same as 'publish-image' format, and should
79
 
   be sorted within each region in "newest first" as:
80
 
     kernel-1
81
 
     matching-ramdisk-b
82
 
     matching-ramdisk-a
83
 
     kernel-2
84
 
     matching-ramdisk-b
85
 
     matching-ramdisk-a
86
 
 
87
 
   if arch given, only do so for 'arch' (default: ${ARCHES[*]})
88
 
 
89
 
   options:
90
 
      -l | --latest           only print the "latest"
91
 
      -m | --match m          restrict kernels to those matching 'm'
92
 
      -r | --regions r        limit search to 'r' (comma or space delimited)
93
 
                              default: all (per describe-regions)
94
 
EOF
95
 
}
96
 
 
97
 
short_opts="blmr"
98
 
long_opts="latest,match:,regions:"
99
 
getopt_out=$(getopt --name "${0##*/}" \
100
 
    --options "${short_opts}" --long "${long_opts}" -- "$@") &&
101
 
    eval set -- "${getopt_out}" ||
102
 
    bad_Usage
103
 
 
104
 
latest=0
105
 
match="";
106
 
regions=( )
107
 
arches=( i386 amd64 )
108
 
 
109
 
while [ $# -ne 0 ]; do
110
 
        cur=${1}; next=${2};
111
 
        case "$cur" in
112
 
                --) shift; break;;
113
 
                -l|--latest) latest=1;;
114
 
                -m|--match) match=${next}; shift;;
115
 
                -r|--regions) regions=( ${next//,/ } ); shift;;
116
 
        esac
117
 
        shift;
118
 
done
119
 
 
120
 
[ $# -lt 1 ] && bad_Usage "must provide suite"
121
 
suite=${1}
122
 
shift;
123
 
 
124
 
[ $# -eq 0 ] || arches=( "${@}" );
125
 
 
126
 
TMPD=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
127
 
        fail "failed to make tmpdir"
128
 
trap cleanup EXIT
129
 
 
130
 
export XC2_XIMAGES_CACHE_D=${XC2_XIMAGES_CACHE_D:-"$TMPD/ximgcache"}
131
 
 
132
 
if [ ${#regions[@]} -eq 0 ]; then
133
 
        get_regions || fail "failed to get region list"
134
 
        regions=( "${REGIONS[@]}" )
135
 
fi
136
 
 
137
 
for region in "${regions[@]}"; do
138
 
        imglist="${TMPD}/images.${region}.txt"
139
 
        xc2 ximages describe-images --region "${region}" --all > "${imglist}" ||
140
 
                fail "failed to get images on ${region}"
141
 
 
142
 
        for arch in "${arches[@]}"; do
143
 
                action='printf("%s\t%s\n",$2,$3);'
144
 
                criteria='$9 == type && $8 == xarch && $3 ~ regex'
145
 
 
146
 
                klist="${TMPD}/kernels-${arch}.txt"
147
 
                rlist="${TMPD}/ramdisks-${arch}.txt"
148
 
                sklist="${klist}.sort"
149
 
                srlist="${rlist}.sort"
150
 
 
151
 
                [ "${arch}" = "x86_64" ] && arch=amd64;
152
 
                xarch=${arch}; [ "${xarch}" = "amd64" ] && xarch=x86_64;
153
 
 
154
 
                # account for region replacement in match input
155
 
                if [ -n "$match" ]; then
156
 
                        # replace '%R' in match with region
157
 
                        rmatch="${match//%R/$region}"
158
 
                        kmatch="^${OWNER_ID}/${rmatch}$"
159
 
                else
160
 
                        kmatch="^${OWNER_ID}/"
161
 
                fi
162
 
                awk '-F\t' "${criteria} { ${action} }" \
163
 
                        "regex=${kmatch}" "type=kernel" "xarch=${xarch}" \
164
 
                        "${imglist}" > "${klist}" ||
165
 
                        fail "failed to search image results for ${kmatch}"
166
 
 
167
 
                [ -s "${klist}" ] || { 
168
 
                        errorp "no matching kernels in %s for suite %s arch %s\n\t%s\n" \
169
 
                                "${region}" "${suite}" "${arch}" "regex:${kmatch}";
170
 
                                continue; 
171
 
                        }
172
 
 
173
 
                krd_sort < "${klist}" > "${sklist}" || fail "failed to sort list"
174
 
 
175
 
                while read k_ami k_manif; do
176
 
 
177
 
                        rmatch=${k_manif%-kernel*} # find ramdisks that match this kernel
178
 
 
179
 
                        awk '-F\t' "${criteria} { ${action} }" \
180
 
                                "regex=${rmatch}" "type=ramdisk" "xarch=${xarch}" \
181
 
                                "${imglist}" > "${rlist}" ||
182
 
                                fail "failed to search image results for ${rmatch}"
183
 
 
184
 
                        krd_sort < "${rlist}" > "${srlist}" ||
185
 
                                fail "failed to sort ramdisk list"
186
 
 
187
 
                        printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${k_ami}" \
188
 
                                "${arch}" kernel "${k_manif}"
189
 
 
190
 
                        # if 'latest', then we only print 1 ramdisk, and 1 kernel
191
 
                        if [ $latest -eq 1 ]; then
192
 
                                read ami manif < "${srlist}"
193
 
                                [ -z "${ami}" ] ||
194
 
                                        printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${ami}" \
195
 
                                                "${arch}" "ramdisk" "${manif}"
196
 
                                break;
197
 
                        else
198
 
                                # loop over ramdisk results
199
 
                                while read ami manif; do
200
 
                                        [ -z "${ami}" ] ||
201
 
                                                printf "%s\t%s\t%s\t%s\t%s\n" "${region}" "${ami}" \
202
 
                                                        "${arch}" "ramdisk" "${manif}"
203
 
                                done < "${srlist}"
204
 
                        fi
205
 
 
206
 
                done < "${sklist}"
207
 
        done
208
 
done
209
 
 
210
 
exit 0