Repository navigation
Expand file tree
/
Copy pathcommand.go
More file actions
187 lines (168 loc) · 5.22 KB
/
Copy pathcommand.go
File metadata and controls
187 lines (168 loc) · 5.22 KB
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
package command
import (
"context"
"fmt"
"io"
"os"
"github.com/go-zoox/command/agent/client"
"github.com/go-zoox/command/config"
"github.com/go-zoox/command/engine"
"github.com/go-zoox/command/engine/host"
"github.com/go-zoox/command/terminal"
"github.com/go-zoox/uuid"
)
// Command is the command runner interface
type Command interface {
Start() error
Wait() error
Cancel() error
//
Run() error
//
Output() ([]byte, error)
//
SetStdin(stdin io.Reader) error
SetStdout(stdout io.Writer) error
SetStderr(stderr io.Writer) error
//
Terminal() (terminal.Terminal, error)
}
// Config is the command runner config
type Config = config.Config
// New creates a new command runner.
func New(cfg *Config) (cmd Command, err error) {
if cfg.Context == nil {
cfg.Context = context.Background()
}
// If sandbox mode is enabled, force docker engine and apply security settings
if cfg.Sandbox {
if cfg.Engine != "" && cfg.Engine != "docker" {
return nil, fmt.Errorf("sandbox mode requires docker engine, but got: %s", cfg.Engine)
}
cfg.Engine = "docker"
// Apply default sandbox security settings if not explicitly set
if !cfg.DisableNetwork && cfg.Network == "" {
// Default to no network in sandbox mode for security
cfg.DisableNetwork = true
}
if cfg.Privileged {
// Force non-privileged in sandbox mode
cfg.Privileged = false
}
// Set default resource limits if not set
if cfg.Memory == 0 {
cfg.Memory = 512 // Default 512MB memory limit
}
if cfg.CPU == 0 {
cfg.CPU = 1.0 // Default 1 CPU core limit
}
}
// Set default engine if not set and sandbox is not enabled
if cfg.Engine == "" {
cfg.Engine = host.Name
}
if cfg.Shell == "" {
cfg.Shell = "/bin/sh"
}
if cfg.ID == "" {
cfg.ID = fmt.Sprintf("go-zoox_command_%s", uuid.V4())
}
environment := map[string]string{
"GO_ZOOX_COMMAND_ENGINE": cfg.Engine,
"GO_ZOOX_COMMAND_ID": cfg.ID,
"GO_ZOOX_COMMAND_SHELL": cfg.Shell,
"GO_ZOOX_COMMAND_USER": cfg.User,
"GO_ZOOX_COMMAND_WORKDIR": cfg.WorkDir,
"GO_ZOOX_COMMAND_COMMAND": cfg.Command,
"GO_ZOOX_COMMAND_IMAGE": cfg.Image,
"GO_ZOOX_COMMAND_MEMORY": fmt.Sprintf("%d", cfg.Memory),
"GO_ZOOX_COMMAND_CPU": fmt.Sprintf("%f", cfg.CPU),
"GO_ZOOX_COMMAND_PLATFORM": cfg.Platform,
"GO_ZOOX_COMMAND_NETWORK": cfg.Network,
"GO_ZOOX_COMMAND_DISABLE_NETWORK": fmt.Sprintf("%t", cfg.DisableNetwork),
//
"HOME": os.Getenv("HOME"),
"USER": os.Getenv("USER"),
"PATH": os.Getenv("PATH"),
}
for k, v := range cfg.Environment {
environment[k] = v
}
cfg.Environment = environment
// support agent
if cfg.Agent != "" {
agent, err := client.New(func(opt *client.Option) {
opt.Server = cfg.Agent
})
if err != nil {
return nil, err
}
if err := agent.Connect(); err != nil {
return nil, err
}
err = agent.New(&config.Config{
// Context: cfg.Context,
Timeout: cfg.Timeout,
Engine: cfg.Engine,
Sandbox: cfg.Sandbox,
Command: cfg.Command,
WorkDir: cfg.WorkDir,
Environment: environment,
User: cfg.User,
Shell: cfg.Shell,
ReadOnly: cfg.ReadOnly,
IsHistoryDisabled: cfg.IsHistoryDisabled,
Image: cfg.Image,
Memory: cfg.Memory,
CPU: cfg.CPU,
Platform: cfg.Platform,
Network: cfg.Network,
DisableNetwork: cfg.DisableNetwork,
Privileged: cfg.Privileged,
DockerHost: cfg.DockerHost,
Server: cfg.Server,
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
SSHHost: cfg.SSHHost,
SSHPort: cfg.SSHPort,
SSHUser: cfg.SSHUser,
SSHPass: cfg.SSHPass,
SSHPrivateKey: cfg.SSHPrivateKey,
SSHPrivateKeySecret: cfg.SSHPrivateKeySecret,
SSHIsIgnoreStrictHostKeyChecking: cfg.SSHIsIgnoreStrictHostKeyChecking,
SSHKnowHostsFilePath: cfg.SSHKnowHostsFilePath,
K8sKubeconfig: cfg.K8sKubeconfig,
K8sNamespace: cfg.K8sNamespace,
K8sImage: cfg.K8sImage,
K8sPodTimeoutSeconds: cfg.K8sPodTimeoutSeconds,
PodmanHost: cfg.PodmanHost,
ID: cfg.ID,
})
if err != nil {
return nil, err
}
return agent, nil
}
var eg engine.Engine
if createEngine, err := engine.Get(cfg.Engine); err != nil {
return nil, fmt.Errorf("unsupported command engine: %s", cfg.Engine)
} else {
eg, err = createEngine(cfg)
if err != nil {
return nil, err
}
}
go func() {
<-cfg.Context.Done()
eg.Cancel()
}()
return &command{
cfg: cfg,
engine: eg,
}, nil
}
type command struct {
cfg *Config
//
engine engine.Engine
}