Exemple #1
0
func (c *CloudConfig) Save() error {
	files := append([]string{OsConfigFile}, CloudConfigDirFiles()...)
	files = util.FilterStrings(files, func(x string) bool { return x != CloudConfigPrivateFile })
	exCfg, err := ChainCfgFuncs(nil,
		func(_ *CloudConfig) (*CloudConfig, error) {
			return ReadConfig(nil, true, files...)
		},
		readCmdline,
		amendNils)
	if err != nil {
		return err
	}
	exData := map[interface{}]interface{}{}
	if err := util.Convert(exCfg, &exData); err != nil {
		return err
	}

	data := map[interface{}]interface{}{}
	if err := util.Convert(c, &data); err != nil {
		return err
	}

	data = util.MapsDifference(data, exData)
	log.WithFields(log.Fields{"diff": data}).Debug("The diff we're about to save")
	if err := saveToDisk(data); err != nil {
		return err
	}
	return nil
}
Exemple #2
0
func (c *CloudConfig) merge(values map[interface{}]interface{}) error {
	t := &CloudConfig{}
	if err := util.Convert(values, t); err != nil {
		return err
	}
	return util.Convert(values, c)
}
Exemple #3
0
func TestUserDocker(t *testing.T) {
	assert := require.New(t)

	config := &CloudConfig{
		Rancher: RancherConfig{
			UserDocker: DockerConfig{
				TLS: true,
			},
		},
	}

	bytes, err := yaml.Marshal(config)
	assert.Nil(err)

	config = NewConfig()
	err = yaml.Unmarshal(bytes, config)
	assert.Nil(err)

	data := make(map[interface{}]map[interface{}]interface{})
	util.Convert(config, data)

	fmt.Println(data)

	val, ok := data["rancher"]["user_docker"]
	assert.True(ok)

	m, ok := val.(map[interface{}]interface{})
	assert.True(ok)
	v, ok := m["tls"]
	assert.True(ok)
	assert.True(v.(bool))

}
Exemple #4
0
func (c *CloudConfig) Merge(values map[interface{}]interface{}) (*CloudConfig, error) {
	t := *c
	if err := util.Convert(values, &t); err != nil {
		return c, err
	}
	return &t, nil
}
Exemple #5
0
func (c *CloudConfig) Get(key string) (interface{}, error) {
	data := make(map[interface{}]interface{})
	err := util.Convert(c, &data)
	if err != nil {
		return nil, err
	}

	return getOrSetVal(key, data, nil), nil
}
Exemple #6
0
func (c *CloudConfig) Get(key string) (interface{}, error) {
	data := map[interface{}]interface{}{}
	if err := util.Convert(c, &data); err != nil {
		return nil, err
	}

	v, _ := getOrSetVal(key, data, nil)
	return v, nil
}
Exemple #7
0
func (c *CloudConfig) Set(key string, value interface{}) (*CloudConfig, error) {
	data := map[interface{}]interface{}{}
	if err := util.Convert(c, &data); err != nil {
		return c, err
	}

	_, data = getOrSetVal(key, data, value)

	return c.Merge(data)
}
Exemple #8
0
func (c *CloudConfig) Import(bytes []byte) (*CloudConfig, error) {
	data, err := readConfig(bytes, false, CloudConfigPrivateFile)
	if err != nil {
		return c, err
	}

	cfg := NewConfig()
	if err := util.Convert(data, cfg); err != nil {
		return c, err
	}

	return cfg, nil
}
Exemple #9
0
func (c *CloudConfig) Set(key string, value interface{}) error {
	data, err := readConfig(nil, LocalConfigFile, PrivateConfigFile)
	if err != nil {
		return err
	}

	getOrSetVal(key, data, value)

	cfg := NewConfig()

	if err := util.Convert(data, cfg); err != nil {
		return err
	}

	if err := saveToDisk(data); err != nil {
		return err
	}

	return c.Reload()
}
Exemple #10
0
func TestUserDocker(t *testing.T) {
	config := &Config{
		UserDocker: DockerConfig{
			TLS: true,
		},
	}

	bytes, err := yaml.Marshal(config)
	if err != nil {
		log.Fatal(err)
	}

	config = NewConfig()
	err = yaml.Unmarshal(bytes, config)
	if err != nil {
		log.Fatal(err)
	}

	data := make(map[interface{}]interface{})
	util.Convert(config, data)

	fmt.Println(data)

	val, ok := data["user_docker"]
	if !ok {
		t.Fatal("Failed to find user_docker")
	}

	if m, ok := val.(map[interface{}]interface{}); ok {
		if v, ok := m["tls"]; ok {
			if v != true {
				t.Fatal("user_docker.tls is not true")
			}
		} else {
			t.Fatal("user_docker.tls is not found")
		}
	} else {
		t.Fatal("Bad data")
	}

}
Exemple #11
0
func (c *Config) Set(key string, value interface{}) error {
	data, err := readSavedConfig(nil)
	if err != nil {
		return err
	}

	getOrSetVal(key, data, value)

	cfg := NewConfig()

	if err := util.Convert(data, cfg); err != nil {
		return err
	}

	err = saveToDisk(data)
	if err != nil {
		return err
	}

	return c.Reload()
}
Exemple #12
0
func saveToDisk(data map[interface{}]interface{}) error {
	config := make(map[interface{}]interface{})
	private := make(map[interface{}]interface{})

	for k, v := range data {
		if k == "ssh" {
			private[k] = v
		} else if k == "user_docker" {
			var userDockerConfig DockerConfig
			var userDockerConfigPrivate DockerConfig
			err := util.Convert(v, &userDockerConfig)
			if err != nil {
				return err
			}

			userDockerConfigPrivate.CAKey = userDockerConfig.CAKey
			userDockerConfigPrivate.CACert = userDockerConfig.CACert
			userDockerConfigPrivate.ServerKey = userDockerConfig.ServerKey
			userDockerConfigPrivate.ServerCert = userDockerConfig.ServerCert

			userDockerConfig.CAKey = ""
			userDockerConfig.CACert = ""
			userDockerConfig.ServerKey = ""
			userDockerConfig.ServerCert = ""

			config[k] = userDockerConfig
			private[k] = userDockerConfigPrivate
		} else {
			config[k] = v
		}
	}

	err := writeToFile(config, ConfigFile)
	if err != nil {
		return err
	}

	return writeToFile(private, PrivateConfigFile)
}
Exemple #13
0
func (c *CloudConfig) merge(values map[interface{}]interface{}) error {
	return util.Convert(values, c)
}
Exemple #14
0
func (c *Config) overlay(newConfig Config) error {
	newConfig.clearReadOnly()
	return util.Convert(&newConfig, c)
}
Exemple #15
0
func (c *Config) merge(values map[interface{}]interface{}) error {
	values = clearReadOnly(values)
	return util.Convert(values, c)
}