config.go 5.5 KB

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