2015-03-15 04:27:04 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/rancherio/os/util"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func writeToFile(data interface{}, filename string) error {
|
|
|
|
content, err := yaml.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filename, content, 400)
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToDisk(data map[interface{}]interface{}) error {
|
2015-07-29 06:52:15 +00:00
|
|
|
private, config := filterDottedKeys(data, []string{
|
|
|
|
"rancher.ssh",
|
2015-09-11 09:10:50 +00:00
|
|
|
"rancher.docker.ca_key",
|
|
|
|
"rancher.docker.ca_cert",
|
|
|
|
"rancher.docker.server_key",
|
|
|
|
"rancher.docker.server_cert",
|
2015-07-29 06:52:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
err := writeToFile(config, LocalConfigFile)
|
2015-03-15 04:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeToFile(private, PrivateConfigFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfig(bytes []byte, files ...string) (map[interface{}]interface{}, error) {
|
2015-03-18 13:22:19 +00:00
|
|
|
// You can't just overlay yaml bytes on to maps, it won't merge, but instead
|
|
|
|
// just override the keys and not merge the map values.
|
|
|
|
left := make(map[interface{}]interface{})
|
2015-03-15 04:27:04 +00:00
|
|
|
for _, conf := range files {
|
|
|
|
content, err := readConfigFile(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-18 13:22:19 +00:00
|
|
|
right := make(map[interface{}]interface{})
|
|
|
|
err = yaml.Unmarshal(content, &right)
|
2015-03-15 04:27:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-18 13:22:19 +00:00
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
left = util.MapsUnion(left, right, util.Replace)
|
2015-03-15 04:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if bytes != nil && len(bytes) > 0 {
|
2015-03-18 13:22:19 +00:00
|
|
|
right := make(map[interface{}]interface{})
|
|
|
|
if err := yaml.Unmarshal(bytes, &right); err != nil {
|
2015-03-15 04:27:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-18 13:22:19 +00:00
|
|
|
|
2015-07-29 06:52:15 +00:00
|
|
|
left = util.MapsUnion(left, right, util.Replace)
|
2015-03-15 04:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 13:22:19 +00:00
|
|
|
return left, nil
|
2015-03-15 04:27:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func readConfigFile(file string) ([]byte, error) {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
content = []byte{}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, err
|
|
|
|
}
|