/
starlark_exec.go
144 lines (124 loc) · 4.7 KB
/
starlark_exec.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
// Copyright (c) 2020 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package starlark
import (
"context"
"errors"
"fmt"
"io"
"github.com/sirupsen/logrus"
"github.com/vmware-tanzu/crash-diagnostics/ssh"
"go.starlark.net/starlark"
)
type Executor struct {
thread *starlark.Thread
predecs starlark.StringDict
result starlark.StringDict
}
func New() *Executor {
return &Executor{
thread: &starlark.Thread{Name: "crashd"},
predecs: newPredeclareds(),
}
}
// AddPredeclared predeclared
func (e *Executor) AddPredeclared(name string, value starlark.Value) {
if e.predecs != nil {
e.predecs[name] = value
}
}
// Preload loads parse and load code modules to be used in other scripts.
// This call should take place prior to calling the Exec call for main script execution.
// The result of the loaded module are added to the global predeclared
// values used in Exec call.
func (e *Executor) Preload(name string, source io.Reader) error {
result, err := starlark.ExecFile(e.thread, name, source, e.predecs)
if err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
return fmt.Errorf(evalErr.Backtrace())
}
return err
}
// add result to predeclared
for k, v := range result {
e.predecs[k] = v
}
return nil
}
func (e *Executor) Exec(name string, source io.Reader) error {
if err := setupLocalDefaults(e.thread); err != nil {
return fmt.Errorf("failed to setup defaults: %s", err)
}
e.thread.SetLocal(identifiers.scriptName, name)
result, err := starlark.ExecFile(e.thread, name, source, e.predecs)
if err != nil {
if evalErr, ok := err.(*starlark.EvalError); ok {
return fmt.Errorf(evalErr.Backtrace())
}
return err
}
e.result = result
// fetch and stop the instance of ssh-agent, if any
if agentVal := e.thread.Local(identifiers.sshAgent); agentVal != nil {
logrus.Debug("stopping ssh-agent")
agent, ok := agentVal.(ssh.Agent)
if !ok {
logrus.Warn("error fetching ssh-agent")
} else {
if e := agent.Stop(); e != nil {
logrus.Warnf("failed to stop ssh-agent: %v", e)
}
}
}
return nil
}
// setupLocalDefaults populates the provided execution thread
// with default configuration values.
func setupLocalDefaults(thread *starlark.Thread) error {
if thread == nil {
return errors.New("thread local is nil")
}
// add script context starlark thread
ctx := context.Background()
thread.SetLocal(identifiers.scriptCtx, ctx)
// add default logger
addDefaultLogger(thread)
if err := addDefaultCrashdConf(thread); err != nil {
return err
}
if err := addDefaultSSHConf(thread); err != nil {
return err
}
if err := addDefaultKubeConf(thread); err != nil {
return err
}
return nil
}
// newPredeclareds creates string dictionary containing the
// global built-ins values and functions available to the
// running script.
func newPredeclareds() starlark.StringDict {
return starlark.StringDict{
identifiers.os: setupOSStruct(),
identifiers.crashdCfg: starlark.NewBuiltin(identifiers.crashdCfg, crashdConfigFn),
identifiers.sshCfg: starlark.NewBuiltin(identifiers.sshCfg, SshConfigFn),
identifiers.hostListProvider: starlark.NewBuiltin(identifiers.hostListProvider, hostListProvider),
identifiers.resources: starlark.NewBuiltin(identifiers.resources, resourcesFunc),
identifiers.archive: starlark.NewBuiltin(identifiers.archive, archiveFunc),
identifiers.run: starlark.NewBuiltin(identifiers.run, runFunc),
identifiers.runLocal: starlark.NewBuiltin(identifiers.runLocal, runLocalFunc),
identifiers.progAvailLocal: starlark.NewBuiltin(identifiers.progAvailLocal, progAvailLocalFunc),
identifiers.capture: starlark.NewBuiltin(identifiers.capture, captureFunc),
identifiers.captureLocal: starlark.NewBuiltin(identifiers.capture, captureLocalFunc),
identifiers.copyFrom: starlark.NewBuiltin(identifiers.copyFrom, copyFromFunc),
identifiers.copyTo: starlark.NewBuiltin(identifiers.copyTo, copyToFunc),
identifiers.kubeCfg: starlark.NewBuiltin(identifiers.kubeCfg, KubeConfigFn),
identifiers.kubeCapture: starlark.NewBuiltin(identifiers.kubeGet, KubeCaptureFn),
identifiers.kubeGet: starlark.NewBuiltin(identifiers.kubeGet, KubeGetFn),
identifiers.kubeNodesProvider: starlark.NewBuiltin(identifiers.kubeNodesProvider, KubeNodesProviderFn),
identifiers.capvProvider: starlark.NewBuiltin(identifiers.capvProvider, CapvProviderFn),
identifiers.capaProvider: starlark.NewBuiltin(identifiers.capaProvider, CapaProviderFn),
identifiers.setDefaults: starlark.NewBuiltin(identifiers.setDefaults, SetDefaultsFunc),
identifiers.log: starlark.NewBuiltin(identifiers.log, logFunc),
}
}