This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
forked from machine-drivers/docker-machine-driver-hyperkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivers.go
125 lines (105 loc) · 3.25 KB
/
drivers.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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package drivers
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/cloudflare/cfssl/log"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/pkg/errors"
)
func GetDiskPath(d *drivers.BaseDriver) string {
return filepath.Join(d.ResolveStorePath("."), d.GetMachineName()+".rawdisk")
}
type CommonDriver struct{}
//Not implemented yet
func (d *CommonDriver) GetCreateFlags() []mcnflag.Flag {
return nil
}
//Not implemented yet
func (d *CommonDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}
func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error {
tarBuf, err := mcnutils.MakeDiskImage(sshKeyPath)
if err != nil {
return err
}
file, err := os.OpenFile(diskPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
file.Seek(0, os.SEEK_SET)
if _, err := file.Write(tarBuf.Bytes()); err != nil {
return err
}
if err := file.Close(); err != nil {
return errors.Wrapf(err, "closing file %s", diskPath)
}
if err := os.Truncate(diskPath, int64(diskSizeMb*1000000)); err != nil {
return err
}
return nil
}
func publicSSHKeyPath(d *drivers.BaseDriver) string {
return d.GetSSHKeyPath() + ".pub"
}
// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func Restart(d drivers.Driver) error {
for _, f := range []func() error{d.Stop, d.Start} {
if err := f(); err != nil {
return err
}
}
return nil
}
func MakeDiskImage(d *drivers.BaseDriver, boot2dockerURL string, diskSize int) error {
//TODO(r2d4): rewrite this, not using b2dutils
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(boot2dockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "Error copying ISO to machine dir")
}
log.Info("Creating ssh key...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
log.Info("Creating raw disk image...")
diskPath := GetDiskPath(d)
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil {
return err
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
}
}
return nil
}
func fixPermissions(path string) error {
os.Chown(path, syscall.Getuid(), syscall.Getegid())
files, _ := ioutil.ReadDir(path)
for _, f := range files {
fp := filepath.Join(path, f.Name())
if err := os.Chown(fp, syscall.Getuid(), syscall.Getegid()); err != nil {
return err
}
}
return nil
}