service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package control
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "github.com/codegangsta/cli"
  7. "github.com/rancherio/os/config"
  8. "github.com/rancherio/os/docker"
  9. "github.com/rancherio/os/util"
  10. )
  11. func serviceSubCommands() []cli.Command {
  12. return []cli.Command{
  13. {
  14. Name: "enable",
  15. Usage: "turn on an service",
  16. Action: enable,
  17. },
  18. {
  19. Name: "disable",
  20. Usage: "turn off an service",
  21. Action: disable,
  22. },
  23. {
  24. Name: "list",
  25. Usage: "list services and state",
  26. Action: list,
  27. },
  28. {
  29. Name: "delete",
  30. Usage: "delete a service",
  31. Action: del,
  32. },
  33. }
  34. }
  35. func disable(c *cli.Context) {
  36. changed := false
  37. cfg, err := config.LoadConfig()
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. for _, service := range c.Args() {
  42. if _, ok := cfg.ServicesInclude[service]; !ok {
  43. continue
  44. }
  45. cfg.ServicesInclude[service] = false
  46. changed = true
  47. }
  48. if changed {
  49. if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
  50. log.Fatal(err)
  51. }
  52. }
  53. }
  54. func del(c *cli.Context) {
  55. changed := false
  56. cfg, err := config.LoadConfig()
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. for _, service := range c.Args() {
  61. if _, ok := cfg.ServicesInclude[service]; !ok {
  62. continue
  63. }
  64. delete(cfg.ServicesInclude, service)
  65. changed = true
  66. }
  67. if changed {
  68. if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
  69. log.Fatal(err)
  70. }
  71. }
  72. }
  73. func enable(c *cli.Context) {
  74. changed := false
  75. cfg, err := config.LoadConfig()
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. for _, service := range c.Args() {
  80. if val, ok := cfg.ServicesInclude[service]; !ok || !val {
  81. if strings.HasPrefix(service, "/") && !strings.HasPrefix(service, "/var/lib/rancher/conf") {
  82. log.Fatalf("ERROR: Service should be in path /var/lib/rancher/conf")
  83. }
  84. if _, err := docker.LoadServiceResource(service, true, cfg); err != nil {
  85. log.Fatalf("could not load service %s", service)
  86. }
  87. cfg.ServicesInclude[service] = true
  88. changed = true
  89. }
  90. }
  91. if changed {
  92. if err = cfg.Set("services_include", cfg.ServicesInclude); err != nil {
  93. log.Fatal(err)
  94. }
  95. }
  96. }
  97. func list(c *cli.Context) {
  98. cfg, err := config.LoadConfig()
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. clone := make(map[string]bool)
  103. for service, enabled := range cfg.ServicesInclude {
  104. clone[service] = enabled
  105. }
  106. services, err := util.GetServices(cfg.Repositories.ToArray())
  107. if err != nil {
  108. log.Fatalf("Failed to get services: %v", err)
  109. }
  110. for _, service := range services {
  111. if enabled, ok := clone[service]; ok {
  112. delete(clone, service)
  113. if enabled {
  114. fmt.Printf("enabled %s\n", service)
  115. } else {
  116. fmt.Printf("disabled %s\n", service)
  117. }
  118. } else {
  119. fmt.Printf("disabled %s\n", service)
  120. }
  121. }
  122. for service, enabled := range clone {
  123. if enabled {
  124. fmt.Printf("enabled %s\n", service)
  125. } else {
  126. fmt.Printf("disabled %s\n", service)
  127. }
  128. }
  129. }