~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
#!/bin/bash
set -eux
: ${SCRIPTS=$(readlink -f $(dirname $0))}
: ${LOCAL_JENKINS_URL=${JENKINS_URL:-http://juju-ci.vapour.ws:8080}}

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

FIXUP_SOURCES=""
IS_LOCAL="false"
RETRY="false"
SAFE="false"
REVISION_BUILD=${revision_build:-}
USE_PPA=""
USE_TMPFS="false"
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)
            SAFE="true"
            ;;
        --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
            ;;
        --use-tmpfs)
            USE_TMPFS="true"
            ;;
    esac
    shift
done


if [[ $IS_LOCAL == "false" ]]; then
    archive="$LOCAL_JENKINS_URL/job/build-revision/lastSuccessfulBuild/artifact"
    test -f buildvars.bash && rm buildvars.bash
    wget -q $archive/buildvars.bash
    source buildvars.bash
    rev=${REVNO-$(echo $REVISION_ID | head -c8)}
    echo "Testing $BRANCH $rev"

    tarfile=$($SCRIPTS/get-tarfile-name $REVISION_BUILD)
    test -f $tarfile && rm $tarfile
    wget -q $archive/$tarfile
fi
tarfile_path=$(readlink -f $tarfile)
juju_version=$(basename $tarfile .tar.gz)


if [[ $INSTANCE_TYPE == "lxc" ]]; then
    instance_id="juju-ci-unit-tester"
    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"
else
    instance_id=$($SCRIPTS/ec2-run-instance-get-id)
    $SCRIPTS/ec2-tag-job-instances $instance_id

    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
ssh-keygen -t rsa -b 2048 -N "" -f ~/.ssh/id_rsa
if [[ "$USE_TMPFS" == "true" ]]; then
    sudo mkdir -m 777 /mnt/tmp
    sudo mount -t tmpfs -o size=2G /mnt/tmp /mnt/tmp
    export TMPDIR=/mnt/tmp
fi
tar -xzf $tarfile
export GOPATH=\$HOME/$juju_version
cd \$GOPATH/src/github.com/juju/juju
if [[ "$INSTANCE_TYPE" != "lxc" ]]; 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
if [[ "$USE_PPA" != "" ]]; then
    sudo apt-add-repository -y $USE_PPA;
fi
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y make python-software-properties
make install-dependencies
bzr whoami 'J. Random Hacker <jrandom@example.org>'
go version || gccgo -v
make build
export JUJU_NOTEST_MONGOJS=1
if [[ "$RETRY" == "true" ]]; then
    go test ./... || go test -p 2 ./...
elif [[  "$SAFE" == "true"  ]]; then
    go test -p 2 ./...
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
else
    $SCRIPTS/ec2-terminate-job-instances
fi
exit $EXIT_STATUS