source: nutchez-0.2/src/test/client_install_func.sh @ 188

Last change on this file since 188 was 188, checked in by rock, 14 years ago

新增語言檔 (client_install)

  • Property svn:executable set to *
File size: 10.2 KB
RevLine 
[122]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
[123]9# 正式版之後,記的將不必要的 echo 拿掉
[167]10function debug_info () {
11  if [ $? -eq 0 ]; then
12    echo -e "\033[1;35;40m info - $1 \033[0m"
13  fi
14}
[123]15
[167]16function show_info () {
17  if [ $? -eq 0 ]; then
18    echo -e "\033[1;32;40m $1 \033[0m"
19  fi
20}
[123]21
[167]22
[188]23# choose Language
24function choose_lang(){
25
26echo -e "\n$choose_lang_1"
27read -p "$choose_lang_2 " langChoice
28
29if [ $langChoice == "2" ]; then
30    source lang_zh_TW_client_install
31else
32    source lang_en_US_client_install
33fi
34}
35
[123]36# 檢查執行這個程式的是否為root權限
[122]37function check_root(){
[123]38# 正式版後可拿掉此 echo
[167]39  debug_info "check_root"
40  if [ $USER != "root" ]; then
[188]41    show_info "$check_root_1"
[122]42    exit
[167]43  fi
[123]44# 正式版後可拿掉此 echo
[188]45  show_info "$check_root_2"
[122]46}
47
48
49# 查出此主機的作業系統,以及版本
50function check_systemInfo(){
[188]51  debug_info "$check_sys_1"
52  show_info "$check_sys_2"
[169]53  Linux_Distribution=$(lsb_release -a 2> /dev/null | grep "Distributor ID:" | awk '{print $3}')
54  Linux_Version=$(lsb_release -a 2> /dev/null | grep "Release" | awk '{print $2}')
[167]55  show_info "$Linux_Distribution , $Linux_Version"
[122]56}
57
58
[132]59# 安裝需要的相依套件 (目前只支援 deb 套件的系統自動安裝,yum或其他套件系統的則需手動安裝)
60function install_packages(){
[167]61  # deb 系列系統
[188]62  debug_info "$install_pack_1"
63  debug_info "$install_pack_2"
[167]64  if [ "$Linux_Distribution" == "Ubuntu" ] || [ "$Linux_Distribution" == "Debian" ] ;then
[188]65    echo -e "\n$install_pack_if_1\n"
[132]66    aptitude install -y expect ssh dialog
[167]67  # rpm 系列系統
68  elif [ "$Linux_Distribution" == "Fedora" ] || [ "$Linux_Distribution" == "CentOS" ] ;then
[188]69    show_info "$install_pack_if_2"
[167]70  else
[188]71    show_info "$install_pack_if_2"
[167]72  fi 
[132]73}
74
75
[122]76# 檢查之前是否有安裝NutchEz
77# 目前先檢查是否有/opt/nutchez 這個資料夾即可
78function check_nez_installed(){
[167]79  debug_info "chcheck_nez_installed"
80  if [ -d /opt/nutchez ]; then
81    show_info "System already had NutchEz."
[132]82    exit
[167]83  else
84    show_info "System does not has NutchEz."
85  fi
[122]86}
87
88
89# 檢查是否有安裝sun java ,並檢查是否為jdk 1.6 以上版本
90# 4種判斷可能性 (1)系統沒安裝 JAVA (2)系統有安裝JAVA,但非sun版本
91# (3)系統有安裝但Sun Java 在非預設路徑下 (4)以正確安裝 Sun JAVA 預設路徑下
92function check_sunJava(){
[167]93  debug_info "check_sunJava"
94  debug_info "NutchEz need Sun Java JDK 1.6.x or above version"
[122]95
[167]96  javaPath="/usr"
97  yesno="no"
98  choice="3"
[122]99
[167]100  if [ -e $javaPath/bin/java ]; then
101    JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)")
102    JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \
103    awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2)
[145]104   
[167]105  if [ "$JAVA_org" == "" ]; then 
106    show_info "Java is not Sun version, plz install sun Java 1.6.X"
107    show_info "Please input your choice: "
108    show_info "(1)System don't have Sun Java (2)Sun Java is in other path (3)Exit"
109    read -p "plz input (1/2/3): " choice
110    case $choice  in
111      "1")
112        show_info "Please install Sun Java manually!"
113        exit 
114        ;;
115      "2")
116        read -p "Input Sun Java home path(ex. '/usr/lib/jvm/java-6-sun-1.6.0.12' or using default '/usr' ): " javaPath
117        ;;
118        "*")
119        exit
120        ;;
121        esac
[142]122
[167]123        if [ $choice == "2" ]; then
124          JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)")
125          JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \
126          awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2)
127             
128          if [ "$JAVA_org" == "" ]; then
129            show_info "It is not Sun Java! Plz install Sun Java manually!"
130            exit
[145]131            fi
[167]132          fi
[145]133        fi 
[122]134
[145]135      large16=$(echo "$JAVA_version >= 1.6" | bc)
136      if [ "${large16}" == 0 ]; then
[167]137        show_info "Java version is too old (it need 1.6.X above)"
138        exit
[145]139      fi 
140     
[167]141      show_info "System has Sun Java 1.6 above version."
[145]142  else
[167]143    show_info "Please install Sun JAVA 1.6.X or above version"
144    exit
[145]145  fi
[122]146
[167]147  unset JAVA_org
148  unset JAVA_version
[123]149}
[122]150
[123]151
152# 檢查是否有安裝openssh, openssh-server
153function check_ssh(){
[167]154  debug_info "check_ssh"
155  if [ -e /usr/bin/ssh ]; then
156    show_info "System has ssh."
157  else
158    show_info "Please install ssh."
[123]159    exit
[167]160  fi
[123]161
[167]162  if [ -e /usr/sbin/sshd ]; then
163    show_info "System has ssh Server (sshd)."
164  else
165    show_info "Please install ssh Server (sshd)."
[123]166    exit
[167]167  fi
[122]168}
[123]169
170
171# 檢查是否有安裝dialog
172function check_dialog(){
[167]173  debug_info "check_dialog"
174  if [ -e /usr/bin/dialog ]; then
175    show_info "System has dialog."
176  else
177    show_info "Please install dialog."
[123]178    exit
[167]179  fi
[123]180}
181
[142]182
[128]183# scp nutchuser@master_ip:~ 把.ssh/目錄複製下來
184# 當使用者輸入nutchuser 密碼時,將此密碼紀錄到Nutchuser_Passwd
185# 此步驟若無法連到 master 則跳出
186function scp_master_nutchuser_sshkey(){
[167]187  debug_info "scp_master_nutchuser_sshkey"
188  debug_info "mkdir -p /home/nutchuser/"
[159]189  mkdir -p /home/nutchuser/.ssh/
190  rm -fr /home/nutchuser/.ssh/*
191  unset Nutchuser_Passwd2
[134]192
[167]193  debug_info "scp nutchuser@$1:~/.ssh /home/nutchuser/"
[142]194expect -c "spawn scp -r -o StrictHostKeyChecking=no nutchuser@$1:~/.ssh /home/nutchuser/
195expect \"*: \" { send \"$Nutchuser_Passwd\r\" }
196expect \"*: \" { send_user \"Password is error\" }
197expect eof"
198
[159]199  if [ -e "/home/nutchuser/.ssh/authorized_keys" ]; then
[167]200    show_info "scp correct."   
[142]201    else
[167]202      show_info "scp error,\n(1)plese check nutchuser password in server\n(2)nutchuser's authorized_keys in server\n(3)server's network status"
203    exit
204  fi
[159]205  ssh-add /home/nutchuser/.ssh/id_rsa
[167]206  debug_info "chown -R nutchuser:nutchuser /home/nutchuser/.ssh"
[159]207  chown -R nutchuser:nutchuser /home/nutchuser/.ssh
[128]208}
209
210# 新增nutchuser 帳號時用 Nutchuser_Passwd 當密碼
211function creat_nutchuser_account(){
[167]212  debug_info "creat_nutchuser_account"
213  while [ "$Nutchuser_Passwd" != "$Nutchuser_Passwd2" ]
214  do
215      echo -e "\n"
216      read -sp "Plz input nutchuser password of master node: " Nutchuser_Passwd
217      echo 
218      read -sp "plz input nutchuser password, again: " Nutchuser_Passwd2
219      echo 
[142]220        if [ "$Nutchuser_Passwd" == "$Nutchuser_Passwd2" ]; then
[167]221          show_info "Two Passwords match."
[142]222        else
[167]223          show_info "Two passwords don't match, please re-input nutchuser's password."
[142]224        fi
[167]225  done                                                                                                                                   
226  unset Nutchuser_Passwd2
[142]227
[167]228  if [ $(cat /etc/passwd | grep nutchuser) ]; then
229    show_info "System already has nutchuser, change nutchuser password."
[139]230    expect -c "spawn passwd nutchuser
231    set timeout 1
232    expect \"*: \"
[142]233    send \"$Nutchuser_Passwd\r\"
[139]234    expect \"*: \"
[142]235    send \"$Nutchuser_Passwd\r\"
[129]236    expect eof"
[167]237    else
[169]238      show_info "Create nutchuser and change password."
[167]239      useradd -m nutchuser -s /bin/bash
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  fi
[128]248}
249
250# 用scp 複製 master 的設定與安裝資料
251# 目前僅需做到能無礙的複製遠端的/opt/nutchez/到local的/opt/
252function scp_packages(){
[167]253  debug_info "scp_packages"
[159]254  mkdir /opt/nutchez
255  mkdir /var/nutchez
256  mkdir /home/nutchuser/nutchez
257  mkdir /home/nutchuser/nutchez/source
[171]258  mkdir /home/nutchuser/nutchez/system
[159]259  chmod 777 /opt/nutchez
[167]260  debug_info "chown -R nutchuser:nutchuser /opt/nutchez"
[159]261  chown -R nutchuser:nutchuser /opt/nutchez
262  chown -R nutchuser:nutchuser /var/nutchez
263  chown -R nutchuser:nutchuser /home/nutchuser/nutchez
264  chmod 755 /opt/nutchez
[178]265  debug_info "scp -r nutchuser@$1:/opt/nutchez/NutchezForClientOf_$Master_IP_Address.tar.gz /home/nutchuser/nutchez/source"
266  su nutchuser -c "scp -r -o StrictHostKeyChecking=no nutchuser@$1:/home/nutchuser/nutchez/source/NutchezForClientOf_$Master_IP_Address.tar.gz /home/nutchuser/nutchez/source"
267
[128]268}
[144]269
270
[147]271function install_nutch_package(){
[167]272  debug_info "install_nutch_package"
[159]273  tar -zxvf /home/nutchuser/nutchez/source/NutchezForClientOf_$Master_IP_Address.tar.gz -C /opt/nutchez
274  sed -i '1a '$Master_IP_Address' '$Master_Hostname'' /etc/hosts
[152]275  #/opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode
276  #/opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker
[144]277}
[147]278
279function recall_hostname_ip(){
[167]280  debug_info "recall_hostname_ip"
281  net_interfaces=$(ifconfig | grep ^eth | cut -d " " -f1)
282  net_nu=$(echo $net_interfaces | wc -w)
283  # 若只有一個 eth 時
284  if [ "$net_nu" == "1" ]; then
285  # ifconfig $net_interfaces | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1
286  net_address=$(ifconfig $net_interfaces | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)
287  net_MacAddr=$(ifconfig $net_interfaces | grep 'HW' | sed 's/^.*HWaddr //g')
288  show_info "net_address is $net_address"
289  show_info "net_MacAddr is $net_MacAddr"
[147]290
[167]291  # 若有多個 eth 時
292    else
293      declare -i i=1
294      show_info "System have multiple network device, which network use for this machine: "
[149]295       
[167]296      for net in $net_interfaces
297      do 
298        echo "($i$net  $(ifconfig $net | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)"
299        i=i+1
300      done
[149]301       
[167]302      read -p "Please choice(1/2/3...): " net_choice
303  if [ -z $net_choice ]; then
304    net_choice=1
305  fi   
[159]306
[167]307  show_info "choice is $net_choice"
308  net_interface=$(echo $net_interfaces | cut -d " " -f $net_choice)
309  # config $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1
310  net_address=$(ifconfig $net_interface | grep "inet addr:" | sed 's/^.*inet addr://g' | cut -d " " -f1)
311  net_MacAddr=$(ifconfig $net_interface | grep 'HW' | sed 's/^.*HWaddr //g') 
312  show_info "net_address is $net_address"
313  show_info "net_MacAddr is $net_MacAddr"
314  fi
[147]315
[171]316  debug_info "ssh nutchuser@$1 echo $net_address $(hostname) $net_MacAddr \>\> ~/nutchez/system/nutch_nodes"
317  su nutchuser -c "ssh nutchuser@$1 echo $net_address $(hostname) $net_MacAddr \>\> ~/nutchez/system/nutch_nodes"
[147]318
319#su nutchuser -c expect -c "spawn ssh nutchuser@$1
320#set timeout 1
321#expect \"*\"
322#send \"echo $net_address $(hostname) >> /home/nutchuser/nutch_nodes\r\"
323#expect \"*\"
324#send \"exit\""
325}
Note: See TracBrowser for help on using the repository browser.