Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-machine tweaks in support of PR #13 #14

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

/*
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.
Expand All @@ -16,10 +19,19 @@ limitations under the License.
package main

import (
"fmt"
"os"

"github.com/docker/machine/libmachine/drivers/plugin"
"github.com/machine-drivers/docker-machine-driver-hyperkit/pkg/hyperkit"
)

func main() {
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println("version:", hyperkit.GetVersion())
fmt.Println("commit:", hyperkit.GetGitCommitID())
return
}

plugin.RegisterDriver(hyperkit.NewDriver("", ""))
}
67 changes: 40 additions & 27 deletions pkg/drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,62 @@ limitations under the License.
package drivers

import (
"io"
"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/golang/glog"
"github.com/pkg/errors"
)

// GetDiskPath returns the path of the machine disk image
func GetDiskPath(d *drivers.BaseDriver) string {
return filepath.Join(d.ResolveStorePath("."), d.GetMachineName()+".rawdisk")
}

// CommonDriver is the common driver base class
type CommonDriver struct{}

//Not implemented yet
// GetCreateFlags is not implemented yet
func (d *CommonDriver) GetCreateFlags() []mcnflag.Flag {
return nil
}

//Not implemented yet
// SetConfigFromFlags is 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
return errors.Wrap(err, "make disk image")
}

file, err := os.OpenFile(diskPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
return errors.Wrap(err, "open")
}
defer file.Close()
file.Seek(0, os.SEEK_SET)
if _, err := file.Seek(0, io.SeekStart); err != nil {
return errors.Wrap(err, "seek")
}

if _, err := file.Write(tarBuf.Bytes()); err != nil {
return err
return errors.Wrap(err, "write tar")
}
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 errors.Wrap(err, "truncate")
}
return nil
}
Expand All @@ -79,46 +84,54 @@ func publicSSHKeyPath(d *drivers.BaseDriver) string {
// 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
}
if err := d.Stop(); err != nil {
return err
}
return nil

return d.Start()
}

// MakeDiskImage makes a boot2docker VM disk image.
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")
glog.Infof("Making disk image using store path: %s", d.StorePath)
b2 := mcnutils.NewB2dUtils(d.StorePath)
if err := b2.CopyIsoToMachineDir(boot2dockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "copy iso to machine dir")
}

log.Info("Creating ssh key...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
keyPath := d.GetSSHKeyPath()
glog.Infof("Creating ssh key: %s...", keyPath)
if err := ssh.GenerateSSHKey(keyPath); err != nil {
return errors.Wrap(err, "generate ssh key")
}

log.Info("Creating raw disk image...")
diskPath := GetDiskPath(d)
glog.Infof("Creating raw disk image: %s...", diskPath)
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil {
return err
return errors.Wrapf(err, "createRawDiskImage(%s)", diskPath)
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
machPath := d.ResolveStorePath(".")
if err := fixPermissions(machPath); err != nil {
return errors.Wrapf(err, "fixing permissions on %s", machPath)
}
}
return nil
}

func fixPermissions(path string) error {
os.Chown(path, syscall.Getuid(), syscall.Getegid())
files, _ := ioutil.ReadDir(path)
glog.Infof("Fixing permissions on %s ...", path)
if err := os.Chown(path, syscall.Getuid(), syscall.Getegid()); err != nil {
return errors.Wrap(err, "chown dir")
}
files, err := ioutil.ReadDir(path)
if err != nil {
return errors.Wrap(err, "read dir")
}
for _, f := range files {
fp := filepath.Join(path, f.Name())
if err := os.Chown(fp, syscall.Getuid(), syscall.Getegid()); err != nil {
return err
return errors.Wrap(err, "chown file")
}
}
return nil
Expand Down
50 changes: 50 additions & 0 deletions pkg/drivers/drivers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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"
"testing"

"k8s.io/minikube/pkg/minikube/tests"
)

func Test_createDiskImage(t *testing.T) {
tmpdir := tests.MakeTempDir()
defer os.RemoveAll(tmpdir)

sshPath := filepath.Join(tmpdir, "ssh")
if err := ioutil.WriteFile(sshPath, []byte("mysshkey"), 0644); err != nil {
t.Fatalf("writefile: %v", err)
}
diskPath := filepath.Join(tmpdir, "disk")

sizeInMb := 100
sizeInBytes := int64(sizeInMb) * 1000000
if err := createRawDiskImage(sshPath, diskPath, sizeInMb); err != nil {
t.Errorf("createDiskImage() error = %v", err)
}
fi, err := os.Lstat(diskPath)
if err != nil {
t.Errorf("Lstat() error = %v", err)
}
if fi.Size() != sizeInBytes {
t.Errorf("Disk size is %v, want %v", fi.Size(), sizeInBytes)
}
}
Loading