/
terminal.go
193 lines (155 loc) · 4.64 KB
/
terminal.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
package executor
import (
"archive/tar"
"bytes"
"context"
"database/sql"
"fmt"
"io"
"path/filepath"
"github.com/docker/docker/api/types"
"github.com/semanser/ai-coder/database"
gmodel "github.com/semanser/ai-coder/graph/model"
"github.com/semanser/ai-coder/graph/subscriptions"
"github.com/semanser/ai-coder/websocket"
)
func ExecCommand(flowID int64, command string, db *database.Queries) (result string, err error) {
container := TerminalName(flowID)
// Create options for starting the exec process
cmd := []string{
"sh",
"-c",
command,
}
// Check if container is running
isRunning, err := IsContainerRunning(container)
if err != nil {
return "", fmt.Errorf("Error inspecting container: %w", err)
}
if !isRunning {
return "", fmt.Errorf("Container is not running")
}
// TODO avoid duplicating here and in the flows table
log, err := db.CreateLog(context.Background(), database.CreateLogParams{
FlowID: sql.NullInt64{Int64: flowID, Valid: true},
Message: command,
Type: "input",
})
if err != nil {
return "", fmt.Errorf("Error creating log: %w", err)
}
subscriptions.BroadcastTerminalLogsAdded(flowID, &gmodel.Log{
ID: uint(log.ID),
Text: websocket.FormatTerminalInput(command),
})
createResp, err := dockerClient.ContainerExecCreate(context.Background(), container, types.ExecConfig{
Cmd: cmd,
AttachStdout: true,
AttachStderr: true,
Tty: true,
})
if err != nil {
return "", fmt.Errorf("Error creating exec process: %w", err)
}
// Attach to the exec process
resp, err := dockerClient.ContainerExecAttach(context.Background(), createResp.ID, types.ExecStartCheck{
Tty: true,
})
if err != nil {
return "", fmt.Errorf("Error attaching to exec process: %w", err)
}
defer resp.Close()
dst := bytes.Buffer{}
_, err = io.Copy(&dst, resp.Reader)
if err != nil && err != io.EOF {
return "", fmt.Errorf("Error copying output: %w", err)
}
// Wait for the exec process to finish
_, err = dockerClient.ContainerExecInspect(context.Background(), createResp.ID)
if err != nil {
return "", fmt.Errorf("Error inspecting exec process: %w", err)
}
results := dst.String()
// TODO avoid duplicating here and in the flows table
log, err = db.CreateLog(context.Background(), database.CreateLogParams{
FlowID: sql.NullInt64{Int64: flowID, Valid: true},
Message: results,
Type: "output",
})
if err != nil {
return "", fmt.Errorf("Error creating log: %w", err)
}
subscriptions.BroadcastTerminalLogsAdded(flowID, &gmodel.Log{
ID: uint(log.ID),
Text: results,
})
result = dst.String()
if result == "" {
result = "Command executed successfully"
}
return result, nil
}
func WriteFile(flowID int64, content string, path string, db *database.Queries) (err error) {
container := TerminalName(flowID)
// Check if container is running
isRunning, err := IsContainerRunning(container)
if err != nil {
return fmt.Errorf("Error inspecting container: %w", err)
}
if !isRunning {
return fmt.Errorf("Container is not running")
}
// TODO avoid duplicating here and in the flows table
log, err := db.CreateLog(context.Background(), database.CreateLogParams{
FlowID: sql.NullInt64{Int64: flowID, Valid: true},
Message: content,
Type: "input",
})
if err != nil {
return fmt.Errorf("Error creating log: %w", err)
}
subscriptions.BroadcastTerminalLogsAdded(flowID, &gmodel.Log{
ID: uint(log.ID),
Text: websocket.FormatTerminalInput(content),
})
// Put content into a tar archive
archive := &bytes.Buffer{}
tarWriter := tar.NewWriter(archive)
filename := filepath.Base(path)
tarHeader := &tar.Header{
Name: filename,
Mode: 0600,
Size: int64(len(content)),
}
err = tarWriter.WriteHeader(tarHeader)
if err != nil {
return fmt.Errorf("Error writing tar header: %w", err)
}
_, err = tarWriter.Write([]byte(content))
if err != nil {
return fmt.Errorf("Error writing tar content: %w", err)
}
dir := filepath.Dir(path)
err = dockerClient.CopyToContainer(context.Background(), container, dir, archive, types.CopyToContainerOptions{})
if err != nil {
return fmt.Errorf("Error writing file: %w", err)
}
message := fmt.Sprintf("Wrote to %s", path)
// TODO avoid duplicating here and in the flows table
log, err = db.CreateLog(context.Background(), database.CreateLogParams{
FlowID: sql.NullInt64{Int64: flowID, Valid: true},
Message: message,
Type: "output",
})
if err != nil {
return fmt.Errorf("Error creating log: %w", err)
}
subscriptions.BroadcastTerminalLogsAdded(flowID, &gmodel.Log{
ID: uint(log.ID),
Text: message,
})
return nil
}
func TerminalName(flowID int64) string {
return fmt.Sprintf("codel-terminal-%d", flowID)
}