-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathboot.go
More file actions
204 lines (185 loc) · 4.23 KB
/
Copy pathboot.go
File metadata and controls
204 lines (185 loc) · 4.23 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strconv"
"strings"
"text/template"
"github.com/deis/minio/src/healthsrv"
"github.com/deis/pkg/aboutme"
"github.com/deis/pkg/utils"
minio "github.com/minio/minio-go"
)
const (
localMinioInsecure = true
defaultMinioHost = "localhost"
defaultMinioPort = "9000"
)
var (
errHealthSrvExited = errors.New("healthcheck server exited with unknown status")
errMinioExited = errors.New("Minio server exited with unknown status")
)
// Secret is a secret for the remote object storage
type Secret struct {
Host string
KeyID string
AccessKey string
Region string
}
const configdir = "/home/minio/.minio/"
const templv3 = `{
"version": "3",
"alias": {
"dl": "https://dl.minio.io",
"localhost": "http://localhost:9000",
"play": "https://play.minio.io:9000",
"s3": "https://s3.amazonaws.com"
},
"hosts": {
{{range .}}
"{{.Host}}": {
"access-key-id": "{{.KeyID}}" ,
"secret-access-key": "{{.AccessKey}}"
},
{{end}}
"127.0.0.1:*": {
"access-key-id": "",
"secret-access-key": ""
}
}
}
`
const templv2 = `{
"version": "2",
"credentials": {
{{range .}}
"accessKeyId": "{{.KeyID}}",
"secretAccessKey": "{{.AccessKey}}",
"region": "{{.Region}}"
{{end}}
},
"mongoLogger": {
"addr": "",
"db": "",
"collection": ""
},
"syslogLogger": {
"network": "",
"addr": ""
},
"fileLogger": {
"filename": ""
}
}`
func run(cmd string) error {
var cmdBuf bytes.Buffer
tmpl := template.Must(template.New("cmd").Parse(cmd))
if err := tmpl.Execute(&cmdBuf, nil); err != nil {
log.Fatal(err)
}
cmdString := cmdBuf.String()
fmt.Println(cmdString)
var cmdl *exec.Cmd
cmdl = exec.Command("sh", "-c", cmdString)
if _, _, err := utils.RunCommandWithStdoutStderr(cmdl); err != nil {
return err
}
return nil
}
func readSecrets() (string, string) {
keyID, err := ioutil.ReadFile("/var/run/secrets/deis/minio/user/accesskey")
checkError(err)
accessKey, err := ioutil.ReadFile("/var/run/secrets/deis/minio/user/secretkey")
checkError(err)
return strings.TrimSpace(string(keyID)), strings.TrimSpace(string(accessKey))
}
func newMinioClient(host, port, accessKey, accessSecret string, insecure bool) (minio.CloudStorageClient, error) {
return minio.New(
fmt.Sprintf("%s:%s", host, port),
accessKey,
accessSecret,
insecure,
)
}
func main() {
pod, err := aboutme.FromEnv()
checkError(err)
key, access := readSecrets()
minioHost := os.Getenv("MINIO_HOST")
if minioHost == "" {
minioHost = defaultMinioHost
}
minioPort := os.Getenv("MINIO_PORT")
if minioPort == "" {
minioPort = defaultMinioPort
}
minioClient, err := newMinioClient(minioHost, minioPort, key, access, localMinioInsecure)
if err != nil {
log.Printf("Error creating minio client (%s)", err)
os.Exit(1)
}
secrets := []Secret{
{
Host: pod.IP,
KeyID: key,
AccessKey: access,
Region: "us-east-1",
},
}
t := template.New("MinioTpl")
t, err = t.Parse(templv2)
checkError(err)
err = os.MkdirAll(configdir, 0755)
checkError(err)
output, err := os.Create(configdir + "config.json")
checkError(err)
err = t.Execute(output, secrets)
checkError(err)
os.Args[0] = "minio"
mc := strings.Join(os.Args, " ")
runErrCh := make(chan error)
log.Printf("starting Minio server")
go func() {
if err := run(mc); err != nil {
runErrCh <- err
} else {
runErrCh <- errMinioExited
}
}()
healthSrvHost := os.Getenv("HEALTH_SERVER_HOST")
if healthSrvHost == "" {
healthSrvHost = healthsrv.DefaultHost
}
healthSrvPort, err := strconv.Atoi(os.Getenv("HEALTH_SERVER_PORT"))
if err != nil {
healthSrvPort = healthsrv.DefaultPort
}
log.Printf("starting health check server on %s:%d", healthSrvHost, healthSrvPort)
healthSrvErrCh := make(chan error)
go func() {
if err := healthsrv.Start(healthSrvHost, healthSrvPort, minioClient); err != nil {
healthSrvErrCh <- err
} else {
healthSrvErrCh <- errHealthSrvExited
}
}()
select {
case err := <-runErrCh:
log.Printf("Minio server error (%s)", err)
os.Exit(1)
case err := <-healthSrvErrCh:
log.Printf("Healthcheck server error (%s)", err)
os.Exit(1)
}
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}