forked from xuweibj/openbmc_simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator
executable file
·198 lines (174 loc) · 5.14 KB
/
simulator
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
usage="Usage:\n
\t-c: Cleanup IPs configuration. If not set it, will setup openbmc simulator environment and start simulator.\n
\t-d: Delay response type, supported parameter: random, constant. \n
\t-t: Delay response time, must be used with -d. If delay type is random, the parameter is the max value. The delay time will be random between 0 and input pamameter.\n
\t-n: NIC name which want to configure IPs on.\n
\t-r: IPs want to configure on NIC. The format is as '10.[1|{1.10}].[1|{1..200}].[1|{1..200}]'.\n
\n
Example:\n
\t./simulator -n eth0 -r 10.1.1.{1..10}\n
\t./simulator -c -n eth0 -r 10.1.1.{1..10}\n
\t./simulator -d random -t 10\n
\n
Note: When setup if no -n or -r input, the simulator will not configure IPs\n
'-d/-t' could be used when start simulator\n
If use option with -r, please put -r at the last\n"
while getopts "hd:t:cn:r:" opt
do
case $opt in
h)
echo -e $usage
exit 0
;;
d)
delay_type=$OPTARG
;;
t)
delay_time=$OPTARG
;;
c)
flag_c=true
;;
n)
nic=$OPTARG
;;
r)
ip_s=$OPTARG
;;
?)
exit 1
;;
esac
done
process=`ps aux | grep "simulation" | grep "python" | awk -F ' ' '{print $2}'`
if [ $process ]; then
echo "Kill existed simulator process"
kill $process
fi
if [[ $* =~ "-c" ]]; then
flag_c=true
fi
if [[ $* =~ "-n" ]] && [ -z $nic ]; then
echo "Please input as example format. './simulator [-c] -n <NIC> -r <IP range>'"
exit 0
fi
if [ $nic ] && [ $ip_s ]; then
nic_info=`ip -4 -o a | grep $nic | awk '{print $4}' | head -n 1`
if [ -z $nic_info ]; then
echo "Invalid NIC name"
exit 1
fi
not_ip=1
if [[ $ip_s =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip_s)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
not_ip=$?
fi
if [ $not_ip != 0 ]; then
echo "Invalid IP. Supported format: 10.[1|{1.10}].[1|{1..200}].[1|{1..200}]"
exit 1
fi
for i in "$@";do
if [ $i = $ip_s ]; then
ip_flag=true
fi
if [ $ip_flag ] && [[ $i =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
ip_range=("${ip_range[@]}" $i)
fi
done
elif [ $flag_c ]; then
echo "No IP configuration to cleanup ---- No NIC or IPs input"
exit 0
fi
if [ $flag_c ]; then
echo "Start to cleanup IP configuration"
for i in ${ip_range[@]}; do
ip addr del $i dev $nic 1>/dev/null 2>&1
done
echo "DONE"
exit
else
echo "Start to setup openbmc simulator environment"
fi
if [ $delay_type ] && [ $delay_time ]; then
echo $delay_time | grep 'm' > /dev/null
if [ $? != 0 ]; then
delay_time=$(echo "0m$delay_time")
fi
minute=`echo "$delay_time" | awk -F 'm' '{print $1}'`
second=`echo "$delay_time" | awk -F 'm' '{print $2}'`
delay_second=$((minute*60+second))
delay_string="-d $delay_type -t $delay_second"
else
echo "No delay type or delay time, will not delay response"
fi
os=`cat /etc/*release*`
basepath=$(cd `dirname $0`; pwd)
if [[ $os =~ "Red Hat" ]] || [[ $os =~ "suse" ]]; then
yum install python-devel.ppc64le --nogpgcheck -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install python-devel Failed."
exit 1
fi
yum install gcc --nogpgcheck -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install gcc Failed."
exit 1
fi
epel_check=`rpm -qa | grep epel`
if [ -z $epel_check ]; then
url="https://dl.fedoraproject.org/pub/epel/7/ppc64le/Packages/e/"
epel_pkg=`curl -s $url | grep "epel-release" | awk -F '=' '{print $4}' | awk -F '>' '{print $1}' | sed 's/\"//g'`
epel_url=${url}${epel_pkg}
yum install $epel_url --nogpgcheck -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install epel Failed."
exit 1
fi
fi
yum install python2-pip.noarch --nogpgcheck -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install python2-pip Failed."
exit 1
fi
yum install nc -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install nc Failed."
exit 1
fi
elif [[ $os =~ "ubuntu" ]]; then
apt-get install python-pip -y 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install python-pip Failed."
exit 1
fi
fi
pip install eventlet 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install eventlet Failed."
exit 1
fi
pip install pecan 1>/dev/null 2>&1
if [ $? != 0 ]; then
echo "Install pecan Failed."
exit 1
fi
nic_num=0
ip_num=${#ip_range[@]}
if [ $ip_num != 0 ]; then
echo "Start to configrue $ip_num IPs on NIC $nic"
fi
for i in ${ip_range[@]}; do
ifconfig $nic:$nic_num $i up
if [ $? != 0 ]; then
echo "Configure IP $i on NIC $nic Failed"
fi
((nic_num++))
done
nohup python -u $basepath/simulation.py $delay_string > $basepath/debug.log 2>&1 &
echo "Simulator is Running"