1 | #!/bin/bash |
---|
2 | # Program: |
---|
3 | # Functions for client_install.sh |
---|
4 | # Author: |
---|
5 | # Waue, Shunfa, Rock {waue, shunfa, rock}@nchc.org.tw |
---|
6 | # History: |
---|
7 | # 2010/05/20 Rock First release(0.1) |
---|
8 | |
---|
9 | # 正式版之後,記的將不必要的 echo 拿掉 |
---|
10 | function debug_info () { |
---|
11 | # if [ $? -eq 0 ]; then |
---|
12 | echo -e "\033[1;35;40m info - $1 \033[0m" |
---|
13 | # fi |
---|
14 | } |
---|
15 | |
---|
16 | function show_info () { |
---|
17 | # if [ $? -eq 0 ]; then |
---|
18 | echo -e "\033[1;32;40m $1 \033[0m" |
---|
19 | # fi |
---|
20 | } |
---|
21 | |
---|
22 | function load_default_lang(){ |
---|
23 | |
---|
24 | lang=$(locale | grep 'LANG=' | cut -d "=" -f2) |
---|
25 | |
---|
26 | # Default: source english |
---|
27 | . $Work_Path/lang/lang_en_US |
---|
28 | # if locale is zh then source chinese |
---|
29 | |
---|
30 | echo $lang | grep 'zh' >> /dev/null && source $Work_Path/lang/lang_zh_TW |
---|
31 | } |
---|
32 | |
---|
33 | # choose Language |
---|
34 | #function choose_lang(){ |
---|
35 | |
---|
36 | #lang=$(locale | grep 'LANG=' | cut -d "=" -f2) |
---|
37 | |
---|
38 | # Default: source english |
---|
39 | #. $Work_Path/lang/lang_en_US_client_install |
---|
40 | # if locale is zh then source chinese |
---|
41 | #echo $lang | grep 'zh' >> /dev/null && source $Work_Path/lang/lang_zh_TW_client_install |
---|
42 | |
---|
43 | # Ask language |
---|
44 | #echo -e "\n$choose_lang_1" |
---|
45 | #read -p "$choose_lang_2 " langChoice |
---|
46 | |
---|
47 | #if [ $langChoice == "2" ]; then |
---|
48 | # source $Work_Path/lang/lang_zh_TW_client_install |
---|
49 | #else |
---|
50 | # source $Work_Path/lang/lang_en_US_client_install |
---|
51 | #fi |
---|
52 | #} |
---|
53 | |
---|
54 | # 檢查執行這個程式的是否為root權限 |
---|
55 | function check_root(){ |
---|
56 | # 正式版後可拿掉此 echo |
---|
57 | debug_info "check_root" |
---|
58 | if [ $USER != "root" ]; then |
---|
59 | show_info "$check_root_1" |
---|
60 | exit |
---|
61 | fi |
---|
62 | # 正式版後可拿掉此 echo |
---|
63 | show_info "$check_root_2" |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | # 查出此主機的作業系統,以及版本 |
---|
68 | function check_systemInfo(){ |
---|
69 | debug_info "$check_sys_1" |
---|
70 | show_info "$check_sys_2" |
---|
71 | Linux_Distribution=$(lsb_release -a 2> /dev/null | grep "Distributor ID:" | awk '{print $3}') |
---|
72 | Linux_Version=$(lsb_release -a 2> /dev/null | grep "Release" | awk '{print $2}') |
---|
73 | show_info "$Linux_Distribution , $Linux_Version" |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | # 安裝需要的相依套件 (目前只支援 deb 套件的系統自動安裝,yum或其他套件系統的則需手動安裝) |
---|
78 | function install_packages(){ |
---|
79 | # deb 系列系統 |
---|
80 | debug_info "$install_pack_1" |
---|
81 | debug_info "$install_pack_2" |
---|
82 | if [ "$Linux_Distribution" == "Ubuntu" ] || [ "$Linux_Distribution" == "Debian" ] ;then |
---|
83 | echo -e "\n$install_pack_if_1\n" |
---|
84 | aptitude install -y expect ssh dialog |
---|
85 | # rpm 系列系統 |
---|
86 | elif [ "$Linux_Distribution" == "Fedora" ] || [ "$Linux_Distribution" == "CentOS" ] ;then |
---|
87 | show_info "$install_pack_if_2" |
---|
88 | else |
---|
89 | show_info "$install_pack_if_2" |
---|
90 | fi |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | # 檢查之前是否有安裝NutchEz |
---|
95 | # 目前先檢查是否有/opt/nutchez 這個資料夾即可 |
---|
96 | function check_nez_installed(){ |
---|
97 | debug_info "$check_nez_1" |
---|
98 | if [ -d "opt/nutchez" ]; then |
---|
99 | show_info "$check_nez_2" |
---|
100 | exit |
---|
101 | else |
---|
102 | show_info "$check_nez_3" |
---|
103 | fi |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | # 檢查是否有安裝sun java ,並檢查是否為jdk 1.6 以上版本 |
---|
108 | # 4種判斷可能性 (1)系統沒安裝 JAVA (2)系統有安裝JAVA,但非sun版本 |
---|
109 | # (3)系統有安裝但Sun Java 在非預設路徑下 (4)以正確安裝 Sun JAVA 預設路徑下 |
---|
110 | function check_sunJava(){ |
---|
111 | debug_info "$check_sunJava_1" |
---|
112 | debug_info "$check_sunJava_2" |
---|
113 | |
---|
114 | javaPath="/usr" |
---|
115 | yesno="no" |
---|
116 | choice="3" |
---|
117 | |
---|
118 | if [ -e $javaPath/bin/java ]; then |
---|
119 | JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)") |
---|
120 | JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \ |
---|
121 | awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2) |
---|
122 | |
---|
123 | if [ "$JAVA_org" == "" ]; then |
---|
124 | show_info "$check_sunJava_if_1" |
---|
125 | show_info "$check_sunJava_if_2" |
---|
126 | show_info "$check_sunJava_if_3" |
---|
127 | read -p "$check_sunJava_if_4" choice |
---|
128 | case $choice in |
---|
129 | "1") |
---|
130 | show_info "$check_sunJava_if_5" |
---|
131 | exit |
---|
132 | ;; |
---|
133 | "2") |
---|
134 | read -p "$check_sunJava_if_6" javaPath |
---|
135 | ;; |
---|
136 | "*") |
---|
137 | exit |
---|
138 | ;; |
---|
139 | esac |
---|
140 | |
---|
141 | if [ $choice == "2" ]; then |
---|
142 | JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)") |
---|
143 | JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \ |
---|
144 | awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2) |
---|
145 | |
---|
146 | if [ "$JAVA_org" == "" ]; then |
---|
147 | show_info "$check_sunJava_if_7" |
---|
148 | exit |
---|
149 | fi |
---|
150 | fi |
---|
151 | fi |
---|
152 | |
---|
153 | large16=$(echo "$JAVA_version >= 1.6" | bc) |
---|
154 | if [ "${large16}" == 0 ]; then |
---|
155 | show_info "$check_sunJava_if_8" |
---|
156 | exit |
---|
157 | fi |
---|
158 | |
---|
159 | show_info "$check_sunJava_if_9" |
---|
160 | else |
---|
161 | show_info "$check_sunJava_if_10" |
---|
162 | exit |
---|
163 | fi |
---|
164 | |
---|
165 | unset JAVA_org |
---|
166 | unset JAVA_version |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | # 檢查是否有安裝openssh, openssh-server |
---|
171 | function check_ssh(){ |
---|
172 | debug_info "$check_ssh_1" |
---|
173 | if [ -e /usr/bin/ssh ]; then |
---|
174 | show_info "$check_ssh_2" |
---|
175 | else |
---|
176 | show_info "$check_ssh_3" |
---|
177 | exit |
---|
178 | fi |
---|
179 | |
---|
180 | if [ -e /usr/sbin/sshd ]; then |
---|
181 | show_info "$check_ssh_4" |
---|
182 | else |
---|
183 | show_info "$check_ssh_5" |
---|
184 | exit |
---|
185 | fi |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | # 檢查是否有安裝dialog |
---|
190 | function check_dialog(){ |
---|
191 | debug_info "$check_dialog_1" |
---|
192 | if [ -e /usr/bin/dialog ]; then |
---|
193 | show_info "$check_dialog_2" |
---|
194 | else |
---|
195 | show_info "$check_dialog_3" |
---|
196 | exit |
---|
197 | fi |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | # scp nutchuser@master_ip:~ 把.ssh/目錄複製下來 |
---|
202 | # 當使用者輸入nutchuser 密碼時,將此密碼紀錄到Nutchuser_Passwd |
---|
203 | # 此步驟若無法連到 master 則跳出 |
---|
204 | function scp_master_nutchuser_sshkey(){ |
---|
205 | debug_info "$scp_sshkey_d1" |
---|
206 | debug_info "$scp_sshkey_d2" |
---|
207 | mkdir -p /home/nutchuser/.ssh/ |
---|
208 | rm -fr /home/nutchuser/.ssh/* |
---|
209 | unset Nutchuser_Passwd2 |
---|
210 | |
---|
211 | debug_info "$scp_sshkey_d3" |
---|
212 | expect -c "spawn scp -r -o StrictHostKeyChecking=no nutchuser@$1:~/.ssh /home/nutchuser/ |
---|
213 | set timeout 1 |
---|
214 | sleep 2 |
---|
215 | expect \"*: \" { send \"$Nutchuser_Passwd\r\" } |
---|
216 | expect \"*: \" { send_user \"$scp_sshkey_expect_1\" } |
---|
217 | expect eof" |
---|
218 | |
---|
219 | if [ -e "/home/nutchuser/.ssh/authorized_keys" ]; then |
---|
220 | show_info "$scp_sshkey_s1" |
---|
221 | else |
---|
222 | show_info "$scp_sshkey_s2" |
---|
223 | exit |
---|
224 | fi |
---|
225 | ssh-add /home/nutchuser/.ssh/id_rsa |
---|
226 | debug_info "$scp_sshkey_d4" |
---|
227 | chown -R nutchuser:nutchuser /home/nutchuser/.ssh |
---|
228 | } |
---|
229 | |
---|
230 | # 新增nutchuser 帳號時用 Nutchuser_Passwd 當密碼 |
---|
231 | function creat_nutchuser_account(){ |
---|
232 | debug_info "$create_nutchuser_d1" |
---|
233 | while [ "$Nutchuser_Passwd" != "$Nutchuser_Passwd2" ] |
---|
234 | do |
---|
235 | echo -e "\n" |
---|
236 | read -sp "$create_nutchuser_1" Nutchuser_Passwd |
---|
237 | echo |
---|
238 | read -sp "$create_nutchuser_2" Nutchuser_Passwd2 |
---|
239 | echo |
---|
240 | if [ "$Nutchuser_Passwd" == "$Nutchuser_Passwd2" ]; then |
---|
241 | show_info "$create_nutchuser_3" |
---|
242 | else |
---|
243 | show_info "$create_nutchuser_4" |
---|
244 | fi |
---|
245 | done |
---|
246 | unset Nutchuser_Passwd2 |
---|
247 | |
---|
248 | if [ $(cat /etc/passwd | grep nutchuser) ]; then |
---|
249 | show_info "$create_nutchuser_s1" |
---|
250 | expect -c "spawn passwd nutchuser |
---|
251 | set timeout 1 |
---|
252 | expect \"*: \" |
---|
253 | send \"$Nutchuser_Passwd\r\" |
---|
254 | expect \"*: \" |
---|
255 | send \"$Nutchuser_Passwd\r\" |
---|
256 | expect eof" |
---|
257 | else |
---|
258 | show_info "$create_nutchuser_s2" |
---|
259 | useradd -m nutchuser -s /bin/bash |
---|
260 | expect -c "spawn passwd nutchuser |
---|
261 | set timeout 1 |
---|
262 | expect \"*: \" |
---|
263 | send \"$Nutchuser_Passwd\r\" |
---|
264 | expect \"*: \" |
---|
265 | send \"$Nutchuser_Passwd\r\" |
---|
266 | expect eof" |
---|
267 | fi |
---|
268 | } |
---|
269 | |
---|
270 | # 用scp 複製 master 的設定與安裝資料 |
---|
271 | # 目前僅需做到能無礙的複製遠端的/opt/nutchez/到local的/opt/ |
---|
272 | function scp_packages(){ |
---|
273 | debug_info "$scp_packages_d1" |
---|
274 | mkdir /opt/nutchez |
---|
275 | mkdir /var/nutchez |
---|
276 | mkdir /var/nutchez/logs |
---|
277 | mkdir /home/nutchuser/nutchez |
---|
278 | mkdir /home/nutchuser/nutchez/source |
---|
279 | mkdir /home/nutchuser/nutchez/system |
---|
280 | chmod 777 /opt/nutchez |
---|
281 | debug_info "$scp_packages_d2" |
---|
282 | chown -R nutchuser:nutchuser /opt/nutchez |
---|
283 | chown -R nutchuser:nutchuser /var/nutchez |
---|
284 | chown -R nutchuser:nutchuser /home/nutchuser/nutchez |
---|
285 | chmod 755 /opt/nutchez |
---|
286 | debug_info "$scp_packages_d3" |
---|
287 | if [ -e "NutchezForClientOf_$Master_IP_Address.tar.gz" ];then |
---|
288 | mv NutchezForClientOf_$Master_IP_Address.tar.gz /home/nutchuser/nutchez/source |
---|
289 | fi |
---|
290 | if [ ! -e "/home/nutchuser/nutchez/source/NutchezForClientOf_$Master_IP_Address.tar.gz" ];then |
---|
291 | su nutchuser -c "scp -r -o StrictHostKeyChecking=no nutchuser@$1:/home/nutchuser/nutchez/source/NutchezForClientOf_$Master_IP_Address.tar.gz /home/nutchuser/nutchez/source" |
---|
292 | fi |
---|
293 | cp -r lang /home/nutchuser/nutchez/system |
---|
294 | cp client_remove lang_link /home/nutchuser/nutchez/system |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | function install_nutch_package(){ |
---|
299 | debug_info "$install_nutch_package_d1" |
---|
300 | tar -zxvf /home/nutchuser/nutchez/source/NutchezForClientOf_$Master_IP_Address.tar.gz -C /opt/nutchez |
---|
301 | cp /etc/hosts /home/nutchuser/nutchez/system/hosts.bak |
---|
302 | sed -i '1a '$Master_IP_Address' '$Master_Hostname'' /etc/hosts |
---|
303 | |
---|
304 | #/opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode |
---|
305 | #/opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker |
---|
306 | } |
---|
307 | |
---|
308 | function recall_hostname_ip(){ |
---|
309 | debug_info "$recall_hostname_ip_d1" |
---|
310 | net_interfaces=$(ifconfig | grep ^eth | cut -d " " -f1) |
---|
311 | net_nu=$(echo $net_interfaces | wc -w) |
---|
312 | # 若只有一個 eth 時 |
---|
313 | if [ "$net_nu" == "1" ]; then |
---|
314 | # ifconfig $net_interfaces | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1 |
---|
315 | net_address=$(ifconfig $net_interfaces | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1) |
---|
316 | net_MacAddr=$(ifconfig $net_interfaces | grep 'HW' | sed 's/^.*HWaddr //g') |
---|
317 | show_info "$recall_hostname_ip_1 $net_address" |
---|
318 | show_info "$recall_hostname_ip_2 $net_MacAddr" |
---|
319 | |
---|
320 | # 若有多個 eth 時 |
---|
321 | else |
---|
322 | declare -i i=1 |
---|
323 | show_info "$recall_hostname_ip_3" |
---|
324 | |
---|
325 | for net in $net_interfaces |
---|
326 | do |
---|
327 | echo "($i) $net $(ifconfig $net | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)" |
---|
328 | i=i+1 |
---|
329 | done |
---|
330 | |
---|
331 | read -p "$recall_hostname_ip_4" net_choice |
---|
332 | if [ -z $net_choice ]; then |
---|
333 | net_choice=1 |
---|
334 | fi |
---|
335 | |
---|
336 | show_info "choice is $net_choice" |
---|
337 | net_interface=$(echo $net_interfaces | cut -d " " -f $net_choice) |
---|
338 | # config $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1 |
---|
339 | net_address=$(ifconfig $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1) |
---|
340 | net_MacAddr=$(ifconfig $net_interface | grep 'HW' | sed 's/^.*HWaddr //g') |
---|
341 | show_info "$recall_hostname_ip_1 $net_address" |
---|
342 | show_info "$recall_hostname_ip_2 $net_MacAddr" |
---|
343 | fi |
---|
344 | |
---|
345 | debug_info "$recall_hostname_ip_d2" |
---|
346 | su nutchuser -c "ssh nutchuser@$1 echo $net_address $(hostname) $net_MacAddr \>\> ~/nutchez/system/nutch_nodes" |
---|
347 | |
---|
348 | #su nutchuser -c expect -c "spawn ssh nutchuser@$1 |
---|
349 | #set timeout 1 |
---|
350 | #expect \"*\" |
---|
351 | #send \"echo $net_address $(hostname) >> /home/nutchuser/nutch_nodes\r\" |
---|
352 | #expect \"*\" |
---|
353 | #send \"exit\"" |
---|
354 | } |
---|