summaryrefslogtreecommitdiff
path: root/s1_setname.sh
blob: bbd86085100df83568b0167fdbafa024ec3e8e1e (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# File: /root/s1_setname.sh
# Package: deployscripts
# Author: bgstack15
# Startdate: 2015
# Title: Template Script 1: Set Name
# Purpose: Sets hostname regardless of OS
# History: 2016-08-16 Given original headers
# Usage: ./s1[tab][enter]
#   observe the /bin/bash shebang. I only run this on a system after bash is installed.
# Reference:
# Improve:

eval flavor=$( grep -iE "^\s*ID=" /etc/os-release 2>/dev/null | sed 's/^.*=//;' )
if test -z "${flavor}"; then test "$( uname -s )" = "FreeBSD" && flavor=freebsd; fi
case "${flavor}" in
   centos)
      motdfile=/etc/motd
      netfile=/etc/sysconfig/network
      templatename=centos7alpha
      ;;
   ubuntu)
      motdfile=/etc/issue
      templatename=ubuntu16alpha
      ;;
   freebsd)
      flavor=freebsd
      motdfile=/etc/motd
      netfile=/etc/rc.conf
      templatename=freebsd10alpha
      ;;
   *)
      echo "$0: Error 1. Cannot determine OS from /etc/os-release. Aborted." 1>&2
      exit 1
      ;;
esac

# OS agnostic
server=
role=
hostnamefile=/etc/hostname
tmpfile1=~/.$$.$RANDOM.tmp

function clean_setname {
   rm -f $tmpfile1 2>/dev/null
   exit
}

trap 'clean_setname' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

while [[ -z "$server" ]];
do
   printf "server (excluding .example.com): "
   read server
done

while [[ -z "$role" ]];
do
   printf "role: "
   read role
done

if [[ "$server" = "${server%%.*}" ]];
then
   serverlong="${server}".example.com
else
   # assume we placed .example.com in it already
   serverlong=${server}
   server="${serverlong%%.*}"
fi

# UPDATE FILES
# MOTD
sed "s/SERVER/${server}/g;s/ROLE/${role}/g;" <${motdfile} > ${tmpfile1}
cp -p ${tmpfile1} ${motdfile}
chmod 444 ${motdfile}
# HOSTNAME
printf "${serverlong}\n" > ${hostnamefile}
chmod 644 ${hostnamefile}

# FLAVOR-SPECIFIC ACTIONS
case "${flavor}" in
   centos)
      # UPDATE hostname and NetworkManager
      hostnamectl set-hostname "${serverlong}"
      {
         echo "NETWORKING=yes"
         echo "HOSTNAME=$serverlong"
      } > ${netfile}
      ;;
   ubuntu)
      # Change volume group names if necessary
      oldvg=$( vgs --rows | grep -E "^\s*VG" | awk '{print $2}' )
      case "${oldvg}" in
         *ubuntu16*-vg)
            vgrename "${oldvg}" "${server}-vg" >/dev/null 2>&1
            sed -i "s/${oldvg%-vg}--vg/${server}--vg/g;" /etc/fstab
            sed -i "s/${oldvg%-vg}--vg/${server}--vg/g;" /boot/grub/grub.cfg
            update-grub >/dev/null 2>&1
            sed -i "s/${templatename}/${server}/g;" /etc/postfix/main.cf
            /etc/init.d/postfix reload >/dev/null 2>&1
            ;;
         *) [ ];;
      esac
      ;;
   freebsd)
         # change hostname for freebsd. Need to update rc.conf, smb4.conf, /etc/mail/freebsd.mc
         hostname "${serverlong}"
         sed -I -e "s/^hostname=\".*\"/hostname=\"${serverlong}\"/;" /etc/rc.conf /etc/rc.conf.example
         sed -I -e "s/netbios name = .*$/netbios name = $( hostname -s )/;" /usr/local/etc/smb4.conf /usr/local/etc/smb4.conf.example
         sed -i -e 's/MASQUERADE_AS.*$/MASQUERADE_AS(\`'"$( hostname )'"')/;' /etc/mail/freebsd.mc

      ;;
# no wildcard needed because already vetted in flavor check earlier
esac
bgstack15