1
0
mirror of https://github.com/rancher/os.git synced 2025-07-19 17:39:04 +00:00

save the elided cmdline into an env var

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit 2017-03-14 12:05:07 +10:00
parent 368a13ed13
commit 73980f9c73
5 changed files with 74 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"strings" "strings"
@ -48,6 +49,7 @@ func loadRawDiskConfig(dirPrefix string, full bool) map[interface{}]interface{}
func loadRawConfig(dirPrefix string, full bool) map[interface{}]interface{} { func loadRawConfig(dirPrefix string, full bool) map[interface{}]interface{} {
rawCfg := loadRawDiskConfig(dirPrefix, full) rawCfg := loadRawDiskConfig(dirPrefix, full)
rawCfg = util.Merge(rawCfg, readCmdline()) rawCfg = util.Merge(rawCfg, readCmdline())
rawCfg = util.Merge(rawCfg, readElidedCmdline(rawCfg))
rawCfg = applyDebugFlags(rawCfg) rawCfg = applyDebugFlags(rawCfg)
return mergeMetadata(rawCfg, readMetadata()) return mergeMetadata(rawCfg, readMetadata())
} }
@ -69,13 +71,31 @@ func LoadConfigWithPrefix(dirPrefix string) *CloudConfig {
return cfg return cfg
} }
func Insert(m interface{}, args ...interface{}) interface{} {
// TODO: move to util.go
if len(args)%2 != 0 {
panic("must have pairs of keys and values")
}
mv := reflect.ValueOf(m)
if mv.IsNil() {
mv = reflect.MakeMap(mv.Type())
}
for i := 0; i < len(args); i += 2 {
mv.SetMapIndex(reflect.ValueOf(args[i]), reflect.ValueOf(args[i+1]))
}
return mv.Interface()
}
func SaveInitCmdline(cmdLineArgs string) { func SaveInitCmdline(cmdLineArgs string) {
log.Infof("INITINIT: %s", cmdLineArgs)
elidedCfg := parseCmdline(cmdLineArgs) elidedCfg := parseCmdline(cmdLineArgs)
log.Infof("SaveInitCmdline: %#v", elidedCfg)
if err := WriteToFile(elidedCfg, CloudConfigInitFile); err != nil { env := Insert(make(map[interface{}]interface{}), interface{}("EXTRA_CMDLINE"), interface{}(cmdLineArgs))
rancher := Insert(make(map[interface{}]interface{}), interface{}("environment"), env)
newCfg := Insert(elidedCfg, interface{}("rancher"), rancher)
// make it easy for readElidedCmdline(rawCfg)
newCfg = Insert(newCfg, interface{}("EXTRA_CMDLINE"), interface{}(cmdLineArgs))
if err := WriteToFile(newCfg, CloudConfigInitFile); err != nil {
log.Errorf("Failed to write init-cmdline config: %s", err) log.Errorf("Failed to write init-cmdline config: %s", err)
} }
} }
@ -171,6 +191,20 @@ func readMetadata() datasource.Metadata {
return metadata return metadata
} }
func readElidedCmdline(rawCfg map[interface{}]interface{}) map[interface{}]interface{} {
for k, v := range rawCfg {
if key, _ := k.(string); key == "EXTRA_CMDLINE" {
if val, ok := v.(string); ok {
cmdLineObj := parseCmdline(strings.TrimSpace(util.UnescapeKernelParams(string(val))))
return cmdLineObj
}
}
}
return nil
}
func readCmdline() map[interface{}]interface{} { func readCmdline() map[interface{}]interface{} {
cmdLine, err := ioutil.ReadFile("/proc/cmdline") cmdLine, err := ioutil.ReadFile("/proc/cmdline")
if err != nil { if err != nil {
@ -241,6 +275,7 @@ func readConfigs(bytes []byte, substituteMetadataVars, returnErr bool, files ...
left := make(map[interface{}]interface{}) left := make(map[interface{}]interface{})
metadata := readMetadata() metadata := readMetadata()
for _, file := range files { for _, file := range files {
//os.Stderr.WriteString(fmt.Sprintf("READCONFIGS(%s)", file))
content, err := readConfigFile(file) content, err := readConfigFile(file)
if err != nil { if err != nil {
if returnErr { if returnErr {

View File

@ -29,6 +29,8 @@ For example, the `kernel` line above could be written as:
kernel ${base-url}/vmlinuz rancher.state.dev=LABEL=RANCHER_STATE rancher.state.autoformat=[/dev/sda] -- rancher.cloud_init.datasources=[url:http://example.com/cloud-config] kernel ${base-url}/vmlinuz rancher.state.dev=LABEL=RANCHER_STATE rancher.state.autoformat=[/dev/sda] -- rancher.cloud_init.datasources=[url:http://example.com/cloud-config]
``` ```
The hidden part of the command line can be accessed with either `sudo ros config get rancher.environment.EXTRA_CMDLINE`, or by using a service file's environment array.
### cloud-init Datasources ### cloud-init Datasources

View File

@ -14,7 +14,7 @@ touch dist/images
for i in $BASE/[0-9]*; do for i in $BASE/[0-9]*; do
name="os-$(echo ${i} | cut -f2 -d-)" name="os-$(echo ${i} | cut -f2 -d-)"
tag="${OS_REPO}/${name}:${VERSION}${SUFFIX}" tag="${OS_REPO}/${name}:${VERSION}${SUFFIX}"
echo Building ${tag} echo "build-image: Building ${tag}"
if [ -x ${i}/prebuild.sh ]; then if [ -x ${i}/prebuild.sh ]; then
${i}/prebuild.sh ${i}/prebuild.sh
fi fi
@ -28,3 +28,5 @@ for i in $BASE/[0-9]*; do
echo "WARN: Skipping ${tag}" echo "WARN: Skipping ${tag}"
fi fi
done done
echo "build-image: DONE"

View File

@ -4,10 +4,14 @@ set -e
cd $(dirname $0)/.. cd $(dirname $0)/..
IMAGES=$(bin/host_ros c images -i build/initrd/usr/share/ros/os-config.yml) IMAGES=$(bin/host_ros c images -i build/initrd/usr/share/ros/os-config.yml)
echo "tar-image: IMAGES=$IMAGES"
for i in $IMAGES; do for i in $IMAGES; do
echo "tar-image: pull($i)"
if [ "${FORCE_PULL}" = "1" ] || ! docker inspect $i >/dev/null 2>&1; then if [ "${FORCE_PULL}" = "1" ] || ! docker inspect $i >/dev/null 2>&1; then
docker pull $i docker pull $i
fi fi
done done
echo "tar-images: docker save ${IMAGES} > build/images.tar"
docker save ${IMAGES} > build/images.tar docker save ${IMAGES} > build/images.tar
echo "tar-images: DONE"

View File

@ -1,15 +1,39 @@
package integration package integration
import . "gopkg.in/check.v1" import . "gopkg.in/check.v1"
import "fmt"
func (s *QemuSuite) TestElideCmdLine(c *C) { func (s *QemuSuite) TestElideCmdLine(c *C) {
extra := "cc.hostname=nope rancher.password=three"
runArgs := []string{ runArgs := []string{
"--fresh", "--fresh",
"--append",
"cc.something=yes rancher.password=two",
"--append-init", "--append-init",
"cc.hostname=nope rancher.debug=true", extra,
} }
s.RunQemuWith(c, runArgs...) s.RunQemuWith(c, runArgs...)
s.CheckOutput(c, "nope\n", Equals, "hostname") s.CheckOutput(c, "nope\n", Equals, "hostname")
s.CheckOutput(c, "printk.devkmsg=on rancher.debug=true rancher.password=rancher console=ttyS0 rancher.autologin=ttyS0 rancher.state.dev=LABEL=RANCHER_STATE rancher.state.autoformat=[/dev/sda,/dev/vda] rancher.rm_usr -- \n", Equals, "cat /proc/cmdline") s.CheckOutput(c,
"printk.devkmsg=on rancher.debug=true rancher.password=rancher console=ttyS0 rancher.autologin=ttyS0 cc.something=yes rancher.password=two rancher.state.dev=LABEL=RANCHER_STATE rancher.state.autoformat=[/dev/sda,/dev/vda] rancher.rm_usr -- \n",
Equals,
"cat /proc/cmdline",
)
s.CheckOutput(c,
fmt.Sprintf("/init %s\n", extra),
Equals,
"sudo ros config get rancher.environment.EXTRA_CMDLINE",
)
// TODO: it seems that rancher.password and rancher.autologin are in `ros config export`, but accessible as `ros config get`
s.CheckOutput(c, "\n", Equals, "sudo ros config get rancher.password")
s.CheckOutput(c,
"EXTRA_CMDLINE: /init cc.hostname=nope rancher.password=three\n"+
" EXTRA_CMDLINE: /init cc.hostname=nope rancher.password=three\n"+
" password: three\n",
Equals,
"sudo ros config export | grep password",
)
// And then add a service.yml file example.
} }