From c0a6c4cf4269870d0f5834d6d2f376f6aa4e1dcf Mon Sep 17 00:00:00 2001 From: Muhammad Hewedy Date: Sat, 26 Dec 2020 13:50:39 +0300 Subject: [PATCH] fix #37 Host name enhancements --- vms/create.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/vms/create.go b/vms/create.go index ebe9ca8..838c678 100644 --- a/vms/create.go +++ b/vms/create.go @@ -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 { @@ -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 {