summaryrefslogtreecommitdiff
path: root/openstack.configure
blob: b50d29461dadd49e87d75465b815324de9b57152 (plain)
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
#!/bin/bash
#
# Copyright © 2015 Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

set +e
set -u

# COMPUTE_HOSTS, OBJECT_STORAGE_HOSTS and BLOCK_STORAGE_HOSTS
# are a list of 'name,ip' strings
#
# CONTROLLER HOST is a single 'name,ip' string
#
# TODO: ipv6

octet='\(\(2[0-5]\?[0-5]\?\)\|\(1[0-9]\?[0-9]\?\)\|\([0-9]\)\)'

valid_ip() {
	echo -n "$1" | grep -xq $octet.$octet.$octet.$octet
}

valid_hostname() {
	echo -n "$1" | grep -xq '[a-zA-Z0-9.-]\+'
}

append_host() {
	# Validate ip and hostname and append to /etc/hosts

	ip="$1"
	hostname="$2"

	if ! valid_ip "$ip"; then echo "ip, $ip" is malformed >&2; exit 1; fi
	if ! valid_hostname "$hostname"; then echo "hostname, $hostname" is malformed >&2; exit 1; fi

	echo "$ip" "$hostname" >> /etc/hosts
}

add_hosts() {
	# Creates a host to ip mapping for a set of ips and adds them to /etc/hosts
	# Each ip is assigned a hostname based on a prefix
	# starting from prefix1.$STACK_ID up to prefixN.$STACK_ID
	#
	# STACK_ID is a unique suffix used to prevent hostname collisions

	if [[ -z "$1" || -z "$2" ]]; then return; fi

	ips="$1"
	prefix="$2"

	i=0
	for ip in $ips
	do
		((i += 1))
		append_host "$ip" "$prefix$i.$STACK_ID"
	done
}

echo >> /etc/hosts  # insert line break

append_host "$CONTROLLER_NODE_IP" "controller.$STACK_ID"

add_hosts "$COMPUTE_NODE_IPS" "compute"
add_hosts "$OBJECT_STORAGE_NODE_IPS" "object"
add_hosts "$BLOCK_STORAGE_NODE_IPS" "block"