|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2017 The Openstack-Helm Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -ex |
| 18 | + |
| 19 | +. /usr/share/debconf/confmodule |
| 20 | +db_version 2.0 |
| 21 | + |
| 22 | +if [ -f /usr/share/dbconfig-common/dpkg/postinst.pgsql ]; then |
| 23 | + . /usr/share/dbconfig-common/dpkg/postinst.pgsql |
| 24 | +fi |
| 25 | + |
| 26 | +RELEASE=`lsb_release -rs` || RELEASE="" |
| 27 | + |
| 28 | +maas_sync_migrate_db(){ |
| 29 | + maas-region dbupgrade |
| 30 | +} |
| 31 | + |
| 32 | +restart_postgresql(){ |
| 33 | + invoke-rc.d --force postgresql restart || true |
| 34 | +} |
| 35 | + |
| 36 | +configure_maas_default_url() { |
| 37 | + local ipaddr="$1" |
| 38 | + # The given address is either "[IPv6_IP]" or "IPv4_IP" or "name", such as |
| 39 | + # [2001:db8::3:1]:5555 or 127.0.0.1 or maas.example.com. |
| 40 | + # The ugly sed splits the given thing as: |
| 41 | + # (string of anything but ":", or [ipv6_ip]), |
| 42 | + # optionally followed by :port. |
| 43 | + local address=$(echo "$ipaddr" | |
| 44 | + sed -rn 's/^([^:]*|\[[0-9a-fA-F:]*\])(|:[0-9]*)?$/\1/p') |
| 45 | + local port=$(echo "$ipaddr" | |
| 46 | + sed -rn 's/^([^:]*|\[[0-9a-fA-F:]*\])(|:[0-9]*)?$/\2/p') |
| 47 | + test -n "$port" || port=":80" |
| 48 | + ipaddr="${ipaddr}${port}" |
| 49 | + maas-region local_config_set --maas-url "http://${ipaddr}/MAAS" |
| 50 | +} |
| 51 | + |
| 52 | +extract_default_maas_url() { |
| 53 | + # Extract DEFAULT_MAAS_URL IP/host setting from config file $1. |
| 54 | + grep "^DEFAULT_MAAS_URL" "$1" | cut -d"/" -f3 |
| 55 | +} |
| 56 | + |
| 57 | +configure_migrate_maas_dns() { |
| 58 | + # This only runs on upgrade. We only run this if the |
| 59 | + # there are forwarders to migrate or no |
| 60 | + # named.conf.options.inside.maas are present. |
| 61 | + maas-region edit_named_options \ |
| 62 | + --migrate-conflicting-options --config-path \ |
| 63 | + /etc/bind/named.conf.options |
| 64 | + invoke-rc.d bind9 restart || true |
| 65 | +} |
| 66 | + |
| 67 | +if [ "$1" = "configure" ] && [ -z "$2" ]; then |
| 68 | + ######################################################### |
| 69 | + ########## Configure DEFAULT_MAAS_URL ################# |
| 70 | + ######################################################### |
| 71 | + |
| 72 | + # Obtain IP address of default route and change DEFAULT_MAAS_URL |
| 73 | + # if default-maas-url has not been preseeded. Prefer ipv4 addresses if |
| 74 | + # present, and use "localhost" only if there is no default route in either |
| 75 | + # address family. |
| 76 | + db_get maas/default-maas-url |
| 77 | + ipaddr="$RET" |
| 78 | + if [ -z "$ipaddr" ]; then |
| 79 | + ipaddr="{{ .Values.ui_service_name }}.{{ .Release.Namespace }}" |
| 80 | + fi |
| 81 | + # Set the IP address of the interface with default route |
| 82 | + configure_maas_default_url "$ipaddr" |
| 83 | + db_subst maas/installation-note MAAS_URL "$ipaddr" |
| 84 | + db_set maas/default-maas-url "$ipaddr" |
| 85 | + |
| 86 | + ######################################################### |
| 87 | + ################ Configure Database ################### |
| 88 | + ######################################################### |
| 89 | + |
| 90 | + # Create the database |
| 91 | + dbc_go maas-region-controller $@ |
| 92 | + maas-region local_config_set \ |
| 93 | + --database-host {{ include "helm-toolkit.postgresql_host" . | quote }} \ |
| 94 | + --database-name {{ .Values.database.db_name | quote }} \ |
| 95 | + --database-user {{ .Values.database.db_user | quote }} \ |
| 96 | + --database-pass {{ .Values.database.db_password | quote }} |
| 97 | + |
| 98 | + # Only syncdb if we have selected to install it with dbconfig-common. |
| 99 | + db_get maas-region-controller/dbconfig-install |
| 100 | + if [ "$RET" = "true" ]; then |
| 101 | + maas_sync_migrate_db |
| 102 | + configure_migrate_maas_dns |
| 103 | + fi |
| 104 | + |
| 105 | + db_get maas/username |
| 106 | + username="$RET" |
| 107 | + if [ -n "$username" ]; then |
| 108 | + db_get maas/password |
| 109 | + password="$RET" |
| 110 | + if [ -n "$password" ]; then |
| 111 | + maas-region createadmin --username "$username" --password "$password" --email "$username@maas" |
| 112 | + fi |
| 113 | + fi |
| 114 | + |
| 115 | + # Display installation note |
| 116 | + db_input low maas/installation-note || true |
| 117 | + db_go |
| 118 | + |
| 119 | +fi |
| 120 | + |
| 121 | +systemctl enable maas-regiond >/dev/null || true |
| 122 | +systemctl restart maas-regiond >/dev/null || true |
| 123 | +invoke-rc.d apache2 restart || true |
| 124 | + |
| 125 | +if [ -f /lib/systemd/system/maas-rackd.service ]; then |
| 126 | + systemctl restart maas-rackd >/dev/null || true |
| 127 | +fi |
| 128 | + |
| 129 | +db_stop |
0 commit comments