~smoser/ubuntu/wily/maas/lp1474417

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/sh

set -e

. /usr/share/debconf/confmodule
db_version 2.0

if [ -f /usr/share/dbconfig-common/dpkg/postinst.pgsql ]; then
	. /usr/share/dbconfig-common/dpkg/postinst.pgsql
fi

RELEASE=`lsb_release -rs` || RELEASE=""

maas_sync_migrate_db(){
	maas-region-admin syncdb --noinput
	maas-region-admin migrate maasserver --noinput
	maas-region-admin migrate metadataserver --noinput
}

restart_postgresql(){
	invoke-rc.d --force postgresql restart || true
}

configure_region_http() {
	case $RELEASE in
		12.04|12.10|13.04)
			# handle apache configs
			if [ -e /etc/maas/maas-http.conf -a \
				! -e /etc/apache2/conf.d/maas-http.conf ]; then
				ln -sf /etc/maas/maas-http.conf /etc/apache2/conf.d/maas-http.conf
			fi
			;;
		*)
			# handle apache configs
			if [ -e /etc/maas/maas-http.conf -a \
				! -e /etc/apache2/conf-enabled/maas-http.conf ]; then
				ln -sf /etc/maas/maas-http.conf /etc/apache2/conf-enabled/maas-http.conf
			fi
			;;
	esac
	# enable apache modules needed
	a2enmod proxy_http
	a2enmod expires
	a2enmod wsgi
}

configure_maas_database() {
	local dbc_dbpass="$1"
	if grep -qs "^\ \{1,\} 'PASSWORD': '[a-zA-Z0-9]\{0,\}',$" /etc/maas/maas_local_settings.py; then
		sed -i "s/^\ \{1,\} 'PASSWORD': '[a-zA-Z0-9]\{0,\}',$/        'PASSWORD': '"$dbc_dbpass"',/" \
                       /etc/maas/maas_local_settings.py
	fi
}

configure_maas_default_url() {
	local ipaddr="$1"

	if grep -qs "^DEFAULT_MAAS_URL\ \= \"[a-zA-Z0-9:/.]\{0,\}\"$" /etc/maas/maas_local_settings.py; then
		sed -i "s/^DEFAULT_MAAS_URL\ \= \"[a-zA-Z0-9:/.]\{0,\}\"$/DEFAULT_MAAS_URL = \"http:\/\/"$ipaddr"\/MAAS\"/" \
                    /etc/maas/maas_local_settings.py
	fi
}

get_default_route_ip() {
	while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT; do
		[ "$Mask" = "00000000" ] && break
	done < /proc/net/route
	interface="$Iface"
	ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global)
	ipaddr=${ipaddr#* inet }
	ipaddr=${ipaddr%%/*}
        echo $ipaddr
}

configure_logging() {
	# Give appropriate permissions
	if [ ! -f /var/log/maas/maas-django.log ]; then
		touch /var/log/maas/maas-django.log
	fi
	chown -R maas:maas /var/log/maas
	chmod -R 775 /var/log/maas/oops

	# Main syslog file.
	if [ ! -f /var/log/maas/maas.log ]; then
		touch /var/log/maas/maas.log
	fi
	chown syslog:syslog /var/log/maas/maas.log

	# Create log directory base
	mkdir -p /var/log/maas/rsyslog
	chown -R syslog:syslog /var/log/maas/rsyslog
	# Make sure rsyslog reads our config
	invoke-rc.d rsyslog restart

	# If proxy log dir exists, set correct permissions
	if [ -d /var/log/maas/proxy ]; then
		chown -R proxy:proxy /var/log/maas/proxy
	fi
}

configure_third_party_log_symlinks() {
	ln -sf /var/log/apache2 /var/log/maas/
}

configure_migrate_maas_dns() {
	# This only runs on upgrade. We only run this if the
	# there are forwarders to migrate or no
	# named.conf.options.inside.maas are present.
	maas-region-admin edit_named_options \
	    --migrate-conflicting-options --config-path \
	    /etc/bind/named.conf.options || true
	invoke-rc.d bind9 restart || true
}

if [ "$1" = "configure" ] && [ -z "$2" ]; then
	#########################################################
	################ Folder Permissions  ####################
	#########################################################
	mkdir -p /var/lib/maas/media/storage
	chown -R maas:maas /var/lib/maas/

	# Config will contain credentials, so should be readable
	# by the application but nobody else.
	chown root:maas /etc/maas/maas_local_settings.py
	chmod 0640 /etc/maas/maas_local_settings.py

	#########################################################
	################  Configure Apache2  ####################
	#########################################################
	configure_region_http

	#########################################################
	##########  Configure DEFAULT_MAAS_URL  #################
	#########################################################

	# Obtain IP address of default route and change DEFAULT_MAAS_URL
	# if default-maas-url has not been preseeded.
	db_get maas/default-maas-url
	ipaddr="$RET"
	if [ -z "$RET" ]; then
		ipaddr=$(get_default_route_ip)
	fi
	# Set the IP address of the interface with default route
	if [ -n "$ipaddr" ]; then
		configure_maas_default_url "$ipaddr"
		db_subst maas/installation-note MAAS_URL "$ipaddr"
		db_set maas/default-maas-url "$ipaddr"
	fi

	#########################################################
	################  Configure Logging  ####################
	#########################################################

	configure_logging
	configure_third_party_log_symlinks

	#########################################################
	################  Configure Database  ###################
	#########################################################

	# Need to for postgresql start so it doesn't fail on the installer
	restart_postgresql

	# Create the database
	dbc_go maas-region-controller $@
	configure_maas_database "$dbc_dbpass"

	# Only syncdb if we have selected to install it with dbconfig-common.
	db_get maas-region-controller/dbconfig-install
	if [ "$RET" = "true" ]; then
		maas_sync_migrate_db
		configure_migrate_maas_dns
	fi

	# Display installation note
	db_input low maas/installation-note || true
	db_go

elif [ -n "$DEBCONF_RECONFIGURE" ]; then
	# Set the IP address of the interface with default route
	db_get maas/default-maas-url
	ipaddr="$RET"
	if [ -n "$ipaddr" ]; then
		configure_maas_default_url "$ipaddr"
	fi

elif [ "$1" = "configure" ] && dpkg --compare-versions "$2" gt 0.1+bzr266+dfsg-0ubuntu1; then
	# Logging changed at r2611, ensure it is set up.
	configure_logging
	configure_third_party_log_symlinks

	# If upgrading to any later package version, then upgrade db.
	invoke-rc.d apache2 stop || true

	# make sure postgresql is running
	restart_postgresql

	# make sure maas http config is symlinked
	configure_region_http

	# we need to regenerate the passwords and update configs.
	db_get maas/default-maas-url
	ipaddr="$RET"
	configure_maas_default_url "$ipaddr"
	# handle database upgrade
	if [ -f /etc/dbconfig-common/maas-region-controller.conf ]; then
		# source dbconfig-common db config for maas-region-controller
		# before upgrading database, otherwise a new config is written
		# but the password is no longer preserved.
		. /etc/dbconfig-common/maas-region-controller.conf
	else
		dbc_go maas-region-controller $@
	fi
	configure_maas_database "$dbc_dbpass"

	maas_sync_migrate_db

	configure_migrate_maas_dns
fi

invoke-rc.d apache2 restart || true

if [ -f /etc/init/maas-clusterd.conf ]; then
	invoke-rc.d maas-clusterd restart || true
fi

db_stop

#DEBHELPER#