~veebers/juju-ci-tools/model_migration_check_all_units_of_charm

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
set -eux
: ${SCRIPTS=$(readlink -f $(dirname $0))}

export INSTANCE_TYPE=$1
export AMI_IMAGE=$2
shift; shift

JOB_NAME=${JOB_NAME:-$USER-run-unit-tests}
export JOB_NAME

FIXUP_SOURCES=""
IS_LOCAL="false"
RETRY="false"
REVISION_BUILD="none"
USE_PPA=""
INSTALL_DEPS=""
USE_TMPFS="false"
GOTESTCMD="go test"
LOG_FILE_PATH="/usr/share/distro-info/ubuntu.csv"
LOG_TXT="17.04,Angsty Antelope,angsty,2016-10-23,2017-04-30,2018-01-29"

while [[ "${1-}" != "" ]]; do
    case $1 in
        --force-archive)
            FIXUP_SOURCES="sudo sed s,//.*ec2.archive.ubuntu.com,//archive.ubuntu.com, /etc/apt/sources.list -i"
            ;;
        --retry)
            RETRY="true"
            ;;
        --safe)
            GOTESTCMD="$GOTESTCMD -p 2 -test.timeout=2400s"
            ;;
        --local)
            IS_LOCAL="true"
            shift
            tarfile=$1
            ;;
        --revision-build)
            shift
            REVISION_BUILD=$1
            ;;
        --use-ppa)
            shift
            USE_PPA=$1
            if [[ -z $(echo $USE_PPA | grep 'ppa:.*/.*') ]]; then
                echo "$USE_PPA is not a ppa."
                exit 1
            fi
            ;;
        --install-deps)
            shift
            INSTALL_DEPS=$1
            ;;
        --use-tmpfs)
            USE_TMPFS="true"
            ;;
        --race)
            GOTESTCMD="$GOTESTCMD -race -test.timeout=2400s"
            ;;
    esac
    shift
done


if [[ $IS_LOCAL == "false" && $REVISION_BUILD == "none" ]]; then
    echo "You must use either the --local or --revision-build option."
    exit 1
fi


if [[ $IS_LOCAL == "false" ]]; then
    # Download the buildvars and tartfile and lean the tarfile name.
    tarfile=$(
        $SCRIPTS/s3ci.py get --config $JUJU_HOME/juju-qa.s3cfg \
        $REVISION_BUILD build-revision '.*.tar.gz' ./)
    $SCRIPTS/s3ci.py get-summary --config $JUJU_HOME/juju-qa.s3cfg \
        $REVISION_BUILD $AMI_IMAGE
fi
tarfile_path=$(readlink -f $tarfile)
tarfile_basename=$(basename $tarfile)
juju_version=$(basename $tarfile .tar.gz)


if [[ $INSTANCE_TYPE == "lxc" ]]; then
    instance_id="juju-ci-unit-tester"
    sudo lxc-stop -n $instance_id || true
    sudo lxc-destroy -n $instance_id || true
    echo "Creating LXC $instance_id"
    if [[ $AMI_IMAGE =~ .*template ]]; then
        sudo lxc-clone -o $AMI_IMAGE -n $instance_id
    else
        sudo lxc-create -n $instance_id -t ubuntu -- \
            -r $AMI_IMAGE -S $HOME/.ssh/id_rsa.pub
    fi
    sudo lxc-start -d -n $instance_id
    instance_name="-"
    while [[ "$instance_name" == "-" ]]; do
        instance_name=$(sudo lxc-ls --fancy |
            grep $instance_id |
            tr -s ' ' |
            cut -d ' ' -f 3)
        sleep 2
    done
    sudoers_file="/var/lib/lxc/$instance_id/rootfs/etc/sudoers.d/91-ubuntu"
    echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' | sudo tee -a $sudoers_file
    sudo chmod 0440 $sudoers_file
    echo "$instance_id is at $instance_name"
elif [[ $INSTANCE_TYPE == "host" ]]; then
    instance_name=$AMI_IMAGE
else
    instance_id=$($SCRIPTS/ec2-run-instance-get-id)
    for attempt in $(seq 5); do
        if $SCRIPTS/ec2-tag-job-instances $instance_id; then
            break
        elif [ "$attempt" == "5" ]; then
            exit 1
        fi
        sleep 15
    done
    set +x
    echo Starting instance $instance_id
    instance_name=$($SCRIPTS/ec2-get-name $instance_id)
    echo Instance has name $instance_name
    sleep 30
    $SCRIPTS/wait-for-port $instance_name 22
fi


# Define the common vars before the embedded scripts; do not make them evalaute
# what is already know in the outer script.
set -x
set +e
scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
    $tarfile_path ubuntu@$instance_name:~/

ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
    ubuntu@$instance_name  <<EOT
set -eux
if  [[ -L /tmp ]]; then
    export TMPDIR=\$(readlink -f /tmp)
fi
if [[ ! -f ~/.ssh/id_rsa ]]; then
    ssh-keygen -t rsa -b 2048 -N "" -f ~/.ssh/id_rsa
fi
if [[ "$USE_TMPFS" == "true" ]]; then
    sudo mkdir -m 777 /mnt/tmp
    sudo mount -t tmpfs -o size=3G /mnt/tmp /mnt/tmp
    export TMPDIR=/mnt/tmp
fi
export GOPATH=\$HOME/$juju_version
if [[ -d \$GOPATH ]]; then
    echo "Cleaning up files unpacked into the GOPATH by a previous test."
    rm -r \$GOPATH
fi
tar -xzf $tarfile_basename
if [[ -d \$HOME/goroot ]]; then
    export GOROOT=\$HOME/goroot
fi
cd \$GOPATH/src/github.com/juju/juju
if [[ "$INSTANCE_TYPE" != "lxc" && "$INSTANCE_TYPE" != "host" ]]; then
    for attempt in \$(seq 10); do
        if grep ec2.archive.ubuntu.com /etc/apt/sources.list > /dev/null; then
            break
        elif [ "\$attempt" == "10" ]; then
            exit 1
        fi
      sleep 10
    done
    $FIXUP_SOURCES
fi
export PATH=/usr/lib/go-1.6/bin:$PATH
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get upgrade -y --force-yes \
    -o Dpkg::Options::="--force-confdef" \
    -o Dpkg::Options::="--force-confold" \
    -o Dpkg::Options::="--force-confnew"
sudo apt-get install -y make gcc software-properties-common distro-info-data
if [[ "$USE_PPA" != "" ]]; then
    sudo apt-add-repository -y $USE_PPA;
    sudo apt-get update
fi
if [[ "$INSTALL_DEPS" == "" ]]; then
    echo "Installing default deps."
    make install-dependencies
else
    echo "Installing deps from command line."
    sudo apt-get install -y $INSTALL_DEPS
fi

# TODO(gz): Remove bzr whoami?
bzr whoami 'J. Random Hacker <jrandom@example.org>'
go version || gccgo -v
make build
export JUJU_NOTEST_MONGOJS=1

if ! grep 'Angsty Antelope' $LOG_FILE_PATH ; then
  echo "$LOG_TXT" | sudo tee -a $LOG_FILE_PATH
fi
# Delay Xenial LTS birthday by one month.
sudo sed -i -e 's,2016-04-21,2016-05-21,' $LOG_FILE_PATH

if [[ $INSTANCE_TYPE == "host" ]]; then
    echo "Cleaning up /tmp if it is dirty."
    sudo killall -SIGABRT /usr/bin/mongod || true
    sudo killall -SIGABRT /usr/lib/juju/bin/mongod || true
    rm -rf /tmp/adduser-* || true
    rm -rf /tmp/check* || true
    rm -rf /tmp/go-* || true
    rm -rf /tmp/gocheck* || true
    rm -rf /tmp/gui* || true
    rm -rf /tmp/juju-* || true
    rm -rf /tmp/pprof* || true
    rm -rf /tmp/test-* || true
fi

make setup-lxd || true

go test -i ./...
if [[ "$RETRY" == "true" ]]; then
    $GOTESTCMD ./... || $GOTESTCMD ./...
elif [[  "$GOTESTCMD" != "go test"  ]]; then
    $GOTESTCMD ./...
else
    make check
fi
EOT
EXIT_STATUS=$?

set -e
if [[ $INSTANCE_TYPE == "lxc" ]]; then
    echo "Deleting lxc"
    sudo lxc-stop -n $instance_id
    sudo lxc-destroy -n $instance_id
elif [[ $INSTANCE_TYPE == "host" ]]; then
    ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" \
        ubuntu@$instance_name  <<EOT
    set -eux
    echo "Cleaning up /tmp"
    sudo killall -SIGABRT /usr/bin/mongod || true
    sudo killall -SIGABRT /usr/lib/juju/bin/mongod || true
    rm -rf /home/ubuntu/$juju_version || true
    rm -rf /tmp/adduser-* || true
    rm -rf /tmp/check* || true
    rm -rf /tmp/go-* || true
    rm -rf /tmp/gocheck* || true
    rm -rf /tmp/gui* || true
    rm -rf /tmp/juju-* || true
    rm -rf /tmp/pprof* || true
    rm -rf /tmp/test-* || true
EOT
else
    set +e
    # EC2 failures cannot change the EXIT_STATUS.
    $SCRIPTS/ec2-terminate-job-instances
fi
exit $EXIT_STATUS