Merge lp:~smoser/charms/precise/virtual-maas/noju into lp:~virtual-maasers/charms/precise/virtual-maas/trunk

Proposed by Scott Moser
Status: Merged
Merged at revision: 51
Proposed branch: lp:~smoser/charms/precise/virtual-maas/noju
Merge into: lp:~virtual-maasers/charms/precise/virtual-maas/trunk
Diff against target: 554 lines (+267/-90)
13 files modified
config.yaml (+10/-0)
hooks/config-changed (+7/-4)
hooks/install (+23/-16)
hooks/start (+4/-2)
hooks/upgrade-charm (+4/-2)
scripts/_util (+46/-0)
scripts/config_get (+51/-0)
scripts/setup-deployer (+3/-14)
scripts/setup-keys (+4/-3)
scripts/setup-maas (+71/-48)
scripts/setup-storage (+2/-1)
scripts/setup-user (+37/-0)
vmaas.conf.example (+5/-0)
To merge this branch: bzr merge lp:~smoser/charms/precise/virtual-maas/noju
Reviewer Review Type Date Requested Status
Virtual Maasers Pending
Review via email: mp+161645@code.launchpad.net

Description of the change

remove / abstract the 'juju-ness'

This takes out the juju specific stuff, and replaces it with some wrappers.
The idea is that a user can conceivably do:
 * install server / launch instance
 * bzr branch my-branch
 * cd my-branch
 * sudo ./hooks/install
 * sudo ./hooks/config-changed
 * sudo ./hooks/start

It also adds some additional function such as:
 * configuration of user (rather than hard code 'ubuntu')
 * configuration of ephemeral images stream

To post a comment you must log in.
71. By Scott Moser

xmaas-addssh-key: handle 'already added'

72. By Scott Moser

add example vmaas.conf

73. By Scott Moser

fix key added already

74. By Scott Moser

raring does not have /etc/maas/commissioning-user-data. mention that

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2013-01-30 09:26:46 +0000
3+++ config.yaml 2013-05-03 14:06:26 +0000
4@@ -20,6 +20,11 @@
5 description: |
6 Space seperated list of releases to import
7 MAAS pxe files for.
8+ ephemeral_stream:
9+ type: string
10+ default: "released"
11+ description: |
12+ The stream to import ephemerals from ("daily" or "released")
13 block-devices:
14 type: string
15 description: |
16@@ -38,3 +43,8 @@
17 default: em1
18 description: |
19 external interface to use for outbound routing
20+ user:
21+ type: string
22+ default: ubuntu
23+ description: |
24+ the unprividged user to set up
25
26=== modified file 'hooks/config-changed'
27--- hooks/config-changed 2013-02-19 09:19:20 +0000
28+++ hooks/config-changed 2013-05-03 14:06:26 +0000
29@@ -1,6 +1,9 @@
30 #!/bin/bash -e
31
32-./scripts/setup-keys
33-./scripts/setup-maas
34-./scripts/setup-storage
35-./scripts/setup-deployer
36+. "${0%/*}/../scripts/_util"
37+
38+setup-keys
39+setup-user
40+setup-maas
41+setup-storage
42+setup-deployer
43
44=== modified file 'hooks/install'
45--- hooks/install 2013-02-19 08:55:47 +0000
46+++ hooks/install 2013-05-03 14:06:26 +0000
47@@ -1,26 +1,33 @@
48 #!/bin/bash -e
49
50+. "${0%/*}/../scripts/_util"
51
52-source=$(config-get source)
53+source=$(config_get source)
54
55 if [ -n "$source" ]; then
56- add-apt-repository $source
57+ add-apt-repository -y "$source"
58 fi
59
60-# TODO: Hacked version of isc-dhcp to ensure that
61-# mac addresses always get assigned the sam IP
62-# LP: 1069570
63-add-apt-repository ppa:virtual-maasers/maas-updated-packages
64-apt-get update
65-
66-apt-get install -y openvswitch-datapath-dkms
67-apt-get install -y maas maas-region-controller maas-cluster-controller \
68- maas-dhcp maas-dns maas-cli libvirt-bin kvm \
69- gdisk lvm2 \
70- python-keystoneclient python-novaclient \
71- python-glanceclient python-quantumclient \
72- python-cinderclient python-swiftclient \
73- openvswitch-switch
74+# maas-updated-packages ppa contains isc-dhcp for precise and quantal
75+# that has the 'nouid' patch applied (LP: #1069570).
76+# It may contain other packages also.
77+add-apt-repository -y ppa:virtual-maasers/maas-updated-packages
78+apt-get update -q
79+
80+maas_pkgs=(
81+ maas maas-region-controller maas-cluster-controller
82+ maas-dhcp maas-dns maas-cli
83+)
84+
85+pkgs=(
86+ openvswitch-datapath-dkms openvswitch-switch
87+ libvirt-bin kvm
88+ gdisk lvm2
89+ linux-image-extra-virtual # if a cloud-guest, get kvm module
90+)
91+
92+DEBIAN_FRONTEND=noninteractive \
93+ apt-get install --quiet --assume-yes "${maas_pkgs[@]}" "${pkgs[@]}"
94
95 # Fixup nested virtualization and permissions on /dev/kvm
96 # LP: 1092715
97
98=== modified file 'hooks/start'
99--- hooks/start 2013-01-23 18:42:03 +0000
100+++ hooks/start 2013-05-03 14:06:26 +0000
101@@ -1,6 +1,8 @@
102 #!/bin/bash
103
104-[ -b /dev/virtualmaas/instances ] && {
105+. "${0%/*}/../scripts/_util"
106+
107+if [ -b /dev/virtualmaas/instances ]; then
108 mkdir -p /var/lib/virtual-maas
109 mount /dev/virtualmaas/instances /var/lib/virtual-maas || true
110-}
111+fi
112
113=== modified file 'hooks/upgrade-charm'
114--- hooks/upgrade-charm 2013-01-23 14:57:37 +0000
115+++ hooks/upgrade-charm 2013-05-03 14:06:26 +0000
116@@ -1,4 +1,6 @@
117 #!/bin/bash -e
118
119-./hooks/install
120-./hooks/config-changed
121+. "${0%/*}/../scripts/_util"
122+
123+"${HOOKS_D}/install"
124+"${HOOKS_D}/config-changed"
125
126=== added file 'scripts/_util'
127--- scripts/_util 1970-01-01 00:00:00 +0000
128+++ scripts/_util 2013-05-03 14:06:26 +0000
129@@ -0,0 +1,46 @@
130+#!/bin/bash
131+
132+my_d=$(readlink -f "${BASH_SOURCE[0]%/*}")
133+TOP_D="${my_d%/*}"
134+SCRIPTS_D="${TOP_D}/scripts"
135+HOOKS_D="${TOP_D}/hooks"
136+tildame="~$(whoami)"
137+HOME=$(sh -c "echo $tildame")
138+
139+add_pathd() {
140+ local p=":$PATH:"
141+ [ "${p#*:${1%/}:}" != "$p" ] || PATH="$1:$PATH"
142+}
143+
144+as_unpriv_user() {
145+ local user="${_VMAAS_UNPRIV_USER}"
146+ if [ -z "$user"]; then
147+ user=$(config_get "user")
148+ [ -z "$user" ] && user="ubuntu"
149+ fi
150+ sudo -Hu "$user" "$@"
151+}
152+
153+error() { echo "$@" 1>&2; }
154+debug() {
155+ local level="$1"
156+ shift
157+ error "$@"
158+}
159+
160+if [ -z "$JUJU_UNIT_NAME" ]; then
161+ if [ -z "$VMAAS_CONFIG" ]; then
162+ for f in "${TOP_D}/vmaas.conf" "/etc/vmaas.conf"; do
163+ [ -f "$f" ] && { export VMAAS_CONFIG="$f"; break; }
164+ done
165+ fi
166+ if [ -z "$VMAAS_OPTIONS" ]; then
167+ for f in "${TOP_D}/config.yaml" "/etc/vmaas-options.yaml"; do
168+ [ -f "$f" ] && { export VMAAS_OPTIONS="$f"; break; }
169+ done
170+ fi
171+fi
172+
173+add_pathd "$SCRIPTS_D"
174+
175+# vi: ts=4 expandtab syntax=sh
176
177=== added file 'scripts/config_get'
178--- scripts/config_get 1970-01-01 00:00:00 +0000
179+++ scripts/config_get 2013-05-03 14:06:26 +0000
180@@ -0,0 +1,51 @@
181+#!/usr/bin/python
182+##
183+## Indirection layer around juju's "config-get".
184+##
185+## supports "noju" and just reading from a config file
186+
187+import sys, os
188+
189+def noju_config_get(name):
190+ import yaml
191+ ## Read an option out of a juju config formated file
192+ ## config file is specified in VMAAS_CONFIG environment variable
193+ ## if VMAAS_OPTIONS is set in environment, read defaults from it
194+ cfg_file = os.environ.get("VMAAS_CONFIG")
195+
196+ if not cfg_file:
197+ dirs = ["/etc"]
198+ for d in dirs:
199+ fp = os.path.join(d, 'vmaas.conf')
200+ if os.path.exists(fp):
201+ cfg_file = fp
202+ break
203+
204+ if cfg_file and os.path.exists(cfg_file):
205+ cfg = yaml.safe_load(open(cfg_file).read())
206+ # assume juju_unit_name would be 'vmaas'
207+ if 'vmaas' in cfg:
208+ cfg = cfg['vmaas']
209+ elif len(cfg) == 1:
210+ # if there was just one top level entry, take it
211+ cfg = cfg[cfg.keys()[0]]
212+ else:
213+ cfg = {}
214+
215+ options_file = os.environ.get("VMAAS_OPTIONS", None)
216+ if options_file:
217+ options = yaml.safe_load(open(options_file).read())["options"]
218+ else:
219+ options = {}
220+
221+ print cfg.get(name, options.get(name, {}).get('default', ""))
222+
223+def juju_config_get(name):
224+ os.execvp('config-get', ('config-get', name))
225+
226+if os.environ.get("JUJU_UNIT_NAME"):
227+ config_get = juju_config_get
228+else:
229+ config_get = noju_config_get
230+
231+config_get(sys.argv[1])
232
233=== modified file 'scripts/setup-deployer'
234--- scripts/setup-deployer 2013-01-24 18:43:41 +0000
235+++ scripts/setup-deployer 2013-05-03 14:06:26 +0000
236@@ -1,16 +1,5 @@
237 #!/bin/bash -e
238
239-su -c "mkdir -p /home/ubuntu/tools" ubuntu
240-
241-[ -d /home/ubuntu/tools/juju-deployer ] || {
242- su -c "bzr branch lp:juju-deployer /home/ubuntu/tools/juju-deployer" ubuntu
243-}
244-
245-[ -d /home/ubuntu/tools/maas-libvirt-utils ] || {
246- su -c "bzr branch lp:~virtual-maasers/+junk/maas-libvirt-utils /home/ubuntu/tools/maas-libvirt-utils" ubuntu
247-}
248-
249-[ -d /home/ubuntu/tools/openstack-ubuntu-testing ] || {
250- su -c "bzr branch lp:openstack-ubuntu-testing /home/ubuntu/tools/openstack-ubuntu-testing" ubuntu
251-}
252-
253+. "${0%/*}/../scripts/_util"
254+
255+:
256
257=== modified file 'scripts/setup-keys'
258--- scripts/setup-keys 2013-02-19 09:19:20 +0000
259+++ scripts/setup-keys 2013-05-03 14:06:26 +0000
260@@ -1,7 +1,8 @@
261 #!/bin/bash -e
262
263-keys=$(config-get ssh-keys)
264+. "${0%/*}/../scripts/_util"
265+
266+keys=$(config_get ssh-keys)
267 if [ -n "$keys" ]; then
268- su -c "ssh-import-id $keys" ubuntu
269+ as_unpriv_user sh -c "ssh-import-id $keys"
270 fi
271-
272
273=== modified file 'scripts/setup-maas'
274--- scripts/setup-maas 2013-04-08 17:00:47 +0000
275+++ scripts/setup-maas 2013-05-03 14:06:26 +0000
276@@ -1,5 +1,7 @@
277 #!/bin/bash -e
278
279+. "${0%/*}/../scripts/_util"
280+
281 BRIDGE="maasbr0"
282
283 MAAS_GW="192.168.64.1"
284@@ -8,7 +10,8 @@
285 MAAS_RANGE="192.168.64.100,192.168.74.254"
286 MAAS_DHCP_INT="$BRIDGE"
287 MAAS_NEXT_SERVER="$MAAS_GW"
288-EXTIF=$(config-get ext-int)
289+MAAS_USER="ubuntu"
290+EXTIF=$(config_get ext-int)
291
292 if sg libvirtd -c "virsh -c qemu:///system net-list" | grep -q $BRIDGE; then
293 echo "Bridge already defined, destroying"
294@@ -74,7 +77,9 @@
295 ## maas-dhcp-server may run before libvirt-bin.
296 ## if it does, then it will fail because the 'maasbr0' interface
297 ## does not yet exist
298-tee -a /etc/init/libvirt-maas-dhcpd-restart.conf <<"EOF"
299+f="/etc/init/libvirt-maas-dhcpd-restart.conf"
300+debug 1 "writing $f"
301+cat > "$f" <<"EOF"
302 description "restart dhcpd"
303 start on started libvirt-bin
304 task
305@@ -84,7 +89,9 @@
306 end script
307 EOF
308
309-tee /usr/local/bin/xmaas-get-user-creds <<"EOF"
310+f="/usr/local/bin/xmaas-get-user-creds"
311+debug 1 "writing $f"
312+cat > "$f" <<"EOF"
313 #!/usr/bin/python
314 import sys, os
315 sys.path.insert(0, "/usr/share/maas")
316@@ -98,10 +105,12 @@
317 token = admin.tokens.all()[0]
318 print convert_tuple_to_string(get_creds_tuple(token))
319 EOF
320-sudo chmod 755 /usr/local/bin/xmaas-get-user-creds
321-
322-
323-tee "/usr/local/bin/xmaas-addssh-key" <<"EOF"
324+chmod 755 "$f"
325+
326+
327+f=/usr/local/bin/xmaas-addssh-key
328+debug 1 "writing $f"
329+cat > "$f" <<"EOF"
330 #!/usr/bin/python
331 # this hackily adds an ssh key until LP: #1056860 is fixed
332 import sys, os
333@@ -113,20 +122,24 @@
334 user = User.objects.get(username=sys.argv[1])
335 kdata = open(sys.argv[2]).read()
336 key = SSHKey(key=kdata, user=user)
337-user.sshkey_set.add(key)
338+try:
339+ user.sshkey_set.add(key)
340+except Exception as e:
341+ if "already been added" in str(e.messages):
342+ print "key from %s already added to user %s" % (sys.argv[2], user)
343+ sys.exit(0)
344+print "added key from %s to %s" % (sys.argv[2], user)
345 user.save()
346 EOF
347-sudo chmod 755 /usr/local/bin/xmaas-addssh-key
348-
349-maas createadmin --username=ubuntu --password=ubuntu \
350- --email=ubuntu@example.com 2>/dev/null || :
351-
352-CREDS=$(sudo xmaas-get-user-creds ubuntu)
353+chmod 755 "$f"
354+
355+maas createadmin "--username=${MAAS_USER}" "--password=${MAAS_USER}" \
356+ "--email=${MAAS_USER}@example.com" 2>/dev/null || :
357+
358+# Setup root and unprivledged user with 'maas-cli' usage
359+CREDS=$(xmaas-get-user-creds "${MAAS_USER}")
360 maas-cli login maaslocal http://localhost/MAAS/api/1.0 "$CREDS"
361-
362-# Setup ubuntu user
363-cp /root/.maascli.db ~ubuntu/.maascli.db
364-chown ubuntu:ubuntu ~ubuntu/.maascli.db
365+as_unpriv_user maas-cli login maaslocal http://localhost/MAAS/api/1.0 "$CREDS"
366
367 maas-cli maaslocal node-groups list > out.tmp
368 sed -n '/^\[$/,$p' out.tmp > out
369@@ -162,28 +175,36 @@
370 service maas-dhcp-server restart
371
372
373-releases=$(config-get releases)
374-sed -i -e "s/^RELEASES.*/RELEASES=\"$releases\"/g" \
375- -e "s/.*ARCHES.*/ARCHES=\"amd64\/generic i386\/generic\"/g" \
376- -e "s/.*IMPORT_SQUASHFS.*/IMPORT_SQUASHFS=0/g" \
377- /etc/maas/import_pxe_files
378-sed -i -e "s/.*RELEASES.*/RELEASES=\"precise\"/g" \
379- /etc/maas/import_ephemerals
380+releases=$(config_get releases)
381+stream=$(config_get ephemeral_stream)
382+
383+cfg="/etc/maas/import_pxe_files"
384+[ -e "$cfg.dist" ] || cp "$cfg" "$cfg.dist"
385+grep -q "${cfg}.vmaas" "$cfg" || echo ". ${cfg}.vmaas" >> "$cfg"
386+
387+cat > "${cfg}.vmaas" <<EOF
388+RELEASES="$releases"
389+ARCHES="amd64/generic i386/generic"
390+IMPORT_SQUASHFS=0
391+IMPORT_EPHEMERALS=1
392+EOF
393+
394+cfg="/etc/maas/import_ephemerals"
395+[ -e "$cfg.dist" ] || cp "$cfg" "$cfg.dist"
396+grep -q "${cfg}.vmaas" "$cfg" || echo ". ${cfg}.vmaas" >> "$cfg"
397+
398+cat > "${cfg}.vmaas" <<EOF
399+RELEASES="$releases"
400+STREAM="$stream"
401+EOF
402+
403 maas-import-pxe-files
404
405-# add your ssh keys (LP: #1056860)
406-[ -f ~ubuntu/.ssh/id_rsa.pub ] || {
407- su -c "ssh-keygen -N '' -f ~/.ssh/id_rsa" ubuntu
408- xmaas-addssh-key ubuntu ~ubuntu/.ssh/id_rsa.pub
409-}
410-
411-# Turn off host key checking for managed KVM instances
412-cat > ~ubuntu/.ssh/config << EOF
413-Host *.virtual-maas.com
414- UserKnownHostsFile /dev/null
415- StrictHostKeyChecking no
416-EOF
417-chown ubuntu:ubuntu ~ubuntu/.ssh/config
418+# add unpriv user's ssh keys
419+# this has to be in subshell to get '~' expansion of variable
420+as_unpriv_user sh -c 'cat ~/.ssh/id_rsa.pub' > pubkey.tmp
421+xmaas-addssh-key ${MAAS_USER} pubkey.tmp
422+rm -f pubkey.tmp
423
424 # Setup system so MAAS can sudo virsh for virtual power control
425 echo "maas ALL= NOPASSWD: /usr/bin/virsh" > /etc/sudoers.d/98-maas-virsh
426@@ -194,8 +215,8 @@
427 # Create juju environments file
428 # Note the origin uses a pre-trunk serialized execution
429 # branch to fix dpkg races between hooks in the same container
430-mkdir -p ~ubuntu/.juju
431-cat > ~ubuntu/.juju/environments.yaml << EOF
432+as_unpriv_user sh -ec 'cd; mkdir -p .juju
433+ cat > .juju/environments.yaml' << EOF
434 default: precise
435 environments:
436 quantal:
437@@ -220,7 +241,6 @@
438 default-series: raring
439 juju-origin: lp:juju
440 EOF
441-chown ubuntu:ubuntu ~ubuntu/.juju/environments.yaml
442
443 ### fix enlistment script ipmi (LP: 1064527). do not attempt enlist
444 sed -i.xdist \
445@@ -229,9 +249,15 @@
446 /usr/share/maas/preseeds/enlist_userdata
447
448 ### fix commisssioing ipmi (LP: 1103716)
449-sed -i.xdist -e 's,-z "power_settings",-z "$power_settings",' \
450- -e 's,\([^#]\)power_settings=$(,\1#power_settings=$(,' \
451- /etc/maas/commissioning-user-data
452+if [ -e /etc/maas/commissioning-user-data ]; then
453+ sed -i.xdist -e 's,-z "power_settings",-z "$power_settings",' \
454+ -e 's,\([^#]\)power_settings=$(,\1#power_settings=$(,' \
455+ /etc/maas/commissioning-user-data
456+else
457+ # FIXME: raring version of maas does not have this file
458+ # likely/possibly have to make same changes to raring
459+ :
460+fi
461
462 ### Max out the connections libvirt can deal with to avoid start/stop failures
463 sed -i.xdist -E \
464@@ -254,7 +280,7 @@
465 options {
466 directory "/var/cache/bind";
467 forwarders {
468- $(config-get dns-server);
469+ $(config_get dns-server);
470 };
471 auth-nxdomain no; # conform to RFC1035
472 listen-on-v6 { any; };
473@@ -262,9 +288,6 @@
474 EOF
475 service bind9 restart
476
477-# Add ubuntu to libvirtd group
478-usermod -G libvirtd -a ubuntu || :
479-
480 # ensure access to the cloud-archive
481 # LP: 1105192
482 echo "ubuntu-cloud.archive.canonical.com" > /etc/squid-deb-proxy/mirror-dstdomain.acl.d/90-ca
483
484=== modified file 'scripts/setup-storage'
485--- scripts/setup-storage 2013-01-28 11:27:09 +0000
486+++ scripts/setup-storage 2013-05-03 14:06:26 +0000
487@@ -1,5 +1,6 @@
488 #!/bin/bash -e
489
490+. "${0%/*}/../scripts/_util"
491
492 function check_mounted() {
493 device=$1
494@@ -12,7 +13,7 @@
495
496 vgname=virtualmaas
497 lvname=instances
498-devices=$(config-get block-devices)
499+devices=$(config_get block-devices)
500
501 [ -z "$devices" ] && {
502 echo "Devices not specificed"
503
504=== added file 'scripts/setup-user'
505--- scripts/setup-user 1970-01-01 00:00:00 +0000
506+++ scripts/setup-user 2013-05-03 14:06:26 +0000
507@@ -0,0 +1,37 @@
508+#!/bin/bash -e
509+
510+. "${0%/*}/../scripts/_util"
511+
512+tool_info=(
513+ "lp:juju-deployer|juju-deployer"
514+ "lp:~virtual-maasers/+junk/maas-libvirt-utils|maas-libvirt-utils"
515+ "lp:openstack-ubuntu-testing|openstack-ubuntu-testing"
516+)
517+
518+as_unpriv_user sh -ec '
519+ mkdir -p ~/tools
520+ cd ~/tools
521+ for tdata in "$@"; do
522+ src=${tdata%|*}
523+ dir=${tdata##*|}
524+ [ -d "$dir" ] && { echo "$src already in $dir" 1>&2; continue; }
525+ echo "branching $src" 1>&2
526+ bzr branch --quiet "$src" "$dir"
527+ done
528+' -- "${tool_info[@]}"
529+
530+as_unpriv_user sh -c \
531+ "[ -f ~/.ssh/id_rsa.pub ] || ssh-keygen -N '' -f ~/.ssh/id_rsa"
532+
533+user=$(config_get "user")
534+adduser "$user" libvirtd
535+adduser "$user" kvm
536+
537+# Turn off host key checking for managed KVM instances
538+as_unpriv_user sh -c '
539+ grep -q .virtual-maas.com ~/.ssh/config && exit 0
540+ cat > ~/.ssh/config' <<EOF
541+Host *.virtual-maas.com
542+ UserKnownHostsFile /dev/null
543+ StrictHostKeyChecking no
544+EOF
545
546=== added file 'vmaas.conf.example'
547--- vmaas.conf.example 1970-01-01 00:00:00 +0000
548+++ vmaas.conf.example 2013-05-03 14:06:26 +0000
549@@ -0,0 +1,5 @@
550+vmaas:
551+ releases: raring
552+ ephemeral_stream: "daily"
553+ dns-server: 10.55.60.1
554+ ext-int: eth0

Subscribers

People subscribed via source and target branches