~julian-edwards/maas/packaging-syslog

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/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_rabbitmq(){
	invoke-rc.d rabbitmq-server restart || true
}

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

restart_squid_deb_proxy() {
	invoke-rc.d squid-deb-proxy 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_txlongpoll_rabbitmq_user() {
	local longpoll_user="maas_longpoll"
	local longpoll_pass=
	local longpoll_vhost="/maas_longpoll"
	longpoll_pass="$(pwgen -s 20)"
	if [ -x /usr/sbin/rabbitmqctl ]; then
		if ! rabbitmqctl list_users | grep -qs "$longpoll_user"; then
			rabbitmqctl add_user "$longpoll_user" "$longpoll_pass" || true
			rabbitmqctl add_vhost "$longpoll_vhost" || true
			rabbitmqctl set_permissions -p "$longpoll_vhost" "$longpoll_user" ".*" ".*" ".*" || true
		else
			rabbitmqctl change_password "$longpoll_user" "$longpoll_pass" || true
		fi
	fi

	if grep -qs "^\ \{1,\}password: \"[a-zA-Z0-9]\{0,\}\"$" /etc/maas/txlongpoll.yaml; then
		sed -i "s/^\ \{1,\}password: \"[a-zA-Z0-9]\{0,\}\"$/  password: \""$longpoll_pass"\"/" \
                       /etc/maas/txlongpoll.yaml
	fi
	if grep -qs "^RABBITMQ_PASSWORD\ \= '[a-zA-Z0-9]\{0,\}'$" /etc/maas/maas_local_settings.py; then
		sed -i "s/^RABBITMQ_PASSWORD\ \= '[a-zA-Z0-9]\{0,\}'$/RABBITMQ_PASSWORD = '"$longpoll_pass"'/" \
                       /etc/maas/maas_local_settings.py
	fi
}

configure_maas_workers_rabbitmq_user() {
	local workers_user="maas_workers"
	local workers_pass="$(pwgen -s 20)"
	local workers_vhost="/maas_workers"
	local amqp_host="$1"
	if [ -z "$amqp_host" ]; then
		amqp_host="localhost"
	fi
	local amqp_port="5672"
	if [ -x /usr/sbin/rabbitmqctl ]; then
		if ! rabbitmqctl list_users | grep -qs "$workers_user"; then
			rabbitmqctl add_user "$workers_user" "$workers_pass" || true
			rabbitmqctl add_vhost "$workers_vhost" || true
			rabbitmqctl set_permissions -p "$workers_vhost" "$workers_user" ".*" ".*" ".*" || true
		else
			rabbitmqctl change_password "$workers_user" "$workers_pass" || true
		fi
	fi

	if grep -qs "^BROKER_URL\ \= '.*'$" /etc/maas/maas_local_celeryconfig.py; then
		local broker_url="amqp://$workers_user:$workers_pass@$amqp_host:$amqp_port/$workers_vhost"
		sed -i "s|^BROKER_URL\ \= '.*'$|BROKER_URL = '"$broker_url"'|" \
                       /etc/maas/maas_local_celeryconfig.py
	fi
}

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_maas_squid_deb_proxy() {
	local ipaddr="$1"

	if [ -e /usr/share/maas/conf/99-maas -a \
	     ! -L /etc/squid-deb-proxy/mirror-dstdomain.acl.d/99-maas ]; then
		ln -sf /usr/share/maas/conf/99-maas \
		       /etc/squid-deb-proxy/mirror-dstdomain.acl.d/99-maas
	fi

	sed -i "s/\(^[a-zA-Z0-9\.\-].*\) # maasurl$/$ipaddr # maasurl/" \
             /usr/share/maas/conf/99-maas
}

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_celeryconfig.py \
		/etc/maas/maas_local_settings.py
	chmod 0640 \
		/etc/maas/maas_local_celeryconfig.py \
		/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"
		configure_maas_squid_deb_proxy "$ipaddr"
		db_subst maas/installation-note MAAS_URL "$ipaddr"
		db_set maas/default-maas-url "$ipaddr"
	fi

	#########################################################
	################  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

	#########################################################
	################### Squid-deb-proxy  ####################
	#########################################################
	# Make sure squid-deb-proxy reads our config (99-maas)
	invoke-rc.d squid-deb-proxy restart

	#########################################################
	########## Configure longpoll rabbitmq config ###########
	#########################################################

	# Handle longpoll/rabbitmq publishing
	restart_rabbitmq
	configure_maas_txlongpoll_rabbitmq_user

	#########################################################
	########## Configure worker rabbitmq config ###########
	#########################################################

	# Handle celery/rabbitmq publishing
	configure_maas_workers_rabbitmq_user "$ipaddr"

	#########################################################
	################  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
	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"
		configure_maas_squid_deb_proxy "$ipaddr"
		configure_maas_workers_rabbitmq_user "$ipaddr"
	fi
	configure_maas_txlongpoll_rabbitmq_user

elif [ "$1" = "configure" ] && dpkg --compare-versions "$2" gt 0.1+bzr266+dfsg-0ubuntu1; then
	# 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"
	configure_maas_squid_deb_proxy "$ipaddr"
	# make sure rabbitmq is running
	restart_rabbitmq
	configure_maas_txlongpoll_rabbitmq_user
	# Handle celery/rabbitmq publishing
	configure_maas_workers_rabbitmq_user "$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

fi

invoke-rc.d apache2 restart || true

restart_squid_deb_proxy

invoke-rc.d maas-txlongpoll restart || true
invoke-rc.d maas-region-celery restart || true

db_stop

#DEBHELPER#