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