Skip to content

Commit

Permalink
#9 polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 7, 2020
1 parent 66428df commit 5fe3ce2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 48 deletions.
9 changes: 4 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ $ vermin create <image> </path/to/shell/script.sh>
ps.Script = args[1]
checkFilePath(ps.Script)

typeStr, _ := cmd.Flags().GetString("type")
switch typeStr {
t, _ := cmd.Flags().GetString("type")
switch t {
case paramShell:
ps.Type = vms.ProvisionShell
ps.Func = vms.ProvisionShell
case paramAnsible:
ps.Type = vms.ProvisionAnsible
ps.Func = vms.ProvisionAnsible
}

}
cpus, _ := cmd.Flags().GetInt("cpus")
mem, _ := cmd.Flags().GetInt("mem")

fmt.Println(ps)
vmName, err := vms.Create(imageName, ps, cpus, mem)
if err != nil {
fmt.Println(err)
Expand Down
46 changes: 3 additions & 43 deletions vms/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ import (
"bufio"
"fmt"
"github.com/mhewedy/vermin/command"
"github.com/mhewedy/vermin/command/scp"
"github.com/mhewedy/vermin/command/ssh"
"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/images"
"github.com/mhewedy/vermin/progress"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
)

const (
ProvisionShell = iota
ProvisionAnsible
)

type Provision int

type ProvisionScript struct {
Script string
Type Provision
Func func(vmName string, script string) error
}

func Create(imageName string, ps ProvisionScript, cpus int, mem int) (string, error) {
Expand Down Expand Up @@ -72,15 +63,8 @@ func Create(imageName string, ps ProvisionScript, cpus int, mem int) (string, er
}

if len(ps.Script) > 0 {
switch ps.Type {
case ProvisionShell:
if err := provision(vmName, ps.Script); err != nil {
return "", err
}
case ProvisionAnsible:
if err := ansible(vmName, ps.Script); err != nil {
return "", err
}
if err := ps.Func(vmName, ps.Script); err != nil {
return "", err
}
}

Expand Down Expand Up @@ -115,30 +99,6 @@ func setNetworkAdapter(vmName string) error {
return nil
}

func provision(vmName string, script string) error {
fmt.Println("Provisioning", vmName)

vmFile := "/tmp/" + filepath.Base(script)
if err := scp.CopyToVM(vmName, script, vmFile); err != nil {
return err
}
if _, err := ssh.Execute(vmName, "chmod +x "+vmFile); err != nil {
return err
}
if err := ssh.ExecInteract(vmName, vmFile); err != nil {
return err
}

return nil
}

func ansible(vmName string, script string) error {

// TODO add ansbile provision processing

return nil
}

func start(vmName string) error {
progress.Immediate("Starting", vmName)
if _, err := command.VBoxManage("startvm", vmName, "--type", "headless").Call(); err != nil {
Expand Down
32 changes: 32 additions & 0 deletions vms/provision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package vms

import (
"fmt"
"github.com/mhewedy/vermin/command/scp"
"github.com/mhewedy/vermin/command/ssh"
"path/filepath"
)

func ProvisionShell(vmName string, script string) error {
fmt.Println("Provisioning", vmName)

vmFile := "/tmp/" + filepath.Base(script)
if err := scp.CopyToVM(vmName, script, vmFile); err != nil {
return err
}
if _, err := ssh.Execute(vmName, "chmod +x "+vmFile); err != nil {
return err
}
if err := ssh.ExecInteract(vmName, vmFile); err != nil {
return err
}

return nil
}

func ProvisionAnsible(vmName string, script string) error {

// TODO add ansbile provisionShell processing

return nil
}

0 comments on commit 5fe3ce2

Please sign in to comment.