1
0
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:
Darren Shepherd 2015-04-21 19:13:32 -07:00
commit cd78981878
3 changed files with 44 additions and 3 deletions

View File

@ -24,6 +24,8 @@ import (
"sync" "sync"
"time" "time"
"gopkg.in/yaml.v2"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/coreos/coreos-cloudinit/config" "github.com/coreos/coreos-cloudinit/config"
"github.com/coreos/coreos-cloudinit/datasource" "github.com/coreos/coreos-cloudinit/datasource"
@ -35,10 +37,10 @@ import (
"github.com/coreos/coreos-cloudinit/datasource/url" "github.com/coreos/coreos-cloudinit/datasource/url"
"github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/pkg"
"github.com/coreos/coreos-cloudinit/system" "github.com/coreos/coreos-cloudinit/system"
"github.com/rancherio/os/cmd/cloudinit/hostname"
rancherNetwork "github.com/rancherio/os/cmd/network" rancherNetwork "github.com/rancherio/os/cmd/network"
rancherConfig "github.com/rancherio/os/config" rancherConfig "github.com/rancherio/os/config"
"github.com/rancherio/os/util" "github.com/rancherio/os/util"
"gopkg.in/yaml.v2"
) )
const ( const (
@ -298,6 +300,13 @@ func executeCloudConfig() error {
log.Info("Merging cloud-config from meta-data and user-data") log.Info("Merging cloud-config from meta-data and user-data")
cc := mergeConfigs(ccu, metadata) 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 { if len(cc.SSHAuthorizedKeys) > 0 {
authorizeSSHKeys("rancher", cc.SSHAuthorizedKeys, sshKeyName) authorizeSSHKeys("rancher", cc.SSHAuthorizedKeys, sshKeyName)
} }
@ -315,7 +324,7 @@ func executeCloudConfig() error {
f := system.File{File: file} f := system.File{File: file}
fullPath, err := system.WriteFile(&f, "/") fullPath, err := system.WriteFile(&f, "/")
if err != nil { if err != nil {
log.Fatalf("%v", err) log.Fatal(err)
} }
log.Printf("Wrote file %s to filesystem", fullPath) log.Printf("Wrote file %s to filesystem", fullPath)
} }

View 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)
}

View File

@ -2,6 +2,7 @@ package util
import ( import (
"archive/tar" "archive/tar"
"bufio"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -13,10 +14,11 @@ import (
"strings" "strings"
"syscall" "syscall"
"gopkg.in/yaml.v2"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/mount"
"gopkg.in/yaml.v2"
) )
var ( var (
@ -25,6 +27,23 @@ var (
ErrNotFound = errors.New("Failed to find resource") 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 { func mountProc() error {
if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) { if _, err := os.Stat("/proc/self/mountinfo"); os.IsNotExist(err) {
if _, err := os.Stat("/proc"); os.IsNotExist(err) { if _, err := os.Stat("/proc"); os.IsNotExist(err) {