自动化部署03

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

 

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

 

 

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

 

 登陆成功界面git

 

 

gitlab平常管理的话,其实不多,就是天天进行备份
每小时备份也能够,假如更新频繁的话github

 

 

去掉注册的功能,不让人注册,正常应该不让人随便注册

 

 拉到下面web

 

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

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

 

 

 

再次推出登陆,就没注册那一项了

 

 

建立个项目以前须要先建立一个组

 

先建立一个组

 

 填写以下shell

 

 

 

访问级别,设置为内部

建立成功json

 

 

在组里面建立项目

 

 

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

 

 

能够经过key链接它

 

 

随便输入一串

 

提交

 

 

添加成功

 

把代码拉下来
经过ssh拉下来

 

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

  

 

title随便写,这里是自动生成的

 

 

添加成功

查看项目

 

 

 

 看到没ssh-key的提示了

 

 

 

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

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

  

 因为实验环境变化,上面的ssh-key是5天前的,环境变化致使key失效,从新配置key

 

删除以前的从新添加

 

 

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

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

   

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

  

 添加index.html

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

 

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

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

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

  

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

这个git id很长,咱们通常取前五、六、8位。不多见前5位重复的。字符串截取

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

  

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

  

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

  

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

  

服务器都有这个包,没解压,网页访问没变化。
检查下脚本
node1解压了。可是node2没解压
1
2
3
4
5
6
7
8
9
10
11
12
drwxr-xr-x 2 www www   40 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17
-rw-rw-r-- 1 www www  222 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17. tar .gz
drwxrwxr-x 3 www www   67 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56
-rw-rw-r-- 1 www www 8015 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56. tar .gz
[www@linux-node1 scripts]$ ll  /opt/webroot/
 
[www@linux-node2 ~]$ ll  /opt/webroot/
-rw-rw-r-- 1 www www  222 Apr 28 21:14 web-demo_456_2017-04-28-21-14-49. tar .gz
drwxr-xr-x 2 www www   58 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17
-rw-rw-r-- 1 www www  222 Apr 28 21:17 web-demo_456_2017-04-28-21-17-17. tar .gz
-rw-rw-r-- 1 www www 8015 Apr 28 22:03 web-demo_8c8b19_2017-04-28-22-03-56. tar .gz
[www@linux-node2 ~]$

  

找到缘由了。由于启动了git。它占用80端口。
而咱们先部署的node1,而node1是先测试,经过才能继续后面的解压部署操做
node1确定测试没经过。由于80端口给git了。
node2理所固然没解压了
脚本没问题

url_test里面加个提示。测试不经过显示在控制台

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

  

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

  

 

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

  

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

  

测试OK

 

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

 

push上去

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

  

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

  

成功

 

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

  

 

回滚成功

 

gitlab相关了解

安装的配置和目录结构

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

  

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

  

查看下配置目录

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

  

 

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

  

改完以后,须要从新配置
1
[root@linux-node1 gitlab] # gitlab-ctl reconfigure

  

从新登陆

 

 

若是从新登陆,发现还没被改掉,应该是改以前的项目没变,新建的项目用的都是IP了

 

 

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

  

 

 
疑难解答。若是 http://192.168.58.12/提示下面这种
很大可能8080端口被别的占用了
相关文章
相关标签/搜索