config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package control
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "sort"
  8. "strings"
  9. "text/template"
  10. log "github.com/Sirupsen/logrus"
  11. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  12. "github.com/codegangsta/cli"
  13. "github.com/rancher/os/config"
  14. "github.com/rancher/os/util"
  15. )
  16. func configSubcommands() []cli.Command {
  17. return []cli.Command{
  18. {
  19. Name: "get",
  20. Usage: "get value",
  21. Action: configGet,
  22. },
  23. {
  24. Name: "set",
  25. Usage: "set a value",
  26. Action: configSet,
  27. },
  28. {
  29. Name: "images",
  30. Usage: "List Docker images for a configuration from a file",
  31. Action: runImages,
  32. Flags: []cli.Flag{
  33. cli.StringFlag{
  34. Name: "input, i",
  35. Usage: "File from which to read config",
  36. },
  37. },
  38. },
  39. {
  40. Name: "generate",
  41. Usage: "Generate a configuration file from a template",
  42. Action: runGenerate,
  43. HideHelp: true,
  44. },
  45. {
  46. Name: "export",
  47. Usage: "export configuration",
  48. Flags: []cli.Flag{
  49. cli.StringFlag{
  50. Name: "output, o",
  51. Usage: "File to which to save",
  52. },
  53. cli.BoolFlag{
  54. Name: "private, p",
  55. Usage: "Include the generated private keys",
  56. },
  57. cli.BoolFlag{
  58. Name: "full, f",
  59. Usage: "Export full configuration, including internal and default settings",
  60. },
  61. },
  62. Action: export,
  63. },
  64. {
  65. Name: "merge",
  66. Usage: "merge configuration from stdin",
  67. Action: merge,
  68. Flags: []cli.Flag{
  69. cli.StringFlag{
  70. Name: "input, i",
  71. Usage: "File from which to read",
  72. },
  73. },
  74. },
  75. }
  76. }
  77. func imagesFromConfig(cfg *config.CloudConfig) []string {
  78. imagesMap := map[string]int{}
  79. for _, service := range cfg.Rancher.BootstrapContainers {
  80. imagesMap[service.Image] = 1
  81. }
  82. for _, service := range cfg.Rancher.Autoformat {
  83. imagesMap[service.Image] = 1
  84. }
  85. for _, service := range cfg.Rancher.Services {
  86. imagesMap[service.Image] = 1
  87. }
  88. images := make([]string, len(imagesMap))
  89. i := 0
  90. for image := range imagesMap {
  91. images[i] = image
  92. i += 1
  93. }
  94. sort.Strings(images)
  95. return images
  96. }
  97. func runImages(c *cli.Context) error {
  98. configFile := c.String("input")
  99. cfg, err := config.ReadConfig(nil, false, configFile)
  100. if err != nil {
  101. log.WithFields(log.Fields{"err": err, "file": configFile}).Fatalf("Could not read config from file")
  102. }
  103. images := imagesFromConfig(cfg)
  104. fmt.Println(strings.Join(images, " "))
  105. return nil
  106. }
  107. func runGenerate(c *cli.Context) error {
  108. if err := genTpl(os.Stdin, os.Stdout); err != nil {
  109. log.Fatalf("Failed to generate config, err: '%s'", err)
  110. }
  111. return nil
  112. }
  113. func genTpl(in io.Reader, out io.Writer) error {
  114. bytes, err := ioutil.ReadAll(in)
  115. if err != nil {
  116. log.Fatal("Could not read from stdin")
  117. }
  118. tpl := template.Must(template.New("osconfig").Parse(string(bytes)))
  119. return tpl.Execute(out, env2map(os.Environ()))
  120. }
  121. func env2map(env []string) map[string]string {
  122. m := make(map[string]string, len(env))
  123. for _, s := range env {
  124. d := strings.Split(s, "=")
  125. m[d[0]] = d[1]
  126. }
  127. return m
  128. }
  129. func configSet(c *cli.Context) error {
  130. key := c.Args().Get(0)
  131. value := c.Args().Get(1)
  132. if key == "" {
  133. return nil
  134. }
  135. err := config.Set(key, value)
  136. if err != nil {
  137. log.Fatal(err)
  138. }
  139. return nil
  140. }
  141. func configGet(c *cli.Context) error {
  142. arg := c.Args().Get(0)
  143. if arg == "" {
  144. return nil
  145. }
  146. val, err := config.Get(arg)
  147. if err != nil {
  148. log.WithFields(log.Fields{"key": arg, "val": val, "err": err}).Fatal("config get: failed to retrieve value")
  149. }
  150. printYaml := false
  151. switch val.(type) {
  152. case []interface{}:
  153. printYaml = true
  154. case map[interface{}]interface{}:
  155. printYaml = true
  156. }
  157. if printYaml {
  158. bytes, err := yaml.Marshal(val)
  159. if err != nil {
  160. log.Fatal(err)
  161. }
  162. fmt.Println(string(bytes))
  163. } else {
  164. fmt.Println(val)
  165. }
  166. return nil
  167. }
  168. func merge(c *cli.Context) error {
  169. input := os.Stdin
  170. inputFile := c.String("input")
  171. if inputFile != "" {
  172. var err error
  173. input, err = os.Open(inputFile)
  174. if err != nil {
  175. log.Fatal(err)
  176. }
  177. defer input.Close()
  178. }
  179. bytes, err := ioutil.ReadAll(input)
  180. if err != nil {
  181. log.Fatal(err)
  182. }
  183. if err = config.Merge(bytes); err != nil {
  184. log.Fatal(err)
  185. }
  186. return nil
  187. }
  188. func export(c *cli.Context) error {
  189. content, err := config.Export(c.Bool("private"), c.Bool("full"))
  190. if err != nil {
  191. log.Fatal(err)
  192. }
  193. output := c.String("output")
  194. if output == "" {
  195. fmt.Println(content)
  196. } else {
  197. err := util.WriteFileAtomic(output, []byte(content), 0400)
  198. if err != nil {
  199. log.Fatal(err)
  200. }
  201. }
  202. return nil
  203. }