-
Notifications
You must be signed in to change notification settings - Fork 4
/
Bootstrap-migration.sh
98 lines (81 loc) · 3.07 KB
/
Bootstrap-migration.sh
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
#!/usr/bin/env bash
unalias -a
trap "_exit" 1 2 3 15
read -p "please input the src host ip:" src_host_ip
read -p "please input the dst host ip:" dst_host_ip
read -p "please input the share_images_dir:" share_images_dir
_usage()
{
echo "Usage: sh Bootstrap-migration.sh [destination ip]"
}
# set an initial value for the flag
QUIET=false
VERBOSE=true
PASSWD="kvmautotest"
_log() { if ! $QUIET; then echo -e $*; fi; }
_log_info() { _log "\033[32mINFO\033[0m\t" $*; }
_log_warn() { _log "\033[33mWARN\033[0m\t" $*; }
_log_error() { _log "\033[31mERROR\033[0m\t" $*; }
_within_dir() { pushd . >/dev/null; cd $1; }
_go_back() { popd >/dev/null; }
_exit()
{
local RET=${1:-0}
if [ $RET -ne 0 ]; then
_log "Please handle the ERROR(s) and re-run this script"
fi
exit $RET
}
_exit_on_error() { if [ $? -ne 0 ]; then _log_error $*; _exit 1; fi; }
_warn_on_error() { if [ $? -ne 0 ]; then _log_warn $*; fi; }
_exec_cmd()
{
local CMD=$*
if $QUIET || (! $VERBOSE); then
eval "$CMD" &>/dev/null
else
_log "\033[1m=> $CMD\033[0m"
eval "$CMD"
fi
return $?
}
if [ ! -n "$dst_host_ip" ]
then
_log_error "Please specify a destination ip."
_usage
exit 1
else
_log "ssh-copy-id to destination host"
_exec_cmd "sshpass -p $PASSWD ssh-copy-id -o \"StrictHostKeyChecking no\" -i /root/.ssh/id_rsa.pub root@$dst_host_ip"
_exit_on_error "Failed to ssh-copy-id to destination host"
_log "update the clock of destination host"
_exec_cmd "ssh root@$dst_host_ip ntpdate clock.redhat.com"
_exit_on_error "Failed to update the clock of destination host"
_log "update the clock of local host"
_exec_cmd "ntpdate clock.redhat.com"
_exit_on_error "Failed to update the clock of local host"
_log "flush iptables rules of local host"
_exec_cmd "iptables -F"
_exit_on_error "Failed to update the clock of local host"
_log "flush iptables rules of destination host"
_exec_cmd "ssh root@$dst_host_ip iptables -F"
_exit_on_error "Failed to update the clock of destination host"
_log "configure nfs of local host"
_exec_cmd "mkdir -p $share_images_dir"
_exec_cmd "echo $share_images_dir *\(rw,sync,no_root_squash\) > /etc/exports"
_exec_cmd "systemctl start nfs"
_exec_cmd "systemctl restart nfs"
_exec_cmd "systemctl status nfs"
_exit_on_error "Failed to start nfs service"
_log "mount src dir to destination host"
_exec_cmd "ssh root@$dst_host_ip mkdir -p $share_images_dir"
_exec_cmd "ssh root@$dst_host_ip umount $share_images_dir"
_exec_cmd "ssh root@$dst_host_ip mount -t nfs $src_host_ip:$share_images_dir $share_images_dir"
_exit_on_error "Failed to mount src dir to destination host"
_log "Install bridge-utils"
_exec_cmd "ssh root@$dst_host_ip yum install -y bridge-utils"
_exit_on_error "Failed to install bridge-utils on $dst_host_ip"
_log "Copy qemu-ifup/ifdown script to destination host"
_exec_cmd "scp /etc/qemu-if* $dst_host_ip:/etc/"
_exit_on_error "Failed to copy qemu-ifup/ifdown script to $dst_host_ip"
fi