forked from unixhot/deploy-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.sh
118 lines (101 loc) · 2.41 KB
/
deploy.sh
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
#!/bin/bash
#Data/Time
CTIME=$(date "+%Y-%m-%d-%H-%M")
#Shell ENV
SHELL_NAME="deploy.sh"
SHELL_DIR="/home/www"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
LOCK_FILE="/tmp/${SHELL_NAME}.lock"
#APP ENV
PNAME="demo"
CODE_DIR="/data/deploy/code"
CONFIG_DIR="/data/deploy/config/"
TMP_DIR="/data/deploy/tmp"
TAR_DIR="/data/deploy/pkg"
PKG_SERVER="192.168.56.11"
shell_log(){
LOG_INFO=$1
echo "$CTIME ${SHELL_NAME} : ${LOG_INFO}" >> ${SHELL_LOG}
}
shell_lock(){
touch ${LOCK_FILE}
}
shell_unlock(){
rm -f ${LOCK_FILE}
}
usage(){
echo "Usage: $0 [env deploy version] | rollback-list | rollback | fastrollback"
}
get_pkg(){
echo "get pkg"
shell_log "Get PKG"
scp www@${PKG_SERVER}:/data/pkg/${PNAME}/${PNAME}.tar.gz ${CODE_DIR}/${PNAME}/
}
config_pkg(){
echo "config pkg"
shell_log "Config PKG"
mkdir ${TMP_DIR}/${PNAME}/${PNAME}
cd ${CODE_DIR}/${PNAME} && tar zxf ${PNAME}.tar.gz -C ${TMP_DIR}/${PNAME}/${PNAME}
/bin/cp ${CONFIG_DIR}/${PNAME}/demo-config/$DEPLOY_ENV/* ${TMP_DIR}/${PNAME}/${PNAME}
/bin/cp -a ${TMP_DIR}/${PNAME}/${PNAME} ${TMP_DIR}/${PNAME}/${PNAME}-${CTIME}
cd ${TMP_DIR}/${PNAME} && tar czf ${TAR_DIR}/${PNAME}/${PNAME}-${CTIME}.tar.gz ${PNAME}-${CTIME}
cd ${TMP_DIR}/${PNAME} && rm -rf *
}
scp_pkg(){
echo "scp pkg"
shell_log "SCP pkg"
scp ${TAR_DIR}/${PNAME}/${PNAME}-${CTIME}.tar.gz [email protected]:/app-data
}
deploy_pkg(){
echo "deploy pkg"
shell_log "Deploy PKG"
ssh [email protected] "cd /app-data/ && tar zxf ${PNAME}-${CTIME}.tar.gz && rm -f /app-root/webroot && ln -s /app-data/${PNAME}-${CTIME} /app-root/webroot"
}
test_pkg(){
echo "Test"
shell_log "Test"
}
fast_rollback(){
echo "fast rollback"
}
rollback(){
echo "rollback"
shell_log "rollback"
ssh [email protected] "rm -f /app-root/webroot && ln -s $DEPLOY_VER /app-root/webroot"
}
rollback_list(){
echo "rollback list"
ssh [email protected] "ls -l /app-data/*.tar.gz"
}
main(){
DEPLOY_ENV=$1
DEPLOY_TYPE=$2
DEPLOY_VER=$3
if [ -f "$LOCK_FILE" ];then
shell_log "${SHELL_NAME} is running"
echo "${SHELL_NAME} is running" && exit
fi
shell_lock;
case $DEPLOY_TYPE in
deploy)
get_pkg;
config_pkg;
scp_pkg;
deploy_pkg;
test_pkg;
;;
rollback)
rollback
;;
fast-rollback)
fast_rollback;
;;
rollback-list)
rollback_list;
;;
*)
usage;
esac
shell_unlock;
}
main $1 $2 $3