自動化部署腳本(一)

傳統部署方式
一、純手工scp
二、純手工登陸git pull 、svn update
三、純手工xftp往上拉
四、開發給打一個壓縮包,rz上去。解壓html

 

傳統部署缺點:
一、全程運維參與,佔用大量時間
二、上線速度慢。
三、認爲失誤多。管理混亂
四、回滾慢,不及時java

 

新項目上線,規劃排在第一位
通常銀行都不提供測試接口。好比一些電商公司測試的話,常常把商品調節成1分,只能特定帳號能看到。node

環境的規劃
一、開發環境-開發者本地有本身的環境,而後運維須要設置的開發環境,放的是你們共用的服務。如開發數據庫mysql,其它:redis、Memcached。
二、測試環境:功能測試環境和性能測試環境
三、預生產環境:通常能夠用生產環境中的某個節點擔任
四、生產環境:直接對用戶提供服務的環境python

預生產環境產生的緣由:
一、數據庫不一致:測試環境和生產環境數據庫確定不同的。
二、使用生產環境的聯調接口。例如,支付接口mysql

 

預生產環境--生產環境--灰度發佈
 
灰度發佈:
阿里雲產品上線,都是一個區一個區上的。確定不是一會兒都上的
qq彈窗:恭喜你得到某某版本資格,請下載新版本享用,你就是小白鼠,這個也是一種灰度發佈

 

規劃
已經有一個能夠上線的代碼在代碼倉庫。咱們如何設計一套生產自動化部署系統。
一、規劃
二、實現
三、總結和擴展。PDCA
四、在生產環境應用

 

自動化部署系統的規劃
 
需求: 
1個集羣有10個節點。一鍵部署這10個節點。
二、一鍵回滾到任意版本
三、一鍵回滾到上個版本

 

部署:linux

一、代碼放在哪裏:svn,git
二、獲取什麼版本代碼?
svn+git直接拉去某個分支
svn:指定版本號
git:指定tag
三、差別解決:
(1)、各個節點直接差別:
(2)、代碼倉庫和實際的差別。配置文件是否在代碼倉庫中
(3)、配置文件未必同樣:crontab.xml預生產節點
四、如何更新。java tomcat。須要重啓。
五、測試。
六、串行和並行 分組部署
7如何執行。(1)shell執行。(2)web界面nginx

 

關於配置文件存放:
配置文件放代碼倉庫裏,豈不是全部開發都能連數據庫了。
所以配置文件不能放git裏。
有的人把配置文件放某個分支裏。讓一些人無法看到
我以爲能夠單獨有個放配置文件的git

 

騰訊藍鯨:我幫你作個平臺,你寫個腳本,我幫你發佈到某個機器上,你經過平臺幫你執行
一些公司如今的運維,無論任何發佈,都是作好界面,讓項目負責人去經過界面管理

 

關於差別文件:git

可能有些節點有特殊的差別性文件
 
 
自動化部署流程設計

 

 

自動化部署實戰-shell函數

 環境準備github

系統版本web

?
1
2
3
4
5
[root@linux-node1 ~] # cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@linux-node1 ~] # uname -rm
3.10.0-229.el7.x86_64 x86_64
[root@linux-node1 ~] #

主機名和IP

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
node1
[root@linux-node1 ~] # hostname
linux-node1.nmap.com
[root@linux-node1 ~] # cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.nmap.com
192.168.56.12 linux-node2 linux-node2.nmap.com
[root@linux-node1 ~] #
 
node2
[root@linux-node2 ~] # hostname
linux-node2.nmap.com
[root@linux-node2 ~] # cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.11 linux-node1 linux-node1.nmap.com
192.168.56.12 linux-node2 linux-node2.nmap.com
[root@linux-node2 ~] #

    

兩臺web服務器,node1和node2做爲兩個web服務器,同時node1也做爲部署分發服務器,去管理2個node節點上的web包

 

兩個節點添加普通用戶www,做爲web服務器管理用戶。

?
1
2
3
4
5
6
7
8
9
[root@linux-node1 scripts] # useradd -u 1001 www
[root@linux-node1 scripts] # id www
uid=1001(www) gid=1001(www) groups =1001(www)
[root@linux-node1 scripts] #
 
[root@linux-node2 ~] # useradd -u 1001 www
[root@linux-node2 ~] # id www
uid=1001(www) gid=1001(www) groups =1001(www)
[root@linux-node2 ~] #

 

配置www用戶登陸其餘機器不用密碼。密鑰認證。之後www用戶做爲管理其它機器的用戶

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@linux-node1 . ssh ] # su - www
[www@linux-node1 ~]$ ssh -keygen -t rsa
Generating public /private rsa key pair.
Enter file in which to save the key ( /home/www/ . ssh /id_rsa ):
Created directory '/home/www/.ssh' .
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/www/ . ssh /id_rsa .
Your public key has been saved in /home/www/ . ssh /id_rsa .pub.
The key fingerprint is:
70:37:ff:d0:17:e0:74:1d:c9:04:28:bb:de:ec:1f:7f www@linux-node1.nmap.com
The key's randomart image is:
+--[ RSA 2048]----+
|            .++++|
|         . .o oo.|
|      . . =  . . |
|       o o o .  .|
|        S . o . .|
|         .   o . |
|        . o  ..  |
|         . o  o E|
|          .... ..|
+-----------------+
[www@linux-node1 ~]$

 

查看公鑰

?
1
2
3
4
5
6
7
8
9
10
11
[www@linux-node1 ~]$ cd . ssh /
[www@linux-node1 . ssh ]$ ll
total 8
-rw------- 1 www www 1679 Apr  5 03:41 id_rsa
-rw-r--r-- 1 www www  406 Apr  5 03:41 id_rsa.pub
[www@linux-node1 . ssh ]$ cat id_rsa.pub
ssh -rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDcZh8EEk2 /rS6B/tLHnRpZGrGIJYFHg7zRFvuT3N9jvOFhYJdWv +8WSQuT0pvxNM4eR0N5Ma9wVvKPo /lVjCaFK +M0dENJVhi6m9OKMtoo2u
jvvuyinNPP4pyoK6ggG5jOlEkHoLcbWCRG /j3pN1rZYV +1twET9xi2IA4UQkgPvKKYWjq7NUR0v5BWsgEQt7VvjcLWTlltTVeGb3FDVKIjDnioIBmLmVwJS64N+GGgAj5YQ+bKHTwY
anEMD39JGKxo0RXTZB5sa734yfNjc3hTZXB4RCcGdzgcMJs /Rt5VeZ277zF86xr4Hd5cioAbV6Y1RvELjmpvrqUUz3tcaKId www@linux-node1.nmap.com
[www@linux-node1 . ssh ]$

  

node2也添加node1的公鑰

改爲600權限才能正常登陸

?
1
2
3
4
5
6
7
8
9
[www@linux-node2 ~]$ cd . ssh /
[www@linux-node2 . ssh ]$ vim authorized_keys
[www@linux-node2 . ssh ]$ cat authorized_keys
ssh -rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDcZh8EEk2 /rS6B/tLHnRpZGrGIJYFHg7zRFvuT3N9jvOFhYJdWv +8WSQuT0pvxNM4eR0N5Ma9wVvKPo /lVjCaFK +M0dENJVhi6m9OKMtoo2u
jvvuyinNPP4pyoK6ggG5jOlEkHoLcbWCRG /j3pN1rZYV +1twET9xi2IA4UQkgPvKKYWjq7NUR0v5BWsgEQt7VvjcLWTlltTVeGb3FDVKIjDnioIBmLmVwJS64N+GGgAj5YQ+bKHTwY
anEMD39JGKxo0RXTZB5sa734yfNjc3hTZXB4RCcGdzgcMJs /Rt5VeZ277zF86xr4Hd5cioAbV6Y1RvELjmpvrqUUz3tcaKId www@linux-node1.nmap.com
[www@linux-node2 . ssh ]$ chmod 600 authorized_keys
[www@linux-node2 . ssh ]$

 

登陸測試--成功

?
1
2
3
[www@linux-node1 . ssh ]$ ssh 192.168.58.12
Last login: Mon Apr 10 00:31:23 2017 from 192.168.58.11
[www@linux-node2 ~]$

 

讓node1的www用戶ssh本身也不須要輸入密碼。

node1添加公鑰

本地也放本身的密鑰,這樣能夠僞裝模擬成3臺機器。2個ssh免密鑰的機器
node1同時做爲部署機
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[www@linux-node1 . ssh ]$ ll
total 12
-rw------- 1 www www 1679 Apr  5 03:41 id_rsa
-rw-r--r-- 1 www www  406 Apr  5 03:41 id_rsa.pub
-rw-r--r-- 1 www www  175 Apr  5 03:43 known_hosts
[www@linux-node1 . ssh ]$ vim authorized_keys
[www@linux-node1 . ssh ]$ chmod 600 authorized_keys
[www@linux-node1 . ssh ]$ ssh 192.168.58.11
The authenticity of host '192.168.58.11 (192.168.58.11)' can't be established.
ECDSA key fingerprint is 8b:4e:2f: cd :37:89:02:60:3c:99:9f:c6:7a:5a:29:14.
Are you sure you want to continue connecting ( yes /no )? yes
Warning: Permanently added '192.168.58.11' (ECDSA) to the list of known hosts.
Last login: Wed Apr  5 03:40:47 2017
[www@linux-node1 ~]$ exit
logout
Connection to 192.168.58.11 closed.
[www@linux-node1 . ssh ]$ ssh 192.168.58.11
Last login: Wed Apr  5 03:46:21 2017 from 192.168.58.11
[www@linux-node1 ~]$

  

開始寫自動化部署腳本

根據上面的流程圖,先把大致框架寫出來

先把框架寫出來,而後每一個函數裏寫echo
看看腳本執行流程是否有問題
 
code_diff 拷貝差別部署文件
這是面向過程的一種開發方式
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
[root@linux-node1 ~] # mkdir /scripts -p
[root@linux-node1 ~] # cd /scripts/
[root@linux-node1 scripts] # vim deploy.sh
[root@linux-node1 scripts] # chmod +x deploy.sh
[root@linux-node1 scripts] # ./deploy.sh
Usage: . /deploy .sh [ deploy | rollback ]
[root@linux-node1 scripts] # cat deploy.sh
#!/bin/bash
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/deploy"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
 
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}
 
code_get(){
     echo code_get
}
 
code_build(){
     echo code_build
}
 
code_config(){
     echo code_config
}
 
code_tar(){
     echo code_tar
}
 
code_scp(){
     echo code_scp
}
 
cluster_node_remove(){
     echo cluster_node_remove
}
 
code_deploy(){
     echo code_deploy
}
 
config_diff(){
     echo config_diff
}
 
code_test(){
     echo code_test
}
 
cluster_node_in(){
     echo cluster_node_in
}
 
rollback(){
     echo rollback
}
 
main(){
     case $1 in
     deploy)
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             cluster_node_remove;
             code_deploy;
             config_diff;
             code_test;
             cluster_node_in;
             ;;
     rollback)
             rollback;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #
最末尾還要加個main,不然沒法執行

 

腳本再優化下
儘可能不要讓$1來回傳,不然可能會亂
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main(){
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             cluster_node_remove;
             code_deploy;
             config_diff;
             code_test;
             cluster_node_in;
             ;;

  

 

繼續完善腳本--添加日誌和鎖
一、凡是不記錄日誌的腳本就是刷流氓,執行到哪一步失敗的啊?
二、腳本是否能夠多我的一塊兒執行?(最好不要多我的一塊兒執行)不容許多人執行的話能夠上鎖

 

通常鎖文件放下面目錄下

?
1
2
3
4
[root@linux-node1 ~] # cd /var/run/lock/
[root@linux-node1 lock] # ls
iscsi lockdev lvm ppp subsys
[root@linux-node1 lock] #

咱們能夠單獨添加個目錄,給它用,由於權限問題,須要受權改變屬組,咱們使用tmp目錄

主函數執行以前,應該先判斷鎖文件是否存在,執行的時候也應該生成這個lock文件
既然2個地方用到了它,是否能夠把它製做成變量

新的腳本以下,主要添加了鎖的功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
[root@linux-node1 scripts] # cat deploy.sh
#!/bin/bash
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/deploy"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}
 
shell_lock(){
     touch ${LOCK_FILE}
}
shell_unlock(){
     rm -f ${LOCK_FILE}
}
 
code_get(){
     echo code_get
     sleep 60;
}
 
code_build(){
     echo code_build
}
 
code_config(){
     echo code_config
}
 
code_tar(){
     echo code_tar
}
 
code_scp(){
     echo code_scp
}
 
cluster_node_remove(){
     echo cluster_node_remove
}
 
code_deploy(){
     echo code_deploy
}
 
config_diff(){
     echo config_diff
}
 
code_test(){
     echo code_test
}
 
cluster_node_in(){
     echo cluster_node_in
}
 
rollback(){
     echo rollback
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             cluster_node_remove;
             code_deploy;
             config_diff;
             code_test;
             cluster_node_in;
         shell_unlock;
             ;;
     rollback)
         shell_lock;
             rollback;
         shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #

  

先執行下檢查語法錯誤

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@linux-node1 scripts] # ./deploy.sh deploy
code_get
code_build
code_config
code_tar
code_scp
cluster_node_remove
code_deploy
config_diff
code_test
cluster_node_in
[root@linux-node1 scripts] # ./deploy.sh rollback
rollback

  

加個sleep測試下鎖的功能
給一個函數加下sleep 測試下執行中,另外的人是否能夠執行這個腳本

?
1
2
3
4
code_get(){
     echo code_get
     sleep 60;
}

  

運行腳本

?
1
2
[root@linux-node1 scripts] # ./deploy.sh deploy
code_get
新打開一個窗口執行測試
正常狀況下一個窗口執行部署,再開一個窗口確定執行不了
?
1
2
3
4
5
6
7
[root@linux-node1 scripts] # ./deploy.sh deploy
Deploy is running
[root@linux-node1 scripts] # ./deploy.sh deploy
Deploy is running
[root@linux-node1 scripts] # ./deploy.sh rollback
Deploy is running
[root@linux-node1 scripts] #

  

 

增長日誌功能

其實就是echo一行到日誌文件中,每一個函數寫加echo 寫到日誌裏,這樣比較low
能不能寫個日誌函數,加時間戳。之後日誌函數能夠複製到其它腳本里

?
1
2
3
4
5
[www@linux-node1 scripts]$ date "+%Y-%m-%d"
2017-04-23
[www@linux-node1 scripts]$ date "+%H-%M-%S"
22-10-34
[www@linux-node1 scripts]$

  

腳本默認從上到下執行,遇到函數先加載,可是不執行
繼續優化下。以下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
 
# Date/Time Veriables
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/deploy"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"

 

還不能這麼寫,否則之後的時間都是同樣的
能夠改爲這樣,它不會執行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
 
# Date/Time Veriables
CDATE= 'date "+%Y-%m-%d"'
CTIME= 'date "+%H-%M-%S"'
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/deploy"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"

打包的時候,也用到時間戳命名了。還得用一個固定不變的時間用於打包
由於解壓的時候,scp的時候用必須知道肯定的包名字。
這裏用到了2個時間,log-date是讓它不執行的,cdate是讓它執行的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/deploy"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
 
本身先測試下
?
1
2
3
4
5
6
7
[root@linux-node1 ~] # LOG_DATE='date "+%Y-%m-%d"'
[root@linux-node1 ~] # LOG_TIME='date "+%H-%M-%S"'
[root@linux-node1 ~] # echo $LOG_DATE
date "+%Y-%m-%d"
[root@linux-node1 ~] # echo $LOG_TIME
date "+%H-%M-%S"
[root@linux-node1 ~] #

  

eval的用法
?
1
2
3
4
5
[root@linux-node1 ~] # eval $LOG_TIME
22-21-05
[root@linux-node1 ~] # eval $LOG_DATE
2017-04-23
[root@linux-node1 ~] #
 
怎麼可讓它在一行呢。暫時沒找到辦法(卻是能夠單獨定義一個時間變量)
?
1
2
3
4
[root@linux-node1 ~] # eval $LOG_DATE && eval $LOG_TIME
2017-04-23
22-22-48
[root@linux-node1 ~] #   

單獨定義一個時間變量(這裏用不到,可是能夠實現)

?
1
2
3
4
5
6
[root@linux-node1 ~] # D_T='date "+%Y-%m-%d-%H-%M-%S"'
[root@linux-node1 ~] # echo $D_T
date "+%Y-%m-%d-%H-%M-%S"
[root@linux-node1 ~] # eval $D_T
2017-04-26-19-33-01
[root@linux-node1 ~] #

  

編寫記錄日誌函數
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}
 
writelog(){
         LOGINFO=$1
         echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
 
shell_lock(){
         touch ${LOCK_FILE}
}
 
shell_unlock(){
         rm -f ${LOCK_FILE}
}

  

這樣code_get函數就記錄日誌了
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
shell_lock(){
         touch ${LOCK_FILE}
}
 
shell_unlock(){
         rm -f ${LOCK_FILE}
}
 
code_get(){
     writelog code_get;
}
 
code_build(){
     echo code_build
}

  

規範點,加上雙引號,分號可要可不要,寫上不會報錯
?
1
2
3
4
5
6
7
8
9
10
11
shell_unlock(){
         rm -f ${LOCK_FILE}
}
 
code_get(){
    writelog "code_get" ;
}
 
code_build(){
     echo code_build
}

  

獲取代碼
把代碼放哪裏?
爲何建立這些目錄,寫着寫着你就知道了
?
1
2
3
4
5
6
7
8
9
10
11
12
[root@linux-node1 scripts] # mkdir /deploy/config -p
[root@linux-node1 scripts] # mkdir /deploy/tmp -p
[root@linux-node1 scripts] # mkdir /deploy/tar -p
[root@linux-node1 scripts] # mkdir /deploy/code -p
[root@linux-node1 scripts] # cd /deploy/
[root@linux-node1 deploy] # ll
total 0
drwxr-xr-x 2 root root 6 Apr 23 22:37 code
drwxr-xr-x 2 root root 6 Apr 23 22:37 config
drwxr-xr-x 2 root root 6 Apr 23 22:37 tar
drwxr-xr-x 2 root root 6 Apr 23 22:37 tmp
[root@linux-node1 deploy] #

  

 

最終目錄創建成這種
?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@linux-node1 deploy] # cd code/
[root@linux-node1 code] # mkdir web-demo -p
[root@linux-node1 code] # cd ..
[root@linux-node1 deploy] # tree
.
├── code
│   └── web-demo
├── config
├── tar
└── tmp
 
5 directories, 0 files
[root@linux-node1 deploy] #

  

修改腳本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}

  

有的不須要編譯,拉下代碼以後,能夠先把配置文件放進去
?
1
2
3
4
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
}

  

配置文件不要放這個目錄下,這個目錄只用來更新---git pull.你很差判斷配置文件是倉庫裏面的,仍是你專門下載下來的(最佳實踐)
規劃的時候,只讓這裏目錄執行git pull

下面這個目錄用於整合配置文件。
?
1
TMP_DIR= "/deploy/tmp"

  

繼續優化獲取代碼的函數

?
1
2
3
4
5
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
}

  

配置操做的函數時候,以爲不合適,應該區分項目,標準化。好比web-demo能夠理解爲一個項目包名字

?
1
2
3
4
5
6
#Code Env
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"

  

目錄新建

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@linux-node1 scripts] # cd /deploy/
[root@linux-node1 deploy] # cd config/
[root@linux-node1 config] # mkdir web-demo
[root@linux-node1 config] # cd ..
[root@linux-node1 deploy] # tree
.
├── code
│   └── web-demo
├── config
│   └── web-demo
├── tar
└── tmp
 
6 directories, 0 files
[root@linux-node1 deploy] #

  

模擬下,在裏面寫上hehe
?
1
2
3
4
5
6
[root@linux-node1 deploy] # cd config/
[root@linux-node1 config] # cd web-demo/
[root@linux-node1 web-demo] # vim config.ini
[root@linux-node1 web-demo] # cat config.ini
hehe
[root@linux-node1 web-demo] #

  

由於web-demo項目出現頻繁,把它弄成變量
?
1
2
3
4
5
6
7
#Code Env
PRO_NAME= "web-demo"
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"

  

調整下腳本,優化code_config函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
}
 
code_build(){
     echo code_build
}
 
code_config(){
     echo code_config
     /bin/cp -r $CONFIG_DIR/* $TMP_DIR/$PRO_NAME
}

  

規範下,給變量加大括號
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
}
 
code_build(){
     echo code_build
}
 
code_config(){
     echo code_config
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
}

注意是/bin/cp ,這樣原先有配置文件,這裏能夠直接替換了
若是開發把配置文件打包進去了。鏈接的是測試的庫,假如你部署生產環境了,鏈接測試的庫。出了問題,誰背黑鍋
運維是最後一道防線。開發和測試沒遇到。你背黑鍋

 
該給包重命名了
?
1
2
3
4
5
code_config(){
     echo code_config
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" - "${CDATE}-${CTIME}"
}

  

繼續優化

?
1
2
3
4
5
6
code_config(){
     echo code_config
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" - "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}

  

添加版本號,先隨便定義個版本

?
1
2
3
4
5
6
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "123"
}

  

如今沒有git pull 僞裝以echo 代替git pull
?
1
2
3
4
5
6
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && echo "git pull"
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "123"
}

  

屬組受權
?
1
2
[root@linux-node1 scripts] # chown -R www:www /deploy/
[root@linux-node1 scripts] #

  

內容爲hehe,生成代碼(頁面)
?
1
2
3
4
5
[root@linux-node1 scripts] # cd /deploy/code/web-demo/
[root@linux-node1 web-demo] # echo hehe>>index.html
[root@linux-node1 web-demo] # cat index.html
hehe
[root@linux-node1 web-demo] #

  

文件和目錄結構以下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@linux-node1 deploy] # tree
.
├── code
│   └── web-demo
│       └── index.html
├── config
│   └── web-demo
│       └── config.ini
├── tar
└── tmp
 
6 directories, 2 files
[root@linux-node1 deploy] #

  

以www用戶測試腳本
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@linux-node1 deploy] # cd /scripts/
[root@linux-node1 scripts] # chown -R www:www /scripts/deploy.sh
[root@linux-node1 scripts] # ll
total 12
-rw-r--r-- 1 root root  234 Apr  3 23:51 cobbler_list.py
-rw-r--r-- 1 root root 1533 Apr  4 00:01 cobbler_system_api.py
-rwxr-xr-x 1 www  www  1929 Apr 23 23:04 deploy.sh
[root@linux-node1 scripts] # su - www
Last login: Sun Apr 23 22:06:44 CST 2017 on pts /0
[www@linux-node1 scripts]$ . /deploy .sh  deploy
git pull
code_build
code_config
code_tar
code_scp
cluster_node_remove
code_deploy
config_diff
code_test
cluster_node_in
[www@linux-node1 scripts]$

  

測試結果
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[www@linux-node1 scripts]$ tree /deploy/
/deploy/
├── code
│   └── web-demo
│       └── index.html
├── config
│   └── web-demo
│       └── config.ini
├── tar
└── tmp
     ├── web-demo_123-2017-04-23-23-12-15
     │   ├── config.ini
     │   └── index.html
     └── web-demo_123-2017-04-23-23-13-20
         ├── config.ini
         └── index.html
 
8 directories, 6 files
[www@linux-node1 scripts]$

  

版本和時間之間改爲下劃線
?
1
2
3
4
5
6
code_config(){
     echo code_config
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}

  

再次執行
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_config
code_tar
code_scp
cluster_node_remove
code_deploy
config_diff
code_test
cluster_node_in
[www@linux-node1 scripts]$ tree /deploy/
/deploy/
├── code
│   └── web-demo
│       └── index.html
├── config
│   └── web-demo
│       └── config.ini
├── tar
└── tmp
     ├── web-demo_123-2017-04-23-23-12-15
     │   ├── config.ini
     │   └── index.html
     ├── web-demo_123-2017-04-23-23-13-20
     │   ├── config.ini
     │   └── index.html
     └── web-demo_123_2017-04-23-23-17-20
         ├── config.ini
         └── index.html
 
9 directories, 8 files
[www@linux-node1 scripts]$

  

 

能夠看到tmp目錄須要按期清理
給下面2個函數加寫日誌功能
?
1
2
3
4
5
6
7
8
9
10
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
 
code_tar(){
     writelog "code_tar"
}

  

打包,記錄日誌
?
1
2
3
4
5
code_tar(){
     writelog "code_tar"
     cd ${TMP_DIR} && tar cfz ${PKG_NAME}. tar .gz ${PKG_NAME}
     writelog "${PKG_NAME}.tar.gz"
}

  

再次測試腳本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
cluster_node_remove
code_deploy
config_diff
code_test
cluster_node_in
[www@linux-node1 scripts]$ tree /deploy/
/deploy/
├── code
│   └── web-demo
│       └── index.html
├── config
│   └── web-demo
│       └── config.ini
├── tar
└── tmp
     ├── web-demo_123-2017-04-23-23-12-15
     │   ├── config.ini
     │   └── index.html
     ├── web-demo_123-2017-04-23-23-13-20
     │   ├── config.ini
     │   └── index.html
     ├── web-demo_123_2017-04-23-23-17-20
     │   ├── config.ini
     │   └── index.html
     ├── web-demo_123_2017-04-23-23-22-09
     │   ├── config.ini
     │   └── index.html
     └── web-demo_123_2017-04-23-23-22-09. tar .gz
 
10 directories, 11 files
[www@linux-node1 scripts]$

  

 

 

準備拷貝到目標服務器

前4步都完畢,開始第五步--拷貝到目標服務器

 

 遍歷節點

?
1
2
3
4
5
[www@linux-node1 scripts]$ node_list= "192.168.58.11 192.168.58.12"
[www@linux-node1 scripts]$ for node in $node_list; do echo $node; done
192.168.58.11
192.168.58.12
[www@linux-node1 scripts]$

  

腳本里添加node_list

?
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
#Node List
NODE_LIST= "192.168.58.11 192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )

  

 分發到目標節點

?
1
2
3
4
5
6
code_scp(){
     echo code_scp
     for node in $NODE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz   $node: /opt/webroot/
       done
}

  

2臺機器創建webroot
?
1
2
3
4
5
6
[root@linux-node1 scripts] # mkdir /opt/webroot -p
[root@linux-node1 scripts] # chown -R www:www /opt/webroot
[root@linux-node1 scripts] #
[root@linux-node2 ~] # mkdir /opt/webroot -p
[root@linux-node2 ~] # chown -R www:www /opt/webroot
[root@linux-node2 ~] #

  

 完善拷貝函數

?
1
2
3
4
5
6
code_scp(){
     echo code_scp
     for node in $NODE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
       done
}

  

 

再次部署測試下
?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_123_2017-04-23-23-33-50. tar .gz                100%  204     0.2KB /s   00:00   
web-demo_123_2017-04-23-23-33-50. tar .gz                100%  204     0.2KB /s   00:00   
cluster_node_remove
code_deploy
config_diff
code_test
cluster_node_in
[www@linux-node1 scripts]$

  

檢查
?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 scripts]$ tree /opt/webroot/
/opt/webroot/
└── web-demo_123_2017-04-23-23-33-50. tar .gz
 
0 directories, 1 file
[www@linux-node1 scripts]$
[root@linux-node2 ~] # tree /opt/webroot/
/opt/webroot/
└── web-demo_123_2017-04-23-23-33-50. tar .gz
 
0 directories, 1 file
[root@linux-node2 ~] #

  

 該第6步了,寫個日誌代替

?
1
2
3
cluster_node_remove(){
writelog "cluster_node_remove"
}
 
解壓完畢,拷貝差別文件。你要把差別文件單獨放一個目錄下。不要和配置文件放一塊兒
修改上面,上面是相同配置目錄文件
?
1
2
3
4
5
6
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR}/* ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
改爲以下
?
1
2
3
4
5
6
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR} /base/ * ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}

  

建立配置文件目錄,base存放相同的配置,other存放差別配置

?
1
2
3
4
5
6
7
8
9
[www@linux-node1 scripts]$ cd /deploy/config/web-demo/
[www@linux-node1 web-demo]$ mkdir base
[www@linux-node1 web-demo]$ mkdir other
[www@linux-node1 web-demo]$ ll
total 4
drwxrwxr-x 2 www www 6 Apr 23 23:38 base
-rw-r--r-- 1 www www 5 Apr 23 22:46 config.ini
drwxrwxr-x 2 www www 6 Apr 23 23:38 other
[www@linux-node1 web-demo]$

 

調整下配置文件所在目錄

?
1
2
3
4
5
6
7
[www@linux-node1 web-demo]$ mv config.ini base/
[www@linux-node1 web-demo]$ cd other/
[www@linux-node1 other]$ echo 192.168.58.12-config >>192.168.58.12. crontab .xml
[www@linux-node1 other]$ ll
total 4
-rw-rw-r-- 1 www www 21 Apr 23 23:39 192.168.58.12. crontab .xml
[www@linux-node1 other]$

  

拷貝差別文件到目標服務器的目標目錄
?
1
2
3
4
5
6
7
8
9
code_deploy(){
     echo code_deploy
     cd /opt/webroot/ && tar xfz ${PKG_NAME}. tar .gz
}
 
config_diff(){
     echo config_diff
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /opt/webroot/ ${PKG_NAME}
}

  

再次測試
?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_123_2017-04-23-23-43-48. tar .gz                       100%  204     0.2KB /s   00:00   
web-demo_123_2017-04-23-23-43-48. tar .gz                       100%  204     0.2KB /s   00:00   
code_deploy
config_diff
192.168.58.12. crontab .xml                                     100%   21     0.0KB /s   00:00   
code_test
cluster_node_in
[www@linux-node1 scripts]$

  

上面還有不足的地方,scp到目標服務器並解壓,應該使用ssh遠程執行、。上面腳本遠程node2上解壓是失敗的
腳本再次改造下,把部署的函數和差別配置合併到一塊兒

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /opt/webroot/ ${PKG_NAME} /crontab .xml
}
 
code_test(){
     echo code_test
}

  

建立webroot

?
1
2
3
4
5
6
7
8
[root@linux-node1 ~] # mkdir /webroot
[root@linux-node1 ~] # chown -R www:www /webroot
[root@linux-node1 ~] #
 
 
[root@linux-node2 ~] # mkdir /webroot
[root@linux-node2 ~] # chown -R www:www /webroot
[root@linux-node2 ~] #

  

再次對部署函數優化,添加使用軟連接參數(這個是秒級回滾的關鍵)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /opt/webroot/ ${PKG_NAME} /crontab .xml
     ln -s /opt/webroot/ ${PKG_NAME} /webroot/web-demo
}
 
code_test(){
     echo code_test
}

  

再次對腳本優化
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /opt/webroot/ ${PKG_NAME} /crontab .xml
     rm -f /webroot/web-demo && ln -s /opt/webroot/ ${PKG_NAME} /webroot/web-demo
}
 
code_test(){
     echo code_test
}

  

自動化部署的精髓,建立軟連接
對腳本優化,每一個服務器要執行相同的操做,所以放在循環裏
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       rm -f /webroot/web-demo && ln -s /opt/webroot/ ${PKG_NAME} /webroot/web-demo
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /opt/webroot/ ${PKG_NAME} /crontab .xml
}
 
code_test(){
     echo code_test
}

  

拷貝差別文件應該建立軟連接以後拷貝,其實就是路徑寫的少點
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       rm -f /webroot/web-demo && ln -s /opt/webroot/ ${PKG_NAME} /webroot/web-demo
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
code_test(){
     echo code_test
}
 
第一次沒軟連接,會報錯。須要先手動建立個軟鏈接,或者先建立web-demo目錄
其實我覺的徹底能夠在上面腳本里加入mkdir /webroot/web-demo -p 這樣永遠不會錯
?
1
2
3
4
5
[www@linux-node1 scripts]$ cd /webroot/
[www@linux-node1 webroot]$ mkdir web-demo -p
[www@linux-node1 webroot]$
[root@linux-node2 webroot] # mkdir web-demo -p
[root@linux-node2 webroot] #

  

再次執行部署腳本。node1完成了
繼續優化腳本,rm -rf 這裏要寫爲-rf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
code_test(){
     echo code_test
}

  

測試腳本

?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_123_2017-04-24-00-01-24. tar .gz                       100%  204     0.2KB /s   00:00   
web-demo_123_2017-04-24-00-01-24. tar .gz                       100%  204     0.2KB /s   00:00   
code_deploy
192.168.58.12. crontab .xml                                     100%   21     0.0KB /s   00:00   
. /deploy .sh: line 113: config_diff: command not found
code_test
cluster_node_in
[www@linux-node1 scripts]$
主函數裏,刪除這個 config_diff
 
繼續測試和檢查
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
測試
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_123_2017-04-24-00-02-44. tar .gz                       100%  205     0.2KB /s   00:00   
web-demo_123_2017-04-24-00-02-44. tar .gz                       100%  205     0.2KB /s   00:00   
code_deploy
192.168.58.12. crontab .xml                                     100%   21     0.0KB /s   00:00   
code_test
cluster_node_in
[www@linux-node1 scripts]$
檢查
[www@linux-node1 scripts]$ ll /webroot/
total 0
lrwxrwxrwx 1 www www 45 Apr 24 00:02 web-demo -> /opt/webroot/web-demo_123_2017-04-24-00-02-44
[www@linux-node1 scripts]$
 
[root@linux-node2 webroot] # ll /webroot/
total 0
lrwxrwxrwx 1 www www 45 Apr 24 00:02 web-demo -> /opt/webroot/web-demo_123_2017-04-24-00-02-44
[root@linux-node2 webroot] #

  

模擬版本更新,把版本寫成456
?
1
2
3
4
5
6
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && echo "git pull"
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "456"
}

  

繼續測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_456_2017-04-24-00-04-05. tar .gz                       100%  204     0.2KB /s   00:00   
web-demo_456_2017-04-24-00-04-05. tar .gz                       100%  204     0.2KB /s   00:00   
code_deploy
192.168.58.12. crontab .xml                                     100%   21     0.0KB /s   00:00   
code_test
cluster_node_in
[www@linux-node1 scripts]$ ll /webroot/
total 0
lrwxrwxrwx 1 www www 45 Apr 24 00:04 web-demo -> /opt/webroot/web-demo_456_2017-04-24-00-04-05
[www@linux-node1 scripts]$

檢查

連接 到了新的版本

 

#################################################################################

node1和node2都裝apache
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@linux-node1 ~] # yum install httpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.zju.edu.cn
  * epel: mirrors.tuna.tsinghua.edu.cn
  * extras: mirrors.zju.edu.cn
  * updates: mirrors.163.com
Package httpd-2.4.6-45.el7.centos.4.x86_64 already installed and latest version
Nothing to do
[root@linux-node1 ~] #
 
[root@linux-node2 ~] # yum install httpd -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.zju.edu.cn
  * extras: mirrors.zju.edu.cn
  * updates: mirrors.163.com
Package httpd-2.4.6-45.el7.centos.4.x86_64 already installed and latest version
Nothing to do
[root@linux-node2 ~] #

  

把apache的根目錄都改爲/opt/webroot

?
1
2
[root@linux-node2 ~] # vim /etc/httpd/conf/httpd.conf
[root@linux-node2 ~] #

上面是默認的,改爲以下配置

 

 node1也改爲以下

 

 啓動apache

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@linux-node2 ~] # systemctl start httpd
[root@linux-node2 ~] # lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE /OFF NODE NAME
httpd   21937   root    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
httpd   21938 apache    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
httpd   21939 apache    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
httpd   21940 apache    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
httpd   21941 apache    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
httpd   21942 apache    4u  IPv6 132900      0t0  TCP *:http (LISTEN)
[root@linux-node2 ~] #
 
[root@linux-node1 ~] # systemctl start httpd
[root@linux-node1 ~] # lsof -i:80
COMMAND   PID   USER   FD   TYPE DEVICE SIZE /OFF NODE NAME
httpd   23765   root    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
httpd   23767 apache    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
httpd   23768 apache    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
httpd   23769 apache    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
httpd   23770 apache    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
httpd   23771 apache    4u  IPv6 117035      0t0  TCP *:http (LISTEN)
[root@linux-node1 ~] #

訪問網頁

修改首頁

?
1
2
3
4
5
6
7
8
9
10
11
[www@linux-node1 ~]$ cd /deploy/code/
[www@linux-node1 code]$ cd web-demo/
[www@linux-node1 web-demo]$ pwd
/deploy/code/web-demo
[www@linux-node1 web-demo]$ ll
total 4
-rw-r--r-- 1 www www 9 Apr 26 22:28 index.html
[www@linux-node1 web-demo]$ cat index.html
nmap.com
[www@linux-node1 web-demo]$ cd /scripts/
[www@linux-node1 scripts]$
 
再次部署
?
1
2
3
4
5
6
7
8
9
10
11
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
code_scp
web-demo_456_2017-04-27-21-40-36. tar .gz     100%  220     0.2KB /s   00:00   
web-demo_456_2017-04-27-21-40-36. tar .gz     100%  220     0.2KB /s   00:00   
code_deploy
192.168.58.12. crontab .xml                   100%   21     0.0KB /s   00:00   
code_test
cluster_node_in
[www@linux-node1 scripts]$
 
 
檢查部署結果,注意ctrl+f5

 

查看此時自動化部署腳本的配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
[root@linux-node1 scripts] # cat deploy.sh
#!/bin/bash
 
#Dir List
#mkdir -p /deploy/code/web-demo/
#mkdir -p /deploy/config/web-demo/base
#mkdir -p /deploy/config/web-demo/other
#mkdir -p /deploy/tar
#mkdir -p /deploy/tmp
#mkdir -p /opt/webroot
#mkdir -p /webroot
#chown -R www:www /deploy
#chown -R www:www /opt/webroot
#chown -R www:www /webroot
 
#Node List
NODE_LIST= "192.168.58.11 192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
PRO_NAME= "web-demo"
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}
 
writelog(){
         LOGINFO=$1
         echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
 
shell_lock(){
     touch ${LOCK_FILE}
}
shell_unlock(){
     rm -f ${LOCK_FILE}
}
 
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && echo "git pull"
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "456"
}
 
code_build(){
     echo code_build
}
 
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR} /base/ * ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
 
code_tar(){
     writelog "code_tar"
     cd ${TMP_DIR} && tar cfz ${PKG_NAME}. tar .gz ${PKG_NAME}
     writelog "${PKG_NAME}.tar.gz"
}
 
code_scp(){
     echo "code_scp"
     for node in $NODE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
code_test(){
     echo code_test
}
 
cluster_node_in(){
     echo cluster_node_in
}
 
rollback(){
     echo rollback
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             cluster_node_remove;
             code_deploy;
             code_test;
             cluster_node_in;
         shell_unlock;
             ;;
     rollback)
         shell_lock;
             rollback;
         shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #

  

 修改測試部分,完善測試函數

過濾到,返回0,過濾不到返回其它的
-s靜默模式

?
1
2
3
4
5
6
7
8
[root@linux-node1 scripts] # curl --head http://192.168.58.11/index.html | grep "200 OK"
   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
   0     9    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP /1 .1 200 OK
[root@linux-node1 scripts] # curl -s --head http://192.168.58.11/index.html | grep "200 OK"
HTTP /1 .1 200 OK
[root@linux-node1 scripts] #

  

準備是這樣的,部署一個,測試一個,通了才繼續往下部署。這樣才合理
不少公司拿預熱節點部署。經過以後再部署生產節點

 

先修改下上面,把主機分組,模擬分組部署
GROUP1_LIST模擬預熱節點
GROUP2_LIST 模擬其他的所有節點
?
1
2
3
4
5
6
7
8
9
10
#chown -R www:www /opt/webroot
#chown -R www:www /webroot
 
#Node List
GROUP1_LIST= "192.168.58.11"
GROUP2_LIST= "192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'

  

scp這裏改下
?
1
2
3
4
5
6
7
8
9
code_scp(){
     writelog "code_scp"
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP2_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
把code_deploy也修改下
下面這裏是修改以前
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
code_deploy(){
     echo code_deploy
     for node in $NODE_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
code_test(){
     echo code_test
}

  

修改以後

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
group1_deploy(){
     echo code_deploy
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
}
 
group2_deploy(){
     echo code_deploy
     for node in $GROUP2_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
cluster_node_in(){
     echo cluster_node_in
}

  

 

第一組弄的只有1臺機器,做爲預生產節點
一個節點若是重啓須要1分鐘的話,5個節點豈不是5分鐘。
因此,1個節點先做爲預生產節點,部署完畢,測試完畢以後,其它節點能夠直接部署了。思想上是這樣

添加group1_test測試函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
group1_deploy(){
     echo code_deploy
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
}
 
group1_test(){
     curl -s -- head http: //192 .168.58.11 /index .html | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       writelog "test error" && exit ;
     fi
}
 
group2_deploy(){
     echo code_deploy
     for node in $GROUP2_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}

  

因爲group2也要測試,而且每一個節點都要測試
所以能夠把測試的部分提取出來,封裝成函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
shell_lock(){
         touch ${LOCK_FILE}
}
 
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       writelog "test error" && exit ;
     fi
}
 
shell_unlock(){
         rm -f ${LOCK_FILE}
}

  

測試部分能夠這麼寫
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cluster_node_remove(){
     writelog "cluster_node_remove"
}
 
group1_deploy(){
     echo 「code_deploy」
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
}
 
group1_test(){
     curl_test "http://192.168.58.11/index.html"
     echo "add to cluster"
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
}
 
group1_test(){
     url_test "http://192.168.58.11/index.html"
     echo "add to cluster"
}
 
group2_deploy(){
     echo 「code_deploy」
     for node in $GROUP2_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
group2_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
cluster_node_in(){
     echo cluster_node_in
}

  

能夠把下面函數刪除。是否能夠加入集羣,放在測試函數便可

?
1
2
3
cluster_node_in(){
     echo cluster_node_in
}

  

 

主函數會這麼寫
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cluster_node_remove(){
     writelog "cluster_node_remove"
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             group1_deploy;
             group1_test;
             group2_deploy;
             group2_test;
             shell_unlock;
             ;;
     rollback)
             shell_lock;
             rollback;
             shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1

  

測試腳本
?
1
2
3
4
5
6
7
8
9
10
11
12
13
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
web-demo_456_2017-04-27-22-41-54. tar .gz                        100%  214     0.2KB /s   00:00   
web-demo_456_2017-04-27-22-41-54. tar .gz                        100%  214     0.2KB /s   00:00   
code_deploy
HTTP /1 .1 200 OK
add to cluster
code_deploy
192.168.58.12. crontab .xml                                      100%   21     0.0KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
[www@linux-node1 scripts]$

  

沒問題,能夠訪問

 

繼續優化腳本
pre是預熱節點,生產只有1個
group1_list裏面有多個

?
1
2
3
4
5
6
7
8
9
10
#Node List
PRE_LIST= "192.168.58.11"
GROUP1_LIST= "192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
下面這裏改下
?
1
2
3
4
5
6
7
8
9
code_scp(){
     writelog "code_scp"
     for node in $PRE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}

  

上面的remove函數不須要了。刪除便可,以下
?
1
2
3
cluster_node_remove(){
     writelog "cluster_node_remove"
}

 

 

下面的group_list 名字還須要改,再次優化下,把原先的group1_list改爲pre_test,而後下面這裏的改爲group1
下面的group1_test應該改爲for循環,測試裏面的節點,能夠測一個加一個節點到集羣。還能夠測試完,一塊加測試的時候,
若是你只有2-3個節點,不必寫for循環
優化以後內容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@linux-node1 scripts] # sed -n '89,128p' deploy.sh
code_scp(){
     writelog "code_scp"
     for node in $PRE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
 
pre_deploy(){
     writelog "remove from cluster"
       ssh $PRE_LIST "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $PRE_LIST "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
}
 
pre_test(){
     url_test "http://${PRE_LIST}/index.html"
     echo "add to cluster"
}
 
group1_deploy(){
     writelog  "remove from cluster"
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
group1_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
rollback(){
     echo rollback
}
[root@linux-node1 scripts] #

測試的時候,若是隻有幾個節點,直接幾行地址就好了嘛。不必寫太複雜

?
1
2
3
4
5
6
group1_test(){
     url_test "http://192.168.58.12/index.html"
     url_test "http://192.168.58.12/index.html"
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
主函數部分改爲下面
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@linux-node1 scripts] # sed -n '130,$p' deploy.sh
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             pre_deploy;
             pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
         shell_lock;
             rollback;
         shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #

  

改下代碼,區分下主頁顯示和以前的不一樣

?
1
2
3
4
5
6
7
[www@linux-node1 scripts]$ cd /deploy/code/web-demo/
[www@linux-node1 web-demo]$ cat index.html
nmap.com
[www@linux-node1 web-demo]$ echo "www.nmap.com" > index.html
[www@linux-node1 web-demo]$ cat index.html
www.nmap.com
[www@linux-node1 web-demo]$
 
再次部署
?
1
2
3
4
5
6
7
8
9
10
11
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
web-demo_456_2017-04-27-23-17-27. tar .gz                                100%  220     0.2KB /s   00:00   
web-demo_456_2017-04-27-23-17-27. tar .gz                                100%  220     0.2KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
192.168.58.12. crontab .xml                                              100%   21     0.0KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
[www@linux-node1 scripts]$

  

檢查頁面,沒問題

 

 

此時腳本內容以下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
[root@linux-node1 scripts] # cat deploy.sh
#!/bin/bash
 
#Dir List
#mkdir -p /deploy/code/web-demo/
#mkdir -p /deploy/config/web-demo/base
#mkdir -p /deploy/config/web-demo/other
#mkdir -p /deploy/tar
#mkdir -p /deploy/tmp
#mkdir -p /opt/webroot
#mkdir -p /webroot
#chown -R www:www /deploy
#chown -R www:www /opt/webroot
#chown -R www:www /webroot
 
#Node List
PRE_LIST= "192.168.58.11"
GROUP1_LIST= "192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
PRO_NAME= "web-demo"
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 [ deploy | rollback ]"
}
 
writelog(){
         LOGINFO=$1
         echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
 
shell_lock(){
     touch ${LOCK_FILE}
}
 
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       writelog "test error" && exit ;
     fi
}
 
shell_unlock(){
     rm -f ${LOCK_FILE}
}
 
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && echo "git pull"
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "456"
}
 
code_build(){
     echo code_build
}
 
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR} /base/ * ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
 
code_tar(){
     writelog "code_tar"
     cd ${TMP_DIR} && tar cfz ${PKG_NAME}. tar .gz ${PKG_NAME}
     writelog "${PKG_NAME}.tar.gz"
}
 
code_scp(){
     writelog "code_scp"
     for node in $PRE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
 
pre_deploy(){
     writelog "remove from cluster"
       ssh $PRE_LIST "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $PRE_LIST "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
}
 
pre_test(){
     url_test "http://${PRE_LIST}/index.html"
     echo "add to cluster"
}
 
group1_deploy(){
     writelog "remove  from cluster"
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
group1_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
rollback(){
     echo rollback
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             pre_deploy;
             pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
         shell_lock;
             rollback;
         shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #

  

 

自動化部署實戰-秒級回滾

 

再加個能緊急回滾的,就不考慮用戶體驗了。以緊急回滾爲主。也不測試了

 

更緊急流程。直接執行回滾上一個版本

 

先寫個第一種正常流程的,列出回滾版本的函數

所以ROLLBACK_VER=$2 和rollback $ROLLBACK_VER ;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@linux-node1 scripts] # sed -n '129,$p' deploy.sh
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     ROLLBACK_VER=$2     
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             pre_deploy;
             pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
         shell_lock;
             rollback $ROLLBACK_VER ; 
         shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1
[root@linux-node1 scripts] #

  

 完善rollback函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
group1_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       ssh $node "rm -f /webroot/web-demo && ln-s /opt/webroo/$1 /webroot/web-demo"
     done
}
 
rollback(){
     case $1 in
         list)
           ls -l /opt/webroot/ *. tar .gz
           ;;
         *)
           rollback_fun $1
     esac
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then

  

最前面加上以下行,而後寫上全部節點
?
1
2
3
4
5
6
7
8
#Node List
PRE_LIST= "192.168.58.11"
GROUP1_LIST= "192.168.58.12"
ROLLBACK_LIST= "192.168.58.11 192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'

  

下面這裏改爲以下
?
1
2
3
usage(){
     echo  $ "Usage: $0 { deploy | rollback [ list |version ]}"
}

上面寫不合適,應該不傳參數列出來能夠回退的版本
如今若是rollback一個不存在的版本,它會把軟連接刪除了,回退也失敗,所以能夠反過來
再改下
之因此把這個if判斷寫在for下面。主要是,若是隻部署了預生產節點,沒部署其它節點
那麼回退的時候就能夠把預部署節點回退了。不然其它的都回退失敗了,而且還要檢測其它節點上有沒有包。我以爲這裏應該ssh -d檢測

?
1
2
3
4
5
6
7
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       if [ -d /opt/webroot/ $1 ]; then
       ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroo/$1 /webroot/web-demo"
       fi
     done
}
繼續改造(其實這裏經過ssh以後判斷其他節點目錄是否存在的)
?
1
2
3
4
5
6
7
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       if [ -d /opt/webroot/ $1 ]; then
         ssh $node "[ -d /opt/webroot/$1 ] && rm -f /webroot/web-demo && ln -s /opt/webroo/$1 /webroot/web-demo"
       fi
     done
}

  

這裏就不對了吧。rollback以後沒跟參數竟然執行了
?
1
2
[www@linux-node1 scripts]$ . /deploy .sh rollback
[www@linux-node1 scripts]$

  

改造下
?
1
2
3
4
5
6
7
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       if [ -d /opt/webroot/ $1 ]; then
       ssh $node "if [ -d /opt/webroot/$1 ];then rm -f /webroot/web-demo && ln -s /opt/webroo/$1 /webroot/web-demo;else echo " hehe ";fi"
       fi
     done
}

  

 執行

?
1
2
[www@linux-node1 scripts]$ . /deploy .sh rollback
[www@linux-node1 scripts]$

  

上面執行爲和以前顯示一致,再改造下。
?
1
2
3
4
5
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       ssh $node "if [ -d /opt/webroot/$1 ];then rm -f /webroot/web-demo && ln -s /opt/webroo/$1 /webroot/web-demo;else echo " hehe ";fi"
     done
}

 測試

?
1
2
[www@linux-node1 scripts]$ . /deploy .sh rollback
[www@linux-node1 scripts]$

執行仍是沒達到效果
先妥協了。如下面爲準。雖然若是少參數,會致使執行不成功,先這樣

?
1
2
3
4
rollback_fun(){
     for node in $ROLLBACK_LIST; do      ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroo/$1 /webroot/web-demo"
     done
}

  

注意把腳本最後一行改爲main $1 $2
?
1
2
3
4
5
6
7
8
9
10
11
12
[root@linux-node1 scripts] # tail -10 deploy.sh
             shell_lock;
             rollback ${ROLLBACK_VER};
             shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1 $2
[root@linux-node1 scripts] #

  

繼續測試,修改首頁內容
?
1
2
3
4
5
6
7
8
9
[www@linux-node1 web-demo]$ cd /deploy/code/web-demo
[www@linux-node1 web-demo]$ ls
index.html
[www@linux-node1 web-demo]$ cat index.html
www.nmap.com
[www@linux-node1 web-demo]$ echo 'www.nmap.org' >index.html
[www@linux-node1 web-demo]$ cat index.html
www.nmap.org
[www@linux-node1 web-demo]$

  

 

繼續執行腳本部署
?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 web-demo]$ cd /scripts/
[www@linux-node1 scripts]$ . /deploy .sh deploy
git pull
code_build
web-demo_456_2017-04-28-21-17-17. tar .gz                                100%  222     0.2KB /s   00:00   
web-demo_456_2017-04-28-21-17-17. tar .gz                                100%  222     0.2KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
192.168.58.12. crontab .xml                                              100%   21     0.0KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
[www@linux-node1 scripts]$

 檢查頁面

 

列出回滾版本
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[www@linux-node1 scripts]$ . /deploy .sh rollback list
-rw-rw-r-- 1 www www 204 Apr 23 23:33 /opt/webroot/web-demo_123_2017-04-23-23-33-50 . tar .gz
-rw-rw-r-- 1 www www 204 Apr 23 23:43 /opt/webroot/web-demo_123_2017-04-23-23-43-48 . tar .gz
-rw-rw-r-- 1 www www 204 Apr 24 00:00 /opt/webroot/web-demo_123_2017-04-24-00-00-14 . tar .gz
-rw-rw-r-- 1 www www 204 Apr 24 00:01 /opt/webroot/web-demo_123_2017-04-24-00-01-24 . tar .gz
-rw-rw-r-- 1 www www 205 Apr 24 00:02 /opt/webroot/web-demo_123_2017-04-24-00-02-44 . tar .gz
-rw-rw-r-- 1 www www 204 Apr 24 00:04 /opt/webroot/web-demo_456_2017-04-24-00-04-05 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 27 21:40 /opt/webroot/web-demo_456_2017-04-27-21-40-36 . tar .gz
-rw-rw-r-- 1 www www 214 Apr 27 22:41 /opt/webroot/web-demo_456_2017-04-27-22-41-54 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 27 23:17 /opt/webroot/web-demo_456_2017-04-27-23-17-27 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 28 20:15 /opt/webroot/web-demo_456_2017-04-28-20-15-56 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 28 20:16 /opt/webroot/web-demo_456_2017-04-28-20-16-06 . tar .gz
-rw-rw-r-- 1 www www 221 Apr 28 20:24 /opt/webroot/web-demo_456_2017-04-28-20-24-23 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 28 20:25 /opt/webroot/web-demo_456_2017-04-28-20-25-47 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 28 20:28 /opt/webroot/web-demo_456_2017-04-28-20-28-03 . tar .gz
-rw-rw-r-- 1 www www 221 Apr 28 20:35 /opt/webroot/web-demo_456_2017-04-28-20-35-37 . tar .gz
-rw-rw-r-- 1 www www 222 Apr 28 20:43 /opt/webroot/web-demo_456_2017-04-28-20-43-32 . tar .gz
-rw-rw-r-- 1 www www 221 Apr 28 20:45 /opt/webroot/web-demo_456_2017-04-28-20-45-23 . tar .gz
-rw-rw-r-- 1 www www 220 Apr 28 20:48 /opt/webroot/web-demo_456_2017-04-28-20-48-01 . tar .gz
-rw-rw-r-- 1 www www 222 Apr 28 21:14 /opt/webroot/web-demo_456_2017-04-28-21-14-49 . tar .gz
-rw-rw-r-- 1 www www 222 Apr 28 21:17 /opt/webroot/web-demo_456_2017-04-28-21-17-17 . tar .gz
[www@linux-node1 scripts]$

  

回滾上一個版本web-demo_456_2017-04-28-21-14-49
?
1
2
[www@linux-node1 scripts]$ . /deploy .sh rollback web-demo_456_2017-04-28-21-14-49
[www@linux-node1 scripts]$

  

測試訪問

 

 

 回滾成功,此時腳本內容以下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
[root@linux-node1 scripts] # cat deploy.sh
#!/bin/bash
 
#Dir List
#mkdir -p /deploy/code/web-demo/
#mkdir -p /deploy/config/web-demo/base
#mkdir -p /deploy/config/web-demo/other
#mkdir -p /deploy/tar
#mkdir -p /deploy/tmp
#mkdir -p /opt/webroot
#mkdir -p /webroot
#chown -R www:www /deploy
#chown -R www:www /opt/webroot
#chown -R www:www /webroot
 
#Node List
PRE_LIST= "192.168.58.11"
GROUP1_LIST= "192.168.58.12"
ROLLBACK_LIST= "192.168.58.11 192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
PRO_NAME= "web-demo"
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 { deploy | rollback [ list |version ]}"
}
 
writelog(){
         LOGINFO=$1
         echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
 
shell_lock(){
     touch ${LOCK_FILE}
}
 
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       writelog "test error" && exit ;
     fi
}
 
shell_unlock(){
     rm -f ${LOCK_FILE}
}
 
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && echo "git pull"
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "456"
}
 
code_build(){
     echo code_build
}
 
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR} /base/ * ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
 
code_tar(){
     writelog "code_tar"
     cd ${TMP_DIR} && tar cfz ${PKG_NAME}. tar .gz ${PKG_NAME}
     writelog "${PKG_NAME}.tar.gz"
}
 
code_scp(){
     writelog "code_scp"
     for node in $PRE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
 
pre_deploy(){
     writelog "remove from cluster"
       ssh $PRE_LIST "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $PRE_LIST "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
}
 
pre_test(){
     url_test "http://${PRE_LIST}/index.html"
     echo "add to cluster"
}
 
group1_deploy(){
     writelog "remove  from cluster"
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
group1_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo"
     done
}
 
rollback(){
if [ -z $1 ]; then
     shell_unlock;
     echo "Please input rollback version" && exit ;
fi
     case $1 in
     list)
                 ls -l /opt/webroot/ *. tar .gz
             ;;
           *)
                 rollback_fun $1
     esac
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     ROLLBACK_VER=$2
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             pre_deploy;
             pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
             shell_lock;
             rollback ${ROLLBACK_VER};
             shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1 $2
[root@linux-node1 scripts] #

  

 再完善下腳本,rollback的時候,後面參數爲空,直接退出。也就是整個腳本的$2 爲空,退出腳本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
rollback(){
if [ -z $1 ]; then
     shell_unlock;
     echo "Please input rollback version" && exit ;
fi
     case $1 in
     list)
                 ls -l /opt/webroot/ *. tar .gz
             ;;
           *)
                 rollback_fun $1
     esac
}

  

自動化部署實戰-gitlab和部署和回滾

 

把gitllab裝上,參照連接
 

GitLab是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的Git項目倉庫,可經過Web界面進行訪問公開的或者私人項目。
GitLab擁有與Github相似的功能,可以瀏覽源代碼,管理缺陷和註釋。能夠管理團隊對倉庫的訪問,它很是易於瀏覽提交過的版本並提供一個文件歷史庫。
它還提供一個代碼片斷收集功能能夠輕鬆實現代碼複用,便於往後有須要的時候進行查找。
1.基礎環境準備

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@linux-node1 ~] # yum install curl policycoreutils openssh-server openssh-clients postfix -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.163.com
  * epel: mirror01.idc.hinet.net
  * extras: mirrors.aliyun.com
  * updates: mirrors.163.com
Package curl-7.29.0-35.el7.centos.x86_64 already installed and latest version
Package policycoreutils-2.5-11.el7_3.x86_64 already installed and latest version
Package openssh-server-6.6.1p1-35.el7_3.x86_64 already installed and latest version
Package openssh-clients-6.6.1p1-35.el7_3.x86_64 already installed and latest version
Package 2:postfix-2.10.1-6.el7.x86_64 already installed and latest version
Nothing to do
[root@linux-node1 ~] #

  

 啓動postfix

?
1
2
[root@linux-node1 ~] # systemctl start postfix
[root@linux-node1 ~] #

 

 

2.安裝gitlab-ce
注:因爲網絡問題,國內用戶,建議使用163或者阿里雲鏡像源進行安裝:

3.配置並啓動gitlab-ce
默認從163的源裏下載,清華大學的鏡像源makecache時報錯
此包286MB,安裝時佔800MB多。生產中注意下

?
1
2
3
4
5
6
7
8
9
10
[root@linux-node1 ~] # yum install gitlab-ce -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.163.com
  * epel: mirrors.ustc.edu.cn
  * extras: mirrors.163.com
  * updates: mirrors.163.com
Package gitlab-ce-9.1.0-ce.0.el7.x86_64 already installed and latest version
Nothing to do
[root@linux-node1 ~] #

  

4.配置並啓動gitlab-ce

?
1
[root@linux-node1 ~] # gitlab-ctl reconfigure

 

可使用gitlab-ctl管理gitlab,例如查看gitlab狀態:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@linux-node1 ~] # gitlab-ctl status
run: gitaly: (pid 46783) 154s; run: log: (pid 46524) 194s
run: gitlab-monitor: (pid 46839) 152s; run: log: (pid 46671) 170s
run: gitlab-workhorse: (pid 46793) 154s; run: log: (pid 46537) 188s
run: logrotate: (pid 46570) 186s; run: log: (pid 46569) 186s
run: nginx: (pid 47163) 2s; run: log: (pid 46544) 187s
run: node-exporter: (pid 46627) 179s; run: log: (pid 46626) 179s
run: postgres-exporter: (pid 46826) 153s; run: log: (pid 46657) 171s
run: postgresql: (pid 46369) 233s; run: log: (pid 46368) 233s
run: prometheus: (pid 46811) 153s; run: log: (pid 46607) 180s
run: redis: (pid 46312) 239s; run: log: (pid 46311) 239s
run: redis-exporter: (pid 46642) 177s; run: log: (pid 46641) 177s
run: sidekiq: (pid 46509) 195s; run: log: (pid 46508) 195s
run: unicorn: (pid 46483) 196s; run: log: (pid 46482) 196s
[root@linux-node1 ~] #

一些經常使用命令

?
1
2
3
4
gitlab-ctl status
gitlab-ctl stop
gitlab-ctl start
gitlab-ctl restart

  

先中止apache,由於gitlab佔用80端口。防止衝突
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@linux-node1 ~] # systemctl stop httpd
[root@linux-node1 ~] # gitlab-ctl restart
ok: run: gitaly: (pid 47781) 1s
ok: run: gitlab-monitor: (pid 47788) 0s
ok: run: gitlab-workhorse: (pid 47791) 1s
ok: run: logrotate: (pid 47808) 0s
ok: run: nginx: (pid 47814) 1s
ok: run: node-exporter: (pid 47823) 0s
ok: run: postgres-exporter: (pid 47829) 0s
ok: run: postgresql: (pid 47847) 0s
ok: run: prometheus: (pid 47855) 1s
ok: run: redis: (pid 47866) 0s
ok: run: redis-exporter: (pid 47870) 1s
ok: run: sidekiq: (pid 47877) 0s
ok: run: unicorn: (pid 47882) 1s
[root@linux-node1 ~] # netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID /Program name   
tcp        0      0 127.0.0.1:9121          0.0.0.0:*               LISTEN      47870 /redis_exporte
tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      47855 /prometheus   
tcp        0      0 127.0.0.1:9187          0.0.0.0:*               LISTEN      47829 /postgres_expo
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1896 /rsync         
tcp        0      0 127.0.0.1:9100          0.0.0.0:*               LISTEN      47823 /node_exporter
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1 /systemd          
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      47814 /nginx : master
tcp        0      0 127.0.0.1:9168          0.0.0.0:*               LISTEN      47788 /ruby         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1346 /dnsmasq       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      45266 /sshd         
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1284 /master        
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      47814 /nginx : master
tcp        0      0 127.0.0.1:25151         0.0.0.0:*               LISTEN      3249 /python2       
tcp6       0      0 :::873                  :::*                    LISTEN      1896 /rsync         
tcp6       0      0 :::3306                 :::*                    LISTEN      41106 /mysqld       
tcp6       0      0 :::111                  :::*                    LISTEN      1 /systemd          
tcp6       0      0 ::1:9168                :::*                    LISTEN      47788 /ruby         
tcp6       0      0 :::22                   :::*                    LISTEN      45266 /sshd         
tcp6       0      0 ::1:25                  :::*                    LISTEN      1284 /master        
[root@linux-node1 ~] #

 

###################################################################################

登陸gitlab
第一次登陸gitlab,須要爲root用戶修改密碼,root用戶也是gitlab的超級管理員。
http://192.168.58.11
設置密碼是gitlab 

 

密碼不夠長,從新設置下,設置密碼是12345678,生產中注意密碼複雜點

 

 

管理gitlab
使用root用戶和剛纔建立的密碼登陸後,你就能夠探索gitlab的奧祕了,能夠點擊圖中紅框的按鈕進入管理區域。

 

 登陸成功界面

 

 

gitlab平常管理的話,其實不多,就是天天進行備份
每小時備份也能夠,假如更新頻繁的話

 

 

去掉註冊的功能,不讓人註冊,正常應該不讓人隨便註冊

 

 拉到下面

 

 找到下面位置,去掉勾,保存

千萬不要找錯地方了。是Sign-up 而不是Sign-in

 

 

 

再次推出登陸,就沒註冊那一項了

 

 

建立個項目以前須要先建立一個組

 

先建立一個組

 

 填寫以下

 

 

 

訪問級別,設置爲內部

建立成功

 

 

在組裏面建立項目

 

 

這裏就能夠從下拉列表選擇了
項目描述
Import project from 這裏能夠看到它還能夠從其它平臺導入
能夠從github遷移過來

 

 

能夠經過key鏈接它

 

 

隨便輸入一串

 

提交

 

 

添加成功

 

把代碼拉下來
經過ssh拉下來

 

如今上傳和克隆都不行,你須要配置驗證
把公鑰放上去
?
1
2
3
4
5
6
7
8
9
10
11
12
[root@linux-node1 ~] # cd .ssh/
[root@linux-node1 . ssh ] # ll
total 12
-rw------- 1 root root 1675 Apr  5 03:38 id_rsa
-rw-r--r-- 1 root root  407 Apr  5 03:38 id_rsa.pub
-rw-r--r-- 1 root root  175 Apr  4 17:23 known_hosts
[root@linux-node1 . ssh ] # cat id_rsa.pub
ssh -rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDYSbAotNl0aNwx09agLbM09A1CCBphaE+g85mQMEnunzg3HSxm6wuFWcDCkK8szqTkMuxneEQOLNwQYsKuEz0pVZlTkN+N38SyhSaMrlCTp8
j3691sbsvMF4owO6jdZpevVoRjaU5v2HmS7yY /NJh7x1F8TTcfgLQt2rwL6ln02lV1glP1crGmpt95bcKBbnwU4o8W0Q +Ay4trqPbGJA9sZfU2j4Aanp+pksHt /yFNnQs2Oy4fXb9c
iQlP5Ue62adN92duMrtGhbo+WevJeEVpqyPbptP906toykpsERb+6GaJwWy /GtMj/b8lwEnAOW1sY5TIkMb34eV3rXX89L3F root@linux-node1.nmap.com
[root@linux-node1 . ssh ] #

  

 

title隨便寫,這裏是自動生成的

 

 

添加成功

查看項目

 

 

 

 看到沒ssh-key的提示了

 

 

 

 複製git@192.168.58.11:web/web-demo.git 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
遇到下面問題
[www@linux-node1 code]$ git
- bash : git: command not found
安裝git便可
[root@linux-node1 scripts] # yum install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
  * base: mirrors.163.com
  * epel: mirrors.tuna.tsinghua.edu.cn
  * extras: mirrors.163.com
  * updates: mirrors.zju.edu.cn
Package git-1.8.3.1-6.el7_2.1.x86_64 already installed and latest version
Nothing to do
[root@linux-node1 scripts] #

  

 因爲實驗環境變化,上面的ssh-key是5天前的,環境變化致使key失效,從新配置key

 

刪除以前的從新添加

 

 

 拉取代碼(在這以前,先刪除code目錄下的web-demo)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[www@linux-node1 code]$ git clone git@192.168.58.11:web /web-demo .git
Cloning into 'web-demo' ...
remote: Counting objects: 3, done .
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3 /3 ), done .
[www@linux-node1 code]$ ll
total 0
drwxrwxr-x 3 www www 33 Apr 28 21:49 web-demo
[www@linux-node1 code]$ cd web-demo/
[www@linux-node1 web-demo]$ ll
total 4
-rw-rw-r-- 1 www www 9 Apr 28 21:49 README.md
[www@linux-node1 web-demo]$

   

在家目錄下也克隆一份
?
1
2
3
4
5
6
7
8
9
10
[www@linux-node1 web-demo]$ cd
[www@linux-node1 ~]$ git clone git@192.168.58.11:web /web-demo .git
Cloning into 'web-demo' ...
remote: Counting objects: 3, done .
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3 /3 ), done .
[www@linux-node1 ~]$ cd web-demo/
[www@linux-node1 web-demo]$ ls
README.md
[www@linux-node1 web-demo]$

  

 添加index.html

?
1
2
3
4
5
6
7
8
9
10
11
[www@linux-node1 ~]$ cd web-demo/
[www@linux-node1 web-demo]$ ls
README.md
[www@linux-node1 web-demo]$ echo 'hehehehhehehe' >index.html
[www@linux-node1 web-demo]$ cat index.html
hehehehhehehe
[www@linux-node1 web-demo]$ ll
total 8
-rw-rw-r-- 1 www www 14 Apr 28 21:54 index.html
-rw-rw-r-- 1 www www  9 Apr 28 21:53 README.md
[www@linux-node1 web-demo]$

 

提交時提示須要配置郵箱等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[www@linux-node1 web-demo]$ git add *
[www@linux-node1 web-demo]$ git commit -m "add index.html"
 
*** Please tell me who you are.
 
Run
 
   git config --global user.email "you@example.com"
   git config --global user.name "Your Name"
 
to set your account's default identity.
Omit --global to set the identity only in this repository.
 
fatal: empty ident name ( for <www@linux-node1.nmap.com>) not allowed
[www@linux-node1 web-demo]$
 
隨便寫個就行
?
1
2
3
4
5
6
[www@linux-node1 web-demo]$ git config --global user.email "nmap@test.com"
[www@linux-node1 web-demo]$ git config --global user.name "nmap"
[www@linux-node1 web-demo]$ git commit -m "add index.html"
[master 8c8b197] add index.html
  1 file changed, 1 insertion(+)
  create mode 100644 index.html  

push到gitlab上
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[www@linux-node1 web-demo]$ git push
warning: push.default is unset ; its implicit value is changing in
Git 2.0 from 'matching' to 'simple' . To squelch this message
and maintain the current behavior after the default changes, use:
 
   git config --global push.default matching
 
To squelch this message and adopt the new behavior now, use:
 
   git config --global push.default simple
 
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
 
Counting objects: 4, done .
Delta compression using up to 4 threads.
Compressing objects: 100% (2 /2 ), done .
Writing objects: 100% (3 /3 ), 278 bytes | 0 bytes /s , done .
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.58.11:web /web-demo .git
    a2c3614..8c8b197  master -> master
[www@linux-node1 web-demo]$

  

在code目錄拉一下。git pull
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[www@linux-node1 ~]$ cd /deploy/code/
[www@linux-node1 code]$ cd web-demo/
[www@linux-node1 web-demo]$ ll
total 4
-rw-rw-r-- 1 www www 9 Apr 28 21:49 README.md
[www@linux-node1 web-demo]$ pwd
/deploy/code/web-demo
[www@linux-node1 web-demo]$ git pull
remote: Counting objects: 3, done .
remote: Compressing objects: 100% (2 /2 ), done .
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3 /3 ), done .
From 192.168.58.11:web /web-demo
    a2c3614..8c8b197  master     -> origin /master
Updating a2c3614..8c8b197
Fast-forward
  index.html | 1 +
  1 file changed, 1 insertion(+)
  create mode 100644 index.html
[www@linux-node1 web-demo]$
[www@linux-node1 web-demo]$ ll
total 8
-rw-rw-r-- 1 www www 14 Apr 28 21:57 index.html
-rw-rw-r-- 1 www www  9 Apr 28 21:49 README.md
[www@linux-node1 web-demo]$ cat index.html
hehehehhehehe
[www@linux-node1 web-demo]$
 
查看版本號
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[www@linux-node1 web-demo]$ git show
commit 8c8b1977f52f30c55d670f53b3be735a189c6f75
Author: nmap <nmap@ test .com>
Date:   Fri Apr 28 21:56:02 2017 +0800
 
     add index.html
 
diff --git a /index .html b /index .html
new file mode 100644
index 0000000..35811e7
--- /dev/null
+++ b /index .html
@@ -0,0 +1 @@
+hehehehhehehe
[www@linux-node1 web-demo]$   
 
獲取版本號
?
1
2
3
4
5
6
[www@linux-node1 web-demo]$ git show | grep commit
commit 8c8b1977f52f30c55d670f53b3be735a189c6f75
[www@linux-node1 web-demo]$
[www@linux-node1 web-demo]$ git show | grep commit | cut -d ' ' -f2
8c8b1977f52f30c55d670f53b3be735a189c6f75
[www@linux-node1 web-demo]$

這個git id很長,咱們通常取前五、六、8位。不多見前5位重複的。字符串截取

?
1
2
3
4
5
6
[www@linux-node1 web-demo]$ git show | grep commit | cut -d ' ' -f2
8c8b1977f52f30c55d670f53b3be735a189c6f75
[www@linux-node1 web-demo]$ API_VERL=$(git show | grep commit | cut -d ' ' -f2)
[www@linux-node1 web-demo]$ echo ${API_VERL:0:6}
8c8b19
[www@linux-node1 web-demo]$

  

修改以前代碼,這裏直接執行git pull。不執行echo了
?
1
2
3
4
5
6
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VER= "456"
}

  

版本也自動獲取
?
1
2
3
4
5
6
7
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VERL=$(git show | grep commit | cut -d ' ' -f2)
     API_VER=$( echo ${API_VERL:0:6})
}

  

保存,退出。從新部署下。看到以下,版本變了規整了
?
1
2
3
4
5
6
7
8
9
[www@linux-node1 web-demo]$ cd /scripts/
[www@linux-node1 scripts]$ . /deploy .sh
Usage: . /deploy .sh { deploy | rollback [ list |version ]}
[www@linux-node1 scripts]$ . /deploy .sh deploy
Already up-to- date .
code_build
web-demo_8c8b19_2017-04-28-22-03-56. tar .gz                             100% 8015     7.8KB /s   00:00   
web-demo_8c8b19_2017-04-28-22-03-56. tar .gz                             100% 8015     7.8KB /s   00:00   
[www@linux-node1 scripts]$

  

服務器都有這個包,沒解壓,網頁訪問沒變化。
檢查下腳本
node1解壓了。可是node2沒解壓
?
1
2
3
4
5
6
7
8
9
10
11
12
drwxr-xr-x 2 www www   40 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17
-rw-rw-r-- 1 www www  222 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17. tar .gz
drwxrwxr-x 3 www www   67 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56
-rw-rw-r-- 1 www www 8015 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56. tar .gz
[www@linux-node1 scripts]$ ll /opt/webroot/
 
[www@linux-node2 ~]$ ll /opt/webroot/
-rw-rw-r-- 1 www www  222 Apr 28 21:14 web-demo_456_2017-04-28-21-14-49. tar .gz
drwxr-xr-x 2 www www   58 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17
-rw-rw-r-- 1 www www  222 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17. tar .gz
-rw-rw-r-- 1 www www 8015 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56. tar .gz
[www@linux-node2 ~]$

  

找到緣由了。由於啓動了git。它佔用80端口。
而咱們先部署的node1,而node1是先測試,經過才能繼續後面的解壓部署操做
node1確定測試沒經過。由於80端口給git了。
node2理所固然沒解壓了
腳本沒問題

url_test裏面加個提示。測試不經過顯示在控制檯

?
1
2
3
4
5
6
7
8
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       writelog "test error" && exit ;
     fi
}
改爲這個便可
?
1
2
3
4
5
6
7
8
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       echo "test error" && exit ;
     fi
}

  

再次部署,就提示error了
?
1
2
3
4
5
6
7
[www@linux-node1 scripts]$ . /deploy .sh deploy
Already up-to- date .
code_build
web-demo_8c8b19_2017-04-28-22-07-08. tar .gz                             100% 8015     7.8KB /s   00:00   
web-demo_8c8b19_2017-04-28-22-07-08. tar .gz                             100% 8015     7.8KB /s   00:00   
test error
[www@linux-node1 scripts]$

  

 

git在用80端口
這裏測試的話,能夠在main函數裏把node1先去掉
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@linux-node1 scripts] # tail -31 deploy.sh
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     ROLLBACK_VER=$2
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
#            pre_deploy;
#            pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
             shell_lock;
             rollback ${ROLLBACK_VER};
             shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1 $2
[root@linux-node1 scripts] #

  

再次執行部署腳本
?
1
2
3
4
5
6
7
8
9
[www@linux-node1 scripts]$ . /deploy .sh deploy
Already up-to- date .
code_build
web-demo_8c8b19_2017-04-28-22-09-43. tar .gz                             100% 8018     7.8KB /s   00:00   
web-demo_8c8b19_2017-04-28-22-09-43. tar .gz                             100% 8018     7.8KB /s   00:00   
192.168.58.12. crontab .xml                                              100%   21     0.0KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
[www@linux-node1 scripts]$

  

測試OK

 

修改下主頁文件,再次提交
?
1
2
3
4
5
6
7
8
9
10
11
12
[www@linux-node1 ~]$ cd web-demo/
[www@linux-node1 web-demo]$ ls
index.html  README.md
[www@linux-node1 web-demo]$ vim index.html
[www@linux-node1 web-demo]$ cat index.html
hehehehhehehe
version 2.0
[www@linux-node1 web-demo]$ git add *
[www@linux-node1 web-demo]$ git commit -m "2.0"
[master 0ac9311] 2.0
  1 file changed, 1 insertion(+)
[www@linux-node1 web-demo]$

 

push上去

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[www@linux-node1 web-demo]$ git push
warning: push.default is unset ; its implicit value is changing in
Git 2.0 from 'matching' to 'simple' . To squelch this message
and maintain the current behavior after the default changes, use:
 
   git config --global push.default matching
 
To squelch this message and adopt the new behavior now, use:
 
   git config --global push.default simple
 
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
 
Counting objects: 5, done .
Delta compression using up to 4 threads.
Compressing objects: 100% (2 /2 ), done .
Writing objects: 100% (3 /3 ), 282 bytes | 0 bytes /s , done .
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.58.11:web /web-demo .git
    8c8b197..0ac9311  master -> master
[www@linux-node1 web-demo]$

  

再次執行deploy
它直接從gitlab上拉取代碼了
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[www@linux-node1 scripts]$ . /deploy .sh deploy
remote: Counting objects: 3, done .
remote: Compressing objects: 100% (2 /2 ), done .
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3 /3 ), done .
From 192.168.58.11:web /web-demo
    76f1c4f..7983661  master     -> origin /master
Updating 76f1c4f..7983661
Fast-forward
  index.html | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
code_build
web-demo_798366_2017-04-28-22-21-31. tar .gz                             100% 9068     8.9KB /s   00:00   
web-demo_798366_2017-04-28-22-21-31. tar .gz                             100% 9068     8.9KB /s   00:00   
192.168.58.12. crontab .xml                                              100%   21     0.0KB /s   00:00   
HTTP /1 .1 200 OK
add to cluster
[www@linux-node1 scripts]$

  

成功

 

回滾到上一個版本
?
1
2
3
4
5
6
7
8
9
-rw-rw-r-- 1 www www  222 Apr 28 21:14 /opt/webroot/web-demo_456_2017-04-28-21-14-49 . tar .gz
-rw-rw-r-- 1 www www  222 Apr 28 21:17 /opt/webroot/web-demo_456_2017-04-28-21-17-17 . tar .gz
-rw-rw-r-- 1 www www 8834 Apr 28 22:19 /opt/webroot/web-demo_76f1c4_2017-04-28-22-19-52 . tar .gz
-rw-rw-r-- 1 www www 9068 Apr 28 22:21 /opt/webroot/web-demo_798366_2017-04-28-22-21-31 . tar .gz
-rw-rw-r-- 1 www www 8015 Apr 28 22:03 /opt/webroot/web-demo_8c8b19_2017-04-28-22-03-56 . tar .gz
-rw-rw-r-- 1 www www 8015 Apr 28 22:07 /opt/webroot/web-demo_8c8b19_2017-04-28-22-07-08 . tar .gz
-rw-rw-r-- 1 www www 8018 Apr 28 22:09 /opt/webroot/web-demo_8c8b19_2017-04-28-22-09-43 . tar .gz
[www@linux-node1 scripts]$ . /deploy .sh rollback web-demo_76f1c4_2017-04-28-22-19-52
[www@linux-node1 scripts]$

  

 

回滾成功

 

gitlab相關了解

安裝的配置和目錄結構

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@linux-node1 scripts] # cd /opt/
[root@linux-node1 opt] # ll
total 12
drwx------  6 root root 4096 Apr  5 01:11 2017-04-05_01-11-16
drwxr-xr-x  3 root root   32 Apr  5 02:00 full
drwxr-xr-x 10 root root 4096 Apr  5 06:44 gitlab
drwxr-xr-x  4 root root   58 Apr  5 02:03 incr
drwxr-xr-x 23 www  www  4096 Apr 28 22:21 webroot
[root@linux-node1 opt] # cd gitlab/
[root@linux-node1 gitlab] # ll
total 1944
drwxr-xr-x  2 root root     103 Apr  5 06:43 bin
drwxr-xr-x 18 root root    4096 Apr  5 07:43 embedded
drwxr-xr-x  6 root root    4096 Apr  5 07:43 etc
drwxr-xr-x  2 root root    4096 Apr  5 06:45 init
-rw-r--r--  1 root root 1931990 Apr 22 21:36 LICENSE
drwxr-xr-x  2 root root    4096 Apr  5 06:43 LICENSES
drwxr-xr-x  2 root root    4096 Apr  5 06:45 service
drwxr-xr-x 15 root root    4096 Apr  5 06:45 sv
drwxr-xr-x  3 root root      20 Apr  5 06:44 var
-rw-r--r--  1 root root   19834 Apr 22 21:36 version-manifest.json
-rw-r--r--  1 root root    8769 Apr 22 21:36 version-manifest.txt
[root@linux-node1 gitlab] #

  

配置文件在etc裏面
?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@linux-node1 gitlab] # cd etc/
[root@linux-node1 etc] # pwd
/opt/gitlab/etc
[root@linux-node1 etc] # ll
total 72
drwxr-xr-x 2 root root    28 Apr  5 07:43 gitaly
-rw-r--r-- 1 root root    50 Apr  5 06:44 gitlab-healthcheck-rc
-rw-r--r-- 1 root root    80 Apr  5 06:44 gitlab-psql-rc
drwx------ 3 git  root    38 Apr  5 07:43 gitlab-rails
-rw-r--r-- 1 root root 65425 Apr 22 21:36 gitlab.rb.template
drwx------ 3 git  root    16 Apr  5 06:44 gitlab-workhorse
drwxr-xr-x 3 root root    16 Apr  5 06:45 postgres-exporter
[root@linux-node1 etc] #

  

查看下配置目錄

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@linux-node1 etc] # tree
.
├── gitaly
│   ├── HOME
│   └── PATH
├── gitlab-healthcheck-rc
├── gitlab-psql-rc
├── gitlab-rails
│   ├── env
│   │   ├── BUNDLE_GEMFILE
│   │   ├── EXECJS_RUNTIME
│   │   ├── HOME
│   │   ├── ICU_DATA
│   │   ├── LD_PRELOAD
│   │   ├── PATH
│   │   ├── PYTHONPATH
│   │   ├── RAILS_ENV
│   │   └── SIDEKIQ_MEMORY_KILLER_MAX_RSS
│   └── gitlab-rails-rc
├── gitlab.rb.template
├── gitlab-workhorse
│   └── env
│       ├── HOME
│       └── PATH
└── postgres-exporter
     └── env
         └── DATA_SOURCE_NAME
 
7 directories, 18 files
[root@linux-node1 etc] #

  

 

找它的配置文件
這裏能夠把external_url以後域名改爲IP(早期我就修改了)
?
1
2
3
4
5
6
7
8
9
[root@linux-node1 etc] # rpm -ql gitlab-ce | less
[root@linux-node1 etc] # cd /etc/gitlab/
[root@linux-node1 gitlab] # grep external_url gitlab.rb
##! For more details on configuring external_url see:
external_url 'http://192.168.58.11'
# registry_external_url 'https://registry.gitlab.example.com'
# pages_external_url "http://pages.example.com/"
# mattermost_external_url 'http://mattermost.example.com'
[root@linux-node1 gitlab] #

  

改完以後,須要從新配置
?
1
[root@linux-node1 gitlab] # gitlab-ctl reconfigure

  

從新登陸

 

 

若是從新登陸,發現還沒被改掉,應該是改以前的項目沒變,新建的項目用的都是IP了

 

 

最終的腳本內容
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
[root@linux-node1 gitlab] # cd /scripts/
[root@linux-node1 scripts] # mv deploy.sh deploy_all.sh
[root@linux-node1 scripts] # vim deploy_all.sh
[root@linux-node1 scripts] # cat deploy_all.sh
#!/bin/bash
 
#Dir List
#mkdir -p /deploy/code/web-demo/
#mkdir -p /deploy/config/web-demo/base
#mkdir -p /deploy/config/web-demo/other
#mkdir -p /deploy/tar
#mkdir -p /deploy/tmp
#mkdir -p /opt/webroot
#mkdir -p /webroot
#chown -R www:www /deploy
#chown -R www:www /opt/webroot
#chown -R www:www /webroot
 
#Node List
PRE_LIST= "192.168.58.11"
GROUP1_LIST= "192.168.58.12"
ROLLBACK_LIST= "192.168.58.11 192.168.58.12"
 
# Date/Time Veriables
LOG_DATE= 'date "+%Y-%m-%d"'
LOG_TIME= 'date "+%H-%M-%S"'
 
CDATE=$( date "+%Y-%m-%d" )
CTIME=$( date "+%H-%M-%S" )
 
#Shell Env
SHELL_NAME= "deploy.sh"
SHELL_DIR= "/home/www"
SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log"
 
#Code Env
PRO_NAME= "web-demo"
CODE_DIR= "/deploy/code/web-demo"
CONFIG_DIR= "/deploy/config/web-demo"
TMP_DIR= "/deploy/tmp"
TAR_DIR= "/deploy/tar"
LOCK_FILE= "/tmp/deploy.lock"
 
usage(){
     echo  $ "Usage: $0 { deploy | rollback [ list |version ]}"
}
 
writelog(){
         LOGINFO=$1
         echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
 
shell_lock(){
     touch ${LOCK_FILE}
}
 
url_test(){
     URL=$1
     curl -s -- head $URL | grep "200 OK"
     if [ $? - ne 0 ]; then
       shell_unlock;
       echo "test error" && exit ;
     fi
}
 
shell_unlock(){
     rm -f ${LOCK_FILE}
}
 
code_get(){
     writelog "code_get" ;
     cd $CODE_DIR  && git pull
     cp -r ${CODE_DIR} ${TMP_DIR}/
     API_VERL=$(git show | grep commit | cut -d ' ' -f2)
     API_VER=$( echo ${API_VERL:0:6})
}
 
code_build(){
     echo code_build
}
 
code_config(){
     writelog "code_config"
     /bin/cp -r ${CONFIG_DIR} /base/ * ${TMP_DIR}/ "${PRO_NAME}"
     PKG_NAME= "${PRO_NAME}" _ "${API_VER}" _ "${CDATE}-${CTIME}"
     cd ${TMP_DIR} && mv ${PRO_NAME} ${PKG_NAME}
}
 
code_tar(){
     writelog "code_tar"
     cd ${TMP_DIR} && tar cfz ${PKG_NAME}. tar .gz ${PKG_NAME}
     writelog "${PKG_NAME}.tar.gz"
}
 
code_scp(){
     writelog "code_scp"
     for node in $PRE_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
     for node in $GROUP1_LIST; do
       scp ${TMP_DIR}/${PKG_NAME}. tar .gz $node: /opt/webroot/
     done
}
 
 
pre_deploy(){
     writelog "remove from cluster"
       ssh $PRE_LIST "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $PRE_LIST "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
}
 
pre_test(){
     url_test "http://${PRE_LIST}/index.html"
     echo "add to cluster"
}
 
group1_deploy(){
     writelog "remove  from cluster"
     for node in $GROUP1_LIST; do
       ssh $node "cd /opt/webroot/ && tar xfz ${PKG_NAME}.tar.gz"
       ssh $node "rm -rf /webroot/web-demo && ln -s /opt/webroot/${PKG_NAME} /webroot/web-demo"
     done
     scp ${CONFIG_DIR} /other/192 .168.58.12. crontab .xml 192.168.58.12: /webroot/web-demo/crontab .xml
}
 
group1_test(){
     url_test "http://192.168.58.12/index.html"
     echo "add to cluster"
}
 
rollback_fun(){
     for node in $ROLLBACK_LIST; do
       ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo"
     done
}
 
rollback(){
if [ -z $1 ]; then
     shell_unlock;
     echo "Please input rollback version" && exit ;
fi
     case $1 in
     list)
                 ls -l /opt/webroot/ *. tar .gz
             ;;
           *)
                 rollback_fun $1
     esac
}
 
main(){
     if [ -f ${LOCK_FILE} ]; then
     echo "Deploy is running"  && exit ;
     fi
     DEPLOY_METHOD=$1
     ROLLBACK_VER=$2
     case $DEPLOY_METHOD in
     deploy)
             shell_lock;
             code_get;
             code_build;
             code_config;
             code_tar;
             code_scp;
             pre_deploy;
             pre_test;
             group1_deploy;
             group1_test;
         shell_unlock;
             ;;
     rollback)
             shell_lock;
             rollback ${ROLLBACK_VER};
             shell_unlock;
             ;;
     *)
             usage;
     esac
 
}
main $1 $2
[root@linux-node1 scripts] #

  

 

 
疑難解答。若是 http://192.168.58.12/提示下面這種
很大可能8080端口被別的佔用了
相關文章
相關標籤/搜索