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
Line 
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 拿掉
10function debug_info () {
11  if [ $? -eq 0 ]; then
12    echo -e "\033[1;35;40m info - $1 \033[0m"
13  fi
14}
15
16function 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
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
36# 檢查執行這個程式的是否為root權限
37function check_root(){
38# 正式版後可拿掉此 echo
39  debug_info "check_root"
40  if [ $USER != "root" ]; then
41    show_info "$check_root_1"
42    exit
43  fi
44# 正式版後可拿掉此 echo
45  show_info "$check_root_2"
46}
47
48
49# 查出此主機的作業系統,以及版本
50function check_systemInfo(){
51  debug_info "$check_sys_1"
52  show_info "$check_sys_2"
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}')
55  show_info "$Linux_Distribution , $Linux_Version"
56}
57
58
59# 安裝需要的相依套件 (目前只支援 deb 套件的系統自動安裝,yum或其他套件系統的則需手動安裝)
60function install_packages(){
61  # deb 系列系統
62  debug_info "$install_pack_1"
63  debug_info "$install_pack_2"
64  if [ "$Linux_Distribution" == "Ubuntu" ] || [ "$Linux_Distribution" == "Debian" ] ;then
65    echo -e "\n$install_pack_if_1\n"
66    aptitude install -y expect ssh dialog
67  # rpm 系列系統
68  elif [ "$Linux_Distribution" == "Fedora" ] || [ "$Linux_Distribution" == "CentOS" ] ;then
69    show_info "$install_pack_if_2"
70  else
71    show_info "$install_pack_if_2"
72  fi 
73}
74
75
76# 檢查之前是否有安裝NutchEz
77# 目前先檢查是否有/opt/nutchez 這個資料夾即可
78function check_nez_installed(){
79  debug_info "chcheck_nez_installed"
80  if [ -d /opt/nutchez ]; then
81    show_info "System already had NutchEz."
82    exit
83  else
84    show_info "System does not has NutchEz."
85  fi
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(){
93  debug_info "check_sunJava"
94  debug_info "NutchEz need Sun Java JDK 1.6.x or above version"
95
96  javaPath="/usr"
97  yesno="no"
98  choice="3"
99
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)
104   
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
122
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
131            fi
132          fi
133        fi 
134
135      large16=$(echo "$JAVA_version >= 1.6" | bc)
136      if [ "${large16}" == 0 ]; then
137        show_info "Java version is too old (it need 1.6.X above)"
138        exit
139      fi 
140     
141      show_info "System has Sun Java 1.6 above version."
142  else
143    show_info "Please install Sun JAVA 1.6.X or above version"
144    exit
145  fi
146
147  unset JAVA_org
148  unset JAVA_version
149}
150
151
152# 檢查是否有安裝openssh, openssh-server
153function check_ssh(){
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."
159    exit
160  fi
161
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)."
166    exit
167  fi
168}
169
170
171# 檢查是否有安裝dialog
172function check_dialog(){
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."
178    exit
179  fi
180}
181
182
183# scp nutchuser@master_ip:~ 把.ssh/目錄複製下來
184# 當使用者輸入nutchuser 密碼時,將此密碼紀錄到Nutchuser_Passwd
185# 此步驟若無法連到 master 則跳出
186function scp_master_nutchuser_sshkey(){
187  debug_info "scp_master_nutchuser_sshkey"
188  debug_info "mkdir -p /home/nutchuser/"
189  mkdir -p /home/nutchuser/.ssh/
190  rm -fr /home/nutchuser/.ssh/*
191  unset Nutchuser_Passwd2
192
193  debug_info "scp nutchuser@$1:~/.ssh /home/nutchuser/"
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
199  if [ -e "/home/nutchuser/.ssh/authorized_keys" ]; then
200    show_info "scp correct."   
201    else
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
205  ssh-add /home/nutchuser/.ssh/id_rsa
206  debug_info "chown -R nutchuser:nutchuser /home/nutchuser/.ssh"
207  chown -R nutchuser:nutchuser /home/nutchuser/.ssh
208}
209
210# 新增nutchuser 帳號時用 Nutchuser_Passwd 當密碼
211function creat_nutchuser_account(){
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 
220        if [ "$Nutchuser_Passwd" == "$Nutchuser_Passwd2" ]; then
221          show_info "Two Passwords match."
222        else
223          show_info "Two passwords don't match, please re-input nutchuser's password."
224        fi
225  done                                                                                                                                   
226  unset Nutchuser_Passwd2
227
228  if [ $(cat /etc/passwd | grep nutchuser) ]; then
229    show_info "System already has nutchuser, change nutchuser password."
230    expect -c "spawn passwd nutchuser
231    set timeout 1
232    expect \"*: \"
233    send \"$Nutchuser_Passwd\r\"
234    expect \"*: \"
235    send \"$Nutchuser_Passwd\r\"
236    expect eof"
237    else
238      show_info "Create nutchuser and change password."
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
248}
249
250# 用scp 複製 master 的設定與安裝資料
251# 目前僅需做到能無礙的複製遠端的/opt/nutchez/到local的/opt/
252function scp_packages(){
253  debug_info "scp_packages"
254  mkdir /opt/nutchez
255  mkdir /var/nutchez
256  mkdir /home/nutchuser/nutchez
257  mkdir /home/nutchuser/nutchez/source
258  mkdir /home/nutchuser/nutchez/system
259  chmod 777 /opt/nutchez
260  debug_info "chown -R nutchuser:nutchuser /opt/nutchez"
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
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
268}
269
270
271function install_nutch_package(){
272  debug_info "install_nutch_package"
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
275  #/opt/nutchez/nutch/bin/hadoop-daemon.sh start datanode
276  #/opt/nutchez/nutch/bin/hadoop-daemon.sh start tasktracker
277}
278
279function recall_hostname_ip(){
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"
290
291  # 若有多個 eth 時
292    else
293      declare -i i=1
294      show_info "System have multiple network device, which network use for this machine: "
295       
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
301       
302      read -p "Please choice(1/2/3...): " net_choice
303  if [ -z $net_choice ]; then
304    net_choice=1
305  fi   
306
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
315
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"
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.