-
Notifications
You must be signed in to change notification settings - Fork 176
/
one.sh
178 lines (154 loc) · 4.12 KB
/
one.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
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
#!/bin/bash
# set java environment
# JAVA_HOME=/usr/java/jdk1.8
# export JRE_HOME=/usr/java/jdk1.8/jre
# export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
# export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
# set maven
export M2_HOME=/opt/apache-maven-3.3.9
export PATH=$PATH:$M2_HOME/bin
BASE_PATH="/home/one"
ONE_EXTEND_PATH=$BASE_PATH/one
ONE_MAIN_PATH=$ONE_EXTEND_PATH/one-main
ONE_UI_PATH=$ONE_EXTEND_PATH/one-ui
ONE_MAIN_STATIC_PATH=$ONE_MAIN_PATH/src/main/resources/static
ONE_JAR_PATH=$BASE_PATH/jar
ONE_BAK_PATH=$ONE_JAR_PATH/bak
ONE_MAIN_JAR=$ONE_JAR_PATH/one-main-0.0.1-SNAPSHOT.jar
USER_EMAIL="[email protected]"
USER_NAME="lcw2004"
GIT_PASSWORD="******"
GIT_URL="https://github.com/lcw2004/one.git"
#GIT_URL=https://$USER_NAME:[email protected]/lcw2004/one.git
function print () {
echo ""
echo "***********************************************"
echo "******** $1"
echo "***********************************************"
}
# 获取最新代码
function pull_code () {
print "1. pull code"
cd $ONE_EXTEND_PATH;
git config core.filemode false
git pull $GIT_URL master;
}
# 编译前端工程
function build_front () {
print "2. build front"
cd $ONE_UI_PATH;
rm -rf $ONE_UI_PATH/dist/*
npm install;
npm run dll;
npm run build;
# 判断是否编译成功,否则退出程序
if [ ! -d "$ONE_UI_PATH/dist/static" ]; then
print "build front fail"
exit 1
fi
# copy front file
cd $BASE_PATH;
rm -rf $ONE_MAIN_STATIC_PATH;
mkdir -p $ONE_MAIN_STATIC_PATH;
cp -rf $ONE_UI_PATH/dist/* $ONE_MAIN_STATIC_PATH/;
}
# 编译后端工程
function build_maven () {
print "3. build spring boot"
cd $ONE_EXTEND_PATH;
mvn clean package -Dmaven.test.skip=true;
}
# 备份数据
function backup (){
print "4. backup"
cp "$ONE_JAR_PATH/one-main-0.0.1-SNAPSHOT.jar" "$ONE_BAK_PATH/one-main-0.0.1-SNAPSHOT.jar.`date +%Y-%m-%d-%H-%M-%S`"
}
# 拷贝Jar包
function copy_jar () {
print "5. copy jar file"
cp "$ONE_MAIN_PATH/target/one-main-0.0.1-SNAPSHOT.jar" "$ONE_MAIN_JAR"
}
# 杀死进程
function kill_progress () {
echo "Stop $1"
boot_id=`ps -ef | grep java | grep $1 | grep -v grep | awk '{print $2}'`
count=`ps -ef | grep java | grep $1 | grep -v grep | wc -l`
if [ $count != 0 ];then
echo "$1 process pid is:$boot_id ."
kill $boot_id
# 如果没干掉进程,再干一遍
sleep 2
boot_id=`ps -ef | grep java | grep $1 | grep -v grep | awk '{print $2}'`
count=`ps -ef | grep java | grep $1 | grep -v grep | wc -l`
if [ $count != 0 ];then
echo "$1 process pid is:$boot_id ."
kill -9 $boot_id
fi
fi
}
# 杀死全部进程
function kill_all () {
print "6. shut down"
kill_progress "$ONE_MAIN_JAR"
}
# 启动程序
function start () {
print "7. start one-extend"
nohup /usr/bin/java -jar -Xms100m -Xmx500m $ONE_MAIN_JAR --spring.config.location=$BASE_PATH/one_main.yml --logging.path=$BASE_PATH/log/one-main > /dev/null 2>&1 &
sleep 3
status
}
# 检查状态
function check_status () {
count=`ps -ef | grep java | grep $1 | grep -v grep|wc -l`
if [ $count != 0 ];then
boot_id=`ps -ef | grep java | grep $1 | grep -v grep | awk '{print $2}'`
echo "$1 is running... process pid is:$boot_id "
else
echo "$1 is not running..."
fi
}
# 检查两个工程的状态
function status(){
print "check status"
check_status "$ONE_MAIN_JAR"
}
# 更新代码并编译运行
function reload () {
pull_code
build_front
build_maven
backup
copy_jar
kill_all
start
print "8. deploy end"
}
# 停止
function stop () {
kill_all
}
# 重启
function restart () {
kill_all
start
}
# 打印帮助手册
function print_usage () {
echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {help|start|stop|restart|status|reload} \033[0m\033[0;31m Example: \033[0m\033[0;33m sh $0 start \033[0m"
}
case $1 in
reload)
reload;;
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
help)
print_usage;;
*)
esac