-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-vm
executable file
·91 lines (77 loc) · 2.19 KB
/
update-vm
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
#!/bin/bash
# base ceph template will be registered - localhost.localdomain
# 8.4 with perl installed
# on the network with 192.168.56.250
if [ "$1" == "" ]; then
echo "usage : update-ceph <template: rhel79|rhel84_rhel86> <new name> <new ip>"
echo "usage : update-ceph cloned <new name> <new ip> -- for manually cloned vm"
exit;
fi
if [ "$1" != "cloned" ]; then
#first param will be the template name
case "$1" in
"rhel79")
template=rhel79-template
;;
"rhel84")
template=rhel84-template
;;
"rhel86")
template=rhel86-template
;;
*)
echo "Unknown template $1"
exit;
;;
esac
# validate $1
virsh list --all |grep template |grep $1 > /dev/null
if [ "$?" == "1" ]; then
echo "Template $1 does not exist. Templates should have dashes and not _"
exit
fi
echo $2 |grep '_' > /dev/null
if [ "$?" == "0" ]; then
echo "destination VM names should have dashes and not _"
exit
fi
echo "clone template $template to $2"
clonevm $template $2
sleep 10
echo "Starting clone $2"
virsh start $2
sleep 60
else
grep $2 /etc/hosts > /dev/null
if [ "$?" == "1" ]; then
echo "Please add $2 to /etc/hosts manually first"
exit;
else
echo "skipping the cloning, using already cloned vm $2"
fi
virsh list |grep $2 |grep running
if [ "$?" == "1" ]; then
echo "Starting clone $2"
virsh start $2
sleep 60
fi
fi
# run this script
ssh-keygen -R "ceph-template"
ssh-copy-id -f -o StrictHostKeyChecking=no ceph-template
echo "Changing hostname to $2"
# change the hostname
ssh ceph-template hostname
ssh ceph-template hostnamectl set-hostname $2.example.com
ssh ceph-template hostname
if [ "$1" != "cloned" ]; then
echo "$3 $2.example.com $2" | sudo tee -a /etc/hosts
IP=$3
else
echo "skipping adding $2 to hosts"
IP=$(grep $2 /etc/hosts |awk '{print $1}')
fi
# change the IP address
echo changing IP to $IP
ssh ceph-template perl -i -p -e "s/192.168.56.250/$IP/g" /etc/sysconfig/network-scripts/ifcfg-e*0
ssh ceph-template reboot