自动化部署脚本(一)

2019年11月24日 阅读数:106
这篇文章主要向大家介绍自动化部署脚本(一),主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

传统部署方式
一、纯手工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端口被别的占用了