Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set hostname on configuration #100

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions device/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewDefault(ssh ssh_helper.Util) *Configurator {
config.AddConfigFn(Wifi, NewCallbackFn(SetWifi, SaveWifi))
config.AddConfigFn(Interface, NewCallbackFn(SetInterface, SaveInterface))
config.AddConfigFn(DNS, NewCallbackFn(SetSecondaryDNS, SaveSecondaryDNS))
config.AddConfigFn("Hostname", NewCallbackFn(SetHostname, SaveHostname))
return config
}

Expand Down Expand Up @@ -411,3 +412,57 @@ func AskInterfaceParams(i *Interfaces) {
i.Netmask = dialogs.GetSingleAnswer("Please enter your netmask: ", dialogs.IpAddressValidator)
i.DNS = dialogs.GetSingleAnswer("Please enter your dns server: ", dialogs.IpAddressValidator)
}

// SetHostname is a default method with a dialog to configure device hostname
func SetHostname(storage map[string]interface{}) error {
ssh, ok := storage["ssh"].(ssh_helper.Util)
if !ok {
return errors.New("Cannot get ssh config")
}
fp := help.AddPathSuffix("unix", MountDir, "/etc/hostname")
out, eut, err := ssh.Run("cat " + fp)
if err != nil || strings.TrimSpace(eut) != "" {
log.WithField("eut", eut).Error(err)
return err
}
hostname := strings.TrimSpace(out)
fmt.Println("[+] Default hostname: ", hostname)

if dialogs.YesNoDialog("Do you want to change default hostname?") {
storage["NewHostname"] = dialogs.GetSingleAnswer("New hostname: ", dialogs.EmptyStringValidator)
storage["OldHostname"] = hostname
}

return nil
}

// SaveHostname is a default method to save hostname into the image
func SaveHostname(storage map[string]interface{}) error {

if _, ok := storage["OldHostname"]; !ok {
return nil
}
if _, ok := storage["NewHostname"]; !ok {
return nil
}

ssh, ok := storage["ssh"].(ssh_helper.Util)
if !ok {
return errors.New("Cannot get ssh config")
}

hosts := help.AddPathSuffix("unix", MountDir, "/etc/hosts")
hostname := help.AddPathSuffix("unix", MountDir, "/etc/hostname")
data := fmt.Sprintf("'s/%s/%s/g'", storage["OldHostname"], storage["NewHostname"])

if _, eut, err := ssh.Run(fmt.Sprintf(`sed -i %s %s`, data, hosts)); err != nil || strings.TrimSpace(eut) != "" {
log.WithField("eut", eut).Error(err)
return err
}
if _, eut, err := ssh.Run(fmt.Sprintf(`sed -i %s %s`, data, hostname)); err != nil || strings.TrimSpace(eut) != "" {
log.WithField("eut", eut).Error(err)
return err
}

return nil
}