Feat: install linux service

This commit is contained in:
zijiren233 2023-11-29 14:45:52 +08:00
parent e3f0dabd4b
commit a270328a01
12 changed files with 110 additions and 56 deletions

View File

@ -29,7 +29,7 @@ jobs:
- name: Build
run: |
bash build.sh -v dev -P -p "windows,linux,darwin"
bash script/build.sh -v dev -P -p "windows,linux,darwin"
- name: Upload artifact
uses: actions/upload-artifact@v3

View File

@ -28,7 +28,7 @@ jobs:
- name: Build
run: |
bash build.sh -v "v${{ steps.get_version.outputs.VERSION }}" -P -p "windows,linux,darwin"
bash script/build.sh -v "v${{ steps.get_version.outputs.VERSION }}" -P -p "windows,linux,darwin"
- uses: "marvinpinto/action-automatic-releases@latest"
with:

View File

@ -22,7 +22,7 @@ jobs:
- name: Build
run: |
bash build.sh -v dev -P -p "windows,linux,darwin"
bash script/build.sh -v dev -P -p "windows,linux,darwin"
- uses: "marvinpinto/action-automatic-releases@latest"
with:

View File

@ -8,13 +8,13 @@ COPY ./ ./
RUN apk add --no-cache bash curl gcc git go musl-dev
RUN bash build.sh -P -v ${VERSION} -b build
RUN bash script/build.sh -P -v ${VERSION}
From alpine:latest
COPY --from=builder /synctv/build/synctv /usr/local/bin/synctv
COPY entrypoint.sh /entrypoint.sh
COPY script/entrypoint.sh /entrypoint.sh
RUN apk add --no-cache bash ca-certificates su-exec tzdata

View File

@ -60,7 +60,7 @@ https://demo.synctv.wiki
您可以使用该脚本来安装和运行 SyncTV。
```bash
sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/install.sh | sudo bash -s -- -v latest
sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/script/install.sh | sudo bash -s -- -v latest
```
## Docker

View File

@ -62,7 +62,7 @@ You can download the latest binary from [release page](https://github.com/synctv
You can use the script to install and run SyncTV.
```bash
sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/install.sh | sudo bash -s -- -v latest
sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/script/install.sh | sudo bash -s -- -v latest
```
## Docker

View File

@ -1,5 +1,7 @@
#!/bin/bash
BIN_NAME="synctv"
function ChToScriptFileDir() {
cd "$(dirname "$0")"
if [ $? -ne 0 ]; then
@ -17,7 +19,7 @@ function Help() {
echo "-l set ldflags (default: -s -w --extldflags \"-static -fpic -Wl,-z,relro,-z,now\")"
echo "-p set platform (default: host platform, support: all, linux, darwin, windows)"
echo "-P set trim path (default: disable)"
echo "-b set build result dir (default: build)"
echo "-d set build result dir (default: build)"
echo "-T set tags (default: jsoniter)"
}
@ -37,12 +39,12 @@ function Init() {
PLATFORM=""
TRIM_PATH=""
SKIP_INIT_WEB=""
BUILD_DIR="build"
BUILD_DIR="../build"
TAGS="jsoniter"
}
function ParseArgs() {
while getopts "hsv:w:m:l:p:Pb:T:" arg; do
while getopts "hsv:w:m:l:p:Pd:T:" arg; do
case $arg in
h)
Help
@ -69,7 +71,7 @@ function ParseArgs() {
P)
TRIM_PATH="true"
;;
b)
d)
BUILD_DIR="$OPTARG"
;;
T)
@ -83,25 +85,6 @@ function ParseArgs() {
done
}
function GetLatestWebVersion() {
while true; do
LATEST=$(curl -sL https://api.github.com/repos/$1/releases/latest)
if [ $? -ne 0 ]; then exit $?; fi
if [ "$(echo "$LATEST" | grep -o "API rate limit exceeded")" ]; then
echo "API rate limit exceeded"
echo "sleep 5s"
sleep 5
elif [ "$(echo "$LATEST" | grep -o "Not Found")" ]; then
echo "Not Found"
exit 1
else
break
fi
done
WEB_VERSION=$(echo "$LATEST" | grep -o '"tag_name": "[^"]*' | grep -o '[^"]*$')
}
# Comply with golang version rules
function CheckVersionFormat() {
if [ "$1" == "dev" ] || [ "$(echo "$1" | grep -oE "^v?[0-9]+\.[0-9]+\.[0-9]+(\-beta.*|\-rc.*|\-alpha.*)?$")" ]; then
@ -116,17 +99,14 @@ function FixArgs() {
CheckAllPlatform
CheckVersionFormat "$VERSION"
if [ ! "$SKIP_INIT_WEB" ] && [ ! "$WEB_VERSION" ]; then
if [ "$VERSION" != "" ]; then
WEB_VERSION="$VERSION"
else
GetLatestWebVersion "synctv-org/synctv-web"
fi
WEB_VERSION="$VERSION"
fi
LDFLAGS="$LDFLAGS \
-X 'github.com/synctv-org/synctv/internal/version.Version=$VERSION' \
-X 'github.com/synctv-org/synctv/internal/version.WebVersion=$WEB_VERSION' \
-X 'github.com/synctv-org/synctv/internal/version.GitCommit=$GIT_COMMIT'"
# trim / at the end
BUILD_DIR="$(echo "$BUILD_DIR" | sed 's#/$##')"
}
@ -135,9 +115,9 @@ function InitDep() {
echo "skip init web"
return
fi
rm -rf public/dist/*
rm -rf "../public/dist/*"
echo "download: https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz"
curl -sL "https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz" | tar --strip-components 1 -C "public/dist" -z -x -v -f -
curl -sL "https://github.com/synctv-org/synctv-web/releases/download/${WEB_VERSION}/dist.tar.gz" | tar --strip-components 1 -C "../public/dist" -z -x -v -f -
if [ $? -ne 0 ]; then
echo "download web error"
exit 1
@ -195,9 +175,9 @@ function Build() {
EXT=""
fi
if [ "$TRIM_PATH" ]; then
CGO_ENABLED=$CGO_ENABLED GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" .
CGO_ENABLED=$CGO_ENABLED GOOS=$GOOS GOARCH=$GOARCH go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$BIN_NAME-$GOOS-$GOARCH$EXT" ../
else
CGO_ENABLED=$CGO_ENABLED GOOS=$GOOS GOARCH=$GOARCH go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)-$GOOS-$GOARCH$EXT" .
CGO_ENABLED=$CGO_ENABLED GOOS=$GOOS GOARCH=$GOARCH go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$BIN_NAME-$GOOS-$GOARCH$EXT" ../
fi
if [ $? -ne 0 ]; then
echo "build $GOOS/$GOARCH error"
@ -215,9 +195,9 @@ function BuildSingle() {
fi
echo "build $GOOS/$GOARCH"
if [ "$TRIM_PATH" ]; then
CGO_ENABLED=$CGO_ENABLED go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)$EXT" .
CGO_ENABLED=$CGO_ENABLED go build -trimpath -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$BIN_NAME$EXT" ../
else
CGO_ENABLED=$CGO_ENABLED go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$(basename $PWD)$EXT" .
CGO_ENABLED=$CGO_ENABLED go build -tags "$TAGS" -ldflags "$LDFLAGS" -o "$BUILD_DIR/$BIN_NAME$EXT" ../
fi
if [ $? -ne 0 ]; then
echo "build $GOOS/$GOARCH error"

View File

@ -6,13 +6,15 @@ download_tools_list=(
)
function Help() {
echo "Usage: sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/install.sh | sudo bash -s -- -v latest"
echo "Usage: sudo -v ; curl https://raw.githubusercontent.com/synctv-org/synctv/main/script/install.sh | sudo bash -s -- -v latest"
echo "-h: help"
echo "-v: install version (default: latest)"
}
function Init() {
VERSION="latest"
InitOS
InitArch
InitDownloadTools
}
@ -43,8 +45,7 @@ function FixArgs() {
}
function InitOS() {
OS="$(uname)"
case "$OS" in
case "$(uname)" in
Linux)
OS='linux'
;;
@ -59,8 +60,7 @@ function InitOS() {
}
function InitArch() {
ARCH="$(uname -m)"
case "$ARCH" in
case "$(uname -m)" in
x86_64 | amd64)
ARCH='amd64'
;;
@ -126,8 +126,7 @@ function Download() {
fi
;;
*)
echo "download tool not supported"
echo "supported tools: ${download_tools_list[*]}"
echo "download tool: $download_tool not supported"
exit 1
;;
esac
@ -135,22 +134,39 @@ function Download() {
function InstallWithVersion() {
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'synctv-install.XXXXXXXXXX')
cd "$tmp_dir"
trap 'rm -rf "$tmp_dir"' EXIT
echo "download: https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}"
Download "https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}" "synctv"
case "$OS" in
linux)
cp synctv /usr/bin/synctv.new
echo "download: https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}"
Download "https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}" "$tmp_dir/synctv"
cp "$tmp_dir/synctv" /usr/bin/synctv.new
if [ $? -ne 0 ]; then
echo "copy synctv to /usr/bin/synctv.new failed"
exit 1
fi
chmod 755 /usr/bin/synctv.new
chown root:root /usr/bin/synctv.new
mv /usr/bin/synctv{.new,}
;;
darwin)
echo "download: https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}"
Download "https://github.com/synctv-org/synctv/releases/download/$1/synctv-${OS}-${ARCH}" "$tmp_dir/synctv"
mkdir -m 0555 -p /usr/local/bin
cp synctv /usr/local/bin/synctv.new
if [ $? -ne 0 ]; then
echo "mkdir /usr/local/bin failed"
exit 1
fi
cp "$tmp_dir/synctv" /usr/local/bin/synctv.new
if [ $? -ne 0 ]; then
echo "copy synctv to /usr/local/bin/synctv.new failed"
exit 1
fi
mv /usr/local/bin/synctv{.new,}
chmod a=x /usr/local/bin/synctv
;;
@ -161,6 +177,54 @@ function InstallWithVersion() {
esac
}
function InitLinuxSystemctlService() {
mkdir -p "/opt/synctv"
if [ ! -d "/etc/systemd/system" ]; then
echo "/etc/systemd/system not found"
exit 1
fi
if [ -f "/etc/systemd/system/synctv.service" ]; then
break
fi
if [ -f "./script/synctv.service" ]; then
echo "use ./script/synctv.service"
cp "./script/synctv.service" "/etc/systemd/system/synctv.service"
if [ $? -ne 0 ]; then
echo "read ./script/synctv.service failed"
exit 1
fi
else
echo "use default synctv.service"
curl -sL "https://raw.githubusercontent.com/synctv-org/synctv/main/script/synctv.service" -o "/etc/systemd/system/synctv.service"
if [ $? -ne 0 ]; then
echo "download synctv.service failed"
exit 1
fi
fi
systemctl daemon-reload
echo "synctv.service installed"
echo "run: systemctl enable synctv.service && systemctl start synctv.service"
}
function InitSystemctlService() {
case "$OS" in
linux)
InitLinuxSystemctlService
;;
darwin)
echo "darwin not support"
exit 1
;;
*)
echo "OS not supported"
exit 1
;;
esac
}
function Install() {
current_version="$(CurrentVersion)"
echo "current version: $current_version"
@ -176,6 +240,5 @@ function Install() {
Init
ParseArgs "$@"
FixArgs
InitOS
InitArch
Install
InitSystemctlService

11
script/synctv.service Normal file
View File

@ -0,0 +1,11 @@
[Unit]
Description=SyncTV Service
After=network.target
[Service]
ExecStart=/usr/bin/synctv server --data-dir /opt/synctv
WorkingDirectory=/opt/synctv
Restart=unless-stopped
[Install]
WantedBy=multi-user.target