From a270328a0170faeea895dd48fec4e1f328cb046a Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Wed, 29 Nov 2023 14:45:52 +0800 Subject: [PATCH] Feat: install linux service --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/release_dev.yml | 2 +- Dockerfile | 4 +- README-CN.md | 2 +- README.md | 2 +- build.sh => script/build.sh | 48 +++------- .../docker-compose.yml | 0 entrypoint.sh => script/entrypoint.sh | 0 install.sh => script/install.sh | 93 ++++++++++++++++--- proto.sh => script/proto.sh | 0 script/synctv.service | 11 +++ 12 files changed, 110 insertions(+), 56 deletions(-) rename build.sh => script/build.sh (83%) rename docker-compose.yml => script/docker-compose.yml (100%) rename entrypoint.sh => script/entrypoint.sh (100%) rename install.sh => script/install.sh (58%) rename proto.sh => script/proto.sh (100%) create mode 100644 script/synctv.service diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bb9c04b..89c33d1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index de6d461..2e88936 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: diff --git a/.github/workflows/release_dev.yml b/.github/workflows/release_dev.yml index e122216..834e3ef 100644 --- a/.github/workflows/release_dev.yml +++ b/.github/workflows/release_dev.yml @@ -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: diff --git a/Dockerfile b/Dockerfile index b76d07b..0be7bb2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README-CN.md b/README-CN.md index bfae649..583128a 100644 --- a/README-CN.md +++ b/README-CN.md @@ -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 diff --git a/README.md b/README.md index 6e91ace..bf4c189 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.sh b/script/build.sh similarity index 83% rename from build.sh rename to script/build.sh index 19cdcd8..e5404d8 100755 --- a/build.sh +++ b/script/build.sh @@ -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" diff --git a/docker-compose.yml b/script/docker-compose.yml similarity index 100% rename from docker-compose.yml rename to script/docker-compose.yml diff --git a/entrypoint.sh b/script/entrypoint.sh similarity index 100% rename from entrypoint.sh rename to script/entrypoint.sh diff --git a/install.sh b/script/install.sh similarity index 58% rename from install.sh rename to script/install.sh index e33f755..7821030 100755 --- a/install.sh +++ b/script/install.sh @@ -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 diff --git a/proto.sh b/script/proto.sh similarity index 100% rename from proto.sh rename to script/proto.sh diff --git a/script/synctv.service b/script/synctv.service new file mode 100644 index 0000000..3fe7788 --- /dev/null +++ b/script/synctv.service @@ -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