-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
219 lines (173 loc) · 4.56 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"archive/tar"
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"runtime/debug"
"time"
"github.com/ahmetalpbalkan/dlog"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
log "github.com/sirupsen/logrus"
)
func checkIfError(err error) {
if err == nil {
return
}
log.Error(err)
debug.PrintStack()
os.Exit(1)
}
func execInContainer(ctx context.Context, cli *client.Client, id string, commands []string) (output string, err error) {
execID, err := cli.ContainerExecCreate(ctx, id, types.ExecConfig{
AttachStdout: true,
AttachStderr: true,
Cmd: commands,
Privileged: true,
})
if err != nil {
return "", err
}
res, err := cli.ContainerExecAttach(ctx, execID.ID, types.ExecStartCheck{})
if err != nil {
return "", err
}
r := dlog.NewReader(res.Reader)
s := bufio.NewScanner(r)
for s.Scan() {
output += fmt.Sprintf(" > %s\n", s.Text())
}
return output, nil
}
// File holds the filename and its content
type File struct {
Name string
Content []byte
Mode int64
}
func generateArchive(files ...File) io.Reader {
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
for _, file := range files {
hdr := &tar.Header{
Name: file.Name,
Size: int64(len(file.Content)),
Mode: file.Mode,
}
err := tw.WriteHeader(hdr)
checkIfError(err)
_, err = tw.Write(file.Content)
checkIfError(err)
}
err := tw.Close()
checkIfError(err)
return buf
}
//go:embed bin/agent
var agent []byte
// OsData type describes Osspecific data requiredfor bootstrapping
type OsData struct {
Archive io.Reader
Command []string
}
func getOsData(containerOs string, cert []byte) (osData OsData, err error) {
certFile := File{
Name: "/.cert-bootstrapper/ssl/cert.pem",
Content: cert,
Mode: 444,
}
agentFileName := "/.cert-bootstrapper/bootstrap-agent"
if containerOs == "windows" {
agentFileName = fmt.Sprintf("%s.exe", agentFileName)
}
agentFile := File{
Name: agentFileName,
Content: agent,
Mode: 555,
}
osData = OsData{
Archive: generateArchive(agentFile, certFile),
Command: []string{agentFileName},
}
return osData, nil
}
func isHyperVContainer(ctx context.Context, cli *client.Client, id string) bool {
info, err := cli.ContainerInspect(ctx, id)
checkIfError(err)
return info.ContainerJSONBase.HostConfig.Isolation == "hyperv"
}
func getContainerOs(ctx context.Context, cli *client.Client, id string) string {
info, err := cli.ContainerInspect(ctx, id)
checkIfError(err)
return info.ContainerJSONBase.Platform
}
func bootstrap(ctx context.Context, cli *client.Client, id string, from string, cert []byte) {
log.WithFields(log.Fields{
"id": id,
"from": from,
}).Info("Bootstrapping container...")
hostname, err := os.Hostname()
checkIfError(err)
log.WithField("hostname", hostname).Info()
if id == hostname {
log.Info("This is me, don't want to touch myself. No action taken.")
return
}
if isHyperVContainer(ctx, cli, id) {
log.Warning("This container is running in Hyper-V isolation, which is not supported. No action taken.")
return
}
containerOs := getContainerOs(ctx, cli, id)
log.WithField("os", containerOs).Info()
if containerOs != runtime.GOOS {
log.Warningf("Container OS %s doesn't match host OS %s. No action taken.", containerOs, runtime.GOOS)
return
}
osData, err := getOsData(containerOs, cert)
checkIfError(err)
log.Info("Copying files into container...")
err = cli.CopyToContainer(ctx, id, "/", osData.Archive, types.CopyToContainerOptions{})
checkIfError(err)
log.Info("Running bootstrap script...")
output, err := execInContainer(ctx, cli, id, osData.Command)
checkIfError(err)
fmt.Println("==== Output bootstrap script ====")
fmt.Println(output)
fmt.Println("=================================")
log.Info("Done!")
fmt.Println("\n")
}
func main() {
log.SetFormatter(&log.TextFormatter{TimestampFormat: time.RFC3339Nano})
certFilename := "ssl/cert.pem"
if len(os.Args) >= 2 {
certFilename = os.Args[1]
}
log.WithField("certFilename", certFilename).Info("Reading certificate file...")
cert, err := ioutil.ReadFile(certFilename)
checkIfError(err)
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
checkIfError(err)
ctx := context.Background()
checkIfError(err)
msgs, errs := cli.Events(ctx, types.EventsOptions{})
log.Info("Listening for new containers...")
for {
select {
case err := <-errs:
log.Error(err)
os.Exit(1)
case msg := <-msgs:
if msg.Status == "start" {
bootstrap(ctx, cli, msg.ID[0:12], msg.From, cert)
}
}
}
}