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= check_root (debug) =" |
---|
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= check_systemInfo (debug) =" |
---|
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= install_packages (debug) =" |
---|
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= chcheck_nez_installed (debug) =" |
---|
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= check_sunJava (debug) =" |
---|
72 | echo -e "\nNutchEz need Sun Java JDK 1.6.x or above version" |
---|
73 | |
---|
74 | yesno="no" |
---|
75 | while [ "$yesno" != "yes" ] |
---|
76 | do |
---|
77 | read -p "Plz input Sun Java home path (ex. '/usr/lib/jvm/java-6-sun-1.6.0.12' or using default '/usr' ): " javaPath |
---|
78 | read -p "Your java is ${javaPath}/bin/java. Is it correct (yes/no): " yesno |
---|
79 | done |
---|
80 | |
---|
81 | test -z JavaPath && $javaPath="/usr" |
---|
82 | |
---|
83 | if [ -e $javaPath/bin/java ]; then |
---|
84 | JAVA_org=$($javaPath/bin/java -version 2>&1 | grep "Java(TM)") |
---|
85 | JAVA_version=$($javaPath/bin/java -version 2>&1 | grep "java version" | \ |
---|
86 | awk '{print $3}' | cut -d "." -f1-2 | cut -d "\"" -f2) |
---|
87 | |
---|
88 | if [ "$JAVA_org" == "" ]; then |
---|
89 | echo "Java is not Sun version, plz install sun Java 1.6.X" |
---|
90 | exit |
---|
91 | fi |
---|
92 | |
---|
93 | large16=$(echo "$JAVA_version >= 1.6" | bc) |
---|
94 | if [ "${large16}" == 0 ]; then |
---|
95 | echo "Java version is too old (it need 1.6.X above)" |
---|
96 | exit |
---|
97 | fi |
---|
98 | |
---|
99 | echo "System has Sun Java 1.6 above version." |
---|
100 | else |
---|
101 | echo "Plz install Sun JAVA 1.6.X or above version" |
---|
102 | exit |
---|
103 | fi |
---|
104 | |
---|
105 | unset JAVA_org |
---|
106 | unset JAVA_version |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | # 檢查是否有安裝openssh, openssh-server |
---|
111 | function check_ssh(){ |
---|
112 | echo -e "\n= check_ssh (debug) =" |
---|
113 | if [ -e /usr/bin/ssh ]; then |
---|
114 | echo -e "\nSystem has ssh." |
---|
115 | else |
---|
116 | echo "Plz install ssh." |
---|
117 | exit |
---|
118 | fi |
---|
119 | |
---|
120 | if [ -e /usr/sbin/sshd ]; then |
---|
121 | echo "System has ssh Server (sshd)." |
---|
122 | else |
---|
123 | echo "Plz install ssh Server (sshd)." |
---|
124 | exit |
---|
125 | fi |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | # 檢查是否有安裝dialog |
---|
130 | function check_dialog(){ |
---|
131 | echo -e "\n= check_dialog (debug) =" |
---|
132 | if [ -e /usr/bin/dialog ]; then |
---|
133 | echo -e "\nSystem has dialog." |
---|
134 | else |
---|
135 | echo "Plz install dialog." |
---|
136 | exit |
---|
137 | fi |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | # scp nutchuser@master_ip:~ 把.ssh/目錄複製下來 |
---|
142 | # 當使用者輸入nutchuser 密碼時,將此密碼紀錄到Nutchuser_Passwd |
---|
143 | # 此步驟若無法連到 master 則跳出 |
---|
144 | function scp_master_nutchuser_sshkey(){ |
---|
145 | echo -e "\n= scp_master_nutchuser_sshkey (debug) =" |
---|
146 | echo -e "mkdir -p /home/nutchuser/" |
---|
147 | mkdir -p /home/nutchuser/.ssh/ |
---|
148 | rm -fr /home/nutchuser/.ssh/* |
---|
149 | |
---|
150 | unset Nutchuser_Passwd2 |
---|
151 | |
---|
152 | echo -e "scp nutchuser@$1:~/.ssh /home/nutchuser/" |
---|
153 | expect -c "spawn scp -r -o StrictHostKeyChecking=no nutchuser@$1:~/.ssh /home/nutchuser/ |
---|
154 | expect \"*: \" { send \"$Nutchuser_Passwd\r\" } |
---|
155 | expect \"*: \" { send_user \"Password is error\" } |
---|
156 | expect eof" |
---|
157 | |
---|
158 | if [ -e "/home/nutchuser/.ssh/authorized_keys" ]; then |
---|
159 | echo -e "\nscp correct." |
---|
160 | else |
---|
161 | 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" |
---|
162 | exit |
---|
163 | fi |
---|
164 | |
---|
165 | echo "chown -R nutchuser:nutchuser /home/nutchuser/.ssh" |
---|
166 | chown -R nutchuser:nutchuser /home/nutchuser/.ssh |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | # 新增nutchuser 帳號時用 Nutchuser_Passwd 當密碼 |
---|
172 | function creat_nutchuser_account(){ |
---|
173 | echo -e "\n= creat_nutchuser_account (debug) =" |
---|
174 | |
---|
175 | while [ "$Nutchuser_Passwd" != "$Nutchuser_Passwd2" ] |
---|
176 | do |
---|
177 | echo -e "\n" |
---|
178 | read -p "Plz input nutchuser password of master node: " Nutchuser_Passwd |
---|
179 | read -p "plz input nutchuser password, again: " Nutchuser_Passwd2 |
---|
180 | if [ "$Nutchuser_Passwd" == "$Nutchuser_Passwd2" ]; then |
---|
181 | echo "Two Passwords match." |
---|
182 | else |
---|
183 | echo "two passwords do not match, plz re-input nutchuser's password." |
---|
184 | fi |
---|
185 | done |
---|
186 | |
---|
187 | unset Nutchuser_Passwd2 |
---|
188 | |
---|
189 | if [ $(cat /etc/passwd | grep nutchuser) ]; then |
---|
190 | echo "System already has nutchuser, change nutchuser password." |
---|
191 | expect -c "spawn passwd nutchuser |
---|
192 | set timeout 1 |
---|
193 | expect \"*: \" |
---|
194 | send \"$Nutchuser_Passwd\r\" |
---|
195 | expect \"*: \" |
---|
196 | send \"$Nutchuser_Passwd\r\" |
---|
197 | expect eof" |
---|
198 | else |
---|
199 | echo "Create nutchuser and change password." |
---|
200 | useradd -m nutchuser -s /bin/bash |
---|
201 | expect -c "spawn passwd nutchuser |
---|
202 | set timeout 1 |
---|
203 | expect \"*: \" |
---|
204 | send \"$Nutchuser_Passwd\r\" |
---|
205 | expect \"*: \" |
---|
206 | send \"$Nutchuser_Passwd\r\" |
---|
207 | expect eof" |
---|
208 | fi |
---|
209 | } |
---|
210 | |
---|
211 | # 用scp 複製 master 的設定與安裝資料 |
---|
212 | # 目前僅需做到能無礙的複製遠端的/opt/nutchez/到local的/opt/ |
---|
213 | function scp_packages(){ |
---|
214 | echo -e "\n= scp_packages (debug) =" |
---|
215 | chmod 777 /opt |
---|
216 | echo "scp -r -o StrictHostKeyChecking=no nutchuser@$1:/opt/nutchez /opt/" |
---|
217 | su nutchuser -c "scp -r -o StrictHostKeyChecking=no nutchuser@$1:/opt/nutchez /opt/" |
---|
218 | |
---|
219 | echo -e "\nchown -R nutchuser:nutchuser /opt/nutchez" |
---|
220 | chown -R nutchuser:nutchuser /opt/nutchez |
---|
221 | chmod 755 /opt |
---|
222 | } |
---|