Feat: install linux service

pull/39/head
zijiren233 2 years ago
parent e3f0dabd4b
commit a270328a01

@ -29,7 +29,7 @@ jobs:
- name: Build - name: Build
run: | 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 - name: Upload artifact
uses: actions/upload-artifact@v3 uses: actions/upload-artifact@v3

@ -28,7 +28,7 @@ jobs:
- name: Build - name: Build
run: | 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" - uses: "marvinpinto/action-automatic-releases@latest"
with: with:

@ -22,7 +22,7 @@ jobs:
- name: Build - name: Build
run: | 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" - uses: "marvinpinto/action-automatic-releases@latest"
with: with:

@ -8,13 +8,13 @@ COPY ./ ./
RUN apk add --no-cache bash curl gcc git go musl-dev 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 From alpine:latest
COPY --from=builder /synctv/build/synctv /usr/local/bin/synctv 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 RUN apk add --no-cache bash ca-certificates su-exec tzdata

@ -60,7 +60,7 @@ https://demo.synctv.wiki
您可以使用该脚本来安装和运行 SyncTV。 您可以使用该脚本来安装和运行 SyncTV。
```bash ```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 ## Docker

@ -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. You can use the script to install and run SyncTV.
```bash ```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 ## Docker

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
BIN_NAME="synctv"
function ChToScriptFileDir() { function ChToScriptFileDir() {
cd "$(dirname "$0")" cd "$(dirname "$0")"
if [ $? -ne 0 ]; then 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 "-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 platform (default: host platform, support: all, linux, darwin, windows)"
echo "-P set trim path (default: disable)" 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)" echo "-T set tags (default: jsoniter)"
} }
@ -37,12 +39,12 @@ function Init() {
PLATFORM="" PLATFORM=""
TRIM_PATH="" TRIM_PATH=""
SKIP_INIT_WEB="" SKIP_INIT_WEB=""
BUILD_DIR="build" BUILD_DIR="../build"
TAGS="jsoniter" TAGS="jsoniter"
} }
function ParseArgs() { 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 case $arg in
h) h)
Help Help
@ -69,7 +71,7 @@ function ParseArgs() {
P) P)
TRIM_PATH="true" TRIM_PATH="true"
;; ;;
b) d)
BUILD_DIR="$OPTARG" BUILD_DIR="$OPTARG"
;; ;;
T) T)
@ -83,25 +85,6 @@ function ParseArgs() {
done 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 # Comply with golang version rules
function CheckVersionFormat() { function CheckVersionFormat() {
if [ "$1" == "dev" ] || [ "$(echo "$1" | grep -oE "^v?[0-9]+\.[0-9]+\.[0-9]+(\-beta.*|\-rc.*|\-alpha.*)?$")" ]; then 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 CheckAllPlatform
CheckVersionFormat "$VERSION" CheckVersionFormat "$VERSION"
if [ ! "$SKIP_INIT_WEB" ] && [ ! "$WEB_VERSION" ]; then if [ ! "$SKIP_INIT_WEB" ] && [ ! "$WEB_VERSION" ]; then
if [ "$VERSION" != "" ]; then
WEB_VERSION="$VERSION" WEB_VERSION="$VERSION"
else
GetLatestWebVersion "synctv-org/synctv-web"
fi
fi fi
LDFLAGS="$LDFLAGS \ LDFLAGS="$LDFLAGS \
-X 'github.com/synctv-org/synctv/internal/version.Version=$VERSION' \ -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.WebVersion=$WEB_VERSION' \
-X 'github.com/synctv-org/synctv/internal/version.GitCommit=$GIT_COMMIT'" -X 'github.com/synctv-org/synctv/internal/version.GitCommit=$GIT_COMMIT'"
# trim / at the end
BUILD_DIR="$(echo "$BUILD_DIR" | sed 's#/$##')" BUILD_DIR="$(echo "$BUILD_DIR" | sed 's#/$##')"
} }
@ -135,9 +115,9 @@ function InitDep() {
echo "skip init web" echo "skip init web"
return return
fi 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" 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 if [ $? -ne 0 ]; then
echo "download web error" echo "download web error"
exit 1 exit 1
@ -195,9 +175,9 @@ function Build() {
EXT="" EXT=""
fi fi
if [ "$TRIM_PATH" ]; then 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 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 fi
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "build $GOOS/$GOARCH error" echo "build $GOOS/$GOARCH error"
@ -215,9 +195,9 @@ function BuildSingle() {
fi fi
echo "build $GOOS/$GOARCH" echo "build $GOOS/$GOARCH"
if [ "$TRIM_PATH" ]; then 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 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 fi
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "build $GOOS/$GOARCH error" echo "build $GOOS/$GOARCH error"

@ -6,13 +6,15 @@ download_tools_list=(
) )
function Help() { 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 "-h: help"
echo "-v: install version (default: latest)" echo "-v: install version (default: latest)"
} }
function Init() { function Init() {
VERSION="latest" VERSION="latest"
InitOS
InitArch
InitDownloadTools InitDownloadTools
} }
@ -43,8 +45,7 @@ function FixArgs() {
} }
function InitOS() { function InitOS() {
OS="$(uname)" case "$(uname)" in
case "$OS" in
Linux) Linux)
OS='linux' OS='linux'
;; ;;
@ -59,8 +60,7 @@ function InitOS() {
} }
function InitArch() { function InitArch() {
ARCH="$(uname -m)" case "$(uname -m)" in
case "$ARCH" in
x86_64 | amd64) x86_64 | amd64)
ARCH='amd64' ARCH='amd64'
;; ;;
@ -126,8 +126,7 @@ function Download() {
fi fi
;; ;;
*) *)
echo "download tool not supported" echo "download tool: $download_tool not supported"
echo "supported tools: ${download_tools_list[*]}"
exit 1 exit 1
;; ;;
esac esac
@ -135,22 +134,39 @@ function Download() {
function InstallWithVersion() { function InstallWithVersion() {
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'synctv-install.XXXXXXXXXX') tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'synctv-install.XXXXXXXXXX')
cd "$tmp_dir"
trap 'rm -rf "$tmp_dir"' EXIT 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 case "$OS" in
linux) 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 chmod 755 /usr/bin/synctv.new
chown root:root /usr/bin/synctv.new chown root:root /usr/bin/synctv.new
mv /usr/bin/synctv{.new,} mv /usr/bin/synctv{.new,}
;; ;;
darwin) 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 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,} mv /usr/local/bin/synctv{.new,}
chmod a=x /usr/local/bin/synctv chmod a=x /usr/local/bin/synctv
;; ;;
@ -161,6 +177,54 @@ function InstallWithVersion() {
esac 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() { function Install() {
current_version="$(CurrentVersion)" current_version="$(CurrentVersion)"
echo "current version: $current_version" echo "current version: $current_version"
@ -176,6 +240,5 @@ function Install() {
Init Init
ParseArgs "$@" ParseArgs "$@"
FixArgs FixArgs
InitOS
InitArch
Install Install
InitSystemctlService

@ -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
Loading…
Cancel
Save