-
Notifications
You must be signed in to change notification settings - Fork 3
/
localcontainer.go
224 lines (194 loc) · 4.2 KB
/
localcontainer.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"archive/tar"
"bytes"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
)
type LocalContainerCreator struct {
Root string
}
func NewLocalContainerCreator() *LocalContainerCreator {
r := &LocalContainerCreator{
Root: filepath.Join(os.TempDir(), "pixelpixel"),
}
return r
}
func (lcc *LocalContainerCreator) CreateContainer(fs *tar.Reader, envInjections []string) (Container, error) {
dir := filepath.Join(lcc.Root, GenerateAlnumString(32))
err := os.MkdirAll(dir, os.FileMode(0755))
if err != nil {
return nil, err
}
ctr := &localContainer{
Root: dir,
LogBuffer: &bytes.Buffer{},
Terminating: make(chan bool),
}
purge := true
defer func() {
if purge {
close(ctr.Terminating)
ctr.Terminated = true
os.RemoveAll(dir)
}
}()
err = ctr.extractFileSystem(fs)
if err != nil {
return nil, err
}
err = ctr.compile()
if err != nil {
return ctr, nil
}
purge = false
ctr.Cmd = exec.Command(filepath.Join(dir, "pixel"))
ctr.Cmd.Dir = dir
ctr.Cmd.Stdout = ctr.LogBuffer
ctr.Cmd.Stderr = ctr.LogBuffer
ctr.Port = <-portGenerator
ctr.Cmd.Env = stringList(os.Environ(), envInjections, fmt.Sprintf("PORT=%d", ctr.Port))
err = ctr.Cmd.Start()
if err != nil {
return ctr, err
}
go func() {
ctr.Cmd.Wait()
close(ctr.Terminating)
ctr.Terminated = true
}()
return ctr, nil
}
type localContainer struct {
Root string
Cmd *exec.Cmd
Terminating chan bool
LogBuffer *bytes.Buffer
Port int
Terminated bool
}
func (lc *localContainer) IsRunning() bool {
return !lc.Terminated
}
func (lc *localContainer) Address() net.Addr {
addr, _ := net.ResolveTCPAddr("tcp4", fmt.Sprintf("localhost:%d", lc.Port))
return addr
}
func (lc *localContainer) Logs() string {
return lc.LogBuffer.String()
}
func (lc *localContainer) SoftKill() {
if !lc.IsRunning() || lc.Cmd == nil || lc.Cmd.Process == nil {
return
}
lc.Cmd.Process.Signal(os.Interrupt)
}
func (lc *localContainer) HardKill() {
if !lc.IsRunning() || lc.Cmd == nil || lc.Cmd.Process == nil {
return
}
lc.Cmd.Process.Signal(os.Kill)
}
func (lc *localContainer) Wait() {
select {
case <-lc.Terminating:
}
}
func (lc *localContainer) Cleanup() {
lc.Wait()
os.RemoveAll(lc.Root)
}
func (lc *localContainer) compile() error {
cmd := exec.Command("go", "get", "-d")
cmd.Dir = lc.Root
cmd.Stdout = lc.LogBuffer
cmd.Stderr = lc.LogBuffer
if err := cmd.Run(); err != nil {
return err
}
files, err := filepath.Glob(filepath.Join(lc.Root, "*.go"))
if err != nil {
return err
}
// add extension .exe to compiled program in windows environment
filename := "pixel"
if runtime.GOOS == "windows" {
filename += ".exe"
}
cmd = exec.Command("go", stringList("build", "-o", filename, files)...)
cmd.Dir = lc.Root
cmd.Stdout = lc.LogBuffer
cmd.Stderr = lc.LogBuffer
return cmd.Run()
}
func (lc *localContainer) extractFileSystem(fs *tar.Reader) error {
hdr, err := fs.Next()
for err != io.EOF {
if err != nil {
return err
}
file := filepath.Join(lc.Root, hdr.Name)
switch hdr.Typeflag {
case tar.TypeDir:
err := os.MkdirAll(file, os.FileMode(0755))
if err != nil {
return err
}
case tar.TypeReg:
err := lc.writeFile(file, fs)
if err != nil {
return err
}
default:
log.Printf("Encountered unknown file type 0x%02x, skipping", hdr.Typeflag)
}
hdr, err = fs.Next()
}
return nil
}
func (lc *localContainer) writeFile(file string, ff io.Reader) error {
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, os.FileMode(0644))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, ff)
return err
}
// Stolen from $GOROOT/src/cmd/go/main.go
// stringList's arguments should be a sequence of string or []string values.
// stringList flattens them into a single []string.
func stringList(args ...interface{}) []string {
var x []string
for _, arg := range args {
switch arg := arg.(type) {
case []string:
x = append(x, arg...)
case string:
x = append(x, arg)
default:
panic("stringList: invalid argument")
}
}
return x
}
var (
portGenerator <-chan int
)
func init() {
c := make(chan int)
portGenerator = c
go func() {
for {
for i := 49000; i < 65535; i++ {
c <- i
}
}
}()
}