cc8300165c5d365fc19312cc6028966ae2e2a9d0
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / bin / mirror-dump-sstream-data
1 #!/bin/bash
2 set -f
3 #
4
5
6 Usage() {
7    cat <<EOF
8 Usage: ${0##*/} mirror_dir
9
10   write tab delimited data for simple streams 'tab2streams' input.
11
12   Example:
13
14     bzr branch lp:simplestreams simplestreams
15     bzr branch lp:cirros cirros
16
17     ./cirros/bin/mirror-dump-sstream-data "\$mirror_d" > mdata.txt
18     ./simplestreams/tools/tenv tab2streams mdata.txt cirros-streams.d
19
20     ./simplestreams/tools/tenv env -u GNUPGHOME \
21        SS_GPG_BATCH=1 SS_GPG_DEFAULT_KEY=A5DDB840  \
22        js2signed cirros-streams.d
23 EOF
24 }
25
26 datefortag() {
27    local loginfo ts out repo="${CIRROS_BZR:-.}"
28    local spec="$1" fmt="${2:-+%Y%m%d}"
29    loginfo=$(bzr log "$repo" --log-format=long --revision "$spec") ||
30       { error "couldn't bzr log tag:$i"; return 1; }
31    ts=$(echo "$loginfo" | sed -n '/^timestamp:/s,.*: ,,p') &&
32       [ -n "$ts" ] || {
33          error "failed to get timestamp from log for $spec";
34          return 1;
35    }
36    out=$(date --date="$ts" "$fmt") ||
37       { error "failed convert of '$ts' to format=$fmt"; return 1; }
38    _RET="$out"
39 }
40
41 error() { echo "$@" 1>&2; }
42 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
43
44 [ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
45 [ "$#" -eq 1 ] || { Usage 1>&2; fail "must give mirror dir"; }
46
47 if [ -z "$CIRROS_BZR" ]; then
48    CIRROS_BZR=$(t=$(readlink -f "$0") && cd "${t%/*}/.." && pwd)
49 fi
50
51 OIFS="$IFS"
52 declare -A VERSION2SERIAL
53 VERSION2SERIAL=( [0.3.0]="20111020" [0.3.1]="20130207"
54                  [0.3.1~pre1]="20120611" [0.3.1~pre3]="20120827"
55                  [0.3.1~pre4]="20130111" [0.3.2~pre1]="20130513"
56                  [0.3.2~pre2]="20131218" [0.3.2~pre3]="20140315"
57                  [0.3.2]="20140317"
58 )
59
60 #content-id product_name version_name img_name [key=value [key=value]]
61 content_id_base="net.cirros-cloud:%(stream)s:download"
62
63 tab="$(printf '\t')"
64
65 top_d="${1:-.}"
66 cd "$top_d"
67 IFS=$'\n'; files=( $(find . -type f -printf "%p|%s\n") ); IFS="$OIFS"
68
69 for ent in "${files[@]}"; do
70    IFS="|"; set -- $ent; IFS="$OIFS"
71    path=${1#./}
72    size=${2}
73
74    case "$path" in
75       *-uec.tar.gz|*-disk.img|*-rootfs.img|*-lxc.tar.gz) :;;
76       *) continue;;
77    esac
78    IFS="/"; set -- $path; IFS=$OIFS
79    version="$1"; fname="$2";
80    serial="${VERSION2SERIAL[$version]}"
81    if [ -z "$serial" ]; then
82       datefortag "tag:$version" ||
83          fail "serial not found for $version!"
84       serial="$_RET"
85       VERSION2SERIAL[$version]="$serial"
86    fi
87
88    # tokenize cirros-0.3.1~pre4-x86_64-lxc.tar.gz, baseproduct gets 'cirros'
89    IFS="-"; set -- $fname; IFS="$OIFS"
90    baseproduct="$1"; _version="$2"; arch="$3"; ftype="$4"
91
92    # tokenize 0.3.1~pre1 or 0.3.0
93    extra=${version#*~}; [ "$extra" = "$version" ] && extra=""
94    tmp=${version%$extra}
95    IFS="."; set -- ${version%$extra}; IFS="$IOFS"
96    vmaj="$1"; vmin="$2"; vmic="$3"
97
98    [ -z "$extra" ] && stream="released" || stream="devel"
99
100    # productname is "net.cirros-cloud[.devel]:standard:0.3:arch"
101    rdns="net.cirros-cloud"
102    [ "$stream" = "released" ] || rdns="${rdns}.${stream}"
103    product_name="$rdns:standard:$vmaj.$vmin:$arch"
104    content_id="${content_id_base//%(stream)s/${stream}}"
105
106    req_out=""
107    for item in "$content_id" "$product_name" "$serial" "$ftype"; do
108       req_out="${req_out}${tab}${item}"
109    done
110    req_out=${req_out#${tab}}
111
112    md5=$(md5sum "${path}") && md5="${md5%%  ${path}}"
113    sha256=$(sha256sum "${path}") && sha256="${sha256%%  ${path}}"
114    pubname="${baseproduct}-${version}-${arch}"
115
116    out=""
117    for i in ftype path arch version size sha256 md5 pubname stream; do
118       out="${out}${tab}${i}=${!i}"
119    done
120    out=${out#${tab}}
121    printf "%s\n" "${req_out}${tab}${out}"
122    #printf "
123    #echo "$content_id
124    #echo "version=$version fname=$fname stream=$stream product_name=$product_name"
125 done