mirror of
https://github.com/rancher/os.git
synced 2025-06-26 23:06:51 +00:00
Implement rancherctl config set
This commit is contained in:
parent
31201786cd
commit
d26cf6485c
@ -20,6 +20,11 @@ func configSubcommands() []cli.Command {
|
|||||||
Usage: "get value",
|
Usage: "get value",
|
||||||
Action: configGet,
|
Action: configGet,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "set",
|
||||||
|
Usage: "set a value",
|
||||||
|
Action: configSet,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "import",
|
Name: "import",
|
||||||
Usage: "list values",
|
Usage: "list values",
|
||||||
@ -42,7 +47,7 @@ func configSubcommands() []cli.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func configGet(c *cli.Context) {
|
func getConfigData() (map[interface{}]interface{}, error) {
|
||||||
cfg, err := config.LoadConfig()
|
cfg, err := config.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -54,17 +59,64 @@ func configGet(c *cli.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data := make(map[interface{}]interface{})
|
data := make(map[interface{}]interface{})
|
||||||
yaml.Unmarshal([]byte(content), data)
|
err = yaml.Unmarshal([]byte(content), data)
|
||||||
|
|
||||||
arg := c.Args().Get(0)
|
return data, err
|
||||||
if arg == "" {
|
}
|
||||||
fmt.Println("")
|
|
||||||
|
func configSet(c *cli.Context) {
|
||||||
|
key := c.Args().Get(0)
|
||||||
|
value := c.Args().Get(1)
|
||||||
|
if key == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(arg, ".")
|
data, err := getConfigData()
|
||||||
|
getOrSetVal(key, data, value)
|
||||||
|
|
||||||
val := lookupVal(parts, data)
|
bytes, err := yaml.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var newConfig config.Config
|
||||||
|
err = yaml.Unmarshal(bytes, &newConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := config.LoadConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
reboot, err := cfg.Merge(newConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cfg.Save()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if reboot {
|
||||||
|
fmt.Println("Reboot needed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func configGet(c *cli.Context) {
|
||||||
|
arg := c.Args().Get(0)
|
||||||
|
if arg == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := getConfigData()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
val := getOrSetVal(arg, data, nil)
|
||||||
|
|
||||||
printYaml := false
|
printYaml := false
|
||||||
switch val.(type) {
|
switch val.(type) {
|
||||||
@ -85,14 +137,27 @@ func configGet(c *cli.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookupVal(parts []string, data map[interface{}]interface{}) interface{} {
|
func getOrSetVal(args string, data map[interface{}]interface{}, value interface{}) interface{} {
|
||||||
|
parts := strings.Split(args, ".")
|
||||||
|
|
||||||
for i, part := range parts {
|
for i, part := range parts {
|
||||||
val, ok := data[part]
|
val, ok := data[part]
|
||||||
|
last := i+1 == len(parts)
|
||||||
|
|
||||||
|
if last && value != nil {
|
||||||
|
if s, ok := value.(string); ok {
|
||||||
|
value = config.DummyMarshall(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
data[part] = value
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if i+1 == len(parts) {
|
if last {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +26,10 @@ const (
|
|||||||
DEBUG = false
|
DEBUG = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ConfigFile = "/var/lib/rancher/rancher.yml"
|
||||||
|
)
|
||||||
|
|
||||||
type InitFunc func(*Config) error
|
type InitFunc func(*Config) error
|
||||||
|
|
||||||
type ContainerConfig struct {
|
type ContainerConfig struct {
|
||||||
@ -40,7 +44,7 @@ type Config struct {
|
|||||||
//UserContainers []ContainerConfig `yaml:"userContainser,omitempty"`
|
//UserContainers []ContainerConfig `yaml:"userContainser,omitempty"`
|
||||||
Debug bool `yaml:"debug,omitempty"`
|
Debug bool `yaml:"debug,omitempty"`
|
||||||
Disable []string `yaml:"disable,omitempty"`
|
Disable []string `yaml:"disable,omitempty"`
|
||||||
Dns []string `yaml:"dns,omitempty"`
|
Dns []string `yaml:"dns,flow,omitempty"`
|
||||||
Rescue bool `yaml:"rescue,omitempty"`
|
Rescue bool `yaml:"rescue,omitempty"`
|
||||||
RescueContainer *ContainerConfig `yaml:"rescue_container,omitempty"`
|
RescueContainer *ContainerConfig `yaml:"rescue_container,omitempty"`
|
||||||
State ConfigState `yaml:"state,omitempty"`
|
State ConfigState `yaml:"state,omitempty"`
|
||||||
@ -55,6 +59,18 @@ type ConfigState struct {
|
|||||||
Required bool `yaml:"required"`
|
Required bool `yaml:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) Merge(newConfig Config) (bool, error) {
|
||||||
|
//Efficient? Nope, but computers are fast
|
||||||
|
newConfig.ClearReadOnly()
|
||||||
|
content, err := newConfig.Dump()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal([]byte(content), c)
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) ClearReadOnly() {
|
func (c *Config) ClearReadOnly() {
|
||||||
c.SystemContainers = []ContainerConfig{}
|
c.SystemContainers = []ContainerConfig{}
|
||||||
c.RescueContainer = nil
|
c.RescueContainer = nil
|
||||||
@ -69,6 +85,16 @@ func (c *Config) Dump() (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Config) Save() error {
|
||||||
|
c.ClearReadOnly()
|
||||||
|
content, err := c.Dump()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ioutil.WriteFile(ConfigFile, []byte(content), 400)
|
||||||
|
}
|
||||||
|
|
||||||
func LoadConfig() (*Config, error) {
|
func LoadConfig() (*Config, error) {
|
||||||
cfg := NewConfig()
|
cfg := NewConfig()
|
||||||
if err := cfg.Reload(); err != nil {
|
if err := cfg.Reload(); err != nil {
|
||||||
@ -105,6 +131,21 @@ func (c *Config) merge(values map[string]interface{}) error {
|
|||||||
return yaml.Unmarshal(override, c)
|
return yaml.Unmarshal(override, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) readFile() error {
|
||||||
|
content, err := ioutil.ReadFile(ConfigFile)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
err = yaml.Unmarshal(content, data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.merge(data)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Config) readCmdline() error {
|
func (c *Config) readCmdline() error {
|
||||||
log.Debug("Reading config cmdline")
|
log.Debug("Reading config cmdline")
|
||||||
cmdLine, err := ioutil.ReadFile("/proc/cmdline")
|
cmdLine, err := ioutil.ReadFile("/proc/cmdline")
|
||||||
@ -122,7 +163,11 @@ func (c *Config) readCmdline() error {
|
|||||||
return c.merge(cmdLineObj)
|
return c.merge(cmdLineObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dummyMarshall(value string) interface{} {
|
func DummyMarshall(value string) interface{} {
|
||||||
|
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
|
||||||
|
return strings.Split(value[1:len(value)-1], ",")
|
||||||
|
}
|
||||||
|
|
||||||
if value == "true" {
|
if value == "true" {
|
||||||
return true
|
return true
|
||||||
} else if value == "false" {
|
} else if value == "false" {
|
||||||
@ -160,11 +205,7 @@ outer:
|
|||||||
keys := strings.Split(kv[0], ".")[1:]
|
keys := strings.Split(kv[0], ".")[1:]
|
||||||
for i, key := range keys {
|
for i, key := range keys {
|
||||||
if i == len(keys)-1 {
|
if i == len(keys)-1 {
|
||||||
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
|
current[key] = DummyMarshall(value)
|
||||||
current[key] = strings.Split(value[1:len(value)-1], ",")
|
|
||||||
} else {
|
|
||||||
current[key] = dummyMarshall(value)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if obj, ok := current[key]; ok {
|
if obj, ok := current[key]; ok {
|
||||||
if newCurrent, ok := obj.(map[string]interface{}); ok {
|
if newCurrent, ok := obj.(map[string]interface{}); ok {
|
||||||
@ -187,6 +228,7 @@ outer:
|
|||||||
|
|
||||||
func (c *Config) Reload() error {
|
func (c *Config) Reload() error {
|
||||||
return util.ShortCircuit(
|
return util.ShortCircuit(
|
||||||
|
c.readFile,
|
||||||
c.readCmdline,
|
c.readCmdline,
|
||||||
c.readArgs,
|
c.readArgs,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user