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