This commit is contained in:
Zheng Kai
2023-03-29 17:42:41 +08:00
parent 94b04a181a
commit 1b107ed035
36 changed files with 617 additions and 0 deletions

1
server/build/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/.git-hash

36
server/build/build-server.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash -e
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR"
GIT_ROOT=$(git rev-parse --show-cdup 2>/dev/null)
if [ -n "$GIT_ROOT" ]; then
TMPDIR="${GIT_ROOT}/tmp"
fi
. ./common.sh
DATE=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S')
GO_VERSION=$(go version)
GIT_VERSION=$(./git-hash.sh)
if [ "$DOCKER_RUNNING" == "yes" ] && [ -f .git-hash ]; then
GIT_VERSION=$(cat .git-hash || :)
HOSTNAME="docker"
fi
LDFLAGS="-X '${BUILD_PACKAGE}.BuildGoVersion=${GO_VERSION}' \
-X '${BUILD_PACKAGE}.BuildTime=${DATE}' \
-X '${BUILD_PACKAGE}.BuildType=${TYPE}' \
-X '${BUILD_PACKAGE}.BuildHost=${HOSTNAME}' \
-X '${BUILD_PACKAGE}.BuildGit=${GIT_VERSION}'"
cd ../src
if [ -d "vendor" ]; then
VENDOR="-mod=vendor"
fi
CGO_ENABLED=0 go build $VENDOR \
-ldflags "$LDFLAGS" \
-o "$EXE_NEXT" \
"../build/${TYPE}/"*.go \
2> >(while read -r line; do echo -e "\e[38;2;255;45;45;48;2;10;10;10m$line\e[0m" >&2; done)

18
server/build/common.sh Normal file
View File

@@ -0,0 +1,18 @@
BIN="orca-server"
BUILD_PACKAGE="project/build"
TYPE="${1:-dev}"
if [ ! -f "${DIR}/${TYPE}/go.mod" ]; then
echo "no build \"${TYPE}\""
exit 1
fi
DIST_DIR="$(dirname "$DIR")/dist/${TYPE}"
mkdir -p "$DIST_DIR"
EXE="${DIST_DIR}/${BIN}"
EXE_NEXT="${EXE}-next"
PID_FILE="${EXE}.pid"
LOG_FILE="${DIST_DIR}/server.log"

1
server/build/config.ini Normal file
View File

@@ -0,0 +1 @@
type = dev

7
server/build/dev/go.mod Normal file
View File

@@ -0,0 +1,7 @@
module dev
go 1.17
require project v0.0.0
replace project v0.0.0 => ../../src

7
server/build/dev/main.go Normal file
View File

@@ -0,0 +1,7 @@
package main
import "project"
func main() {
project.Start()
}

36
server/build/get-pid.sh Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash -e
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR" || exit 1
. ./common.sh
if [ -n "$PID_FILE" ] && [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE" 2>/dev/null|| :)
fi
if [ -z "$PID" ] || [ -z "$EXE" ]; then
>&2 echo no pid or exec
exit 1
fi
EXE_LINK="/proc/${PID}/exe"
if [ ! -L "$EXE_LINK" ]; then
>&2 echo "no pid $PID"
exit 2
fi
if [ ! -r "$EXE_LINK" ]; then
>&2 echo "can not read pid $PID"
exit 3
fi
EXE_READLINK=$(readlink -f "$EXE_LINK" || :)
if [ -z "$EXE_READLINK" ]; then
>&2 echo "unknown pid $PID"
exit 4
fi
if [ "$EXE_READLINK" != "$EXE" ]; then
>&2 echo "pid $PID not file $EXE (${EXE_READLINK})"
exit 5
fi
echo "$PID"

26
server/build/git-hash.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
HASH=$(git rev-parse --short HEAD 2>/dev/null || :)
if [ -z "$HASH" ]; then
echo "empty"
exit
fi
TAG=$(git describe --exact-match "$HASH" 2>/dev/null | head -n 1)
if [ -n "$TAG" ]; then
HASH="$TAG"
fi
ST=$(git status -s -u | head -n 1)
if [ -n "$ST" ]; then
HASH="${HASH}-dirty"
fi
BRANCH=$(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/")
if [[ "$BRANCH" == *"("* ]]; then
BRANCH=''
else
BRANCH="${BRANCH}:"
fi
echo "${BRANCH}${HASH}"

7
server/build/prod/go.mod Normal file
View File

@@ -0,0 +1,7 @@
module prod
go 1.17
require project v0.0.0
replace project v0.0.0 => ../../src

View File

@@ -0,0 +1,7 @@
package main
import "project"
func main() {
project.Prod()
}

22
server/build/run-server.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash -e
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR" || exit 1
. ./common.sh
if [ ! -f config.ini ]; then
echo "type = $TYPE" > config.ini
fi
"${DIR}/build-server.sh" "$TYPE"
echo 'done'
echo
"${DIR}/stop-server.sh" "$TYPE" || :
echo 'done'
mv "$EXE_NEXT" "$EXE"
echo
"${DIR}/start-server.sh" "$TYPE"
echo 'done'

22
server/build/start-server.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash -e
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR" || exit 1
. ./common.sh
LOG="log file: $LOG_FILE"
PID=$(./get-pid.sh "$TYPE" 2>/dev/null || :)
if [ -n "$PID" ]; then
echo "server running, pid = $PID"
echo "$LOG"
exit
fi
ulimit -n 65535 >/dev/null 2>&1 || :
echo "ulimit = $(ulimit -n)"
nohup "$EXE" > "$LOG_FILE" 2>&1 &
PID="$!"
echo "$PID" > "$PID_FILE"
echo "new server started, pid = $PID"
echo "$LOG"

19
server/build/status.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR" || exit 1
. ./common.sh
PID=$(./get-pid.sh "$TYPE")
if [ -z "$PID" ]; then
exit
fi
cat "/proc/${PID}/limits"
echo
cat "/proc/${PID}/status"
echo
cat "/proc/${PID}/io"
echo
echo "more in /proc/${PID}"

20
server/build/stop-server.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/bash -e
DIR=$(readlink -f "$0") && DIR=$(dirname "$DIR") && cd "$DIR" || exit 1
. ./common.sh
PID=$(./get-pid.sh "$TYPE" 2>/dev/null || :)
if [ -z "$PID" ]; then
echo server is not running
exit
fi
echo 'stoping server' >> "$LOG_FILE" 2>&1 &
echo "kill pid $PID $EXE"
sudo kill "$PID"
while [ -e "/proc/${PID}/exe" ];
do
sleep 1;
done;