-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (167 loc) · 4.19 KB
/
main.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
package main
import (
"context"
"fmt"
"github.com/creack/pty"
"golang.org/x/crypto/ssh/terminal"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/x-qdo/qudosh/packages/localcommand"
"github.com/x-qdo/qudosh/packages/tty"
)
func main() {
os.Exit(process())
}
func process() int {
var err error
// os.Args
shell := os.Getenv("QUDOSH_SHELL")
if shell == "" {
shellPath := os.Getenv("QUDOSH_SHELL_PATH")
if shellPath == "" {
fmt.Println("QUDOSH_SHELL_PATH environment variable is not set")
return 1
}
shell = filepath.Join(shellPath, filepath.Base(os.Args[0]))
}
arguments := os.Args[1:]
ctx, cancel := context.WithCancel(context.Background())
options := localcommand.Options{CloseSignal: 1}
factory, err := localcommand.NewFactory(shell, arguments, &options)
if err != nil {
cancel()
return exit(err, 3)
}
slave, err := factory.New(nil)
defer slave.Close()
// We need to make sure that we will read each symbol separately
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
cancel()
return exit(err, 1)
}
defer terminal.Restore(int(os.Stdin.Fd()), oldState)
timeNow := time.Now().Format("2006_02_01_15_04_05")
fileName := fmt.Sprintf("lab/session_%s.ttyrec", timeNow)
storePrefix := os.Getenv("LOCAL_PREFIX")
if storePrefix == "" {
storePrefix = "."
}
proxyTTY, err := tty.New(
os.Stdin,
os.Stdout,
slave,
tty.WithPermitWrite(),
tty.WithTtyRecording(ctx, storePrefix, fileName, saveFileHandler()),
)
sigwinch := make(chan os.Signal, 1)
signal.Notify(sigwinch, syscall.SIGWINCH)
go func() {
// Send initial resize at start
_ = resizeBasedOnCurrentShell(os.Stdin, proxyTTY.ResizeEvents)
for {
select {
case <-sigwinch:
_ = resizeBasedOnCurrentShell(os.Stdin, proxyTTY.ResizeEvents)
}
}
}()
errs := make(chan error, 1)
go func() {
errs <- proxyTTY.Run(ctx)
}()
err = waitSignals(errs, cancel)
if err != nil && err != context.Canceled {
return exit(err, 8)
}
return 0
}
func resizeBasedOnCurrentShell(stdin *os.File, resizeEvents chan *tty.ArgResizeTerminal) error {
rows, cols, err := pty.Getsize(stdin)
if err != nil {
return err
}
initialSize := tty.ArgResizeTerminal{Columns: cols, Rows: rows}
resizeEvents <- &initialSize
return nil
}
func saveFileHandler() tty.Hook {
return func(r *tty.Recorder) error {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
save := func(postfix string) error {
s3FileName := fmt.Sprintf("%s/%s%s", os.Getenv("S3_PREFIX"), r.FileName, postfix)
fmt.Printf("Uploading to s3: %s\r\n", s3FileName)
fileName := fmt.Sprintf("%s/%s%s", r.FilePrefix, r.FileName, postfix)
file, err := os.Open(fileName)
if err != nil {
return err
}
defer file.Close()
uploader := s3manager.NewUploaderWithClient(s3.New(sess, aws.NewConfig()))
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(os.Getenv("S3_BUCKET")),
ACL: aws.String("private"),
Key: aws.String(s3FileName),
ServerSideEncryption: aws.String("AES256"),
Body: file,
})
return err
}
err := save("")
if err != nil {
fmt.Printf("ERROR: Uploading ttyrec failed\r\n")
return err
}
err = save(".csv")
if err != nil {
fmt.Printf("ERROR: Uploading csv failed\r\n")
return err
}
return nil
}
}
func exit(err error, code int) int {
if err != nil {
fmt.Printf("Error: %s", err)
}
return code
}
func waitSignals(errs chan error, cancel context.CancelFunc) error {
sigChan := make(chan os.Signal, 1)
signal.Notify(
sigChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
)
select {
case err := <-errs:
return err
case s := <-sigChan:
switch s {
case syscall.SIGINT:
// gracefulCancel()
fmt.Println("C-C to force close")
select {
case err := <-errs:
return err
case <-sigChan:
fmt.Println("Force closing...")
cancel()
return <-errs
}
default:
cancel()
return <-errs
}
}
}