This file is indexed.

postinst is in maas 0.1+bzr482+dfsg-0ubuntu1.

This file is a maintainer script. It is executed when installing (*inst) or removing (*rm) the package.

The actual contents of the file can be viewed below.

  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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/sh -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

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

restart_apache2(){
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d apache2 restart || true
	else
		/etc/init.d/apache2 restart || true
	fi
}

restart_rabbitmq(){
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d rabbitmq-server restart || true
	else
		/etc/init.d/rabbitmq-server restart || true
	fi
}

restart_postgresql(){
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d --force postgresql restart || true
	else
		/etc/init.d/postgresql restart || true
	fi
}

restart_cobbler(){
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d cobbler restart || true
	else
		/etc/init.d/cobbler restart || true
	fi
}

add_user_group(){
	local user="maas"
	local group="maas"
	if ! getent group "$group" >/dev/null; then
		addgroup --quiet --system "$group" || true
	fi
	if ! getent passwd "$user" > /dev/null 2>&1; then
		adduser --quiet \
                    --system \
                    --group \
                    --no-create-home \
                    "$user" || true
	fi
}

configure_maas_cobbler_user() {
	# Create 'maas' user and password to autoconfigure
	local cblr_pass=
	local hash=
	cblr_pass="$(pwgen -s 20)"
	hash=$(printf "maas:Cobbler:$cblr_pass" | md5sum | awk '{print $1}')
	[ -e /etc/cobbler/users.digest ] || install -o root -g root -m 0600 /dev/null /etc/cobbler/users.digest
	htpasswd -D /etc/cobbler/users.digest "maas" || true
	printf "maas:Cobbler:$hash\n" >> /etc/cobbler/users.digest

	if grep -qs "^\ \{1,\}password:.*$" /etc/maas/pserv.yaml; then
		sed -i "s/^\ \{1,\}password:.*$/  password: \""$cblr_pass"\"/" /etc/maas/pserv.yaml
	fi
}

configure_maas_pserv_user() {
	local pserv_pass=
	pserv_pass="$(pwgen -s 20)"
	if grep -qs "^password: \".*\"$" /etc/maas/pserv.yaml; then
		sed -i '/^password:/s/".*"$/"'"${pserv_pass}"'"/' /etc/maas/pserv.yaml
	fi
	if grep -qs "^PSERV_URL\ =\ " /etc/maas/maas_local_settings.py; then
		sed -i '/^PSERV_URL[ =]/s/maas:.*@/'"maas:${pserv_pass}"'@/' /etc/maas/maas_local_settings.py
	fi
}

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_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_tgt(){
	local tgtcfg="/etc/tgt/targets.conf"
	[ -d /etc/tgt/conf.d/ ] || 
	   echo "Warning! $tgtcfg did not exist" 1>&2;
	mkdir -p /etc/tgt/conf.d/ /var/lib/maas/ephemeral/
	ln -sf /var/lib/maas/ephemeral/tgt.conf /etc/tgt/conf.d/maas.conf
}

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"\/\"/" \
                    /etc/maas/maas_local_settings.py
	fi

	# Replace for PXE
	if grep -qs "^next_server:.*$" /etc/cobbler/settings; then
		sed -i "s/^next_server:.*$/next_server: $ipaddr/" /etc/cobbler/settings
	fi
	if grep -qs "^server:.*$" /etc/cobbler/settings; then
		sed -i "s/^server:.*$/server: $ipaddr/" /etc/cobbler/settings
	fi
}

if [ "$1" = "configure" ] && [ -z "$2" ]; then
	#########################################################
	################ User/Group Creatiion  ##################
	#########################################################
	add_user_group

	#########################################################
	################ Folder Permissions  ####################
	#########################################################
	mkdir -p /var/lib/maas/media/storage
	chown -R maas:maas /var/lib/maas/

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

	# enable apache modules needed
	a2enmod expires
	a2enmod wsgi

	# Need to restart apache to pickup web configs
	restart_apache2

	#########################################################
	########### Configure maas user for Cobbler #############
	#########################################################

	configure_maas_cobbler_user

	#########################################################
	############ Configure maas user for pserv ##############
	#########################################################

	configure_maas_pserv_user

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

	# Obtain IP address of default route and change DEFAULT_MAAS_URL.
	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%%/*}
	# 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  ####################
	#########################################################

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

	# Create log directory base
	mkdir -p /var/log/maas/rsyslog
	chown -R syslog:syslog /var/log/maas/rsyslog
	# Make sure rsyslog reads our config
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d rsyslog restart
	fi

	#########################################################
	################### Squid-deb-proxy  ####################
	#########################################################
	# Make sure squid-deb-proxy reads our config (99-maas)
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d squid-deb-proxy restart
	fi

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

	# Handle longpoll/rabbitmq publishing
	restart_rabbitmq
	configure_maas_txlongpoll_rabbitmq_user

	#########################################################
	######## add maas.conf to tgt conf.d ####################
	#########################################################
	configure_maas_tgt

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

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

	# Create the database
	dbc_go maas $@
	configure_maas_database "$dbc_dbpass"

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

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

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

elif [ "$1" = "configure" ] && dpkg --compare-versions "$2" gt 0.1+bzr266+dfsg-0ubuntu1; then
	# If upgrading to any later package version, then upgrade db.
	if [ -x /usr/sbin/invoke-rc.d ]; then
		invoke-rc.d apache2 stop || true
	fi

	# make sure postgresql is running
	restart_postgresql

	# If upgrading from any version lower than 0.1+bzr445+dfsg-0ubuntu1
	# we need to update the user/group.
	if dpkg --compare-versions "$2" lt 0.1+bzr445+dfsg-0ubuntu1; then
		# add user/group
		add_user_group
		# set correct permissions
		chown -R maas:maas /var/lib/maas/
		chown -R maas:maas /var/log/maas
		chown -R syslog:syslog /var/log/maas/rsyslog
	fi
	if dpkg --compare-versions "$2" lt 0.1+bzr459+dfsg-0ubuntu1; then
		configure_maas_tgt
	fi
	# If upgrading from any version lower than 0.1+bzr462+dfsg-0ubuntu1
	# we need to regenerate the passwords and update configs.
	if dpkg --compare-versions "$2" lt 0.1+bzr473+dfsg-0ubuntu1; then
		# configure maas user for cobbler
		configure_maas_cobbler_user
		# configure pserv user for cobbler
		configure_maas_pserv_user
		# configure maas default url
		db_get maas/default-maas-url
		ipaddr="$RET"
		# If nothing in the database, obtain the IP from cobbler settings
		# which is the same as MAAS_DEFAULT_URL
		if [ -z "$ipaddr" ]; then
			ipaddr=$(awk '$1 == "server:" { print $2 }' /etc/cobbler/settings)
			db_set maas/default-maas-url "$ipaddr"
		fi
		configure_maas_default_url "$ipaddr"
		# make sure rabbitmq is running
		restart_rabbitmq
		configure_maas_txlongpoll_rabbitmq_user
		# handle database upgrade
		dbc_go maas $@
		configure_maas_database "$dbc_dbpass"
	fi

	maas_sync_migrate_db
	restart_apache2

fi

db_stop

# Automatically added by dh_installinit
if [ -e "/etc/init/maas-pserv.conf" ]; then
	invoke-rc.d maas-pserv start || exit $?
fi
# End automatically added section
# Automatically added by dh_installinit
update-rc.d -f maas-pserv remove >/dev/null || exit $?
# End automatically added section
# Automatically added by dh_installinit
if [ -e "/etc/init/maas-txlongpoll.conf" ]; then
	invoke-rc.d maas-txlongpoll start || exit $?
fi
# End automatically added section
# Automatically added by dh_installinit
update-rc.d -f maas-txlongpoll remove >/dev/null || exit $?
# End automatically added section

# Automatically added by dh_python2:
if which pycompile >/dev/null 2>&1; then
	pycompile -p maas 
fi

if which pycompile >/dev/null 2>&1; then
	pycompile -p maas /usr/share/maas -V 2.7-
fi

# End automatically added section


exit 0