mirror of
https://github.com/rancher/os.git
synced 2025-07-03 18:16:13 +00:00
Merge pull request #207 from ibuildthecloud/hostname
set hostname from cloud-init
This commit is contained in:
commit
cd78981878
@ -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)
|
||||
}
|
||||
|
13
cmd/cloudinit/hostname/hostname.go
Normal file
13
cmd/cloudinit/hostname/hostname.go
Normal file
@ -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)
|
||||
}
|
21
util/util.go
21
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user