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