Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2046 from SvenDowideit/add-rsyslog
Browse files Browse the repository at this point in the history
Add rsyslog
  • Loading branch information
SvenDowideit authored Aug 16, 2017
2 parents 69b5401 + 38ac3b0 commit 96257a5
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 249 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.dapper
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ARG DOCKER_BUILD_VERSION=1.10.3
ARG DOCKER_BUILD_PATCH_VERSION=v${DOCKER_BUILD_VERSION}-ros1
ARG SELINUX_POLICY_URL=https://github.com/rancher/refpolicy/releases/download/v0.0.3/policy.29

ARG KERNEL_VERSION_amd64=4.9.40-rancher
ARG KERNEL_VERSION_amd64=4.9.42-rancher
ARG KERNEL_URL_amd64=https://github.com/rancher/os-kernel/releases/download/v${KERNEL_VERSION_amd64}/linux-${KERNEL_VERSION_amd64}-x86.tar.gz
#ARG KERNEL_URL_arm64=https://github.com/imikushin/os-kernel/releases/download/Estuary-4.4.0-arm64.8/linux-4.4.0-rancher-arm64.tar.gz

Expand Down
5 changes: 4 additions & 1 deletion cmd/cloudinitexecute/cloudinitexecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func ApplyConsole(cfg *rancherConfig.CloudConfig) {
}
}

util.RunCommandSequence(cfg.Runcmd)
err := util.RunCommandSequence(cfg.Runcmd)
if err != nil {
log.Error(err)
}
}

func WriteFiles(cfg *rancherConfig.CloudConfig, container string) {
Expand Down
5 changes: 4 additions & 1 deletion cmd/control/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func bootstrapAction(c *cli.Context) error {
}

log.Debugf("bootstrapAction: RunCommandSequence(%v)", cfg.Bootcmd)
util.RunCommandSequence(cfg.Bootcmd)
err := util.RunCommandSequence(cfg.Bootcmd)
if err != nil {
log.Error(err)
}

if cfg.Rancher.State.Dev != "" && cfg.Rancher.State.Wait {
waitForRoot(cfg)
Expand Down
3 changes: 2 additions & 1 deletion cmd/control/console_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/codegangsta/cli"
"github.com/rancher/os/cmd/cloudinitexecute"
"github.com/rancher/os/config"
"github.com/rancher/os/config/cmdline"
"github.com/rancher/os/log"
"github.com/rancher/os/util"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func consoleInitFunc() error {
createHomeDir(rancherHome, 1100, 1100)
createHomeDir(dockerHome, 1101, 1101)

password := config.GetCmdline("rancher.password")
password := cmdline.GetCmdline("rancher.password")
if password != "" {
cmd := exec.Command("chpasswd")
cmd.Stdin = strings.NewReader(fmt.Sprint("rancher:", password))
Expand Down
66 changes: 65 additions & 1 deletion cmd/control/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ func runInstall(image, installType, cloudConfig, device, partition, statedir, ka
func mountBootIso() error {
deviceName := "/dev/sr0"
deviceType := "iso9660"
if d, t := util.Blkid("RancherOS"); d != "" {
d, t, err := util.Blkid("RancherOS")
if err != nil {
log.Errorf("Failed to run blkid: %s", err)
}
if d != "" {
deviceName = d
deviceType = t
}
Expand Down Expand Up @@ -728,6 +732,66 @@ func formatdevice(device, partition string) error {
return nil
}

func mountdevice(baseName, bootDir, device, partition string, raw bool) (string, string, error) {
log.Debugf("mountdevice %s, raw %v", partition, raw)

if partition == "" {
if raw {
log.Debugf("util.Mount (raw) %s, %s", partition, baseName)

cmd := exec.Command("lsblk", "-no", "pkname", partition)
log.Debugf("Run(%v)", cmd)
cmd.Stderr = os.Stderr
device := ""
// TODO: out can == "" - this is used to "detect software RAID" which is terrible
if out, err := cmd.Output(); err == nil {
device = "/dev/" + strings.TrimSpace(string(out))
}

log.Debugf("mountdevice return -> d: %s, p: %s", device, partition)
return device, partition, util.Mount(partition, baseName, "", "")
}

//rootfs := partition
// Don't use ResolveDevice - it can fail, whereas `blkid -L LABEL` works more often

cfg := config.LoadConfig()
d, _, err := util.Blkid("RANCHER_BOOT")
if err != nil {
log.Errorf("Failed to run blkid: %s", err)
}
if d != "" {
partition = d
baseName = filepath.Join(baseName, "boot")
} else {
if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" {
// try the rancher.state.dev setting
partition = dev
} else {
d, _, err := util.Blkid("RANCHER_STATE")
if err != nil {
log.Errorf("Failed to run blkid: %s", err)
}
if d != "" {
partition = d
}
}
}
cmd := exec.Command("lsblk", "-no", "pkname", partition)
log.Debugf("Run(%v)", cmd)
cmd.Stderr = os.Stderr
// TODO: out can == "" - this is used to "detect software RAID" which is terrible
if out, err := cmd.Output(); err == nil {
device = "/dev/" + strings.TrimSpace(string(out))
}
}
os.MkdirAll(baseName, 0755)
cmd := exec.Command("mount", partition, baseName)
//cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
log.Debugf("mountdevice return2 -> d: %s, p: %s", device, partition)
return device, partition, cmd.Run()
}

func formatAndMount(baseName, device, partition string) (string, string, error) {
log.Debugf("formatAndMount")

Expand Down
12 changes: 10 additions & 2 deletions cmd/control/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ func MountDevice(baseName, device, partition string, raw bool) (string, string,
// Don't use ResolveDevice - it can fail, whereas `blkid -L LABEL` works more often

cfg := config.LoadConfig()
if d, _ := util.Blkid("RANCHER_BOOT"); d != "" {
d, _, err := util.Blkid("RANCHER_BOOT")
if err != nil {
log.Errorf("Failed to run blkid: %s", err)
}
if d != "" {
partition = d
baseName = filepath.Join(baseName, BootDir)
} else {
if dev := util.ResolveDevice(cfg.Rancher.State.Dev); dev != "" {
// try the rancher.state.dev setting
partition = dev
} else {
if d, _ := util.Blkid("RANCHER_STATE"); d != "" {
d, _, err := util.Blkid("RANCHER_STATE")
if err != nil {
log.Errorf("Failed to run blkid: %s", err)
}
if d != "" {
partition = d
}
}
Expand Down
171 changes: 171 additions & 0 deletions config/cmdline/cmdline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package cmdline

import (
"io/ioutil"
"strings"

yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/rancher/os/util"
)

func Read(parseAll bool) (m map[interface{}]interface{}, err error) {
cmdLine, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
return nil, err
}

if len(cmdLine) == 0 {
return nil, nil
}

cmdLineObj := Parse(strings.TrimSpace(util.UnescapeKernelParams(string(cmdLine))), parseAll)

return cmdLineObj, nil
}

func GetCmdline(key string) interface{} {
parseAll := true
if strings.HasPrefix(key, "cc.") || strings.HasPrefix(key, "rancher.") {
// the normal case
parseAll = false
}
cmdline, _ := Read(parseAll)
v, _ := GetOrSetVal(key, cmdline, nil)
return v
}

func GetOrSetVal(args string, data map[interface{}]interface{}, value interface{}) (interface{}, map[interface{}]interface{}) {
parts := strings.Split(args, ".")

tData := data
if value != nil {
tData = util.MapCopy(data)
}
t := tData
for i, part := range parts {
val, ok := t[part]
last := i+1 == len(parts)

// Reached end, set the value
if last && value != nil {
if s, ok := value.(string); ok {
value = UnmarshalOrReturnString(s)
}

t[part] = value
return value, tData
}

// Missing intermediate key, create key
if !last && value != nil && !ok {
newData := map[interface{}]interface{}{}
t[part] = newData
t = newData
continue
}

if !ok {
break
}

if last {
return val, tData
}

newData, ok := val.(map[interface{}]interface{})
if !ok {
break
}

t = newData
}

return "", tData
}

// Replace newlines, colons, and question marks with random strings
// This is done to avoid YAML treating these as special characters
var (
newlineMagicString = "9XsJcx6dR5EERYCC"
colonMagicString = "V0Rc21pIVknMm2rr"
questionMarkMagicString = "FoPL6JLMAaJqKMJT"
)

func reverseReplacement(result interface{}) interface{} {
switch val := result.(type) {
case map[interface{}]interface{}:
for k, v := range val {
val[k] = reverseReplacement(v)
}
return val
case []interface{}:
for i, item := range val {
val[i] = reverseReplacement(item)
}
return val
case string:
val = strings.Replace(val, newlineMagicString, "\n", -1)
val = strings.Replace(val, colonMagicString, ":", -1)
val = strings.Replace(val, questionMarkMagicString, "?", -1)
return val
}

return result
}

func UnmarshalOrReturnString(value string) (result interface{}) {
value = strings.Replace(value, "\n", newlineMagicString, -1)
value = strings.Replace(value, ":", colonMagicString, -1)
value = strings.Replace(value, "?", questionMarkMagicString, -1)
if err := yaml.Unmarshal([]byte(value), &result); err != nil {
result = value
}
result = reverseReplacement(result)
return
}

func Parse(cmdLine string, parseAll bool) map[interface{}]interface{} {
result := make(map[interface{}]interface{})

outer:
for _, part := range strings.Split(cmdLine, " ") {
if strings.HasPrefix(part, "cc.") {
part = part[3:]
} else if !strings.HasPrefix(part, "rancher.") {
if !parseAll {
continue
}
}

var value string
kv := strings.SplitN(part, "=", 2)

if len(kv) == 1 {
value = "true"
} else {
value = kv[1]
}

current := result
keys := strings.Split(kv[0], ".")
for i, key := range keys {
if i == len(keys)-1 {
current[key] = UnmarshalOrReturnString(value)
} else {
if obj, ok := current[key]; ok {
if newCurrent, ok := obj.(map[interface{}]interface{}); ok {
current = newCurrent
} else {
continue outer
}
} else {
newCurrent := make(map[interface{}]interface{})
current[key] = newCurrent
current = newCurrent
}
}
}
}

return result
}
18 changes: 10 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

yaml "github.com/cloudfoundry-incubator/candiedyaml"
"github.com/rancher/os/config/cmdline"
"github.com/rancher/os/util"
)

Expand Down Expand Up @@ -41,6 +42,13 @@ func Export(private, full bool) (string, error) {
bytes, err := yaml.Marshal(rawCfg)
return string(bytes), err
}
func filterPrivateKeys(data map[interface{}]interface{}) map[interface{}]interface{} {
for _, privateKey := range PrivateKeys {
_, data = filterKey(data, strings.Split(privateKey, "."))
}

return data
}

func Get(key string) (interface{}, error) {
cfg := LoadConfig()
Expand All @@ -50,23 +58,17 @@ func Get(key string) (interface{}, error) {
return nil, err
}

v, _ := getOrSetVal(key, data, nil)
v, _ := cmdline.GetOrSetVal(key, data, nil)
return v, nil
}

func GetCmdline(key string) interface{} {
cmdline := readCmdline()
v, _ := getOrSetVal(key, cmdline, nil)
return v
}

func Set(key string, value interface{}) error {
existing, err := readConfigs(nil, false, true, CloudConfigFile)
if err != nil {
return err
}

_, modified := getOrSetVal(key, existing, value)
_, modified := cmdline.GetOrSetVal(key, existing, value)

c := &CloudConfig{}
if err = util.Convert(modified, c); err != nil {
Expand Down
Loading

0 comments on commit 96257a5

Please sign in to comment.