diff --git a/cmd/cloudinit/cloudinit.go b/cmd/cloudinit/cloudinit.go index e590041a..ee5ad2de 100644 --- a/cmd/cloudinit/cloudinit.go +++ b/cmd/cloudinit/cloudinit.go @@ -24,6 +24,8 @@ import ( "sync" "time" + "gopkg.in/yaml.v2" + log "github.com/Sirupsen/logrus" "github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/datasource" @@ -35,10 +37,10 @@ import ( "github.com/coreos/coreos-cloudinit/datasource/url" "github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/system" + "github.com/rancherio/os/cmd/cloudinit/hostname" rancherNetwork "github.com/rancherio/os/cmd/network" rancherConfig "github.com/rancherio/os/config" "github.com/rancherio/os/util" - "gopkg.in/yaml.v2" ) const ( @@ -298,6 +300,13 @@ func executeCloudConfig() error { log.Info("Merging cloud-config from meta-data and user-data") cc := mergeConfigs(ccu, metadata) + if cc.Hostname != "" { + //set hostname + if err := hostname.SetHostname(cc.Hostname); err != nil { + log.Fatal(err) + } + } + if len(cc.SSHAuthorizedKeys) > 0 { authorizeSSHKeys("rancher", cc.SSHAuthorizedKeys, sshKeyName) } @@ -315,7 +324,7 @@ func executeCloudConfig() error { f := system.File{File: file} fullPath, err := system.WriteFile(&f, "/") if err != nil { - log.Fatalf("%v", err) + log.Fatal(err) } log.Printf("Wrote file %s to filesystem", fullPath) } diff --git a/cmd/cloudinit/hostname/hostname.go b/cmd/cloudinit/hostname/hostname.go new file mode 100644 index 00000000..8e08bdfc --- /dev/null +++ b/cmd/cloudinit/hostname/hostname.go @@ -0,0 +1,13 @@ +package hostname + +import ( + "io/ioutil" + "syscall" +) + +func SetHostname(hostname string) error { + if err := syscall.Sethostname([]byte(hostname)); err != nil { + return err + } + return ioutil.WriteFile("/etc/hostname", []byte(hostname), 0644) +} diff --git a/util/util.go b/util/util.go index e898cf96..acb4c71c 100644 --- a/util/util.go +++ b/util/util.go @@ -2,6 +2,7 @@ package util import ( "archive/tar" + "bufio" "errors" "fmt" "io" @@ -13,10 +14,11 @@ import ( "strings" "syscall" + "gopkg.in/yaml.v2" + log "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/mount" - "gopkg.in/yaml.v2" ) var ( @@ -25,6 +27,23 @@ var ( ErrNotFound = errors.New("Failed to find resource") ) +func GetOSType() string { + f, err := os.Open("/etc/os-release") + defer f.Close() + if err != nil { + return "busybox" + } + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + if len(line) > 8 && line[:8] == "ID_LIKE=" { + return line[8:] + } + } + return "busybox" + +} + func mountProc() error { if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) { if _, err := os.Stat("/proc"); os.IsNotExist(err) {