#!/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 ROOT="$1" octet='\(\(2[0-5]\?[0-5]\?\)\|\(1[0-9]\?[0-9]\?\)\|\([0-9]\)\)' valid_ip() { #echo -n "$1" | grep -xq $octet.$octet.$octet.$octet echo -n "$1" | grep -q $octet.$octet.$octet.$octet } valid_hostname() { #echo -n "$1" | grep -xq '[a-zA-Z0-9.-]\+' echo -n "$1" | grep -q '[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" >> "$ROOT"/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 >> "$ROOT"/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"