-
Notifications
You must be signed in to change notification settings - Fork 111
/
hab_cmd.go
281 lines (237 loc) · 8.92 KB
/
hab_cmd.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package target
import (
"context"
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/chef/automate/components/automate-deployment/pkg/habpkg"
"github.com/chef/automate/lib/platform/command"
)
var stdHabOptions = []command.Opt{
// Don't emit ANSI color escape codes
command.Envvar("HAB_NOCOLORING", "true"),
// Don't use progress bars in output
command.Envvar("HAB_NONINTERACTIVE", "true"),
// Don't prompt for license acceptance
command.Envvar("HAB_LICENSE", "accept-no-persist"),
}
const (
// HabTimeoutInstallPackage is the timeout for InstallPackage
// commands. Since package installs also install dependencies,
// a given package installation can often take considerable
// time.
HabTimeoutInstallPackage = 1 * time.Hour
// HabTimeoutIsInstalled is the timeout for
// IsInstalled. IsInstalled runs hab pkg path which we expect
// to be very fast typically.
HabTimeoutIsInstalled = 1 * time.Minute
// HabTimeoutDefault is the timeout for hab commands that
// don't have other timeouts.
HabTimeoutDefault = 5 * time.Minute
)
// A HabCmd runs the `hab` command-line tool with a standard set of
// options.
type HabCmd interface {
// InstallPackage installs an Installable habitat package
// (a hartifact or a package from the Depot)
InstallPackage(context.Context, habpkg.Installable, string) (string, error)
// IsInstalled returns true if the specified package is
// installed and false otherwise. An error is returned when
// the underlying habitat commands have failed.
IsInstalled(context.Context, habpkg.VersionedPackage) (bool, error)
// BinlinkPackage binlinks a binary in the given Habitat
// package. An error is returned if the underlying hab command
// failed.
BinlinkPackage(context.Context, habpkg.VersionedPackage, string) (string, error)
// LoadService loads the given habpkg.VersionedPackage as a service
// with the provided options.
LoadService(context.Context, habpkg.VersionedPackage, ...LoadOption) (string, error)
// UnloadService unloads a given habpkg.VersionedPackage
UnloadService(context.Context, habpkg.VersionedPackage) (string, error)
// StartService starts an already-loaded service identified by
// the given habpkg.VersionedPackage.
StartService(context.Context, habpkg.VersionedPackage) (string, error)
// StopService stops an already-loaded service identified by
// the given habpkg.VersionedPackage.
StopService(context.Context, habpkg.VersionedPackage) (string, error)
// Terminate supervisor, does not block
SupTerm(context.Context) error
}
type LoadOption func([]string) []string
// Binds is a LoadOption that applies the passed bind to the service's
// load command line arguments.
func Binds(binds []string) LoadOption {
return func(args []string) []string {
if binds != nil {
for _, b := range binds {
args = append(args, "--bind", b)
}
}
return args
}
}
// BindMode is a LoadOption that applies the passed binding mode to
// the service's load command line arguments.
func BindMode(mode string) LoadOption {
return func(args []string) []string {
if mode != "" {
return append(args, "--binding-mode", mode)
}
return args
}
}
type habCmd struct {
offlineMode bool
executor command.Executor
}
// NewHabCmd returns an habCmd that uses the given
// command.Executor. If offlineMode is true then any InstallPackage()
// calls will use Habitat's OFFLINE_INSTALL feature.
func NewHabCmd(c command.Executor, offlineMode bool) HabCmd {
return &habCmd{
executor: c,
offlineMode: offlineMode,
}
}
func devDebugOutputOnTimeout(ctx context.Context) {
if os.Getenv("CHEF_DEV_ENVIRONMENT") != "true" {
return
}
go func() {
<-ctx.Done()
// If Done is not yet closed, Err returns nil.
// If Done is closed, Err returns a non-nil error explaining why:
// Canceled if the context was canceled
// or DeadlineExceeded if the context's deadline passed.
// After Err returns a non-nil error, successive calls to Err return the same error.
err := ctx.Err()
switch err {
case context.Canceled:
return // this is the happy case
case context.DeadlineExceeded:
devPrintHungHabDebug()
case nil:
return // should not happen
default:
logrus.Errorf("Unknown error return from ctx.Err(): %s", err.Error())
}
}()
}
func devPrintHungHabDebug() {
logrus.Error("DEVELOPMENT INFO: HABITAT COMMAND TIMEOUT")
logrus.Error("DEVELOPMENT INFO: ps auxww")
err := command.Run("ps",
command.Args("auxww"),
command.Stdout(os.Stdout),
command.Stderr(os.Stderr))
if err != nil {
logrus.WithError(err).Error("failed to run ps during devPrintHungHabDebug")
}
logrus.Error("DEVELOPMENT INFO: free -m")
err = command.Run("free",
command.Args("-m"),
command.Stdout(os.Stdout),
command.Stderr(os.Stderr))
if err != nil {
logrus.WithError(err).Error("failed to run free during devPrintHungHabDebug")
}
}
// Install installs the given Installable. If the install fails an
// error is returned.
//
// TODO(ssd) 2018-07-16: Can we rip channel out of here? I don't
// think anything really uses channel anymore.
func (c *habCmd) InstallPackage(ctx context.Context, pkg habpkg.Installable, channel string) (string, error) {
args := []string{"pkg", "install", pkg.InstallIdent()}
opts := standardHabOptions()
ctx, cancel := context.WithTimeout(ctx, HabTimeoutInstallPackage)
defer cancel()
devDebugOutputOnTimeout(ctx)
if c.offlineMode {
args = append(args, "--offline")
opts = append(opts, command.Envvar("HAB_FEAT_OFFLINE_INSTALL", "true"))
}
if channel != "" {
args = append(args, "--channel", channel)
}
opts = append(opts, command.Context(ctx), command.Args(args...))
return c.executor.CombinedOutput("hab", opts...)
}
// IsInstalled checks if a package is already installed
func (c *habCmd) IsInstalled(ctx context.Context, pkg habpkg.VersionedPackage) (bool, error) {
args := command.Args("pkg", "path", habpkg.Ident(pkg))
ctx, cancel := context.WithTimeout(ctx, HabTimeoutIsInstalled)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), args)
err := c.executor.Run("hab", cmdOpts...)
if err != nil {
return false, nil
}
return true, nil
}
// BinlinkPackage binlinks an executable from a Habitat package
func (c *habCmd) BinlinkPackage(ctx context.Context, pkg habpkg.VersionedPackage, exe string) (string, error) {
args := command.Args("pkg", "binlink", "--force", habpkg.Ident(pkg), exe)
ctx, cancel := context.WithTimeout(ctx, HabTimeoutDefault)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), args)
return c.executor.CombinedOutput("hab", cmdOpts...)
}
func (c *habCmd) LoadService(ctx context.Context, svc habpkg.VersionedPackage, opts ...LoadOption) (string, error) {
args := []string{"svc", "load", "--force", habpkg.Ident(svc), "--strategy", "none"}
for _, o := range opts {
args = o(args)
}
ctx, cancel := context.WithTimeout(ctx, HabTimeoutDefault)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), command.Args(args...))
return c.executor.CombinedOutput("hab", cmdOpts...)
}
func (c *habCmd) UnloadService(ctx context.Context, svc habpkg.VersionedPackage) (string, error) {
args := command.Args("svc", "unload", habpkg.ShortIdent(svc))
ctx, cancel := context.WithTimeout(ctx, HabTimeoutDefault)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), args)
output, err := c.executor.CombinedOutput("hab", cmdOpts...)
// NOTE: hab <= 0.79 returned 0 if the service was already unloaded.
// Beginning with 0.80, hab will exit 1 if the service was already unloaded.
// As we need to support mixed versions because of upgrades we'll do our
// best to handle backwards compatibility of this interface.
if err != nil {
if strings.Contains(output, "not loaded") {
return output, nil
}
}
return output, err
}
func (c *habCmd) StartService(ctx context.Context, svc habpkg.VersionedPackage) (string, error) {
args := command.Args("svc", "start", habpkg.ShortIdent(svc))
ctx, cancel := context.WithTimeout(ctx, HabTimeoutDefault)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), args)
return c.executor.CombinedOutput("hab", cmdOpts...)
}
func (c *habCmd) StopService(ctx context.Context, svc habpkg.VersionedPackage) (string, error) {
args := command.Args("svc", "stop", habpkg.ShortIdent(svc))
ctx, cancel := context.WithTimeout(ctx, HabTimeoutDefault)
defer cancel()
devDebugOutputOnTimeout(ctx)
cmdOpts := append(standardHabOptions(), command.Context(ctx), args)
return c.executor.CombinedOutput("hab", cmdOpts...)
}
func (c *habCmd) SupTerm(ctx context.Context) error {
cmdOpts := append(standardHabOptions(), command.Context(ctx), command.Args("sup", "term"))
_, err := c.executor.Start("hab", cmdOpts...)
return err
}
func standardHabOptions() []command.Opt {
cmdOpts := make([]command.Opt, len(stdHabOptions))
copy(cmdOpts, stdHabOptions)
return cmdOpts
}