/
cmd_ctl_watchdog2.go
147 lines (128 loc) · 3.41 KB
/
cmd_ctl_watchdog2.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"path/filepath"
"runtime"
"time"
"github.com/keybase/cli"
"github.com/keybase/client/go/install"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/go-updater/watchdog"
)
// CmdWatchdog2 defines watchdog command
type CmdWatchdog2 struct {
libkb.Contextified
}
// ParseArgv is args for the watchdog command
func (c *CmdWatchdog2) ParseArgv(ctx *cli.Context) error {
return nil
}
// Run watchdog
func (c *CmdWatchdog2) Run() error {
env, log := c.G().Env, c.G().Log
log.Info("Starting watchdog")
runMode := env.GetRunMode()
if runMode != libkb.ProductionRunMode {
return fmt.Errorf("Watchdog is only supported in production")
}
// Don't run updater on linux
excludeUpdater := runtime.GOOS == "linux"
programs := []watchdog.Program{}
// Service
keybasePath, err := install.BinPath()
if err != nil {
return err
}
serviceLogPath := filepath.Join(env.GetLogDir(), libkb.ServiceLogFileName)
serviceProgram := watchdog.Program{
Path: keybasePath,
Args: []string{
"-d",
"--log-file=" + serviceLogPath,
"service",
"--watchdog-forked",
},
ExitOn: watchdog.ExitOnSuccess,
}
programs = append(programs, serviceProgram)
// KBFS
kbfsPath, err := install.KBFSBinPath(runMode, "")
if err != nil {
return err
}
var mountDirArg string
if runtime.GOOS == "windows" {
mountDirArg = "-mount-from-service"
} else {
mountDirArg, err = env.GetMountDir()
if err != nil {
return err
}
}
kbfsProgram := watchdog.Program{
Path: kbfsPath,
Args: []string{
"-debug",
"-log-to-file",
mountDirArg,
},
ExitOn: watchdog.ExitOnSuccess,
}
programs = append(programs, kbfsProgram)
// Updater
if !excludeUpdater {
updaterPath, err := install.UpdaterBinPath()
if err != nil {
return err
}
updaterProgram := watchdog.Program{
Path: updaterPath,
Args: []string{
"-log-to-file",
"-path-to-keybase=" + keybasePath,
},
}
programs = append(programs, updaterProgram)
}
// Start and monitor all the programs
if err := watchdog.Watch(programs, 10*time.Second, c.G().GetLogf()); err != nil {
return err
}
// Wait forever (watchdog watches programs in separate goroutines)
select {}
}
// NewCmdWatchdog2 constructs watchdog command
func NewCmdWatchdog2(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "watchdog2",
Usage: "Start and monitor background services",
Action: func(c *cli.Context) {
cl.ChooseCommand(&CmdWatchdog2{Contextified: libkb.NewContextified(g)}, "watchdog2", c)
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
},
}
}
// GetUsage returns library usage for this command
func (c *CmdWatchdog2) GetUsage() libkb.Usage {
return libkb.Usage{}
}
// Debugf (for watchdog.Log interface)
func (c *CmdWatchdog2) Debugf(s string, args ...interface{}) {
c.G().Log.Debug(s, args...)
}
// Infof (for watchdog.Log interface)
func (c *CmdWatchdog2) Infof(s string, args ...interface{}) {
c.G().Log.Info(s, args...)
}
// Warningf (for watchdog Log interface)
func (c *CmdWatchdog2) Warningf(s string, args ...interface{}) {
c.G().Log.Warning(s, args...)
}
// Errorf (for watchdog Log interface)
func (c *CmdWatchdog2) Errorf(s string, args ...interface{}) {
c.G().Log.Errorf(s, args...)
}