-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuildenv.sh
65 lines (55 loc) · 1.66 KB
/
buildenv.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
#!/bin/bash
set -x
set -e
LXC_TEMPLATE="zvm"
# TODO: Add a real check for these
# The following env vars should be set:
# - JOB_NAME (set by jenkins)
# - BUILD_NUMBER (set by jenkins)
# - GITURL
# - RAWGITURL
CURRENT_JOB_ID=$JOB_NAME-$BUILD_NUMBER
IP="-"
do_get_ip () {
export IP=`sudo lxc-ls --fancy --fancy-format=name,ipv4 | grep "^$CURRENT_JOB_ID\s" | awk '{print $2}'`
}
# Attempt to get the IP of the lxc instance. If successful, bind it to the `IP`
# env var
get_ip () {
if [ $IP = "-" ]; then
# it may take some time for the IP lease to take effect,
# so we can try multiple times before failing
times=(1 2 4 8 16)
for t in "${times[@]}"; do
echo "No IP yet assigned to LXC $CURRENT_JOB_ID -- waiting for $t seconds"
sleep $t
do_get_ip
if [ $IP = "-" ]; then
# not yet defined
continue
else
break
fi
done
fi
if [ $IP = "-" ]; then
echo "The LXC '$CURRENT_JOB_ID' did not get an IP"
exit 1
fi
}
# Run a command remotely on an lxc instance
lxc_run () {
# SSH takes its positional arguments, joins them with space and
# passes that to a shell on the remote host. The shell will remove
# one level of quoting, so we need to add this here while we still
# have the positional arguments in their original form.
ssh ubuntu@$IP -oStrictHostKeyChecking=no $(printf "%q " "$@")
}
lxc_scp () {
scp -oStrictHostKeyChecking=no $*
}
# clone an lxc container
sudo lxc-clone -o $LXC_TEMPLATE -n $CURRENT_JOB_ID
# start lxc, daemonized
sudo lxc-start -n $CURRENT_JOB_ID -d
get_ip