forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_driver.go
96 lines (82 loc) · 2.01 KB
/
dynamic_driver.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
package drivers
import (
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type DynamicDriver struct {
BaseDriver
}
var DockerMachineDriverPrefix = "docker-machine-driver-"
func NewDynamicDriver(builtin bool, name, url, hash string) *DynamicDriver {
d := &DynamicDriver{
BaseDriver{
Builtin: builtin,
DriverName: name,
URL: url,
DriverHash: hash,
BinaryPrefix: DockerMachineDriverPrefix,
},
}
if !strings.HasPrefix(d.DriverName, DockerMachineDriverPrefix) {
d.DriverName = DockerMachineDriverPrefix + d.DriverName
}
return d
}
func (d *DynamicDriver) Install() error {
if d.Builtin {
return nil
}
binaryPath := path.Join(binDir(), d.DriverName)
tmpPath := binaryPath + "-tmp"
f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return errors.Wrapf(err, "Couldn't open %v for writing", tmpPath)
}
defer f.Close()
src, err := os.Open(d.srcBinName())
if err != nil {
return errors.Wrapf(err, "Couldn't open %v for copying", d.srcBinName())
}
defer src.Close()
logrus.Infof("Copying %v => %v", d.srcBinName(), tmpPath)
_, err = io.Copy(f, src)
if err != nil {
return errors.Wrapf(err, "Couldn't copy %v to %v", d.srcBinName(), tmpPath)
}
err = os.Rename(tmpPath, binaryPath)
if err != nil {
return errors.Wrapf(err, "Couldn't copy driver %v to %v", d.Name(), binaryPath)
}
return nil
}
func (d *BaseDriver) Excutable() error {
if d.DriverName == "" {
return fmt.Errorf("Empty driver name")
}
if d.Builtin {
return nil
}
binaryPath := path.Join(binDir(), d.DriverName)
_, err := os.Stat(binaryPath)
if err != nil {
return fmt.Errorf("Driver %s not found", binaryPath)
}
err = exec.Command(binaryPath).Start()
if err != nil {
return errors.Wrapf(err, "Driver binary %s couldn't execute", binaryPath)
}
return nil
}
func (d *DynamicDriver) binDir() string {
dest := os.Getenv("GMS_BIN_DIR")
if dest != "" {
return dest
}
return "./management-state/bin"
}