This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
semaphore.sh
executable file
·229 lines (177 loc) · 4.46 KB
/
semaphore.sh
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
#!/bin/bash
set -e
function finish {
error_code=$?
set +e
if [ $created -eq 1 ]
then
ccloudvm delete semaphore
fi
if [ -f $tfile ]
then
rm $tfile
fi
echo ""
echo "=============================="
if [[ $error_code -eq 0 ]]
then
echo "= SUCCESS ="
else
echo "= FAILURE ="
fi
echo "=============================="
rm -rf ${TEMPDIR}
}
function get_latest_go {
GO_URL=$(curl -s https://golang.org/dl/#stable | grep 'linux-amd64' | grep downloadBox | awk -F '"' '{print $4}')
TEMPDIR=$(mktemp -d)
curl -L https://golang.org/${GO_URL} --output -| tar -C ${TEMPDIR} -xzf -
export GOROOT=${TEMPDIR}/go
export PATH=${TEMPDIR}/go/bin:$PATH
}
trap finish EXIT
created=0
if [[ ! -z "${SEMAPHORE_REPO_SLUG}" ]]
then
echo ""
echo "===== Download stable go ====="
echo ""
get_latest_go
echo ""
echo "===== Cloning repo ====="
echo ""
if [ "$SEMAPHORE_REPO_SLUG" != "intel/ccloudvm" ]
then
mkdir -p $GOPATH/src/github.com/intel
mv $GOPATH/src/github.com/${SEMAPHORE_REPO_SLUG} $GOPATH/src/github.com/intel/ccloudvm
cd $GOPATH/src/github.com/intel/ccloudvm
git checkout ${BRANCH_NAME}
fi
go get -d -t ./...
echo ""
echo "===== Installing packages ====="
echo ""
sudo apt-get update
sudo apt-get install qemu xorriso -y
sudo chmod ugo+rwx /dev/kvm
fi
echo ""
echo "===== Building ccloudvm ====="
echo ""
go version
go get github.com/intel/ccloudvm
go get github.com/intel/ccloudvm/ccvm
export PATH=$GOPATH/bin:$PATH
if [[ ! -z "${SEMAPHORE_REPO_SLUG}" ]]
then
ccvm --systemd=false &
else
ccloudvm setup
fi
# Create and boot a semaphore instance
echo ""
echo "===== Creating instance ====="
echo ""
if [[ ! -z "${SEMAPHORE_REPO_SLUG}" ]]
then
# There's a race condition here when running on semaphore as we don't
# when the ccvm server is up and running. As we can't fork in Go we
# can't easily create a proper daemon. It's doable but lots of work
# and as this is only a test feature we'll just retry a few times.
retry=0
set +e
until [ $retry -ge 10 ]
do
ccloudvm create --name semaphore --debug --port "8000-80" --package-upgrade=false semaphore
if [ $? -eq 0 ]
then
set -e
break
fi
echo "Retrying create instance"
let retry=retry+1
if [ $retry -eq 9 ]
then
set -e
fi
sleep 1
done
else
ccloudvm create --name semaphore --debug --port "8000-80" --package-upgrade=false semaphore
fi
created=1
echo ""
echo "===== Testing SSH ====="
echo ""
# SSH to the instance and execute a command to determine the remote user
lsb_release_cmd="ccloudvm run semaphore -- lsb_release -c -s"
remote_distro=`$lsb_release_cmd`
echo "Check $remote_distro == xenial"
test $remote_distro = "xenial"
echo ""
echo "===== Testing port mapping ====="
echo ""
# Get port mapping is working by send a http GET to the nginx server running in
# the VM
instance_ip=$(ccloudvm status semaphore | sed '2q;d' | cut -d ":" -f 2 | xargs)
http_proxy= curl http://$instance_ip:8000
# Copy files between the host and the guest
echo ""
echo "===== Testing ccloudvm copy ====="
echo ""
tfile=$(mktemp /tmp/README.XXXXXXXXX)
ccloudvm copy -t semaphore /etc/resolv.conf $tfile
# Stop the VM
echo ""
echo "===== Testing ccloudvm stop ====="
echo ""
ccloudvm stop semaphore
# Check there are no qemu processes running. A bit racy I know. It would
# be better if stop waited until the qemu process had actually exited.
#
# For the time being we're only going to enable this test inside semaphore
# builds as it could easily fail on a development machine.
if [[ ! -z "${SEMAPHORE_REPO_SLUG}" ]]
then
retry=0
until [ $retry -ge 12 ]
do
if ! pidof qemu-system-x86_64
then
break
fi
let retry=retry+1
sleep 5
done
if [ $retry -eq 12 ]
then
echo "qemu process did not exit"
false
fi
fi
# Delete the instance
echo ""
echo "===== Testing ccloudvm delete ====="
echo ""
ccloudvm delete semaphore
created=0
# Check it's really gone
set +e
ccloudvm status semaphore 2> /dev/null
error_code=$?
set -e
if [[ $error_code -eq 0 ]]
then
echo "instance still exists"
false
fi
# Run the unit tests
if [ "$SEMAPHORE_REPO_SLUG" = "intel/ccloudvm" ]
then
which go
echo ${GOROOT}
go get github.com/mattn/goveralls
$GOPATH/bin/goveralls --race -v -service=semaphore --package github.com/intel/ccloudvm/ccvm
else
go test --race -v github.com/intel/ccloudvm/ccvm
fi