123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package control
- import (
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "sort"
- "strings"
- "text/template"
- log "github.com/Sirupsen/logrus"
- yaml "github.com/cloudfoundry-incubator/candiedyaml"
- "github.com/codegangsta/cli"
- "github.com/rancher/os/config"
- "github.com/rancher/os/util"
- )
- func configSubcommands() []cli.Command {
- return []cli.Command{
- {
- Name: "get",
- Usage: "get value",
- Action: configGet,
- },
- {
- Name: "set",
- Usage: "set a value",
- Action: configSet,
- },
- {
- Name: "images",
- Usage: "List Docker images for a configuration from a file",
- Action: runImages,
- Flags: []cli.Flag{
- cli.StringFlag{
- Name: "input, i",
- Usage: "File from which to read config",
- },
- },
- },
- {
- Name: "generate",
- Usage: "Generate a configuration file from a template",
- Action: runGenerate,
- HideHelp: true,
- },
- {
- Name: "export",
- Usage: "export configuration",
- Flags: []cli.Flag{
- cli.StringFlag{
- Name: "output, o",
- Usage: "File to which to save",
- },
- cli.BoolFlag{
- Name: "private, p",
- Usage: "Include the generated private keys",
- },
- cli.BoolFlag{
- Name: "full, f",
- Usage: "Export full configuration, including internal and default settings",
- },
- },
- Action: export,
- },
- {
- Name: "merge",
- Usage: "merge configuration from stdin",
- Action: merge,
- Flags: []cli.Flag{
- cli.StringFlag{
- Name: "input, i",
- Usage: "File from which to read",
- },
- },
- },
- }
- }
- func imagesFromConfig(cfg *config.CloudConfig) []string {
- imagesMap := map[string]int{}
- for _, service := range cfg.Rancher.BootstrapContainers {
- imagesMap[service.Image] = 1
- }
- for _, service := range cfg.Rancher.Autoformat {
- imagesMap[service.Image] = 1
- }
- for _, service := range cfg.Rancher.Services {
- imagesMap[service.Image] = 1
- }
- images := make([]string, len(imagesMap))
- i := 0
- for image := range imagesMap {
- images[i] = image
- i += 1
- }
- sort.Strings(images)
- return images
- }
- func runImages(c *cli.Context) error {
- configFile := c.String("input")
- cfg, err := config.ReadConfig(nil, false, configFile)
- if err != nil {
- log.WithFields(log.Fields{"err": err, "file": configFile}).Fatalf("Could not read config from file")
- }
- images := imagesFromConfig(cfg)
- fmt.Println(strings.Join(images, " "))
- return nil
- }
- func runGenerate(c *cli.Context) error {
- if err := genTpl(os.Stdin, os.Stdout); err != nil {
- log.Fatalf("Failed to generate config, err: '%s'", err)
- }
- return nil
- }
- func genTpl(in io.Reader, out io.Writer) error {
- bytes, err := ioutil.ReadAll(in)
- if err != nil {
- log.Fatal("Could not read from stdin")
- }
- tpl := template.Must(template.New("osconfig").Parse(string(bytes)))
- return tpl.Execute(out, env2map(os.Environ()))
- }
- func env2map(env []string) map[string]string {
- m := make(map[string]string, len(env))
- for _, s := range env {
- d := strings.Split(s, "=")
- m[d[0]] = d[1]
- }
- return m
- }
- func configSet(c *cli.Context) error {
- key := c.Args().Get(0)
- value := c.Args().Get(1)
- if key == "" {
- return nil
- }
- err := config.Set(key, value)
- if err != nil {
- log.Fatal(err)
- }
- return nil
- }
- func configGet(c *cli.Context) error {
- arg := c.Args().Get(0)
- if arg == "" {
- return nil
- }
- val, err := config.Get(arg)
- if err != nil {
- log.WithFields(log.Fields{"key": arg, "val": val, "err": err}).Fatal("config get: failed to retrieve value")
- }
- printYaml := false
- switch val.(type) {
- case []interface{}:
- printYaml = true
- case map[interface{}]interface{}:
- printYaml = true
- }
- if printYaml {
- bytes, err := yaml.Marshal(val)
- if err != nil {
- log.Fatal(err)
- }
- fmt.Println(string(bytes))
- } else {
- fmt.Println(val)
- }
- return nil
- }
- func merge(c *cli.Context) error {
- input := os.Stdin
- inputFile := c.String("input")
- if inputFile != "" {
- var err error
- input, err = os.Open(inputFile)
- if err != nil {
- log.Fatal(err)
- }
- defer input.Close()
- }
- bytes, err := ioutil.ReadAll(input)
- if err != nil {
- log.Fatal(err)
- }
- if err = config.Merge(bytes); err != nil {
- log.Fatal(err)
- }
- return nil
- }
- func export(c *cli.Context) error {
- content, err := config.Export(c.Bool("private"), c.Bool("full"))
- if err != nil {
- log.Fatal(err)
- }
- output := c.String("output")
- if output == "" {
- fmt.Println(content)
- } else {
- err := util.WriteFileAtomic(output, []byte(content), 0400)
- if err != nil {
- log.Fatal(err)
- }
- }
- return nil
- }
|