Skip to content

Commit

Permalink
#11 allow create vagrant images - step 2 ssh into the machine
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 20, 2020
1 parent 1716066 commit a168ae8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 34 deletions.
16 changes: 8 additions & 8 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func VBoxManage(args ...string) *cmd {
}
}

func Scp(extraArgs ...string) *cmd {
func Scp(vmName string, extraArgs ...string) *cmd {
args := []string{"-q", "-r",
"-i", db.PrivateKeyPath,
"-i", db.GetPrivateKeyPath(vmName),
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
}
Expand All @@ -32,13 +32,13 @@ func Arp(args ...string) *cmd {
}
}

func Ssh(ipAddr string, extraArgs ...string) *cmd {
args := []string{"-i", db.PrivateKeyPath,
func Ssh(vmName, ipAddr string, extraArgs ...string) *cmd {
args := []string{"-i", db.GetPrivateKeyPath(vmName),
"-o", "StrictHostKeyChecking=no",
"-o", "GlobalKnownHostsFile=/dev/null",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "LogLevel=error",
db.Username + "@" + ipAddr}
db.GetUsername(vmName) + "@" + ipAddr}

return &cmd{
command: "ssh",
Expand All @@ -60,13 +60,13 @@ func Ping(ip string) *cmd {
}
}

func AnsiblePlaybook(ip string, playbook string) *cmd {
func AnsiblePlaybook(vmName, ip string, playbook string) *cmd {
return &cmd{
command: "ansible-playbook",
args: []string{
"-i", ip + ",",
"-e", "ansible_user=" + db.Username,
"-e", "ansible_private_key_file=" + db.PrivateKeyPath,
"-e", "ansible_user=" + db.GetUsername(vmName),
"-e", "ansible_private_key_file=" + db.GetPrivateKeyPath(vmName),
"--ssh-common-args", "-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null",
playbook,
},
Expand Down
10 changes: 5 additions & 5 deletions command/scp/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func copyFromVM(vmPath vmPath, localFile string) error {
return err
}

_, err = command.Scp(db.Username+"@"+ipAddr+":"+vmPath.file, localFile).Call()
_, err = command.Scp(vmPath.name, db.GetUsername(vmPath.name)+"@"+ipAddr+":"+vmPath.file, localFile).Call()
return err
}

Expand All @@ -60,7 +60,7 @@ func copyToVM(localFile string, vmPath vmPath) error {
return err
}

_, err = command.Scp(localFile, db.Username+"@"+ipAddr+":"+vmPath.file).Call()
_, err = command.Scp(vmPath.name, localFile, db.GetUsername(vmPath.name)+"@"+ipAddr+":"+vmPath.file).Call()
return err
}

Expand All @@ -76,9 +76,9 @@ func copyBetweenVMs(srcVmPath vmPath, destVmPath vmPath) error {
return err
}

_, err = command.Scp("-3",
db.Username+"@"+srcIPAddr+":"+srcVmPath.file,
db.Username+"@"+destIPAddr+":"+destVmPath.file).Call()
_, err = command.Scp(srcVmPath.name, "-3",
db.GetUsername(srcVmPath.name)+"@"+srcIPAddr+":"+srcVmPath.file,
db.GetUsername(destVmPath.name)+"@"+destIPAddr+":"+destVmPath.file).Call()
return err
}

Expand Down
8 changes: 4 additions & 4 deletions command/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func OpenTerminal(vmName string) error {
return err
}

return command.Ssh(ipAddr, "-tt").Interact()
return command.Ssh(vmName, ipAddr, "-tt").Interact()
}

func Execute(vmName string, cmd string) (string, error) {
Expand All @@ -20,7 +20,7 @@ func Execute(vmName string, cmd string) (string, error) {
return "", err
}

return command.Ssh(ipAddr, "--", cmd).Call()
return command.Ssh(vmName, ipAddr, "--", cmd).Call()
}

//ExecInteract execute ssh in interactive mode
Expand All @@ -30,7 +30,7 @@ func ExecInteract(vmName string, cmd string) error {
return err
}

return command.Ssh(ipAddr, "--", cmd).Interact()
return command.Ssh(vmName, ipAddr, "--", cmd).Interact()
}

// WithArgs run ssh command with args passed
Expand All @@ -40,5 +40,5 @@ func WithArgs(vmName string, args []string) error {
return err
}

return command.Ssh(ipAddr, args...).Interact()
return command.Ssh(vmName, ipAddr, args...).Interact()
}
36 changes: 29 additions & 7 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package db
import (
"log"
"os"
)

const (
Username = "vermin"
"path/filepath"
"strings"
)

const (
Expand All @@ -15,9 +13,8 @@ const (
)

var (
ImagesDir = getVerminDir() + string(os.PathSeparator) + "images"
VMsBaseDir = getVerminDir() + string(os.PathSeparator) + "vms"
PrivateKeyPath = getVerminDir() + string(os.PathSeparator) + "vermin_rsa"
ImagesDir = getVerminDir() + string(os.PathSeparator) + "images"
VMsBaseDir = getVerminDir() + string(os.PathSeparator) + "vms"
)

func GetImageFilePath(imageName string) string {
Expand All @@ -35,3 +32,28 @@ func getVerminDir() string {
}
return dir + string(os.PathSeparator) + ".vermin"
}

func GetUsername(vmName string) string {
vmdb, _ := Load(vmName)
if IsVagrantImage(vmdb.Image) {
return "vagrant"
} else {
return "vermin"
}
}

func GetPrivateKeyPath(vmName string) string {
vmdb, _ := Load(vmName)
if IsVagrantImage(vmdb.Image) {
return filepath.Join(getVerminDir(), "vagrant_insecure_private_key")
} else {
return filepath.Join(getVerminDir(), "vermin_rsa")
}
}

// IsValidImage check the image name format to be "vagrant/<base>/<image>[:version]",
// example vagrant/hashicorp/bionic64
func IsVagrantImage(image string) bool {
s := strings.Split(image, "/")
return len(s) == 3 && s[0] == "vagrant"
}
3 changes: 2 additions & 1 deletion images/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package images

import (
"fmt"
"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/vagrant"
"github.com/schollz/progressbar/v3"
"io"
Expand All @@ -24,7 +25,7 @@ func Download(image string) error {

var dbImg *dbImage

if vagrant.IsValidImage(image) {
if db.IsVagrantImage(image) {
url, err := vagrant.GetImageURL(image)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion images/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func writeNewImage(tmpFile *os.File, imageName string) error {
return err
}

if vagrant.IsValidImage(imageName) {
if db.IsVagrantImage(imageName) {
if err := vagrant.ProcessImage(imagePath); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion provisioners/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func (Ansible) Exec(vmName string, script string) error {
return err
}

return command.AnsiblePlaybook(ipAddr, script).Interact()
return command.AnsiblePlaybook(vmName, ipAddr, script).Interact()
}
7 changes: 0 additions & 7 deletions vagrant/vagrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ import (
"strings"
)

// IsValidImage check the image name format to be "vagrant/<base>/<image>[:version]",
// example vagrant/hashicorp/bionic64
func IsValidImage(image string) bool {
s := strings.Split(image, "/")
return len(s) == 3 && s[0] == "vagrant"
}

func GetImageURL(image string) (string, error) {
// TODO
// get json and parse it to return download URL
Expand Down

0 comments on commit a168ae8

Please sign in to comment.