-
-
Notifications
You must be signed in to change notification settings - Fork 684
/
handler.go
119 lines (99 loc) · 3.27 KB
/
handler.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
package debug
import (
log "github.com/sirupsen/logrus"
"github.com/slimtoolkit/slim/pkg/app"
"github.com/slimtoolkit/slim/pkg/app/master/command"
//"github.com/slimtoolkit/slim/pkg/app/master/container"
//"github.com/slimtoolkit/slim/pkg/app/master/inspectors/image"
"github.com/slimtoolkit/slim/pkg/app/master/version"
cmd "github.com/slimtoolkit/slim/pkg/command"
"github.com/slimtoolkit/slim/pkg/docker/dockerclient"
"github.com/slimtoolkit/slim/pkg/report"
//"github.com/slimtoolkit/slim/pkg/util/errutil"
"github.com/slimtoolkit/slim/pkg/util/fsutil"
v "github.com/slimtoolkit/slim/pkg/version"
)
const appName = command.AppName
type ovars = app.OutVars
// OnCommand implements the 'debug' command
func OnCommand(
xc *app.ExecutionContext,
gparams *command.GenericParams,
commandParams *CommandParams) {
logger := log.WithFields(log.Fields{"app": appName, "cmd": Name})
viChan := version.CheckAsync(gparams.CheckVersion, gparams.InContainer, gparams.IsDSImage)
cmdReport := report.NewDebugCommand(gparams.ReportLocation, gparams.InContainer)
cmdReport.State = cmd.StateStarted
xc.Out.State("started")
paramVars := ovars{
"runtime": commandParams.Runtime,
"target": commandParams.TargetRef,
"debug-image": commandParams.DebugContainerImage,
"entrypoint": commandParams.Entrypoint,
"cmd": commandParams.Cmd,
"terminal": commandParams.DoTerminal,
}
if commandParams.Runtime == KubernetesRuntime {
paramVars["namespace"] = commandParams.TargetNamespace
paramVars["pod"] = commandParams.TargetPod
}
xc.Out.Info("params", paramVars)
sid := generateSessionID()
debugContainerName := generateContainerName(sid)
logger = logger.WithFields(
log.Fields{
"sid": sid,
"debug.container.name": debugContainerName,
})
switch commandParams.Runtime {
case DockerRuntime:
client, err := dockerclient.New(gparams.ClientConfig)
if err == dockerclient.ErrNoDockerInfo {
exitMsg := "missing Docker connection info"
if gparams.InContainer && gparams.IsDSImage {
exitMsg = "make sure to pass the Docker connect parameters to the slim app container"
}
xc.Out.Info("docker.connect.error",
ovars{
"message": exitMsg,
})
exitCode := command.ECTCommon | command.ECCNoDockerConnectInfo
xc.Out.State("exited",
ovars{
"exit.code": exitCode,
"version": v.Current(),
"location": fsutil.ExeDir(),
})
xc.Exit(exitCode)
}
xc.FailOn(err)
if gparams.Debug {
version.Print(xc, Name, logger, client, false, gparams.InContainer, gparams.IsDSImage)
}
HandleDockerRuntime(logger, xc, gparams, commandParams, client, sid, debugContainerName)
case KubernetesRuntime:
if gparams.Debug {
version.Print(xc, Name, logger, nil, false, gparams.InContainer, gparams.IsDSImage)
}
HandleKubernetesRuntime(logger, xc, gparams, commandParams, sid, debugContainerName)
default:
xc.Out.Error("runtime", "unsupported runtime")
xc.Out.State("exited",
ovars{
"exit.code": -1,
})
xc.Exit(-1)
}
xc.Out.State("completed")
cmdReport.State = cmd.StateCompleted
xc.Out.State("done")
vinfo := <-viChan
version.PrintCheckVersion(xc, "", vinfo)
cmdReport.State = cmd.StateDone
if cmdReport.Save() {
xc.Out.Info("report",
ovars{
"file": cmdReport.ReportLocation(),
})
}
}