自动化部署--shell脚本--3

时间:2021-11-30 21:34:26

登录gitlab
第一次登录gitlab,需要为root用户修改密码,root用户也是gitlab的超级管理员。
http://192.168.58.11
设置密码是gitlab 

自动化部署--shell脚本--3

 

密码不够长,重新设置下,设置密码是12345678,生产中注意密码复杂点

自动化部署--shell脚本--3

 

 

管理gitlab
使用root用户和刚才创建的密码登录后,你就可以探索gitlab的奥秘了,可以点击图中红框的按钮进入管理区域。

自动化部署--shell脚本--3

 

 登录成功界面

自动化部署--shell脚本--3

 

 

gitlab日常管理的话,其实很少,就是每天进行备份
每小时备份也可以,假如更新频繁的话

 

 

去掉注册的功能,不让人注册,正常应该不让人随便注册
自动化部署--shell脚本--3

 

 拉到下面

自动化部署--shell脚本--3

 

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

千万不要找错地方了。是Sign-up 而不是Sign-in

自动化部署--shell脚本--3

 

 

 

再次推出登录,就没注册那一项了
自动化部署--shell脚本--3

 

 

创建个项目之前需要先创建一个组

 

先创建一个组
自动化部署--shell脚本--3

 

 填写如下

自动化部署--shell脚本--3

 

 

 

访问级别,设置为内部
自动化部署--shell脚本--3

创建成功

自动化部署--shell脚本--3

 

 

在组里面创建项目

自动化部署--shell脚本--3

 

 

这里就可以从下拉列表选择了
项目描述
Import project from 这里可以看到它还可以从其它平台导入
可以从github迁移过来

自动化部署--shell脚本--3

 

 

可以通过key连接它
自动化部署--shell脚本--3

 

 

自动化部署--shell脚本--3

随便输入一串
自动化部署--shell脚本--3

 

提交

自动化部署--shell脚本--3

 

 

添加成功
自动化部署--shell脚本--3

 

把代码拉下来
通过ssh拉下来
自动化部署--shell脚本--3

 

现在上传和克隆都不行,你需要配置验证
把公钥放上去
[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随便写,这里是自动生成的
自动化部署--shell脚本--3

 

 

添加成功
自动化部署--shell脚本--3

查看项目

自动化部署--shell脚本--3

 

 自动化部署--shell脚本--3

 

 看到没ssh-key的提示了

自动化部署--shell脚本--3

 

 

 

 复制git@192.168.58.11:web/web-demo.git 

遇到下面问题
[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

自动化部署--shell脚本--3

 

删除之前的重新添加

自动化部署--shell脚本--3

 

 

 拉取代码(在这之前,先删除code目录下的web-demo)

[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]$

   

在家目录下也克隆一份
[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

[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]$

 

提交时提示需要配置邮箱等

[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]$
 
随便写个就行
[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上
[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
[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]$
 
查看版本号
 
[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]$   
 
获取版本号
[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位重复的。字符串截取

[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了
code_get(){
writelog "code_get";
cd $CODE_DIR && git pull
cp -r ${CODE_DIR} ${TMP_DIR}/
API_VER="456"
}

  

版本也自动获取
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})
}

  

保存,退出。重新部署下。看到如下,版本变了规整了
[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]$

  

服务器都有这个包,没解压,网页访问没变化。
自动化部署--shell脚本--3
检查下脚本
node1解压了。但是node2没解压
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里面加个提示。测试不通过显示在控制台

url_test(){
URL=$1
curl -s --head $URL | grep "200 OK"
if [ $? -ne 0 ];then
shell_unlock;
writelog "test error" && exit;
fi
}
改成这个即可
url_test(){
URL=$1
curl -s --head $URL | grep "200 OK"
if [ $? -ne 0 ];then
shell_unlock;
echo "test error" && exit;
fi
}

  

再次部署,就提示error了
[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先去掉
[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]#

  

再次执行部署脚本
[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
自动化部署--shell脚本--3

 

修改下主页文件,再次提交
[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上去

[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上拉取代码了
[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]$

  

成功
自动化部署--shell脚本--3

 

回滚到上一个版本
-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]$

  

 

回滚成功
自动化部署--shell脚本--3

 

gitlab相关了解

安装的配置和目录结构

[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里面
[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]#

  

查看下配置目录

[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(早期我就修改了)
[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]#

  

改完之后,需要重新配置
[root@linux-node1 gitlab]# gitlab-ctl reconfigure

  

重新登录
自动化部署--shell脚本--3

 

 

如果重新登录,发现还没被改掉,应该是改之前的项目没变,新建的项目用的都是IP了
自动化部署--shell脚本--3

 

 

最终的脚本内容
[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端口被别的占用了
自动化部署--shell脚本--3