oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package runnersetup
2
3import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strconv"
9 "strings"
10)
11
12// command runs a system program from /, never the folder the installer
13// was started from (which this user may not read).
14func command(ctx context.Context, name string, args ...string) *exec.Cmd {
15 cmd := exec.CommandContext(ctx, name, args...)
16 cmd.Dir = "/"
17 return cmd
18}
19
20func systemdQuote(s string) string { return strconv.Quote(strings.ReplaceAll(s, "%", "%%")) }
21func serviceUnit(binary, env string) string {
22 return "[Unit]\nDescription=equipo-runner\n[Service]\nType=simple\nEnvironmentFile=" + systemdQuote(env) + "\nExecStart=" + systemdQuote(strings.ReplaceAll(binary, "$", "$$")) + " run\nRestart=on-failure\nRestartSec=5\nTimeoutStopSec=45\nKillMode=control-group\nUMask=0077\nNoNewPrivileges=yes\n[Install]\nWantedBy=default.target\n"
23}
24func userServiceAvailable(ctx context.Context) bool {
25 return command(ctx, "systemctl", "--user", "show-environment").Run() == nil
26}
27func readLingering(ctx context.Context) (bool, error) {
28 b, e := command(ctx, "loginctl", "show-user", strconv.Itoa(os.Getuid()), "--property=Linger", "--value").Output()
29 if e != nil {
30 return false, e
31 }
32 switch strings.TrimSpace(string(b)) {
33 case "yes":
34 return true, nil
35 case "no":
36 return false, nil
37 default:
38 return false, errUnavailable
39 }
40}
41func enableService(ctx context.Context, path string) error {
42 for _, args := range [][]string{{"--user", "link", path}, {"--user", "daemon-reload"}, {"--user", "enable", "equipo-runner.service"}, {"--user", "restart", "equipo-runner.service"}} {
43 if e := command(ctx, "systemctl", args...).Run(); e != nil {
44 return e
45 }
46 }
47 return nil
48}
49
50// CheckServiceEnvironment refuses to enable a unit without its private local
51// configuration. It reads only the explicit installation root.
52func CheckServiceEnvironment(root, origin, state string) error {
53 values, e := ReadServiceEnvironment(root)
54 if e != nil {
55 return e
56 }
57 if values["EQUIPO_ORIGIN"] != origin || values["EQUIPO_RUNNER_STATE_DIR"] != state {
58 return errUnavailable
59 }
60 return nil
61}
62
63func SaveServiceEnvironment(root string, data []byte) error {
64 return writeOwned(root+"/runner.env", data, 0600)
65}
66
67func ReadServiceEnvironment(root string) (map[string]string, error) {
68 return ReadEnvFile(root + "/runner.env")
69}
70
71// ReadEnvFile reads a private runner environment file: EQUIPO_ keys
72// only, each once, values optionally quoted; the file the runner's own,
73// 0600, no symlink (a Mac's launchd agent passes it with --env-file).
74func ReadEnvFile(path string) (map[string]string, error) {
75 if !filepath.IsAbs(path) || filepath.Clean(path) != path {
76 return nil, errUnavailable
77 }
78 real, e := filepath.EvalSymlinks(path)
79 if e != nil || real != path {
80 return nil, errUnavailable
81 }
82 i, e := os.Lstat(path)
83 if e != nil || !i.Mode().IsRegular() || i.Mode().Perm() != 0600 || i.Size() > 65536 {
84 return nil, errUnavailable
85 }
86 b, e := os.ReadFile(path)
87 if e != nil {
88 return nil, e
89 }
90 values := map[string]string{}
91 for _, line := range strings.Split(string(b), "\n") {
92 if strings.HasPrefix(line, "#") || line == "" {
93 continue
94 }
95 k, v, ok := strings.Cut(line, "=")
96 _, duplicate := values[k]
97 if !ok || !strings.HasPrefix(k, "EQUIPO_") || duplicate {
98 return nil, errUnavailable
99 }
100 if strings.HasPrefix(v, "\"") {
101 var err error
102 v, err = strconv.Unquote(v)
103 if err != nil {
104 return nil, errUnavailable
105 }
106 }
107 values[k] = v
108 }
109 return values, nil
110}
111