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

NAME=${1}
LOG=/tmp/$(basename $0).log

echo "Using lxc $NAME"

# create lxc if doesn't exist
sudo lxc-info -n $NAME &> /dev/null || ( echo "Creating lxc $NAME"; sudo lxc-create -n $NAME -t ubuntu -- -b $USER )

# start lxc if not started
STATE="$(sudo lxc-info -Hsn $NAME)"
if [ "$STATE" = "STOPPED" ]
then
    echo "Starting lxc $NAME"
    sudo lxc-start -dn $NAME
    sleep 6  # wait for network
fi

# grab ip so we're not dependant on dns
IP=$(sudo lxc-info -Hin $NAME)

attach()
{
    sudo lxc-attach -n $NAME -- $@
}

attach_bash()
{
    echo "$@" | sudo lxc-attach -n $NAME -- bash -s
}

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

# runs something in bash on the lxc
run()
{
    local msg=$1
    shift
    [ -n "$msg" ] && echo -n "$msg..."
    # use IP rather than name so dns/ssh configuration is not essential.
    ssh $IP bash &> $LOG <<< "DEBIAN_FRONTEND=noninteractive $@" || bail
    [ -n "$msg" ] && echo "done."
}

# puts a file on to container. goes away if we can use lxd.
put()
{
    local content=$1
    local file=$2
    local mode=${3:-0644}
    local owner=${4:-root:root}
    attach_bash "echo '$content' > $file"
    attach chmod $mode $file
    attach chown $owner $file
}

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
}


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

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

# things that are default in cloud image, but not in ubuntu template
# TODO: fix this by using an actual cloud image
CLOUD_DEPS="
systemd-services
python-cheetah
python-yaml
ufw
unattended-upgrades
"

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:-}
bzr
bash-completion
command-not-found
psmisc
ack-grep
tree
curl
telnet
wget
httpie
"

tmp=$(mktemp)

echo "$CLOUD_DEPS $JUJU_DEPS $DEVELOPER_PACKAGES" > $tmp

if [ -f dependencies.txt ]
then
    cat dependencies.txt >> $tmp
fi

if [ -f dependencies-devel.txt ]
then
    cat dependencies-devel.txt >> $tmp
fi

PKGS="$(cat $tmp | sort | uniq)"


JFDI="-qq -y"
run "Updating package index" sudo apt-get $JFDI update
run "Installing ppa deps" sudo apt-get $JFDI install software-properties-common python-software-properties 
run "Installing juju stable ppa" sudo apt-add-repository -y ppa:juju/stable
run "Updating package index (again)" sudo apt-get $JFDI update
run "Updating system packages" sudo apt-get $JFDI upgrade
[ -n "$PKGS" ] && run "Installing required packages" sudo apt-get $JFDI install --no-install-recommends $PKGS
run "Cleaning up packages" sudo apt-get autoremove $JFDI

run "Removing old basenode" sudo rm -rf /tmp/basenode
run "Branching basenode" bzr branch lp:~canonical-sysadmins/basenode/trunk /tmp/basenode
run "Installing basenode" sudo /tmp/basenode/basenode_init --skip setup_etc_in_bzr

# we need ufw installed, as some charms expect it present as it's on the cloud
# image, but we will disable it as this is development
run "Disabling ufw" sudo ufw disable

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