Skip to content

Commit

Permalink
polish code: introduce provisioners package
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 15, 2020
1 parent da2d698 commit 856ec21
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 43 deletions.
7 changes: 1 addition & 6 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ package cmd

import (
"errors"
"fmt"
"github.com/mhewedy/vermin/vms"
"github.com/spf13/cobra"
"os"
)

// commitCmd represents the commit command
Expand All @@ -39,10 +37,7 @@ $ vermin commit vm_01 elk/elastic
override, _ := cmd.Flags().GetBool("override")

err := vms.Commit(vmName, imageName, override)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
exitOnError(err)
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
Expand Down
29 changes: 7 additions & 22 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ package cmd
import (
"errors"
"fmt"
"github.com/mhewedy/vermin/provisioners"
"github.com/mhewedy/vermin/vms"
"github.com/spf13/cobra"
"os"
"strings"
)

const paramShell = "shell"
const paramAnsible = "ansible"

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Expand All @@ -48,26 +44,20 @@ $ vermin create <image> </path/to/shell/script.sh>
var ps vms.ProvisionScript

if len(args) > 1 {
ps.Script = args[1]
checkFilePath(ps.Script)

t, _ := cmd.Flags().GetString("type")
switch t {
case paramShell:
ps.Func = vms.ProvisionShell
case paramAnsible:
ps.Func = vms.ProvisionAnsible
}
pf, err := provisioners.Load(t)
exitOnError(err)

ps.Func = pf
ps.Script = args[1]
checkFilePath(ps.Script)
}
cpus, _ := cmd.Flags().GetInt("cpus")
mem, _ := cmd.Flags().GetInt("mem")

vmName, err := vms.Create(imageName, ps, cpus, mem)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
exitOnError(err)

fmt.Printf("\nVM is ready, to connect to it use:\n$ vermin ssh %s\n", vmName)
},
Expand All @@ -76,11 +66,6 @@ $ vermin create <image> </path/to/shell/script.sh>
return errors.New("Image required\nUse the command 'vermin images' to list all images available")
}

typeStr, _ := cmd.Flags().GetString("type")
if !(strings.EqualFold(typeStr, paramShell) || strings.EqualFold(typeStr, paramAnsible)) {
return errors.New("provision script type should be either shell or ansible")
}

return nil
},
ValidArgsFunction: listImages,
Expand Down
7 changes: 7 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ func checkFilePath(path string) {
func preRun(cmd *cobra.Command, args []string) {
config.CheckForUpdates(version)
}

func exitOnError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
22 changes: 22 additions & 0 deletions provisioners/ansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package provisioners

import (
"github.com/mhewedy/vermin/command"
"github.com/mhewedy/vermin/ip"
)

type Ansible struct{}

func (Ansible) Accept(ptype string) bool {
return ptype == "ansible"
}

func (Ansible) Exec(vmName string, script string) error {

ipAddr, err := ip.Find(vmName, false)
if err != nil {
return err
}

return command.AnsiblePlaybook(ipAddr, script).Interact()
}
29 changes: 29 additions & 0 deletions provisioners/provisioners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package provisioners

import "fmt"

type Func func(vmName string, script string) error

type Provisioner interface {
Accept(ptype string) bool
Exec(vmName string, script string) error
}

func Load(pType string) (Func, error) {

// TODO in future if we need to implement plugin arch,
// we might construct this slice dynamically by checking some directory of binaries of some name
// e.g. vermin-provisioner-<name>
ps := []Provisioner{
Shell{},
Ansible{},
}

for _, p := range ps {
if p.Accept(pType) {
return p.Exec, nil
}
}

return nil, fmt.Errorf("Cannot load provisioner of type: %s", pType)
}
22 changes: 8 additions & 14 deletions vms/provision.go → provisioners/shell.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package vms
package provisioners

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

func ProvisionShell(vmName string, script string) error {
type Shell struct{}

func (Shell) Accept(ptype string) bool {
return "shell" == ptype
}

func (Shell) Exec(vmName string, script string) error {

vmFile := "/tmp/" + filepath.Base(script)
if err := scp.CopyToVM(vmName, script, vmFile); err != nil {
Expand All @@ -23,13 +27,3 @@ func ProvisionShell(vmName string, script string) error {

return nil
}

func ProvisionAnsible(vmName string, script string) error {

ipAddr, err := ip.Find(vmName, false)
if err != nil {
return err
}

return command.AnsiblePlaybook(ipAddr, script).Interact()
}
3 changes: 2 additions & 1 deletion vms/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/images"
"github.com/mhewedy/vermin/progress"
"github.com/mhewedy/vermin/provisioners"
"os"
"runtime"
"sort"
Expand All @@ -17,7 +18,7 @@ import (

type ProvisionScript struct {
Script string
Func func(vmName string, script string) error
Func provisioners.Func
}

func Create(imageName string, ps ProvisionScript, cpus int, mem int) (string, error) {
Expand Down

0 comments on commit 856ec21

Please sign in to comment.