-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschedule_linux.go
More file actions
77 lines (62 loc) · 1.69 KB
/
schedule_linux.go
File metadata and controls
77 lines (62 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//go:build linux
package main
import (
"fmt"
"os"
"path/filepath"
"time"
)
func systemdUserDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config", "systemd", "user")
}
func servicePath() string { return filepath.Join(systemdUserDir(), "blocknet-upgrade.service") }
func timerPath() string { return filepath.Join(systemdUserDir(), "blocknet-upgrade.timer") }
func scheduleInstalled() bool {
_, err := os.Stat(timerPath())
return err == nil
}
func installSchedule(binPath string, interval time.Duration) error {
dir := systemdUserDir()
os.MkdirAll(dir, 0755)
service := fmt.Sprintf(`[Unit]
Description=Blocknet auto-upgrade check
[Service]
Type=oneshot
ExecStart=%s upgrade
`, binPath)
hours := int(interval.Hours())
if hours < 1 {
hours = 1
}
onCalendar := "daily"
if hours != 24 {
onCalendar = fmt.Sprintf("*-*-* 0/%d:00:00", hours)
}
timer := fmt.Sprintf(`[Unit]
Description=Blocknet auto-upgrade timer
[Timer]
OnCalendar=%s
Persistent=true
[Install]
WantedBy=timers.target
`, onCalendar)
if err := os.WriteFile(servicePath(), []byte(service), 0644); err != nil {
return fmt.Errorf("write service: %w", err)
}
if err := os.WriteFile(timerPath(), []byte(timer), 0644); err != nil {
return fmt.Errorf("write timer: %w", err)
}
runCmd("systemctl", "--user", "daemon-reload")
if err := runCmd("systemctl", "--user", "enable", "--now", "blocknet-upgrade.timer"); err != nil {
return fmt.Errorf("enable timer: %w", err)
}
return nil
}
func uninstallSchedule() error {
runCmd("systemctl", "--user", "disable", "--now", "blocknet-upgrade.timer")
os.Remove(servicePath())
os.Remove(timerPath())
runCmd("systemctl", "--user", "daemon-reload")
return nil
}