-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon_common.go
92 lines (84 loc) · 2.34 KB
/
daemon_common.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package omcmd
import (
"context"
"fmt"
"os"
"path"
"time"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/util/command"
"github.com/opensvc/om3/util/file"
"github.com/opensvc/om3/util/hostname"
)
type (
CmdDaemonCommon struct{}
)
// isRunning returns true if daemon is running
func (t *CmdDaemonCommon) isRunning() bool {
cli, err := client.New()
if err != nil {
return false
}
if resp, err := cli.GetNodePing(context.Background(), hostname.Hostname()); err != nil {
return false
} else if resp.StatusCode != 204 {
return false
}
return true
}
func (t *CmdDaemonCommon) nodeDrain() (err error) {
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs([]string{"node", "drain", "--wait"}),
)
_, _ = fmt.Fprintf(os.Stdout, "Draining node\n")
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s: %w", cmd, err)
}
return nil
}
func (t *CmdDaemonCommon) backupLocalConfig(name string) error {
pathEtc := rawconfig.Paths.Etc
if v, err := file.ExistsAndDir(pathEtc); err != nil {
return err
} else if !v {
_, _ = fmt.Fprintf(os.Stdout, "Empty %s, skip backup\n", pathEtc)
return nil
}
cmd := command.New(
command.WithBufferedStdout(),
command.WithName(os.Args[0]),
command.WithArgs([]string{"**", "print", "config", "--local", "--format", "json"}),
// allow exit code 1 (Error: no match)
command.WithIgnoredExitCodes(0, 1),
)
_, _ = fmt.Fprintln(os.Stdout, "Dump all configs")
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s: %w", cmd, err)
}
backup := path.Join(pathEtc, time.Now().Format(name+"-2006-01-02T15:04:05.json"))
_, _ = fmt.Fprintf(os.Stdout, "Save configs to %s\n", backup)
if err := os.WriteFile(backup, cmd.Stdout(), 0o400); err != nil {
return err
}
return nil
}
func (t *CmdDaemonCommon) deleteLocalConfig() error {
pathEtc := rawconfig.Paths.Etc
if v, err := file.ExistsAndDir(pathEtc); err != nil {
return err
} else if v {
cmd := command.New(
command.WithName(os.Args[0]),
command.WithArgs([]string{"**", "delete", "--local"}),
)
_, _ = fmt.Fprintf(os.Stdout, "Delete all config\n")
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s: %w", cmd, err)
}
} else {
_, _ = fmt.Fprintf(os.Stdout, "Empty %s, skip delete local config\n", pathEtc)
}
return rawconfig.CreateMandatoryDirectories()
}