-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathreplace.go
57 lines (48 loc) · 1.73 KB
/
replace.go
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
package main
import (
"io/fs"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/shellx"
)
// replaceDeps contains dependencies for [replaceRunningInstance].
type replaceDeps struct {
// CopyFile is MANDATORY and MUST behave like [shellx.CopyFile].
CopyFile func(source string, dest string, perms fs.FileMode) error
// Run is MANDATORY and MUST behave like [shellx.Run].
Run func(logger model.Logger, command string, args ...string) error
}
// replaceDryRun is an internal flag used for testing.
var replaceDryRun bool
// newReplaceDeps creates a fully initialized instance of [replaceDeps].
func newReplaceDeps() *replaceDeps {
deps := &replaceDeps{
CopyFile: shellx.CopyFile,
Run: shellx.Run,
}
if replaceDryRun {
deps.CopyFile = func(source, dest string, perms fs.FileMode) error {
return nil
}
deps.Run = func(logger model.Logger, command string, args ...string) error {
return nil
}
}
return deps
}
// replaceRunningInstance executes the command to replace
// a running instance of oohelperd with this instance.
func replaceRunningInstance(deps *replaceDeps) {
// stop the running instance.
runtimex.Try0(deps.Run(log.Log, "systemctl", "stop", "oohelperd.service"))
// copy oohelperd to the destination path.
executable := runtimex.Try1(filepath.Abs(runtimex.Try1(os.Executable())))
destpath := string(filepath.Separator) + filepath.Join("usr", "bin", "oohelperd")
log.Infof("+ cp %s %s", executable, destpath)
runtimex.Try0(deps.CopyFile(executable, destpath, 0755))
// restart the running instance.
runtimex.Try0(deps.Run(log.Log, "systemctl", "start", "oohelperd.service"))
}