Add image-properties support in helper
[snf-image] / snf-image-helper / tasks / 50AssignHostname.in
1 #! /bin/bash
2
3 ### BEGIN TASK INFO
4 # Provides:             AssignHostname
5 # RunBefore:            UmountImage
6 # RunAfter:             InstallUnattend
7 # Short-Description:    Assign Hostname/Computer Name to the instance
8 ### END TASK INFO
9
10 set -e
11 . "@commondir@/common.sh"
12
13 windows_hostname() {
14     local target="$1"
15     local password="$2"
16
17     local tmp_unattend=`mktemp` || exit 1
18     add_cleanup rm "$tmp_unattend"
19
20     echo -n "Assigning new computer name..."
21
22     local namespace="urn:schemas-microsoft-com:unattend"
23     
24     "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
25
26     cat "$tmp_unattend" > "$target/Unattend.xml"
27     echo done
28 }
29
30 linux_hostname() {
31     local target="$1"
32     local hostname="$2"
33
34     local distro=$(get_base_distro "$target")
35
36     case "$distro" in
37         debian)
38             echo "$hostname" > "$target/etc/hostname";;
39         redhat)
40             sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
41         slackware|suse)
42         #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
43         
44         # In slackware hostname and domain name are joined together. For now I
45         # will not retain the domain name.
46         
47         echo "$hostname" > "${target}/etc/HOSTNAME";;
48     gentoo)
49         sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
50     esac
51
52     # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
53     # resolvable to an IP address. Lets replace this if found in /etc/hosts
54     sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
55 }
56
57 if [ ! -d "$SNF_IMAGE_TARGET" ]; then
58     log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"     
59 fi
60
61 if [ -z "$SNF_IMAGE_HOSTNAME" ]; then
62     log_error "Hostname is missing"
63 fi
64
65 if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "windows" ]; then
66     windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
67 elif [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "linux" ]; then
68     linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
69 fi
70
71 cleanup
72 trap - EXIT
73
74 exit 0
75
76 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
77