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