Commit 9867964e authored by Cédric BRINER's avatar Cédric BRINER

vgrename; tidy

parent 09e4343d
#!/bin/bash
SYSTEMD_NETWORK_PATH="/etc/systemd/network/ug-static-ipv46.network"
progname="master-2-vm"
current_hostname="$(cat /etc/hostname)"
################################################################################
# C B R   LIB
_B="\e[34m"
_R="\e[31m"
_G="\e[32m"
_N="\e[0m"
function pb()
{
echo -e "${_B}$1${_N}"
}
function pr()
{
echo -e "${_R}$1${_N}"
}
function pg()
{
echo -e "${_G}$1${_N}"
}
function pbr()
{
echo -e "${_B}$1 : ${_R}${_N}"
}
function pbg()
{
echo -e "${_B}$1 : ${_G}${_N}"
}
function tab()
{
if [[ $1 -eq 0 ]]; then
cat -
fi
t="$(printf %${1}s)"
sed "s|^|${t}|"
}
################################################################################
function test_prerequesite
{
do_exit="False"
pb "test_prerequesite"
pb " - check commands"
for cmd in uuidgen salt-call sshd systemctl; do
if hash $cmd &> /dev/null; then
pbg " - Found cmd : ${cmd}"
else
pbr " - Did not found cmd : ${cmd}"
do_exit="True"
fi
done
pb " - check packages"
for pkg in open-vm-tools openssh-server; do
if dpkg -l "${pkg}" &> /dev/null; then
pbg " - Found package : ${pkg}"
else
pbr " - Did not found package : ${pkg}"
do_exit="True"
fi
done
if [[ ${do_exit} = "True" ]]; then
pb "Some commands are missing ! Exit."
exit
fi
}
function clean_up
{
pb "clean_up"
pb " - rm -fr /etc/unige_prod/ug-vmware/"
rm -fr /etc/unige_prod/ug-vmware/ | tab 3
}
function change_hostname
{
pb "change_hostname"
#hostname
pb " - hostname"
pb " - edit /etc/hostname with : ${new_hostname}"
echo "${new_hostname}" > /etc/hostname
pb " - /etc/hosts with: sed -i \"s|${current_hostname}|${new_hostname}|g\" /etc/hosts"
if grep -qw "${current_hostname}" /etc/hosts; then
sed -i "s|${current_hostname}|${new_hostname}|g" /etc/hosts
pg " - changed."
else
pg " - no need"
fi
pb " - hostname (the command) with : hosname ${new_hostname}"
hostname "${new_hostname}" | tab 5
# ssh
echo ""
pb " - ssh"
pb " - remove /etc/ssh/ssh_host_*"
rm -f /etc/ssh/ssh_host_*
pb " - regenerate the ssh_host_*"
dpkg-reconfigure openssh-server 2>&1 | tab 5
pg " Done !"
pb " - remove /root/.ssh/id_rsa*"
rm -f /root/.ssh/id_rsa*
pb " - regenerate the /root/id_rsa*"
ssh-keygen -t rsa -N "" -f /root/.ssh/id_rsa | tab 5
pg " Done !"
#minion
echo ""
pb " - minion"
pb " - edit /etc/salt/minion_id with : ${minion_id}"
echo "${minion_id}" > /etc/salt/minion_id
pb " - Regenerate the pki by deleting pki and restarting salt"
pb " - remove /etc/salt/pki"
rm -fr /etc/salt/pki
pb " - enable salt"
systemctl enable salt-minion | tab 7
pb " - start it"
systemctl start salt-minion | tab 7
}
function construct_ug_static_ipv46
{
echo "Name=ens192"
echo ""
echo "[Network]"
echo "DHCP=no"
echo ""
if [[ "${is_ipv4_enabled}" = "True" ]]; then
echo "# IPV4 : enabled"
echo "Address=${ipv4_address_n_lenmask}"
echo "Gateway=${ipv4_gateway}"
echo ""
else
echo "# IPV4 : disabled"
echo ""
fi
if [[ "${is_ipv6_enabled}" = "True" ]]; then
echo "# IPV6 : enabled"
echo "IPv6AcceptRA=false"
echo "Address=${ipv6_address_n_lenmask}"
echo "Gateway=${ipv6_gateway}"
echo ""
else
echo "# IPV6 : disabled"
echo "IPv6AcceptRA=false"
echo ""
fi
echo "Domains=unige.ch"
echo ""
echo "${lip_dns}" | tr "," "\n" | sed 's|^|DNS=|'
}
function change_network
{
pb "change_network"
pb " - write network file: $SYSTEMD_NETWORK_PATH"
construct_ug_static_ipv46 > "${SYSTEMD_NETWORK_PATH}"
pb " - restart network"
systemctl restart systemd-networkd | tab 3
}
function change_vg_name
{
pb "change_vg_name"
pb " - rename all the vg"
for current_vg_name in $(vgs | grep "${current_hostname}" | awk '{print $1}' ); do
new_vg_name="$(echo ${current_vg_name} | sed "s|${current_hostname}|${new_hostname}|g")"
pb " - vgrename \"${current_vg_name}\" \"${new_vg_name}\""
vgrename "${current_vg_name}" "${new_vg_name}" | tab 5
done
echo ""
pb " - change the uuid for /boot"
boot_dev=$(mount| grep -w /boot | awk '{print $1}')
new_uuid="$(uuidgen)"
cur_uuid="$(blkid -s UUID -o value "${boot_dev}")"
pb " cur_uuid: ${cur_uuid}, new_uuid: ${new_uuid}"
pb " with cmd: tune2fs \"${boot_dev}\" -U \"${new_uuid}\""
tune2fs "${boot_dev}" -U "${new_uuid}" | tab 3
echo ""
pb " - rename the mapper /etc/fstab"
current_mappername="$(echo ${current_hostname}| sed 's|-|--|g')"
new_mappername="$(echo ${new_hostname}| sed 's|-|--|g')"
sed -i -e "s|${current_mappername}|${new_mappername}|" -e "s|${cur_uuid}|${new_uuid}|g" /etc/fstab
sed -i -e "s|${current_mappername}|${new_mappername}|" -e "s|${cur_uuid}|${new_uuid}|g" /etc/initramfs-tools/conf.d/resume
sed -i -e "s|${current_mappername}|${new_mappername}|" -e "s|${cur_uuid}|${new_uuid}|g" /boot/grub/grub.cfg
grep -Po "/dev/mapper/\S+vg\S+" /etc/fstab \
| while read dev ; do
if [[ -L ${dev} ]]; then
pbg " - Device (${dev}) exists."
else
pbr " - Device (${dev}) does not exist. Exit !"
fi
done
echo ""
pb " - grub install with: grub-install /dev/sda"
grub-install /dev/sda |& tab 3
echo ""
pb " - update-initramfs with update-initramfs -k all -u "
update-initramfs -k all -u | tab 3
}
function usage
{
cat << EOF
master-2-vm <new_hostname> <minion_id>\
<is_ipv4_enabled> <ipv4_address_n_lenmask> <ipv4_gateway> \\
<is_ipv6_enabled> <ipv6_address_n_lenmask> <ipv6_gateway> \\
<ip_dns1>[,<ip_dns2>...]
EOF
}
new_hostname="$1"
minion_id="$2"
is_ipv4_enabled="$3"
ipv4_address_n_lenmask="$4"
ipv4_gateway="$5"
is_ipv6_enabled="$6"
ipv6_address_n_lenmask="$7"
ipv6_gateway="$8"
lip_dns="$9"
test_prerequesite
clean_up
change_hostname
change_vg_name
exit
change_network
pb "rm /usr/local/bin/master-2-instance"
rm /usr/local/bin/master-2-instance
pb "stop and disable ssh"
systemctl disable ssh
systemctl stop ssh
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment