自動化部署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端口被別的佔用了
相關文章
相關標籤/搜索