/
runtime.go
96 lines (77 loc) · 2.09 KB
/
runtime.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
//
// Last.Backend LLC CONFIDENTIAL
// __________________
//
// [2014] - [2018] Last.Backend LLC
// All Rights Reserved.
//
// NOTICE: All information contained herein is, and remains
// the property of Last.Backend LLC and its suppliers,
// if any. The intellectual and technical concepts contained
// herein are proprietary to Last.Backend LLC
// and its suppliers and may be covered by Russian Federation and Foreign Patents,
// patents in process, and are protected by trade secret or copyright law.
// Dissemination of this information or reproduction of this material
// is strictly forbidden unless prior written permission is obtained
// from Last.Backend LLC.
//
package docker
import (
"github.com/docker/docker/api"
"github.com/docker/docker/client"
"github.com/lastbackend/lastbackend/pkg/log"
"strconv"
)
const (
logLevel = 5
logPrefix = "runtime:docker"
)
type Runtime struct {
client *client.Client
}
type config map[string]interface{}
func New(cfg config) (*Runtime, error) {
var (
err error
r = new(Runtime)
clientOptions = make([]func(*client.Client) error, 0)
)
if cfg == nil {
cfg = make(config, 0)
}
log.V(logLevel).Debug("Use docker runtime interface")
host := client.DefaultDockerHost
if v, ok := cfg["host"]; ok {
host = v.(string)
}
clientOptions = append(clientOptions, client.WithHost(host))
version := api.DefaultVersion
if v, ok := cfg["version"]; ok {
switch i := v.(type) {
case string:
version = string(i)
case float64:
version = strconv.FormatFloat(float64(i), 'f', 2, 64)
}
}
clientOptions = append(clientOptions, client.WithVersion(version))
if v, ok := cfg["tls"]; ok {
var tls = v.(map[string]string)
var caFile, certFile, keyFile string
if v, ok := tls["ca_file"]; ok {
caFile = v
}
if v, ok := tls["cert_file"]; ok {
certFile = v
}
if v, ok := tls["key_file"]; ok {
keyFile = v
}
clientOptions = append(clientOptions, client.WithTLSClientConfig(caFile, certFile, keyFile))
}
r.client, err = client.NewClientWithOpts(clientOptions...)
if err != nil {
return nil, err
}
return r, nil
}