Skip to content

Commit

Permalink
Feat: install linux service
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Nov 29, 2023
1 parent e3f0dab commit a270328
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 14 additions & 34 deletions build.sh → script/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

BIN_NAME="synctv"

function ChToScriptFileDir() {
cd "$(dirname "$0")"
if [ $? -ne 0 ]; then
Expand All @@ -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)"
}

Expand All @@ -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
Expand All @@ -69,7 +71,7 @@ function ParseArgs() {
P)
TRIM_PATH="true"
;;
b)
d)
BUILD_DIR="$OPTARG"
;;
T)
Expand All @@ -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
Expand All @@ -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#/$##')"
}

Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
93 changes: 78 additions & 15 deletions install.sh → script/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -43,8 +45,7 @@ function FixArgs() {
}

function InitOS() {
OS="$(uname)"
case "$OS" in
case "$(uname)" in
Linux)
OS='linux'
;;
Expand All @@ -59,8 +60,7 @@ function InitOS() {
}

function InitArch() {
ARCH="$(uname -m)"
case "$ARCH" in
case "$(uname -m)" in
x86_64 | amd64)
ARCH='amd64'
;;
Expand Down Expand Up @@ -126,31 +126,47 @@ function Download() {
fi
;;
*)
echo "download tool not supported"
echo "supported tools: ${download_tools_list[*]}"
echo "download tool: $download_tool not supported"
exit 1
;;
esac
}

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
;;
Expand All @@ -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"
Expand All @@ -176,6 +240,5 @@ function Install() {
Init
ParseArgs "$@"
FixArgs
InitOS
InitArch
Install
InitSystemctlService
File renamed without changes.
11 changes: 11 additions & 0 deletions script/synctv.service
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a270328

Please sign in to comment.