~bloodearnest/+junk/ols-tools

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
#!/bin/bash
set -eu

NAME=${1}
LOG=/tmp/$(basename $0).log
ALIAS=ubuntu-cloud/trusty

echo "Using lxc $NAME"

import-image()
{
    echo "Importing ubuntu cloud base image (this might take a while)"
    lxd-images import ubuntu --alias $ALIAS --sync
    echo -n "Setting up cron job to keep lxd images in sync..."
    echo -e "#!/bin/bash\nlxd-images sync" | sudo tee /etc/cron.daily/lxd-images-sync
    sudo chmod +x /etc/cron.daily/lxd-images-sync
    echo done.
}

create_lxc()
{
    lxc image info $ALIAS &> /dev/null || import-image
    echo "Creating lxc $NAME"
    lxc launch $ALIAS $NAME
    sleep 5  # wait for network and cloud init
    # move the uid/gid of default ubuntu user, so we can use uid 1000
    # If we could use a custom cloud-init, this would go away
    lxc exec $NAME -- groupmod -g 999 ubuntu
    lxc exec $NAME -- usermod -u 999 -g 999 ubuntu
    # mirror the current user onto the lxc
    lxc exec $NAME -- groupadd -g $(id -g $USER) $(id -ng $USER)
    lxc exec $NAME -- useradd -u $(id -u $USER) -g $(id -g $USER) -d $HOME -s $SHELL $USER
}

bail()
{
    EXIT=$?
    echo -ne "\n"
    [ -f $LOG ] && cat $LOG
    exit $EXIT
}

# run a command, with log message and error handling
run()
{
    local msg=$1
    shift
    [ -n "$msg" ] && echo -n "$msg..."
    lxc exec $NAME -- "$@"  &> $LOG || bail
    [ -n "$msg" ] && echo "done."
}

# puts a string as a file on to container
puts()
{
    local content=$1
    local path=$2
    local file=$(mktemp)
    echo "$content" > $file
    shift; shift
    lxc file push $@ $file $NAME/$path
}

detect_proxy()
{
    PROXY=$(apt-config dump | grep Acquire::http::Proxy | tail -1)
    DISCOVER=/usr/share/squid-deb-proxy-client/apt-avahi-discover
    if [ -z "$PROXY" -a -f $DISCOVER ]
    then
        PROXY_IP=$($DISCOVER)
        if [ -n "$PROXY_IP" ]
        then
            PROXY="Acquire::http::Proxy \"$PROXY_IP\";"
        fi
    fi
    echo $PROXY
}

# check lxd is setup up correctly
lxc list &> /dev/null || { echo "lxd not installed/configured - exiting" && exit 1; }

# create lxc if not present
lxc info $NAME &> /dev/null || create_lxc

# mount the directories
# home: user's home dir, for config
# src: the code
# etc: directory to use config
# logs: log dir
BASEDIR=/srv/login.ubuntu.com/devel
ARCHIVE=$BASEDIR/code/archives/devel
lxc exec $NAME -- mkdir -p $ARCHIVE
lxc stop $NAME || true
lxc config set $NAME security.privileged true
lxc config device remove $NAME home
lxc config device remove $NAME src
lxc config device remove $NAME logs
lxc config device remove $NAME etc
lxc config device add $NAME home disk source=$HOME path=$HOME
lxc config device add $NAME src  disk source=$PWD path=$BASEDIR/code/devel
lxc config device add $NAME logs disk source=$PWD/logs path=$BASEDIR/logs
lxc config device add $NAME etc disk  source=$PWD/django_project path=$BASEDIR/etc
lxc start $NAME
sleep 3  # wait for network

# This make the charm think the code has been extracted from a tarball
# temporary workaround
run "Creating fake tarball" touch $ARCHIVE/canonical-identity-provider.tbz2
touch EXTRACTED

echo -n "Setting up $USER for passwordless sudo..."
puts "$USER ALL=(ALL) NOPASSWD: ALL" /etc/sudoers.d/99-$USER --mode=0440
echo done

# set up proxy
PROXY=$(detect_proxy)
if [ -n "$PROXY" ]
then
    echo -n "Enabling apt-proxy on lxc..."
    puts "$PROXY" /etc/apt/apt.conf.d/99-proxy
    echo done
fi

JUJU_DEPS="
cpu-checker
bridge-utils
rsyslog-gnutls
cloud-utils
cloud-image-utils
juju-core
juju-deployer
"

# things to make the lxc more like a dev env, customizable
DEVELOPER_PACKAGES="${DEVELOPER_PACKAGES:-}
language-pack-en
bzr
bash-completion
command-not-found
psmisc
ack-grep
tree
curl
telnet
wget
httpie
jq
"

tmp=$(mktemp)

echo "$JUJU_DEPS $DEVELOPER_PACKAGES" > $tmp

[ -f dependencies.txt ] && cat dependencies.txt >> $tmp
[ -f dependencies-devel.txt ] && cat dependencies-devel.txt >> $tmp
PKGS="$(cat $tmp | sort | uniq)"

JFDI="-qq -y"
run "Checking for network access" nc -vz google.com 443
run "Checking for CAT access" nc -vz archive.admin.canonical.com 443
run "Updating package index" apt-get $JFDI update
run "Installing ppa deps" apt-get $JFDI install software-properties-common python-software-properties 
run "Installing juju stable ppa" apt-add-repository -y ppa:juju/stable
run "Updating package index (again)" sudo apt-get $JFDI update
run "Updating system packages" apt-get $JFDI upgrade
[ -n "$PKGS" ] && run "Installing required packages" apt-get $JFDI install --no-install-recommends $PKGS
run "Cleaning up packages" apt-get autoremove $JFDI

IP=$(lxc info $NAME | grep eth0 | awk '{print $3}')

# need to use ssh agent forwarding to get our ssh key
lxc exec $NAME -- test -d /tmp/basenode && ssh -A $IP bzr pull -d /tmp/basenode || ssh -A $IP bzr branch lp:~canonical-sysadmins/basenode/trunk /tmp/basenode 
run "Installing basenode" /tmp/basenode/basenode_init --skip setup_etc_in_bzr

DIR=$(dirname $(readlink -f $0))
$DIR/update-dev-juju-env $NAME $IP