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