From 7b75959aee179c04ee3e24c4147d632f16bc0dbb Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 24 Jul 2024 04:21:45 +0800 Subject: [PATCH 1/4] Makefile: Use install(1) instead of cp(1) --- Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 42e9a02..4ffe8e3 100644 --- a/Makefile +++ b/Makefile @@ -38,12 +38,12 @@ yukictl: deb_dir := $(shell mktemp -d) deb: | yukid yukictl - mkdir -p $(addprefix $(deb_dir)/, DEBIAN etc/bash_completion.d etc/yuki lib/systemd/system usr/local/bin) - cp etc/daemon.example.toml $(deb_dir)/etc/yuki - cp etc/yukid.service $(deb_dir)/lib/systemd/system - cp yukid yukictl $(deb_dir)/usr/local/bin + install -Dm644 etc/daemon.example.toml -t $(deb_dir)/etc/yuki + install -Dm644 etc/yukid.service -t $(deb_dir)/lib/systemd/system + install -Dm755 yukid yukictl -t $(deb_dir)/usr/local/bin ln -s yukictl $(deb_dir)/usr/local/bin/yuki + install -d $(addprefix $(deb_dir), /etc/bash_completion.d /DEBIAN) $(deb_dir)/usr/local/bin/yukictl completion bash > $(deb_dir)/etc/bash_completion.d/yukictl - sed "s/\$$VERSION\>/$(version)/g;s/^Version: v/Version: /g;s/\$$ARCH\>/$(shell go env GOARCH)/g" \ + sed "s/\$$VERSION\>/$(version:v%=%)/g;s/\$$ARCH\>/$(shell go env GOARCH)/g" \ etc/debian-control > $(deb_dir)/DEBIAN/control dpkg-deb --root-owner-group --build -Zxz $(deb_dir) . From 18dbc597ce6ea5fbb75ab29263e3e9b0b6987781 Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 24 Jul 2024 15:49:53 +0800 Subject: [PATCH 2/4] Use GoReleaser to generate deb package --- .goreleaser.yaml | 39 +++++++++++++++++++++++++++++++++++---- Makefile | 20 +++++--------------- etc/debian-control | 9 --------- etc/postinst.sh | 6 ++++++ etc/yukid.service | 4 ++-- 5 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 etc/debian-control create mode 100755 etc/postinst.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml index e82d5d0..f2bb004 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,9 +1,9 @@ +version: 2 before: hooks: - go mod tidy builds: - - - id: yukid + - id: yukid binary: yukid env: - CGO_ENABLED=0 @@ -16,8 +16,7 @@ builds: - -trimpath ldflags: - -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} -X github.com/ustclug/Yuki/pkg/info.GitCommit={{.Commit}} - - - id: yukictl + - id: yukictl binary: yukictl env: - CGO_ENABLED=0 @@ -30,6 +29,11 @@ builds: - -trimpath ldflags: - -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} -X github.com/ustclug/Yuki/pkg/info.GitCommit={{.Commit}} + hooks: + post: + - cmd: sh -c "{{ .Path }} completion bash > bash_completion" + dir: "{{ dir (dir .Path) }}" + output: true archives: - format: binary name_template: "{{ .Binary }}_{{ .Os }}_{{ .Arch }}" @@ -39,6 +43,33 @@ snapshot: name_template: "{{ incpatch .Version }}-next" changelog: use: github-native +nfpms: + - id: default + package_name: yuki + homepage: https://github.com/ustclug/Yuki + maintainer: "USTC LUG " + description: |- + USTC Mirror Manager + formats: + - deb + umask: 0o022 + dependencies: + - "docker.io | docker-engine | docker-ce" + section: admin + priority: extra + provides: + - yukid + - yukictl + contents: + - src: dist/bash_completion + dst: /etc/bash_completion.d/yukictl + - src: etc/daemon.example.toml + dst: /etc/yuki/ + - src: etc/yukid.service + dst: /lib/systemd/system/ + - src: yukictl + dst: /usr/bin/yuki + type: symlink # modelines, feel free to remove those if you don't want/use them: # yaml-language-server: $schema=https://goreleaser.com/static/schema.json diff --git a/Makefile b/Makefile index 4ffe8e3..2fc3f64 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,13 @@ .PHONY: all all: yukid yukictl +.PHONY: release +release: + goreleaser release --snapshot --clean + .PHONY: clean clean: - rm -f yukid yukictl *.deb + rm -rf yukid yukictl dist/ .PHONY: lint lint: @@ -33,17 +37,3 @@ yukid: .PHONY: yukictl yukictl: go build -ldflags "$(go_ldflags)" -trimpath ./cmd/yukictl - -.PHONY: deb - -deb_dir := $(shell mktemp -d) -deb: | yukid yukictl - install -Dm644 etc/daemon.example.toml -t $(deb_dir)/etc/yuki - install -Dm644 etc/yukid.service -t $(deb_dir)/lib/systemd/system - install -Dm755 yukid yukictl -t $(deb_dir)/usr/local/bin - ln -s yukictl $(deb_dir)/usr/local/bin/yuki - install -d $(addprefix $(deb_dir), /etc/bash_completion.d /DEBIAN) - $(deb_dir)/usr/local/bin/yukictl completion bash > $(deb_dir)/etc/bash_completion.d/yukictl - sed "s/\$$VERSION\>/$(version:v%=%)/g;s/\$$ARCH\>/$(shell go env GOARCH)/g" \ - etc/debian-control > $(deb_dir)/DEBIAN/control - dpkg-deb --root-owner-group --build -Zxz $(deb_dir) . diff --git a/etc/debian-control b/etc/debian-control deleted file mode 100644 index 457fb7d..0000000 --- a/etc/debian-control +++ /dev/null @@ -1,9 +0,0 @@ -Package: yuki -Version: $VERSION -Architecture: $ARCH -Priority: optional -Section: admin -Maintainer: USTC LUG -Homepage: https://github.com/ustclug/Yuki -Depends: docker-engine | docker.io | docker-ce -Description: USTC Mirror Manager diff --git a/etc/postinst.sh b/etc/postinst.sh new file mode 100755 index 0000000..dfea659 --- /dev/null +++ b/etc/postinst.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Only run if systemd is running +[ -d /run/systemd ] || exit 0 + +systemctl enable yukid.service diff --git a/etc/yukid.service b/etc/yukid.service index f41e3a3..2157df3 100644 --- a/etc/yukid.service +++ b/etc/yukid.service @@ -8,8 +8,8 @@ ConditionPathExists=/etc/yuki/daemon.toml [Service] Type=exec User=mirror -ExecStart=/usr/local/bin/yukid -ExecReload=/usr/local/bin/yukictl reload +ExecStart=/usr/bin/yukid +ExecReload=/usr/bin/yukictl reload Restart=on-failure RestartSec=5 From 71029788637861cd6d1182657bad8ec8f95790d3 Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 24 Jul 2024 16:11:25 +0800 Subject: [PATCH 3/4] Add postinstall script --- .goreleaser.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f2bb004..1ea86a3 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -60,6 +60,8 @@ nfpms: provides: - yukid - yukictl + scripts: + postinstall: etc/postinst.sh contents: - src: dist/bash_completion dst: /etc/bash_completion.d/yukictl From cdfb256a4a6de1a395cb81565fb5dbe4fd854c5d Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 24 Jul 2024 16:32:58 +0800 Subject: [PATCH 4/4] Add preremove script --- .goreleaser.yaml | 1 + etc/prerm.sh | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100755 etc/prerm.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 1ea86a3..9fdb561 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -62,6 +62,7 @@ nfpms: - yukictl scripts: postinstall: etc/postinst.sh + preremove: etc/prerm.sh contents: - src: dist/bash_completion dst: /etc/bash_completion.d/yukictl diff --git a/etc/prerm.sh b/etc/prerm.sh new file mode 100755 index 0000000..5b815df --- /dev/null +++ b/etc/prerm.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Only run if systemd is running +[ -d /run/systemd ] || exit 0 + +systemctl disable --now yukid.service