summaryrefslogtreecommitdiff
path: root/tuf-stuff.sh
blob: 6a9178e101cfe2a66faac44480cb496651fea07d (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
#!/bin/bash

# Needed files:
#   Bitmask-linux32-0.7.0.tar.bz2  # fresh bundled bundle
#   Bitmask-linux64-0.7.0.tar.bz2  # fresh bundled bundle
#   tuf_private_key.pem            # private key
#   tuf-stuff.sh                   # this script

# Output:
#   workdir/ <-- temporary folder: virtualenv, bundle, repo.tar.gz, key
#   output/  <-- here you'll find the resulting compressed repo/bundle


# Expected directory structure for the repo after the script finishes:
# $ tree workdir/repo/
# repo
# ├── metadata.staged
# │   ├── root.json
# │   ├── snapshot.json
# │   ├── snapshot.json.gz
# │   ├── targets.json
# │   ├── targets.json.gz
# │   └── timestamp.json
# └── targets
#     ... Bitmask bundle files ...

set -e  # Exit immediately if a command exits with a non-zero status.

# Set some colors variables
esc=`echo -en "\033"`
cc_red="${esc}[31m"
cc_green="${esc}[32m"
cc_yellow="${esc}[33m"
cc_normal="${esc}[39m"

show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-s] [-a (32|64)] -v VERSION
Do stuff for version VERSION and arch ARCH.

    -h          display this help and exit.
    -a ARCH     do the tuf stuff for that ARCH, 32 or 64 bits. The default is '64'.
    -s          run the setup process, create virtualenv and install dependencies.
    -v VERSION  version to work with. This is a mandatory argument.
EOF
}

get_args() {
    # from: http://mywiki.wooledge.org/BashFAQ/035#getopts
    local OPTIND

    ARCH="64"

    while getopts "hsv:a:" opt; do
        case "$opt" in
            h)
                show_help
                exit 0
                ;;
            v)  VERSION=$OPTARG
                ;;
            s)  SETUP='YES'
                ;;
            a)  ARCH=$OPTARG
                ;;
            '?')
                show_help >&2
                exit 1
                ;;
        esac
    done
    shift "$((OPTIND-1))" # Shift off the options and optional --.

    if [[ -z $VERSION ]]; then
        echo 'Missing -v flag'
        show_help
        exit 1
    fi
}

# ----------------------------------------

do_init(){
    # Initialize the needed variables and create the work directory.

    BASE=`pwd`
    WORKDIR=$BASE/workdir
    VENVDIR=$WORKDIR/tuf.venv

    BITMASK="Bitmask-linux$ARCH-$VERSION"
    KEY="$BASE/tuf_private_key.pem"
    RELEASE=$BASE/../bitmask_client/pkg/tuf/release.py

    # Initialize path
    mkdir -p $WORKDIR
}

do_setup() {
    # Create a clean virtualenv and install the needed dependencies.
    echo "${cc_yellow}-> Setting up virtualenv and installing dependencies...${cc_normal}"
    cd $WORKDIR

    # remove existing virtualenv
    [[ -d $VENVDIR ]] && rm -fr $VENVDIR

    virtualenv $VENVDIR
    source $VENVDIR/bin/activate
    pip install tuf[tools] pycrypto
}

do_tuf_stuff() {
    cd $WORKDIR
    cp $BASE/$BITMASK.tar.bz2 .

    rm -fr repo/
    mkdir repo && cd repo/

    if [[ $ARCH == "64" ]]; then
        TUF_ARCH='linux-x86_64'
    else
        TUF_ARCH='linux-i386'
    fi

    # Download old repo metadata
    echo "${cc_yellow}-> Downloading metadata files from the old bundle...${cc_normal}"
    wget --quiet --recursive --no-host-directories --cut-dirs=2 --no-parent --reject "index.html*" https://dl.bitmask.net/tuf/$TUF_ARCH/metadata/
    mv metadata metadata.staged

    echo "${cc_yellow}-> Uncompressing bundle and moving to its place...${cc_normal}"
    tar xjf $BASE/$BITMASK.tar.bz2  # fresh bundled bundle
    rm -fr $BITMASK/repo/  # We must not add that folder to the tuf repo.
    rm -fr targets
    mv $BITMASK targets

    echo "${cc_yellow}-> Doing release magic...${cc_normal}"
    $RELEASE $WORKDIR/repo $KEY

    echo "${cc_yellow}-> Creating output file...${cc_normal}"
    cd $WORKDIR
    mkdir -p output
    rm -f output/$BITMASK-tuf.tar.bz2
    tar cjf output/$BITMASK-tuf.tar.bz2 repo/
}


get_args $@

do_init

if [[ -n $SETUP ]]; then
    do_setup
else
    if [[ ! -f $VENVDIR/bin/activate ]]; then
        echo "${cc_red}Error:${cc_normal} missing virtualenv, you need to use the -s switch."
        exit 1
    fi
    source $VENVDIR/bin/activate
fi

do_tuf_stuff

echo "${cc_green}TUF release complete.${cc_normal}"
echo "You can find the resulting file in:"
echo "$WORKDIR/output/$BITMASK-tuf.tar.bz2"