自动化部署脚本(一)
传统部署方式
1、纯手工scp
2、纯手工登录git pull 、svn update
3、纯手工xftp往上拉
4、开发给打一个压缩包,rz上去。解压
传统部署缺点:
1、全程运维参与,占用大量时间
2、上线速度慢。
3、认为失误多。管理混乱
4、回滚慢,不及时
新项目上线,规划排在第一位
一般银行都不提供测试接口。比如一些电商公司测试的话,经常把商品调节成1分,只能特定账号能看到。
环境的规划
1、开发环境-开发者本地有自己的环境,然后运维需要设置的开发环境,放的是大家共用的服务。如开发数据库mysql,其它:redis、Memcached。
2、测试环境:功能测试环境和性能测试环境
3、预生产环境:一般可以用生产环境中的某个节点担任
4、生产环境:直接对用户提供服务的环境
预生产环境产生的原因:
1、数据库不一致:测试环境和生产环境数据库肯定不一样的。
2、使用生产环境的联调接口。例如,支付接口
部署:
1、代码放在哪里:svn,git
2、获取什么版本代码?
svn+git直接拉去某个分支
svn:指定版本号
git:指定tag
3、差异解决:
(1)、各个节点直接差异:
(2)、代码仓库和实际的差异。配置文件是否在代码仓库中
(3)、配置文件未必一样:crontab.xml预生产节点
4、如何更新。java tomcat。需要重启。
5、测试。
6、串行和并行 分组部署
7如何执行。(1)shell执行。(2)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添加公钥
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 ~]$ |
根据上面的流程图,先把大体框架写出来
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] # |
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、脚本是否可以多个人一起执行?(最好不要多个人一起执行)不允许多人执行的话可以上锁
一般锁文件放下面目录下
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 ~] # |
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} } |
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] # |
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] # |
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" } |
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] # |
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] # |
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]$ |
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 } |
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 } |
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] # |
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]$ |
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] # |
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]$ |
检查
#################################################################################
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]$ |
查看此时自动化部署脚本的配置
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] # |
准备是这样的,部署一个,测试一个,通了才继续往下部署。这样才合理
很多公司拿预热节点部署。通过之后再部署生产节点
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"' |
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 } |
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 } |
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 } |
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 } |
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 } |
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]$ |
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是一个利用 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 |
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 |
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日常管理的话,其实很少,就是每天进行备份
每小时备份也可以,假如更新频繁的话
拉到下面
找到下面位置,去掉勾,保存
填写如下
创建成功
这里就可以从下拉列表选择了
项目描述
Import project from 这里可以看到它还可以从其它平台导入
可以从github迁移过来
提交
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 ] # |
查看项目
看到没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]$ |
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很长,我们一般取前5、6、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]$ |
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]$ |
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 } |
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]$ |
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]$ |
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]$ |
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] # |
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] # |
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 |
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] # |