Skip to content

Commit

Permalink
Use mac address for hostname. Save and reboot when complete
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Sep 12, 2017
1 parent a0bc1ec commit 4a7a18e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
31 changes: 28 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"bytes"
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -47,12 +49,17 @@ func start(cmd *cobra.Command, args []string) {

// See if we need to reset the host name and reboot:
name, _ := os.Hostname()
if !strings.Contains(name, "appliancemonitor") {
guid := xid.New()
err := network.ResetHostname(fmt.Sprintf("%s-%x", "appliancemonitor", guid.Machine()))
properhostname := fmt.Sprintf("%s-%s", "am", getMacAddr())

if name != properhostname {
err := network.ResetHostname(properhostname)
if err != nil {
log.Printf("[ERROR] There was a problem resetting the host name: %v", err)
return
}
log.Println("Rebooting...")
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}

// Get a reference to the config database
Expand Down Expand Up @@ -201,3 +208,21 @@ func handleSignals(ctx context.Context, sigch <-chan os.Signal, cancel context.C
os.Exit(0)
}
}

// getMacAddr gets the MAC hardware
// address of the host machine
func getMacAddr() (addr string) {
interfaces, err := net.Interfaces()

if err == nil {
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 {
// Don't use random as we have a real address
addr = strings.Replace(i.HardwareAddr.String(), ":", "", -1)
break
}
}
}

return
}
12 changes: 6 additions & 6 deletions network/hostname_linux_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
// ResetHostname changes the hostname for the local machine
func ResetHostname(newname string) error {
// Get hostname:
name, err := os.Hostname()
currentname, err := os.Hostname()
if err != nil {
log.Printf("[ERROR] Problem getting hostname: %v", err.Error())
return err
}

log.Printf("[INFO] Current hostname: %v -- desired new hostname: %v\n", name, newname)
log.Printf("[INFO] Current hostname: %v -- desired new hostname: %v\n", currentname, newname)

// Update /etc/hostname file
hostname, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
log.Printf("[ERROR] Problem reading /etc/hostname: %v", err.Error())
return err
}
newhostname := strings.Replace(string(hostname[:]), "raspberrypi", newname, -1)
err = ioutil.WriteFile("/etc/hostname", newhostname, 0644)
newhostname := strings.Replace(string(hostname[:]), currentname, newname, -1)
err = ioutil.WriteFile("/etc/hostname", []byte(newhostname), 0644)
if err != nil {
log.Printf("[ERROR] Problem writing /etc/hostname: %v", err.Error())
return err
Expand All @@ -37,8 +37,8 @@ func ResetHostname(newname string) error {
log.Printf("[ERROR] Problem reading /etc/hosts: %v", err.Error())
return err
}
newhosts := strings.Replace(string(hosts[:]), "raspberrypi", newname, -1)
err = ioutil.WriteFile("/etc/hosts", newhosts, 0644)
newhosts := strings.Replace(string(hosts[:]), currentname, newname, -1)
err = ioutil.WriteFile("/etc/hosts", []byte(newhosts), 0644)
if err != nil {
log.Printf("[ERROR] Problem writing /etc/hosts: %v", err.Error())
return err
Expand Down

0 comments on commit 4a7a18e

Please sign in to comment.