config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package control
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "sort"
  8. "strings"
  9. "text/template"
  10. yaml "github.com/cloudfoundry-incubator/candiedyaml"
  11. "github.com/rancher/os/log"
  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. Name: "validate",
  77. Usage: "validate configuration from stdin",
  78. Action: validate,
  79. Flags: []cli.Flag{
  80. cli.StringFlag{
  81. Name: "input, i",
  82. Usage: "File from which to read",
  83. },
  84. },
  85. },
  86. }
  87. }
  88. func imagesFromConfig(cfg *config.CloudConfig) []string {
  89. imagesMap := map[string]int{}
  90. for _, service := range cfg.Rancher.BootstrapContainers {
  91. imagesMap[service.Image] = 1
  92. }
  93. for _, service := range cfg.Rancher.Services {
  94. imagesMap[service.Image] = 1
  95. }
  96. images := make([]string, len(imagesMap))
  97. i := 0
  98. for image := range imagesMap {
  99. images[i] = image
  100. i++
  101. }
  102. sort.Strings(images)
  103. return images
  104. }
  105. func runImages(c *cli.Context) error {
  106. configFile := c.String("input")
  107. cfg, err := config.ReadConfig(nil, false, configFile)
  108. if err != nil {
  109. log.WithFields(log.Fields{"err": err, "file": configFile}).Fatalf("Could not read config from file")
  110. }
  111. images := imagesFromConfig(cfg)
  112. fmt.Println(strings.Join(images, " "))
  113. return nil
  114. }
  115. func runGenerate(c *cli.Context) error {
  116. if err := genTpl(os.Stdin, os.Stdout); err != nil {
  117. log.Fatalf("Failed to generate config, err: '%s'", err)
  118. }
  119. return nil
  120. }
  121. func genTpl(in io.Reader, out io.Writer) error {
  122. bytes, err := ioutil.ReadAll(in)
  123. if err != nil {
  124. log.Fatal("Could not read from stdin")
  125. }
  126. tpl := template.Must(template.New("osconfig").Parse(string(bytes)))
  127. return tpl.Execute(out, env2map(os.Environ()))
  128. }
  129. func env2map(env []string) map[string]string {
  130. m := make(map[string]string, len(env))
  131. for _, s := range env {
  132. d := strings.Split(s, "=")
  133. m[d[0]] = d[1]
  134. }
  135. return m
  136. }
  137. func configSet(c *cli.Context) error {
  138. if c.NArg() < 2 {
  139. return nil
  140. }
  141. key := c.Args().Get(0)
  142. value := c.Args().Get(1)
  143. if key == "" {
  144. return nil
  145. }
  146. err := config.Set(key, value)
  147. if err != nil {
  148. log.Fatal(err)
  149. }
  150. return nil
  151. }
  152. func configGet(c *cli.Context) error {
  153. arg := c.Args().Get(0)
  154. if arg == "" {
  155. return nil
  156. }
  157. val, err := config.Get(arg)
  158. if err != nil {
  159. log.WithFields(log.Fields{"key": arg, "val": val, "err": err}).Fatal("config get: failed to retrieve value")
  160. }
  161. printYaml := false
  162. switch val.(type) {
  163. case []interface{}:
  164. printYaml = true
  165. case map[interface{}]interface{}:
  166. printYaml = true
  167. }
  168. if printYaml {
  169. bytes, err := yaml.Marshal(val)
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. fmt.Println(string(bytes))
  174. } else {
  175. fmt.Println(val)
  176. }
  177. return nil
  178. }
  179. func merge(c *cli.Context) error {
  180. bytes, err := inputBytes(c)
  181. if err != nil {
  182. log.Fatal(err)
  183. }
  184. if err = config.Merge(bytes); err != nil {
  185. log.Fatal(err)
  186. }
  187. return nil
  188. }
  189. func export(c *cli.Context) error {
  190. content, err := config.Export(c.Bool("private"), c.Bool("full"))
  191. if err != nil {
  192. log.Fatal(err)
  193. }
  194. output := c.String("output")
  195. if output == "" {
  196. fmt.Println(content)
  197. } else {
  198. err := util.WriteFileAtomic(output, []byte(content), 0400)
  199. if err != nil {
  200. log.Fatal(err)
  201. }
  202. }
  203. return nil
  204. }
  205. func validate(c *cli.Context) error {
  206. bytes, err := inputBytes(c)
  207. if err != nil {
  208. log.Fatal(err)
  209. }
  210. validationErrors, err := config.Validate(bytes)
  211. if err != nil {
  212. log.Fatal(err)
  213. }
  214. for _, validationError := range validationErrors.Errors() {
  215. log.Error(validationError)
  216. }
  217. return nil
  218. }
  219. func inputBytes(c *cli.Context) ([]byte, error) {
  220. input := os.Stdin
  221. inputFile := c.String("input")
  222. if inputFile != "" {
  223. var err error
  224. input, err = os.Open(inputFile)
  225. if err != nil {
  226. return nil, err
  227. }
  228. defer input.Close()
  229. }
  230. return ioutil.ReadAll(input)
  231. }