-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrator.sh
134 lines (105 loc) · 3.68 KB
/
migrator.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# Banner
echo "
#######################################
# #
# Port Migration Script #
# #
#######################################
Mandatory Input Data:
--opt key should followed by either clone or noclone value
clone: will copy source IP and MAC addresses
noclone: will create a new port in target network / subnet with random IP and MAC addresses
1. Server Name
2. Destination Network in Sprut SDN
3. Destination Subnet in Sprut SDN
"
# User Input
function init {
read -p "Enter Server Name: " sname
read -p "Enter Sprut Network Name: " defnet
read -p "Enter Sprut Subnet Name: " defsubnet
echo "
Port information will be captured from $sname server"
echo "
New port will be created in $defnet network in $defsubnet subnet"
}
# Step 1.1 opt 1. Port Specifications
function capture_info_full {
port_output=$(openstack port list --server $sname -c id -c "MAC Address" -c "Fixed IP Addresses")
srcpid=$(echo "$port_output" | awk -F'|' 'NR==4{print $2}' | sed 's/ //g')
mcs=$(echo "$port_output" | awk -F'|' 'NR==4{print $3}' | sed 's/ //g')
ips=$(echo "$port_output" | awk -F'|' 'NR==4{print $4}' | grep -oP "ip_address='\K[^']+" )
echo "
Source Port ID is: $srcpid"
echo "
Source Port IP Addr is: $ips"
echo "
Source Port MAC Addr is: $mcs"
}
# Step 1.1 opt 2. Port Specifications
function capture_info_short {
port_output=$(openstack port list --server $sname -c id -c "MAC Address" -c "Fixed IP Addresses")
srcpid=$(echo "$port_output" | awk -F'|' 'NR==4{print $2}' | sed 's/ //g')
echo "
Source Port ID is: $srcpid"
}
# Step 1.2 Server ID
function capture_id {
server_output=$(openstack server show $sname)
servid=$(echo "$server_output" | awk -F'|' '/\| id / {print $3}' | sed 's/ //g')
echo "
Server ID is: $servid"
echo "
Step 1 complete (Source Port Data Captured)"
}
# Step 2 opt 1. Create Port in Sprut with source IP and MAC.
function sprut_port_mac_ip {
new_port_output=$(openstack port create --network $defnet --fixed-ip subnet=$defsubnet,ip-address=$ips --mac-address=$mcs "${sname}_migrated_port")
pmigid=$(echo "$new_port_output" | awk -F'|' '/\| id / {print $3}' | sed 's/ //g')
echo "
Step 2 complete (New Port Created)"
}
# Step 2 opt 2. Create Port in Sprut witout source IP and MAC.
function sprut_port_nomac_noip {
new_port_output=$(openstack port create --network $defnet --fixed-ip subnet=$defsubnet "${sname}_migrated_port")
pmigid=$(echo "$new_port_output" | awk -F'|' '/\| id / {print $3}' | sed 's/ //g')
echo "
Step 2 complete (New Port Created)"
}
# Step 3. Disconnect Existing Port From Server
function neutron_port_detach {
openstack server remove port $servid $srcpid
echo "
Step 3 complete (Source port disconnected From server $sname)"
}
# Step 4. Connect New Port To Server
function sprut_port_attach {
openstack server add port $servid $pmigid
echo "
The Port has been moved to Sprut SDN"
}
# Execute Flow
if [ "$1" != "--opt" ] || [ -z "$2" ]; then
echo "Error: --opt argument is mandatory and must be followed by a value <noclone> or <clone>."
exit 1
else
if [ "$2" == "noclone" ]; then
init
capture_info_short
capture_id
sprut_port_nomac_noip
neutron_port_detach
sprut_port_attach
elif [ "$2" == "clone" ]; then
init
capture_info_full
capture_id
sprut_port_mac_ip
neutron_port_detach
sprut_port_attach
else
echo "Error: unknown option has been provided."
exit 1
fi
fi