-
Notifications
You must be signed in to change notification settings - Fork 2
/
spec.go
63 lines (54 loc) · 1.32 KB
/
spec.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
package docker
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/Scalingo/sand/config"
"github.com/pkg/errors"
)
const (
DockerPluginName = "sand"
DockerPluginFile = "sand.json"
DockerSpecDir = "/etc/docker/plugins"
)
type PluginSpec struct {
Name string
Addr string
TLSConfig struct {
InsecureSkipVerify bool
CAFile string
CertFile string
KeyFile string
}
}
func WritePluginSpecsOnDisk(ctx context.Context, c *config.Config) error {
err := os.MkdirAll(DockerSpecDir, 0755)
if err != nil {
return errors.Wrapf(err, "fail to create docker plugins directory")
}
fd, err := os.OpenFile(filepath.Join(DockerSpecDir, DockerPluginFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return errors.Wrapf(err, "fail to open spec file")
}
defer fd.Close()
scheme := "http"
if c.HttpTLSCA != "" {
scheme = "https"
}
spec := PluginSpec{
Name: DockerPluginName,
Addr: fmt.Sprintf("%s://%s:%d", scheme, c.PublicHostname, c.DockerPluginHttpPort),
}
if c.HttpTLSCA != "" {
spec.TLSConfig.CAFile = c.HttpTLSCA
spec.TLSConfig.CertFile = c.HttpTLSCert
spec.TLSConfig.KeyFile = c.HttpTLSKey
}
err = json.NewEncoder(fd).Encode(&spec)
if err != nil {
return errors.Wrapf(err, "fail to encode spec in JSON")
}
return nil
}