-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver_spec.go
98 lines (90 loc) · 2.63 KB
/
driver_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
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
package dockerdriver
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path"
"strings"
"code.cloudfoundry.org/lager/v3"
)
func WriteDriverSpec(logger lager.Logger, pluginsDirectory string, driver string, extension string, contents []byte) error {
err := os.MkdirAll(pluginsDirectory, 0755)
if err != nil {
logger.Error("error-creating-directory", err)
return err
}
f, err := os.Create(path.Join(pluginsDirectory, driver+"."+extension))
if err != nil {
logger.Error("error-creating-file ", err)
return err
}
defer f.Close()
_, err = f.Write(contents)
if err != nil {
logger.Error("error-writing-file ", err)
return err
}
err = f.Sync()
if err != nil {
logger.Error("error-syncing-file ", err)
return err
}
return nil
}
func ReadDriverSpec(logger lager.Logger, specName string, driverPath string, specFile string) (*DriverSpec, error) {
logger = logger.Session("read-driver-spec", lager.Data{"spec-name": specName, "spec-file": specFile})
logger.Info("start")
defer logger.Info("end")
var driverSpec DriverSpec
if strings.Contains(specFile, ".") {
extension := strings.Split(specFile, ".")[1]
switch extension {
case "sock":
driverSpec = DriverSpec{
Name: specName,
Address: path.Join(driverPath, specFile),
}
case "spec":
configFile, err := os.Open(path.Join(driverPath, specFile))
if err != nil {
logger.Error("error-opening-config", err, lager.Data{"DriverFileName": specFile})
return nil, err
}
reader := bufio.NewReader(configFile)
addressBytes, _, err := reader.ReadLine()
if err != nil { // no real value in faking this as bigger problems exist when this fails
logger.Error("error-reading-driver-file", err, lager.Data{"DriverFileName": specFile})
return nil, err
}
driverSpec = DriverSpec{
Name: specName,
Address: string(addressBytes),
}
case "json":
// extract url from json file
var driverJsonSpec DriverSpec
configFile, err := os.Open(path.Join(driverPath, specFile))
if err != nil {
logger.Error("error-opening-config", err, lager.Data{"DriverFileName": specFile})
return nil, err
}
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&driverJsonSpec); err != nil {
logger.Error("parsing-config-file-error", err)
return nil, err
}
driverSpec = DriverSpec{
Name: specName,
Address: driverJsonSpec.Address,
TLSConfig: driverJsonSpec.TLSConfig,
UniqueVolumeIds: driverJsonSpec.UniqueVolumeIds,
}
default:
err := fmt.Errorf("unknown-driver-extension: %s", extension)
logger.Error("driver", err)
return nil, err
}
}
return &driverSpec, nil
}