diff --git a/build.go b/build.go index b38e1cd58..0a7f5b755 100644 --- a/build.go +++ b/build.go @@ -30,6 +30,7 @@ import ( "runtime" "strconv" "strings" + "text/template" "time" ) @@ -59,12 +60,11 @@ type target struct { debname string debdeps []string debpre string - debpost string description string buildPkgs []string binaryName string archiveFiles []archiveFile - systemdServices []string + systemdService string installationFiles []archiveFile tags []string } @@ -86,7 +86,6 @@ var targets = map[string]target{ name: "syncthing", debname: "syncthing", debdeps: []string{"libc6", "procps"}, - debpost: "script/post-upgrade", description: "Open Source Continuous File Synchronization", buildPkgs: []string{"github.com/syncthing/syncthing/cmd/syncthing"}, binaryName: "syncthing", // .exe will be added automatically for Windows builds @@ -97,6 +96,7 @@ var targets = map[string]target{ {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644}, // All files from etc/ and extra/ added automatically in init(). }, + systemdService: "syncthing@*.service", installationFiles: []archiveFile{ {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755}, {src: "README.md", dst: "deb/usr/share/doc/syncthing/README.txt", perm: 0644}, @@ -141,9 +141,7 @@ var targets = map[string]target{ {src: "LICENSE", dst: "LICENSE.txt", perm: 0644}, {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644}, }, - systemdServices: []string{ - "cmd/stdiscosrv/etc/linux-systemd/stdiscosrv.service", - }, + systemdService: "cmd/stdiscosrv/etc/linux-systemd/stdiscosrv.service", installationFiles: []archiveFile{ {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755}, {src: "cmd/stdiscosrv/README.md", dst: "deb/usr/share/doc/syncthing-discosrv/README.txt", perm: 0644}, @@ -170,9 +168,7 @@ var targets = map[string]target{ {src: "LICENSE", dst: "LICENSE.txt", perm: 0644}, {src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644}, }, - systemdServices: []string{ - "cmd/strelaysrv/etc/linux-systemd/strelaysrv.service", - }, + systemdService: "cmd/strelaysrv/etc/linux-systemd/strelaysrv.service", installationFiles: []archiveFile{ {src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755}, {src: "cmd/strelaysrv/README.md", dst: "deb/usr/share/doc/syncthing-relaysrv/README.txt", perm: 0644}, @@ -654,11 +650,13 @@ func buildDeb(target target) { for _, dep := range target.debdeps { args = append(args, "-d", dep) } - for _, service := range target.systemdServices { - args = append(args, "--deb-systemd", service) - } - if target.debpost != "" { - args = append(args, "--after-upgrade", target.debpost) + if target.systemdService != "" { + debpost, err := createPostInstScript(target) + defer os.Remove(debpost) + if err != nil { + log.Fatal(err) + } + args = append(args, "--after-upgrade", debpost) } if target.debpre != "" { args = append(args, "--before-install", target.debpre) @@ -666,6 +664,28 @@ func buildDeb(target target) { runPrint("fpm", args...) } +func createPostInstScript(target target) (string, error) { + scriptname := filepath.Join("script", "deb-post-inst.template") + t, err := template.ParseFiles(scriptname) + if err != nil { + return "", err + } + scriptname = strings.TrimSuffix(scriptname, ".template") + w, err := os.Create(scriptname) + if err != nil { + return "", err + } + defer w.Close() + if err = t.Execute(w, struct { + Service, Command string + }{ + target.systemdService, target.binaryName, + }); err != nil { + return "", err + } + return scriptname, nil +} + func shouldBuildSyso(dir string) (string, error) { type M map[string]interface{} version := getVersion() diff --git a/script/deb-post-inst.template b/script/deb-post-inst.template new file mode 100644 index 000000000..b98d6e629 --- /dev/null +++ b/script/deb-post-inst.template @@ -0,0 +1,14 @@ +#!/bin/sh + +command -v deb-systemd-invoke > /dev/null +has_systemd=$? + +if [ $has_systemd -eq 0 ]; then + systemctl daemon-reload +fi + +if [ $has_systemd -eq 0 ] && [ -n "$(systemctl list-units --plain --no-legend {{.Service}})" ]; then + deb-systemd-invoke try-restart "{{.Service}}" +else + pkill -HUP -x {{.Command}} +fi diff --git a/script/post-upgrade b/script/post-upgrade deleted file mode 100644 index 5fb8ad187..000000000 --- a/script/post-upgrade +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -if command -v deb-systemd-invoke > /dev/null; then - deb-systemd-invoke restart procps.service -else - sysctl -q --system -fi -pkill -HUP -x syncthing || true