diff --git a/Qqci/Makefile.tpl b/Qqci/Makefile.tpl new file mode 100644 index 0000000..cf734f7 --- /dev/null +++ b/Qqci/Makefile.tpl @@ -0,0 +1,23 @@ +SHELL=/bin/bash + +APPNAME ?= {{.Appname}} +PKGNAME ?= $(APPNAME).tar.gz +BUILD_DIR = $(shell pwd) +TEMP_OUTPUT_DIR = $(shell pwd)/_output/$(APPNAME) + + +tar: build package + @echo -e "======\033[44m all done \033[0m" + +build: + go mod tidy + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/$(APPNAME) ./ + CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o $(BUILD_DIR)/$(APPNAME).exe ./ +package: + @-rm -rf $(TEMP_OUTPUT_DIR) >/dev/null 2>&1 + mkdir -p $(TEMP_OUTPUT_DIR) >/dev/null 2>&1 + cp -rL $(BUILD_DIR)/$(APPNAME) $(TEMP_OUTPUT_DIR)/ + cp -rL $(BUILD_DIR)/$(APPNAME).exe $(TEMP_OUTPUT_DIR)/ + cp -rL $(BUILD_DIR)/README.md $(TEMP_OUTPUT_DIR)/ + cd $(TEMP_OUTPUT_DIR)/.. && tar -czf $(PKGNAME) $(APPNAME) + diff --git a/Qqci/load.tpl b/Qqci/load.tpl new file mode 100644 index 0000000..67c1918 --- /dev/null +++ b/Qqci/load.tpl @@ -0,0 +1,170 @@ +#!/bin/bash +approot=$(pwd) +appname={{.Appname}} +appexec={{.Command}} +isdaemon=${isdaemon:=true} +usemonitor=${usemonitor:=false} + +########################################## +# main function. +########################################## +function main() { + echo "=="$(date +"%Y-%m-%d %H:%M:%S")"==" + case "$1" in + start) + $usemonitor && start_by_monitor || start_directly + ;; + restart|reload) + $usemonitor && restart_by_monitor || restart_directly + ;; + stop) + $usemonitor && stop_by_monitor || stop_directly + ;; + install) + $usemonitor && install_with_monitor || install_no_monitor + ;; + *) + echo "Usage: ./load.sh {install|start|stop|restart}" + exit 1 + esac +} + +########################################## +# install: +# do something before reload after deploy +########################################## +function install_no_monitor() { + ##### make logs directory at app directory ############## + function isemptydir() { + [ `ls -A "$approot/logs" 2>/dev/null | wc -l` -eq 0 ] && echo true || echo false; + } + if [ ! -d $approot/logs -o $(isemptydir) == true ]; then + mkdir -p /data/logs/${appname}; + rm -rf $approot/logs; + ln -s /data/logs/${appname} $approot/logs; + fi +} +function install_with_monitor() { + ##### do install no monitor actions ##################### + install_no_monitor + + ##### get the supervisor config & incloude directory #### + appmonitor=supervisor + monitconf=${SUPERVISOR_CONF:=$(find /etc -name supervisord.conf -type f 2>/dev/null)} + monitconf=${monitconf:=/etc/supervisor/supervisord.conf} + echo "The path of supervisord.conf is: '${monitconf}'" + monitinclude=${SUPERVISOR_INCLUDE:=$(grep '^\[include\]$' $monitconf -A2 | sed -n 's/^files=\([^*]*\)\(\*.conf\)*/\1/p')} + monitinclude=${monitinclude:=/etc/supervisord.d} + echo "The folder of supervisord files is: ${monitincloude}" + + ##### install ignore if file exist. ##################### + if [ -f ${monitinclude}/${appname}.ini ]; then + echo "WARNING: install ignore, because of file '${monitinclude}/${appname}.ini' is exist" + exit 0 + fi + + cat >${monitinclude}/${appname}.ini <<-EOF + [program:${appname}] + command=${appexec} + process_name=%(program_name)s + numprocs=1 + priority=1 + directory=${approot} + autostart=true + autorestart=true + + stdout_logfile=/data/logs/supervisor/%(program_name)s.log + stderr_logfile=/data/logs/supervisor/%(program_name)s.err + + stdout_logfile_maxbytes=500MB + stderr_logfile_maxbytes=500MB + stdout_logfile_backups=5 + stderr_logfile_backups=5 +EOF + + + ##### update supervisor & show supervisor status ######## + echo "Update ing..." + supervisorctl update + echo "Update done" + echo "-----current supervisor result: -----"; + supervisorctl status ${appname} + echo "-----current supervisor result done -----"; + +} + +########################################## +# start: +# start the application +########################################## +function start_directly() { + if $isdaemon; then + #### start app backgroud ##### + nohup $appexec & + echo $! > $approot/logs/pid + wait $! + else + #### start app frontgroud #### + rm -f $approot/logs/stoped + while [ ! -f $approot/logs/stoped ]; do + $appexec + sleep 30 + done + fi +} + +function start_by_monitor() { + echo "Start application ..."; + supervisorctl start ${appname} + sleep 0.2; + echo "-----current supervisor result: -----"; + supervisorctl status ${appname} + echo "-----current supervisor result done -----"; + echo "START DONE"; +} + +########################################## +# stop: +# stop the application +########################################## +function stop_directly() { + #### check pid file ############ + if [ ! -f $approot/logs/pid ]; then + echo "ERROR: no pid file: '$approot/logs/pid'" + return 1 + fi + #### check cmdline ############ + local pid=$(cat $approot/logs/pid) + if [ -z $pid ] || [ "$(/proc/$pid/cmdline)"x == "$appexec"x ]; then + echo "ERROR: pid($pid) is invalid or command line mismatch"; + return 1 + fi + #### do stop ################## + echo "stoped at: $(date +'%Y-%m-%d %H:%M:%S')" > $approot/logs/stoped + kill -9 $pid +} +function stop_by_monitor() { + echo "Stop application ..."; + supervisorctl stop ${appname} + sleep 0.1; + echo "-----current supervisor result: -----"; + supervisorctl status ${appname} + echo "-----current supervisor result done -----"; + echo "STOP DONE" +} + +########################################## +# restart: +# restart the application +########################################## +function restart_directly() { + stop_directly && start_directly +} +function restart_by_monitor() { + stop_by_monitor && start_by_monitor +} + +############################################# +############## run ########################## +main $@ +#############################################