Merge "Add retry to RHEL registration"
[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     for tmpl in /etc/cloud/templates/hosts.*.tmpl ; do
34         write_entries "$tmpl" "$hosts"
35     done
36     write_entries "/etc/hosts" "$hosts"
37 else
38     echo "No hosts in Heat, nothing written."
39 fi