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

Feature/Add hostname configuration #99

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
74 changes: 72 additions & 2 deletions device/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strings"

log "github.com/sirupsen/logrus"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/xshellinc/tools/dialogs"
"github.com/xshellinc/tools/lib/help"
"github.com/xshellinc/tools/lib/ping"
Expand Down 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 @@ -266,6 +267,21 @@ func SaveWifi(storage map[string]interface{}) error {
return errors.New(err.Error() + ":" + eut)
}

if _, ok := storage[Interface]; !ok {
log.Debug("Adding wlan0 to interfaces...")
fp := help.AddPathSuffix("unix", MountDir, IsaaxConfDir, "network", "interfaces")

_, eut, err := ssh.Run(fmt.Sprintf(`echo "%s" > %s`, `
auto wlan0
iface wlan0 inet dhcp
source-directory /etc/network/interfaces.d
`, fp))
if err != nil {
log.WithField("eut", eut).Error(err.Error())
}

}

return nil
}

Expand All @@ -291,7 +307,7 @@ func SetInterface(storage map[string]interface{}) error {
if dialogs.YesNoDialog("Change values?") {
AskInterfaceParams(&i)
}

//TODO: allow user to setup several static interfaces at once
switch device[num] {
case "eth0":
storage[Interface] = fmt.Sprintf(InterfaceETH, i.Address, i.Netmask, i.Gateway, i.DNS)
Expand Down Expand Up @@ -396,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
}
1 change: 1 addition & 0 deletions device/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
"iface eth0 inet manual\n" +
"\n" +
"allow-hotplug wlan0\n" +
"auto wlan0\n" +
"iface wlan0 inet static\n" +
"address %s\n" +
"netmask %s\n" +
Expand Down
2 changes: 1 addition & 1 deletion mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
"Pass": "linaro"
},
{
"URL": "http://isaax-distro.s3-website-ap-northeast-1.amazonaws.com/boards/tinker/v1.8/20170417-tinker-board-linaro-stretch-alip-v1.8.img.zip",
"URL": "http://isaax-distro.s3-website-ap-northeast-1.amazonaws.com/boards/tinker/v2.0.1/20170817-tinker-board-linaro-stretch-alip-v2.0.1.img.zip",
"Title": "TinkerOS Debian",
"Alias": "debian",
"User": "linaro",
Expand Down