Merge pull request #174 from forgethub/master

优化自动部署脚本
This commit is contained in:
lanyulei 2021-04-14 15:20:03 +08:00 committed by GitHub
commit ceaf2e8a49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 59 deletions

141
build.sh
View File

@ -5,7 +5,7 @@
# #
# id : AL1009 # id : AL1009
# #
# Last modified: 2021-04-07 21:45 # Last modified: 2021-04-12 21:18
# #
# Filename: build.sh # Filename: build.sh
# #
@ -35,7 +35,6 @@ function echo_done() {
} }
function get_db_config() { function get_db_config() {
cwd=$(pwd)
key=$1 key=$1
value=$(sed -n '/database/,/domain/p' "${CONFIG_FILE}" |awk -F : /^[[:space:]]*${key}/'{print $2}') value=$(sed -n '/database/,/domain/p' "${CONFIG_FILE}" |awk -F : /^[[:space:]]*${key}/'{print $2}')
@ -81,7 +80,7 @@ EOF
function check_soft { function check_soft {
local _soft_name=$1 local _soft_name=$1
command -v ${_soft_name} > /dev/null || { command -v ${_soft_name} > /dev/null || {
echo_red "请安装 ${_soft_name} 后再执行本脚本安装ferry。" echo_red "请安装 ${_soft_name} 后再执行本脚本安装ferry。"
exit 1 exit 1
} }
} }
@ -123,14 +122,20 @@ function mk_ferry_dir {
} }
function test_mysql_connect() { function test_mysql_connect() {
host=$1 host=$1
port=$2 port=$2
user=$3 user=$3
password=$4 password=$4
db=$5 db=$5
command="CREATE TABLE IF NOT EXISTS test(id INT); DROP TABLE test;" sql_command="CREATE TABLE IF NOT EXISTS test(id INT); DROP TABLE test;"
echo_green "\n>>> $(gettext '拉取mysql docker 镜像如果是首次需要耗时,请稍等...')" command -v mysql > /dev/null
docker run -it --rm mysql:5 mysql -h${host} -P${port} -u${user} -p${password} ${db} -e "${command}" 2>/dev/null if [ "$?" -eq 0 ];then
mysql -h${host} -P${port} -u${user} -p${password} ${db} -e "${sql_command}" 2>/dev/null
else
[ "${host}" == "127.0.0.1" ] && return 0
echo_green "\n>>> $(gettext '拉取mysql docker 镜像如果是首次需要耗时,请稍等...')"
docker run -it --rm mysql:5 mysql -h${host} -P${port} -u${user} -p${password} ${db} -e "${sql_command}" 2>/dev/null
fi
} }
function init(){ function init(){
@ -148,41 +153,57 @@ function init(){
if [ -f "${BASE_DIR}/build/config/settings.yml" ];then if [ -f "${BASE_DIR}/build/config/settings.yml" ];then
CONFIG_FILE=${BASE_DIR}/build/config/settings.yml CONFIG_FILE=${BASE_DIR}/build/config/settings.yml
else else
echo_red "配置文件不存在,请检查配置文件是否存在。" echo_red "配置文件: ${BASE_DIR}/build/config/settings.yml 不存在,请检查。"
exit 1 exit 1
fi fi
echo_done }
function set_external_redis() {
redis_host=$(awk --re-interval '/url: redis/{match($0,/([0-9]{1,3}\.){3}[0-9]{1,3}/,a); print a[0]}' ${CONFIG_FILE})
read_from_input redis_host "$(gettext 'Please enter Redis server IP')" "" "${redis_host}"
redis_port=$(awk -F : '/url: redis/{print $NF}' ${CONFIG_FILE})
read_from_input redis_port "$(gettext 'Please enter Redis server port')" "" "${redis_port}"
redis_pass=$(awk -F '/' '/url: redis/{if($0~"@")print $3}' ${CONFIG_FILE} |cut -f 1 -d '@')
read_from_input redis_pass "$(gettext 'Please enter Redis password, 密码里面不能带@ /, 密码为空请务必修改配置文件!!')" "" "${redis_pass}"
# 设置redis账号密码到配置文件
if [ "${redis_pass}" == "" ];then
sed -i "s/url: redis:\/\/.*/url: redis:\/\/${redis_host}:${redis_port}/g" ${CONFIG_FILE}
else
sed -i "s/url: redis:\/\/.*/url: redis:\/\/${redis_pass}@${redis_host}:${redis_port}/g" ${CONFIG_FILE}
fi
} }
function set_external_mysql() { function set_external_mysql() {
mysql_host=$(get_db_config host) mysql_host=$(get_db_config host)
read_from_input mysql_host "$(gettext 'Please enter MySQL server IP')" "" "${mysql_host}" read_from_input mysql_host "$(gettext 'Please enter MySQL server IP')" "" "${mysql_host}"
mysql_port=$(get_db_config port) mysql_port=$(get_db_config port)
read_from_input mysql_port "$(gettext 'Please enter MySQL server port')" "" "${mysql_port}" read_from_input mysql_port "$(gettext 'Please enter MySQL server port')" "" "${mysql_port}"
mysql_db=$(get_db_config name) mysql_db=$(get_db_config name)
read_from_input mysql_db "$(gettext 'Please enter MySQL database name')" "" "${mysql_db}" read_from_input mysql_db "$(gettext 'Please enter MySQL database name')" "" "${mysql_db}"
mysql_user=$(get_db_config username) mysql_user=$(get_db_config username)
read_from_input mysql_user "$(gettext 'Please enter MySQL username')" "" "${mysql_user}" read_from_input mysql_user "$(gettext 'Please enter MySQL username')" "" "${mysql_user}"
mysql_pass=$(get_db_config password) mysql_pass=$(get_db_config password)
read_from_input mysql_pass "$(gettext 'Please enter MySQL password')" "" "${mysql_pass}" read_from_input mysql_pass "$(gettext 'Please enter MySQL password')" "" "${mysql_pass}"
test_mysql_connect ${mysql_host} ${mysql_port} ${mysql_user} ${mysql_pass} ${mysql_db} test_mysql_connect ${mysql_host} ${mysql_port} ${mysql_user} ${mysql_pass} ${mysql_db}
if [[ "$?" != "0" ]]; then if [[ "$?" != "0" ]]; then
echo_red "测试连接数据库失败, 请重新设置" echo_red "测试连接数据库失败, 请重新设置"
echo echo
set_external_mysql set_external_mysql
fi fi
set_db_config "host" ${mysql_host} set_db_config "host" ${mysql_host}
set_db_config "port" ${mysql_port} set_db_config "port" ${mysql_port}
set_db_config "username" ${mysql_user} set_db_config "username" ${mysql_user}
set_db_config "password" ${mysql_pass} set_db_config "password" ${mysql_pass}
set_db_config "name" ${mysql_db} set_db_config "name" ${mysql_db}
} }
function config_mysql { function config_mysql {
@ -197,8 +218,20 @@ function config_mysql {
fi fi
} }
function config_redis {
echo_green "\n>>> $(gettext '回车前请确保你已经安装了Redis,且启动服务')"
read_from_input confirm "$(gettext 'Do you have been installed Redis')?" "y/n" "y"
if [[ "${confirm}" == "y" ]]; then
set_external_redis
else
echo_red "未安装Redis结束此次编译"
exit 1
fi
}
function get_variables { function get_variables {
read_from_input front_url "$(gettext '请输入您的程序访问地址: ')" "" "https://fdevops.com:8001" read_from_input front_url "$(gettext '请输入您的程序访问地址: ')" "" "https://fdevops.com:8002"
read_from_input front_clone_from "$(gettext '请选择从哪里拉取前端代码默认是gitee: 1:gitee, 2: github, 3:自定义地址')" "" "1" read_from_input front_clone_from "$(gettext '请选择从哪里拉取前端代码默认是gitee: 1:gitee, 2: github, 3:自定义地址')" "" "1"
if [ $front_clone_from == 1 ]; then if [ $front_clone_from == 1 ]; then
@ -210,15 +243,23 @@ function get_variables {
fi fi
config_mysql config_mysql
read_from_input confirm "$(gettext '请确认是否创建配置文件中的redis库')?" "y/n[y]" "y" config_redis
if [[ "${confirm}" != "y" ]]; then
echo_red "结束此次编译"
exit 1
fi
echo_done echo_done
} }
function config_front {
echo_green "\n>>> $(gettext '替换程序访问地址...')"
cat > ${BASE_DIR}/ferry_web/.env.production << EOF
# just a flag
ENV = 'production'
# base api
VUE_APP_BASE_API = '$front_url'
EOF
}
function install_front { function install_front {
echo_green "\n>>> $(gettext '开始拉取前端程序...')" echo_green "\n>>> $(gettext '开始拉取前端程序...')"
read_from_input confirm "$(gettext '此处会执行 rm -rf ./ferry_web 的命令,若此命令不会造成当前环境的损伤则请继续')?" "y/n[y]" "y" read_from_input confirm "$(gettext '此处会执行 rm -rf ./ferry_web 的命令,若此命令不会造成当前环境的损伤则请继续')?" "y/n[y]" "y"
@ -238,7 +279,7 @@ function install_front {
echo_red "克隆代码失败请检查git地址: ${ui_address}或者网络质量" echo_red "克隆代码失败请检查git地址: ${ui_address}或者网络质量"
exit 1 exit 1
fi fi
config_front
echo_green "\n>>> $(gettext '开始安装前端依赖...')" echo_green "\n>>> $(gettext '开始安装前端依赖...')"
cnpm_base_dir=$(dirname $(dirname $(which npm))) cnpm_base_dir=$(dirname $(dirname $(which npm)))
npm install -g cnpm --registry=https://registry.npm.taobao.org --prefix ${cnpm_base_dir} npm install -g cnpm --registry=https://registry.npm.taobao.org --prefix ${cnpm_base_dir}
@ -246,19 +287,6 @@ function install_front {
} }
function config_front {
echo_green "\n>>> $(gettext '替换程序访问地址...')"
cat > ${BASE_DIR}/ferry_web/.env.production << EOF
# just a flag
ENV = 'production'
# base api
VUE_APP_BASE_API = '$front_url'
EOF
}
function install_backend { function install_backend {
echo_green "\n>>> $(gettext '开始编译后端程序...')" echo_green "\n>>> $(gettext '开始编译后端程序...')"
@ -282,7 +310,7 @@ function install_backend {
fi fi
cp -r ${BASE_DIR}/ferry ${BASE_DIR}/build/ cp -r ${BASE_DIR}/ferry ${BASE_DIR}/build/
cd ${BASE_DIR}/build cd ${BASE_DIR}/build
${BASE_DIR}/ferry init -c=config/settings.yml ${BASE_DIR}/build/ferry init -c=config/settings.yml
cd - &>/dev/null cd - &>/dev/null
} }
@ -291,13 +319,12 @@ function install_app() {
init init
get_variables get_variables
install_front install_front
config_front
install_backend install_backend
} }
function start_backend { function start_backend {
cd ${BASE_DIR}/build cd ${BASE_DIR}/build
${BASE_DIR}/ferry server -c=config/settings.yml ./ferry server -c=config/settings.yml
} }
function main { function main {

View File

@ -53,7 +53,7 @@ settings:
maxsize: 10240 maxsize: 10240
path: ./logs/ferry.log path: ./logs/ferry.log
redis: redis:
url: redis://ferry123456@127.0.0.1:6379 url: redis://127.0.0.1:6379
ssl: ssl:
key: keystring key: keystring
pem: temp/pem.pem pem: temp/pem.pem

View File

@ -35,7 +35,7 @@ func InitSysRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddleware) *gin
} }
func sysStaticFileRouter(r *gin.RouterGroup, g *gin.Engine) { func sysStaticFileRouter(r *gin.RouterGroup, g *gin.Engine) {
r.Static("/static", "./static") r.Static("/static", "./template/web/static")
g.LoadHTMLGlob("template/web/index.html") g.LoadHTMLGlob("template/web/index.html")
} }