-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathboot.go
178 lines (159 loc) · 3.73 KB
/
boot.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
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/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 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() {
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{
{
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)
}
}