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

Last change on this file since 240 was 240, checked in by shunfa, 14 years ago

修改client_deploy.sh

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