forked from itwanger/paicoding-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.sh
125 lines (108 loc) · 2.84 KB
/
launch.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
119
120
121
122
123
124
125
#!/usr/bin/env bash
# file to upload
WEB_PKG="dist.tar.gz"
WEB_PKG_BK="dist_bk.tar.gz"
TMP_WEB_PKG="tmp.tar.gz"
DEPLOY_SCRIPT="launch.sh"
START_FUNC_NAME="start"
BUILD_FUNC_NAME="build"
INSTALL_FUNC_NAME="install"
SERVER_FUNC_NAME="server"
#env, ssh remote, work dir
ENV_PRO="pro"
SSH_HOST_PRO=("[email protected]")
WORK_DIR_PRO="/home/admin/workspace/admin/"
ADMIN_WORKSPACE="dist"
function build() {
echo "---- start to build admin ----"
echo "npm run build:pro"
npm run build:pro
tar -zcvf ${WEB_PKG} dist
echo "---------- 静态资源包dist.tar.gz已打包完成 -------------"
}
function upload() {
# upload jar
# rename to *.jar.bak
scp ${WEB_PKG} $1:$2${TMP_WEB_PKG}
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo "Failed to scp ${WEB_PKG}"
return 1
fi
# upload script
scp ${DEPLOY_SCRIPT} $1:$2
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to scp launch.sh'
return 1
fi
}
function install() {
npm install
}
function server() {
npm run dev
}
function start() {
echo "---- 开始部署 ----"
cd ${WORK_DIR_PRO}
mv ${WEB_PKG} ${WEB_PKG_BK}
mv ${ADMIN_WORKSPACE} "${ADMIN_WORKSPACE}_bk"
mv ${TMP_WEB_PKG} ${WEB_PKG}
tar -zxvf ${WEB_PKG}
echo "---- 部署完成 ----"
}
function deploy() {
# package
echo "*******Start to package*******"
build $1
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to compile'
exit ${ret}
fi
if [ "$1" = "${ENV_PRO}" ]; then
SSH_HOST=${SSH_HOST_PRO[@]}
WORK_DIR=${WORK_DIR_PRO}
else
echo "Unknown env: $1"
exit
fi
for host in ${SSH_HOST[@]}
do
# upload jar and launch.sh
echo "*******Start to upload:${host} *******"
upload ${host} ${WORK_DIR}
ret=$?
if [[ ${ret} -ne 0 ]] ; then
echo 'Failed to upload files'
exit ${ret}
fi
done
for host in ${SSH_HOST[@]}
do
# run
echo "*******Start service:${host} *******"
ssh ${host} "bash ${WORK_DIR}${DEPLOY_SCRIPT} ${START_FUNC_NAME}"
echo "*******Done*******"
done
}
if [ "$1" = "${START_FUNC_NAME}" ]; then
start "$@"
elif [ "$1" = "${BUILD_FUNC_NAME}" ]; then
build $1
elif [ "$1" = "${INSTALL_FUNC_NAME}" ]; then
install $1
elif [ "$1" = "${SERVER_FUNC_NAME}" ]; then
server $1
elif [ "$1" = "${ENV_PRO}" ]; then
deploy $1
else
echo "=========== 本地环境安装 & 调试 =============="
echo "安装依赖: ./launch.sh install"
echo "本地启动: ./launch.sh server"
echo "=========== 上传服务器 & 服务器解压使用 =============="
echo "打包 dist.tar.gz: ./launch.sh build"
echo "打包静态资源并上传到服务器 & 解压执行: ./launch.sh pro"
echo "服务器上资源启用: ./launch.sh start"
fi