-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move drivers to pkg/drivers, share utils
Share most of the disk image setup between hyperkit and kvm drivers. Move and remove a lot of shared configuration between all the in-tree drivers: kvm, hyperkit, none.
- Loading branch information
Showing
14 changed files
with
136 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
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 GetDiskFilename(machineName string) string { | ||
return machineName + ".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 | ||
} | ||
file.Close() | ||
|
||
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, shouldFixPermissions bool) (string, 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 := filepath.Join(d.ResolveStorePath("."), GetDiskFilename(d.MachineName)) | ||
if _, err := os.Stat(diskPath); os.IsNotExist(err) { | ||
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil { | ||
return "", err | ||
} | ||
if shouldFixPermissions { | ||
if err := fixPermissions(d.ResolveStorePath(".")); err != nil { | ||
return "", err | ||
} | ||
} | ||
} | ||
return diskPath, 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()) | ||
log.Debugf(fp) | ||
if err := os.Chown(fp, syscall.Getuid(), syscall.Getegid()); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.