summaryrefslogtreecommitdiff
path: root/s1_setname.sh
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2016-10-20 16:15:23 -0400
committerB Stack <bgstack15@gmail.com>2016-10-20 16:15:23 -0400
commitd60ed67fa4eeeba312646358316e49c0a6cbc6f6 (patch)
tree7cec285c82c1e15cc021086899165afb7bed2385 /s1_setname.sh
downloaddeployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.tar.gz
deployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.tar.bz2
deployscripts-d60ed67fa4eeeba312646358316e49c0a6cbc6f6.zip
initial commit
Diffstat (limited to 's1_setname.sh')
-rwxr-xr-xs1_setname.sh115
1 files changed, 115 insertions, 0 deletions
diff --git a/s1_setname.sh b/s1_setname.sh
new file mode 100755
index 0000000..bbd8608
--- /dev/null
+++ b/s1_setname.sh
@@ -0,0 +1,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