Skip to content

Commit

Permalink
prepare to move service installer position
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Mar 26, 2024
1 parent 459730c commit 1a8f0b6
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ A: 请确认中间所有的反向代理都支持 WebSocket
1. **确保systemd是您的*启动进程***
2. 执行
```sh
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/service/installer.sh | sudo bash -s
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/installer/service/installer.sh | sudo bash -s
```
> 注意, 新版新增使用 `openbmclapi` 用户执行程序, 可能需要执行 `sudo chown -R openbmclapi /opt/openbmclapi` 指令修复权限

国内对 Github 的支持较差, 可以使用 ghproxy 等镜像站运行脚本, 本例中使用了 [crashmc.com](https://crashmc.com) 提供的 CDN:
```sh
MIRROR_PREFIX=https://cdn.crashmc.com/
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/service/installer.sh | sudo bash -s --mirror "${MIRROR_PREFIX}"
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/installer/service/installer.sh | sudo bash -s --mirror "${MIRROR_PREFIX}"
```

如果需要下载指定版本, 只需设置 `-t|--tag` 标志即可
```sh
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/service/installer.sh | sudo bash -s --tag v1.9.7-8
curl -fsSL ${MIRROR_PREFIX}https://raw.githubusercontent.com/LiterMC/go-openbmclapi/HEAD/installer/service/installer.sh | sudo bash -s --tag v1.9.7-8
```
3. 配置`/opt/openbmclapi/config.yaml`配置文件
4. 使用`systemctl start go-openbmclapi.service`启动服务
Expand Down
18 changes: 18 additions & 0 deletions installer/service/go-openbmclapi.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=Go-OpenBmclAPI Service
Documentation=https://github.com/LiterMC/go-openbmclapi
Wants=basic.target
After=network.target

[Service]
Type=idle
User=openbmclapi
WorkingDirectory=/opt/openbmclapi
ExecStart=/opt/openbmclapi/service-linux-go-openbmclapi
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=30
Restart=on-failure
TimeoutSec=30

[Install]
WantedBy=multi-user.target
157 changes: 157 additions & 0 deletions installer/service/installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/bin/bash

# if [ $(id -u) -ne 0 ]; then
# echo -e "\e[31mERROR: Not root user\e[0m"
# exit 1
# fi

ARGS=()

while [ $# -gt 0 ]; do
case "$1" in
-m|--mirror)
shift
MIRROR_PREFIX=$1
;;
-t|--tag)
shift
TARGET_TAG=$1
;;
-*)
echo -e "\e[31mERROR: Unknown option $1\e[0m"
exit 1
;;
*)
ARGS+=("$1")
;;
esac
shift
done

if [ ${#ARGS[@]} -ge 1 ]; then
TARGET_TAG="${ARGS[0]}"
fi

if [ -n "$MIRROR_PREFIX" ] && [[ "$MIRROR_PREFIX" != */ ]]; then
MIRROR_PREFIX="${MIRROR_PREFIX}/"
fi

echo "MIRROR_PREFIX=${MIRROR_PREFIX}"
echo "TARGET_TAG=${TARGET_TAG}"

REPO='LiterMC/go-openbmclapi'
RAW_PREFIX="${MIRROR_PREFIX}https://raw.githubusercontent.com"
RAW_REPO="$RAW_PREFIX/$REPO"
BASE_PATH=/opt/openbmclapi
USERNAME=openbmclapi

if ! systemd --version >/dev/null 2>&1 ; then
echo -e "\e[31mERROR: Failed to test systemd\e[0m"
exit 1
fi

if [ ! -d /usr/lib/systemd/system/ ]; then
echo -e "\e[31mERROR: /usr/lib/systemd/system/ is not exist\e[0m"
exit 1
fi

if ! id $USERNAME >/dev/null 2>&1; then
echo -e "\e[34m==> Creating user $USERNAME\e[0m"
useradd $USERNAME || {
echo -e "\e[31mERROR: Could not create user $USERNAME\e[0m"
exit 1
}
fi


function fetchGithubLatestTag(){
prefix="location: https://github.com/$REPO/releases/tag/"
location=$(curl -fsSI "https://github.com/$REPO/releases/latest" | grep "$prefix" | tr -d "\r")
[ $? = 0 ] || return 1
export LATEST_TAG="${location#${prefix}}"
}

function fetchBlob(){
file=$1
target=$2
filemod=$3

source="$RAW_REPO/$TARGET_TAG/$file"
echo -e "\e[34m==> Downloading $source\e[0m"
tmpf=$(mktemp -t go-openbmclapi.XXXXXXXXXXXX.downloading)
curl -fsSL -o "$tmpf" "$source" || { rm "$tmpf"; return 1; }
echo -e "\e[34m==> Downloaded $source\e[0m"
mv "$tmpf" "$target" || return $?
echo -e "\e[34m==> Installed to $target\e[0m"
chown $USERNAME "$target"
if [ -n "$filemod" ]; then
chmod "$filemod" "$target" || return $?
fi
}

echo

if [ -f /usr/lib/systemd/system/go-openbmclapi.service ]; then
echo -e "\e[33m==> WARN: go-openbmclapi.service is already installed, stopping\e[0m"
systemctl disable --now go-openbmclapi.service
fi

if [ ! -n "$TARGET_TAG" ]; then
echo -e "\e[34m==> Fetching latest tag for https://github.com/$REPO\e[0m"
fetchGithubLatestTag
TARGET_TAG=$LATEST_TAG
echo
echo -e "\e[32m*** go-openbmclapi LATEST TAG: $TARGET_TAG ***\e[0m"
echo
fi

fetchBlob service/go-openbmclapi.service /usr/lib/systemd/system/go-openbmclapi.service 0644 || exit $?

[ -d "$BASE_PATH" ] || { mkdir -p "$BASE_PATH" && chmod 0755 "$BASE_PATH" && chown $USERNAME "$BASE_PATH"; } || exit $?

# fetchBlob service/start-server.sh "$BASE_PATH/start-server.sh" 0755 || exit $?
# fetchBlob service/stop-server.sh "$BASE_PATH/stop-server.sh" 0755 || exit $?
# fetchBlob service/reload-server.sh "$BASE_PATH/reload-server.sh" 0755 || exit $?

ARCH=$(uname -m)
case "$ARCH" in
amd64|x86_64)
ARCH="amd64"
;;
i386|i686)
ARCH="386"
;;
aarch64|armv8|arm64)
ARCH="arm64"
;;
armv7l|armv6|armv7)
ARCH="arm"
;;
*)
echo -e "\e[31m
Unknown CPU architecture: $ARCH
Please report to https://github.com/LiterMC/go-openbmclapi/issues/new\e[0m"
exit 1
esac

source="${MIRROR_PREFIX}https://github.com/$REPO/releases/download/$TARGET_TAG/go-openbmclapi-linux-$ARCH"
echo -e "\e[34m==> Downloading $source\e[0m"

curl -fL -o "$BASE_PATH/service-linux-go-openbmclapi" "$source" && \
chmod 0755 "$BASE_PATH/service-linux-go-openbmclapi" && \
chown $USERNAME "$BASE_PATH/service-linux-go-openbmclapi" || \
exit 1

[ -f $BASE_PATH/config.yaml ] || fetchBlob config.yaml $BASE_PATH/config.yaml 0600 || exit $?

echo -e "\e[34m==> Enabling go-openbmclapi.service\e[0m"
systemctl enable go-openbmclapi.service || exit $?

echo -e "
================================ Install successed ================================
Use 'systemctl start go-openbmclapi.service' to start openbmclapi server
Use 'systemctl stop go-openbmclapi.service' to stop openbmclapi server
Use 'systemctl reload go-openbmclapi.service' to reload openbmclapi server configs
Use 'journalctl -f --output cat -u go-openbmclapi.service' to watch the openbmclapi logs
"

0 comments on commit 1a8f0b6

Please sign in to comment.