Skip to content

Commit

Permalink
fix #37 Host name enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Dec 26, 2020
1 parent 9dc27bd commit c0a6c4c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions vms/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func Create(imageName string, ps ProvisionScript, cpus int, mem int) (string, er
return "", err
}

if err := changeHostname(vmName); err != nil {
return "", err
}

if len(ps.Script) > 0 {
fmt.Println("Provisioning", vmName)
if err := ps.Func(vmName, ps.Script); err != nil {
Expand All @@ -64,6 +68,36 @@ func Create(imageName string, ps ProvisionScript, cpus int, mem int) (string, er
return vmName, nil
}

func changeHostname(vmName string) error {

stop := progress.Show("Setting hostname", false)
defer stop()

oldHostname, err := ssh.Execute(vmName, "hostname")
if err != nil {
return err
}

oldHostname = strings.TrimSuffix(oldHostname, "\n")
oldHostname = strings.ReplaceAll(oldHostname, ".localdomain", "")

newHostname := strings.Split(vmName, "_")[1]

cmds := []string{
fmt.Sprintf("sudo hostname %s", newHostname),
fmt.Sprintf("sudo sh -c 'echo %s > /etc/hostname'", newHostname),
fmt.Sprintf("sudo sed -i 's/%s/%s/g' /etc/hosts", oldHostname, newHostname),
}

for _, cmd := range cmds {
if _, err := ssh.Execute(vmName, cmd); err != nil {
return err
}
}

return nil
}

func start(vmName string) error {
progress.Immediate("Starting", vmName)
if err := hypervisor.Start(vmName); err != nil {
Expand Down

0 comments on commit c0a6c4c

Please sign in to comment.