Merge "Initial support for composable upgrades with Heat+Ansible"
[apex-tripleo-heat-templates.git] / scripts / hosts-config.sh
1 #!/bin/bash
2 set -eux
3 set -o pipefail
4
5 write_entries() {
6     local file="$1"
7     local entries="$2"
8
9     # Don't do anything if the file isn't there
10     if [ ! -f "$file" ]; then
11         return
12     fi
13
14     if grep -q "^# HEAT_HOSTS_START" "$file"; then
15         temp=$(mktemp)
16         awk -v v="$entries" '/^# HEAT_HOSTS_START/ {
17             print $0
18             print v
19             f=1
20             }f &&!/^# HEAT_HOSTS_END$/{next}/^# HEAT_HOSTS_END$/{f=0}!f' "$file" > "$temp"
21             echo "INFO: Updating hosts file $file, check below for changes"
22             diff "$file" "$temp" || true
23             cat "$temp" > "$file"
24     else
25         echo -ne "\n# HEAT_HOSTS_START - Do not edit manually within this section!\n" >> "$file"
26         echo "$entries" >> "$file"
27         echo -ne "# HEAT_HOSTS_END\n\n" >> "$file"
28     fi
29
30 }
31
32 if [ ! -z "$hosts" ]; then
33     # cloud-init files are /etc/cloud/templates/hosts.OSNAME.tmpl
34     DIST=$(lsb_release -is | tr -s [A-Z] [a-z])
35     case $DIST in
36         fedora|redhatenterpriseserver)
37             name="redhat"
38             ;;
39         *)
40             name="$DIST"
41             ;;
42     esac
43     write_entries "/etc/cloud/templates/hosts.${name}.tmpl" "$hosts"
44     write_entries "/etc/hosts" "$hosts"
45 else
46     echo "No hosts in Heat, nothing written."
47 fi