source: nutchez-0.2/src/shell/install_func.sh @ 218

Last change on this file since 218 was 218, checked in by waue, 14 years ago

更改 nutchez-0.2pack 的版本

  • Property svn:executable set to *
File size: 15.6 KB
Line 
1#!/bin/bash
2source $Work_Path/lang_link
3
4# shell檔及壓縮檔在同一目錄中
5####### 環境變數section###########
6User_HOME=/home/nutchuser/nutchez
7NutchEZ_HOME=/opt/nutchez
8Nutch_HOME=$NutchEZ_HOME/nutch
9Tomcat_HOME=$NutchEZ_HOME/tomcat
10Index_DB=$User_HOME/search
11Admin_email=nutchuser@nutch
12Start_PATH=`pwd`
13MasterIP_Address=`/sbin/ifconfig eth0 | grep 'inet addr' |  sed 's/^.*addr://g' | sed 's/Bcast.*$//g' | sed 's/ .*// '` 
14net_MacAddr=`/sbin/ifconfig eth0 | grep 'HW' | sed 's/^.*HWaddr //g'`
15
16######function section#######
17
18##########  echo function  ##########
19function debug_info () {
20  if [ $? -eq 0 ]; then
21    echo -e "\033[1;35;40m info - $1 \033[0m"
22  fi
23}
24
25
26function show_info () {
27  if [ $? -eq 0 ]; then
28    echo -e "\033[1;32;40m $1 \033[0m"
29  fi
30}
31##########end echo function ##########
32
33function choose_lang(){
34
35lang=$(locale | grep 'LANG=' | cut -d "=" -f2)
36
37# Default: source english
38. $Work_Path/install_lang.en
39# if locale is zh then source chinese
40echo $lang | grep 'zh' >> /dev/null && source $Work_Path/install_lang.zh
41
42# Ask language
43echo -e "\n$choose_lang_1"
44read -p "$choose_lang_2 " langChoice
45
46if [ $langChoice == "2" ]; then
47    source $Work_Path/install_lang.zh
48else
49    source $Work_Path/install_lang.en
50fi
51}
52
53function check_root(){
54  debug_info "check_root"
55  if [ $USER != "root" ]; then
56    show_info "$MI_check_root_1"
57    exit
58  fi
59  show_info "$MI_check_root_2"
60}
61
62function check_systemInfo(){
63  debug_info "$MI_check_sys_1"
64  show_info "$MI_check_sys_2"
65  Linux_Distribution=$(lsb_release -a 2> /dev/null | grep "Distributor ID:" | awk '{print $3}')
66  Linux_Version=$(lsb_release -a 2> /dev/null | grep "Release" | awk '{print $2}')
67  show_info "$Linux_Distribution , $Linux_Version"
68}
69
70function install_packages(){
71  # deb 系列系統
72  debug_info "$MI_install_pack_1"
73  debug_info "$MI_install_pack_2"
74  if [ "$Linux_Distribution" == "Ubuntu" ] || [ "$Linux_Distribution" == "Debian" ] ;then
75    echo -e "\n$MI_install_pack_if_1\n"
76    aptitude install -y expect ssh dialog
77  # rpm 系列系統
78  elif [ "$Linux_Distribution" == "Fedora" ] || [ "$Linux_Distribution" == "CentOS" ] ;then
79    show_info "$MI_install_pack_if_2"
80  else
81    show_info "$MI_install_pack_if_2"
82  fi
83}
84
85function unzip_nV2_pack(){
86  local pac_name=nutchez-0.2pack-current.tar.gz
87  if [ ! -d "./package" ];then
88    mkdir ./package
89  fi
90  if [ ! -e "./package/$pac_name" ];then
91  wget "http://trac.nchc.org.tw/cloud/export/217/nutchez-0.2/src/package/$pac_name"
92  echo wget "http://trac.nchc.org.tw/cloud/export/217/nutchez-0.2/src/package/$pac_name"
93    if [ -z $? ];then
94  mv $pac_name ./package
95  echo "movied"
96    else
97  show_info "$pac_name not found, installation was not finished!"
98  exit 8
99    fi
100  fi
101  #tar -zxvf ./package/$pac_name -C /opt/
102}
103
104function check_nez_installed(){
105  debug_info "$MI_check_nez_1"
106  if [ -d "/opt/nutchez" ]; then
107    show_info "$MI_check_nez_2"
108    exit
109  else
110    show_info "$MI_check_nez_3"
111  fi
112}
113
114function check_sunJava(){
115  debug_info "$MI_check_sunJava_1"
116  debug_info "$MI_check_sunJava_2"
117
118  javaPath="/usr"
119  yesno="no"
120  choice="3"
121
122  if [ -e $javaPath/bin/java ]; then
123    JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)")
124    JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \
125    awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2)
126
127  if [ "$JAVA_org" == "" ]; then
128    show_info "$MI_check_sunJava_if_1"
129    show_info "$MI_check_sunJava_if_2"
130    show_info "$MI_check_sunJava_if_3"
131    read -p "$MI_check_sunJava_if_4" choice
132    case $choice  in
133      "1")
134        show_info "$MI_check_sunJava_if_5"
135        exit
136        ;;
137      "2")
138        read -p "$MI_check_sunJava_if_6" javaPath
139        ;;
140        "*")
141        exit
142        ;;
143        esac
144
145        if [ $choice == "2" ]; then
146          JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)")
147          JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \
148          awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2)
149
150          if [ "$JAVA_org" == "" ]; then
151            show_info "$MI_check_sunJava_if_7"
152            exit
153            fi
154          fi
155        fi
156
157      large16=$(echo "$JAVA_version >= 1.6" | bc)
158      if [ "${large16}" == 0 ]; then
159        show_info "$MI_check_sunJava_if_8"
160        exit
161      fi
162
163      show_info "$MI_check_sunJava_if_9"
164  else
165    show_info "$MI_check_sunJava_if_10"
166    exit
167  fi
168
169  unset JAVA_org
170  unset JAVA_version
171}
172
173# 檢查是否有安裝openssh, openssh-server
174function check_ssh(){
175  debug_info "$MI_check_ssh_1"
176  if [ -e /usr/bin/ssh ]; then
177    show_info "$MI_check_ssh_2"
178  else
179    show_info "$MI_check_ssh_3"
180    exit
181  fi
182
183  if [ -e /usr/sbin/sshd ]; then
184    show_info "$MI_check_ssh_4"
185  else
186    show_info "$MI_check_ssh_5"
187    exit
188  fi
189}
190
191
192# 檢查是否有安裝dialog
193function check_dialog(){
194  debug_info "$MI_check_dialog_1"
195  if [ -e /usr/bin/dialog ]; then
196    show_info "$MI_check_dialog_2"
197  else
198    show_info "$MI_check_dialog_3"
199    exit
200  fi
201}
202
203check_info () {
204  check_root
205  check_systemInfo
206  install_packages
207  check_nez_installed
208  check_sunJava
209  check_ssh
210  check_dialog
211}
212
213function set_install_information () { 
214  set_nutchuser_passwd
215  select_eth
216  MasterIP_Address=$net_address
217}
218
219function set_nutchuser_passwd () {
220  read -sp "$MI_set_nutchuser_passwd_echo_1" Nutchuser_Passwd
221# read -sp "Please enter nutchuser's password :  " Nutchuser_Passwd
222  echo -e "\n"
223  read -sp "$MI_set_nutchuser_passwd_echo_2" Nutchuser_Passwd2
224# read -sp "Please enter nutchuser's password again:  " Nutchuser_Passwd2
225  echo -e "\n"
226  if [ $Nutchuser_Passwd != $Nutchuser_Passwd2 ]; then
227    set_nutchuser_passwd
228  fi
229}
230
231# 新增nutchuser 帳號時用 Nutchuser_Passwd 當密碼
232function creat_nutchuser_account(){
233  debug_info "$create_nutchuser_d1"
234  while [ "$Nutchuser_Passwd" != "$Nutchuser_Passwd2" ]
235  do
236      echo -e "\n"
237      read -sp "$create_nutchuser_1" Nutchuser_Passwd
238      echo 
239      read -sp "$create_nutchuser_2" Nutchuser_Passwd2
240      echo 
241        if [ "$Nutchuser_Passwd" == "$Nutchuser_Passwd2" ]; then
242          show_info "$create_nutchuser_3"
243        else
244          show_info "$create_nutchuser_4"
245        fi
246  done                                                                                                                         
247  unset Nutchuser_Passwd2
248
249  if [ $(cat /etc/passwd | grep nutchuser) ]; then
250    show_info "$create_nutchuser_s1"
251    expect -c "spawn passwd nutchuser
252    set timeout 1
253    expect \"*: \"
254    send \"$Nutchuser_Passwd\r\"
255    expect \"*: \"
256    send \"$Nutchuser_Passwd\r\"
257    expect eof"
258    else
259      show_info "$create_nutchuser_s2"
260      useradd -m nutchuser -s /bin/bash
261      expect -c "spawn passwd nutchuser
262      set timeout 1
263      expect \"*: \"
264      send \"$Nutchuser_Passwd\r\"
265      expect \"*: \"
266      send \"$Nutchuser_Passwd\r\"
267      expect eof"
268  fi
269}
270
271function select_eth () {
272  net_interfaces=$(ifconfig | grep ^eth | cut -d " " -f1)
273  net_nu=$(echo $net_interfaces | wc -w)
274
275  # 若只有一個 eth 時
276  if [ "$net_nu" == "1" ]; then
277    net_address=$(ifconfig $net_interfaces | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)
278    net_MacAddr=$(ifconfig $net_interfaces | grep 'HW' | sed 's/^.*HWaddr //g')
279
280  # 若有多個 eth 時
281  else
282    declare -i i=1
283    show_info "$MI_select_eth_echo_1"
284#   show_info  "\nSystem have multiple network device, which network use for this machine: "
285
286    for net in $net_interfaces
287      do
288        show_info "($i$net  $(ifconfig $net | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)"
289        i=i+1
290      done
291      read -p "$MI_select_eth_echo_2" net_choice
292#     read -p "Please choice(1/2/3...): " net_choice
293    if [ -z $net_choice ]; then
294      net_choice=1
295    fi
296
297    show_info "$MI_select_eth_echo_3 $net_choice"
298#   show_info "Your choice is $net_choice"
299    net_interface=$(echo $net_interfaces | cut -d " " -f $net_choice)
300    #ifconfig $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1
301    net_address=$(ifconfig $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)
302    net_MacAddr=$(ifconfig $net_interface | grep 'HW' | sed 's/^.*HWaddr //g')
303
304    show_info "$MI_select_eth_echo_4 $net_address"
305#   show_info "net_address is $net_address"
306    show_info "$MI_select_eth_echo_5 $net_MacAddr"
307#   show_info "net_MacAddr is $net_MacAddr"
308  fi
309}
310
311
312function show_master_info () {
313  show_info "$MI_show_master_info_echo_1 $MasterIP_Address"
314  show_info "$MI_show_master_info_echo_2 $net_MacAddr"
315
316#  show_info "The Master IP Address is $MasterIP_Address"
317#  show_info "The Master MacAddr is $net_MacAddr"
318}
319
320function make_ssh_key () {
321  debug_info "$MI_make_ssh_key_echo_1"
322# debug_info "Make ssh key(begin...)"
323  su nutchuser -c 'ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""'
324  su nutchuser -c "cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys"
325  su nutchuser -c "ssh-add /home/nutchuser/.ssh/id_rsa"
326  debug_info "$MI_make_ssh_key_echo_2"
327# debug_info "Make ssh key(done!)"
328}
329
330
331function set_haoop-site () {
332  debug_info "$MI_set_haoop_site_echo_1"
333# debug_info "set hadoop-site.xml(begin...)"
334  cd $Nutch_HOME/conf/
335  cat > hadoop-site.xml << EOF
336<configuration>
337  <property>
338    <name>fs.default.name</name>
339    <value>hdfs://$MasterIP_Address:9000</value>
340  </property>
341  <property>
342    <name>mapred.job.tracker</name>
343    <value>$MasterIP_Address:9001</value>
344  </property>
345  <property>
346    <name>hadoop.tmp.dir</name>
347    <value>/var/nutchez/nutch-nutchuser</value>
348  </property>
349</configuration>
350EOF
351  debug_info "$MI_set_haoop_site_echo_2"
352# debug_info "set hadoop-site.xml(done!)"
353}
354
355# 修改nutch-site.xml中-http.agent.url, http.agent.email
356function set_nutch-site () {
357  debug_info "$MI_set_nutch_site_echo_1"
358# debug_info "set nutch-site.xml(begin...)"
359  Line_NO=`cat $Nutch_HOME'/conf/nutch-site.xml' | grep -n 'http.agent.url' | sed 's/:.*//g'`
360  debug_info "$MI_set_nutch_site_echo_2"
361# debug_info "debug...http.agent.url line number = $Line_NO..."
362  sed -i ''$((Line_NO+1))'d' $Nutch_HOME/conf/nutch-site.xml
363  debug_info "$MI_set_nutch_site_echo_3"
364# debug_info "debug...edit http.agent.url delete line $((Line_NO+1))..."
365  sed -i ''$Line_NO'a <value>'$MasterIP_Address'</value>' $Nutch_HOME/conf/nutch-site.xml
366  debug_info "$MI_set_nutch_site_echo_4"
367# debug_info "debug...edit http.agent.url done..."
368
369  Line_NO=`cat $Nutch_HOME'/conf/nutch-site.xml' | grep -n 'http.agent.email' | sed 's/:.*//g'`
370  debug_info "$MI_set_nutch_site_echo_5"
371# debug_info "debug...http.agent.email line number = $Line_NO..."
372
373  sed -i ''$((Line_NO+1))'d' $Nutch_HOME/conf/nutch-site.xml
374  debug_info "$MI_set_nutch_site_echo_6"
375# debug_info "debug...edit http.agent.email delete line $((Line_NO+1))..."
376  sed -i ''$Line_NO'a <value>'$Admin_email'</value>' $Nutch_HOME/conf/nutch-site.xml
377  debug_info "$MI_set_nutch_site_echo_7"
378# debug_info "debug...edit http.agent.email done..."
379  debug_info "$MI_set_nutch_site_echo_8"
380# debug_info "set nutch-site.xml(done!)"
381}
382
383function format_HDFS () {
384  debug_info "$MI_format_HDFS_echo_1"
385  su nutchuser -c "$Nutch_HOME/bin/hadoop namenode -format"
386  debug_info "$MI_format_HDFS_echo_2"
387}
388
389function start_up_NutchEZ (){
390  debug_info "$MI_start_up_NutchEZ_echo_1"
391# debug_info "start up NutchEZ..."
392  su nutchuser -c "$Nutch_HOME/bin/start-all.sh"
393}
394
395
396function set_hosts () {
397  debug_info "$MI_set_hosts_echo_1"
398  cp /etc/hosts /home/nutchuser/nutchez/system/hosts.bak
399  Line_NO=`cat /etc/hosts | grep -n $(hostname) | sed 's/:.*//g'`
400  content=$(cat /etc/hosts | awk 'NR=='$Line_NO'{printf "# " ; print}' )
401  sed -i ""$Line_NO"c $content" /etc/hosts
402  sed -i '1i '$MasterIP_Address' '$(hostname)'' /etc/hosts
403}
404
405function Install_Nutch () {
406  debug_info "$MI_install_Nutch_echo_1 $MasterIP_Address "
407# debug_info "MasterIP_Address=$MasterIP_Address"
408  debug_info "$MI_install_Nutch_echo_2 $(hostname)"
409# debug_info "Master_Hostname=$(hostname)"
410  su nutchuser -c "echo $net_address $(hostname) $net_MacAddr \>\> ~/nutchez/system/nutch_nodes"
411  set_hosts
412  set_haoop-site
413  set_nutch-site
414}
415
416
417function client_PassMasterIPAddr () {
418  cd $Start_PATH
419  Line_NO=`cat client_install | grep -n '# Master IP here' | sed 's/:.*//g'`
420  debug_info "$MI_client_PassMasterIPAddr_echo_1"
421# debug_info "debug...Master IP here line number = $Line_NO..."
422  sed -i ''$((Line_NO+1))'d' client_install
423  debug_info "$MI_client_PassMasterIPAddr_echo_2"
424# debug_info "debug...edit Master IP at line $((Line_NO+1))..."
425  sed -i ''$Line_NO'a Master_IP_Address='$MasterIP_Address'' client_install
426  debug_info "$MI_client_PassMasterIPAddr_echo_3"
427# debug_info "edit client_install done..."
428}
429
430
431function client_PassMaster_Hostname () {
432  cd $Start_PATH
433  Line_NO=`cat client_install | grep -n '# Master Hostname here' | sed 's/:.*//g'`
434  debug_info "$MI_client_PassMaster_Hostname_echo_1"
435# debug_info "debug...Master hostname here line number = $Line_NO..."
436  sed -i ''$((Line_NO+1))'d' client_install
437  debug_info "$MI_client_PassMaster_Hostname_echo_2"
438# debug_info "debug...edit Master Hostname at line $((Line_NO+1))..."
439  sed -i ''$Line_NO'a Master_Hostname='$(hostname)'' client_install
440  debug_info "$MI_client_PassMaster_Hostname_echo_3"
441# debug_info "edit client_install done..."
442}
443
444function client_PassMasterIPAddr_for_Remove () {
445  cd $Start_PATH
446  Line_NO=`cat client_remove.sh | grep -n "# Master IP here" | sed 's/:.*//g'`
447  sed -i ''$((Line_NO+1))'d' client_remove.sh
448  sed -i ''$Line_NO'a Master_IP_Address='$MasterIP_Address'' client_remove.sh
449}
450
451
452function make_client_install () {
453  # 建立資料夾(用來存放client的安奘檔)
454  su nutchuser -c "mkdir $User_HOME/source"
455
456  # 將Master_IP_Address給client
457  # 打包安裝目錄(不含tomcat)
458 
459  debug_info "$MI_make_client_install_echo_1"
460# debug_info "function make_client_install..."
461
462  client_PassMasterIPAddr
463  client_PassMaster_Hostname
464  client_PassMasterIPAddr_for_Remove
465  cd /opt/nutchez/
466  su nutchuser -c "tar -cvzf NutchezForClientOf_$MasterIP_Address.tar.gz  nutch"
467 
468  # 複製檔案至$User_HOME/source目錄下
469  mv NutchezForClientOf_$MasterIP_Address.tar.gz /home/nutchuser/nutchez/source
470  cp $Start_PATH/client_install $Start_PATH/client_install_func.sh $Start_PATH/client_remove.sh $Start_PATH/lang_link  /home/nutchuser/nutchez/source
471  cp -r $Start_PATH/lang /home/nutchuser/nutchez/source
472  cp -r $Start_PATH/lang /home/nutchuser/nutchez/system
473  cp $Start_PATH/nutchez $Start_PATH/lang_link $Start_PATH/add_hosts $Start_PATH/duplicate_del $Start_PATH/master_remove.sh /home/nutchuser/nutchez/system
474
475#  cp $Start_PATH/client_install $Start_PATH/client_install /home/nutchuser/nutchez/source
476#  cp $Start_PATH/client_install $Start_PATH/client_remove.sh /home/nutchuser/nutchez/source
477#  cp $Start_PATH/client_install $Start_PATH/lang* /home/nutchuser/nutchez/source
478}
479
480function start_up_tomcat () {
481  debug_info "$MI_start_up_tomcat_echo_1"
482# debug_info "start up tomcat..."
483
484  i=10
485  debug_info "$MI_start_up_tomcat_echo_2"
486  until [ $i -lt 1 ]
487    do
488      sleep 1s
489      i=`expr $i - 1`
490    done
491  su nutchuser -c "$Tomcat_HOME/bin/startup.sh"
492  debug_info "$MI_start_up_tomcat_echo_3"
493# debug_info "tomcat has been started..."
494}
495
496###最後再整理###
497# client簡易步驟
498function client_install_commands () {
499  show_info "$MI_client_install_commands_echo_1"
500  show_info "$MI_client_install_commands_echo_2"
501  show_info "$MI_client_install_commands_echo_3"
502  show_info "$MI_client_install_commands_echo_4"
503  show_info "$MI_client_install_commands_echo_5"
504  show_info "$MI_client_install_commands_echo_6"
505  show_info "$MI_client_install_commands_echo_7"
506
507#  show_info "Client Install Command as Follows:"
508#  show_info "cd ~"
509#  show_info "mkdir nutchez_client_install"
510#  show_info "cd nutchez_client_install"
511#  show_info "scp nutchuser@$MasterIP_Address:/home/nutchuser/nutchez/source/* ."
512#  show_info "sudo su"
513#  show_info "./client_install"
514}
Note: See TracBrowser for help on using the repository browser.