service.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package control
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/codegangsta/cli"
  7. "github.com/docker/libcompose/cli/command"
  8. dockerApp "github.com/docker/libcompose/cli/docker/app"
  9. "github.com/docker/libcompose/project"
  10. "github.com/rancher/os/compose"
  11. "github.com/rancher/os/config"
  12. "github.com/rancher/os/util/network"
  13. )
  14. type projectFactory struct {
  15. }
  16. func (p *projectFactory) Create(c *cli.Context) (project.APIProject, error) {
  17. cfg := config.LoadConfig()
  18. return compose.GetProject(cfg, true, false)
  19. }
  20. func beforeApp(c *cli.Context) error {
  21. if c.GlobalBool("verbose") {
  22. logrus.SetLevel(logrus.DebugLevel)
  23. }
  24. return nil
  25. }
  26. func serviceCommand() cli.Command {
  27. factory := &projectFactory{}
  28. app := cli.Command{}
  29. app.Name = "service"
  30. app.ShortName = "s"
  31. app.Usage = "Command line interface for services and compose."
  32. app.Before = beforeApp
  33. app.Flags = append(command.CommonFlags(), dockerApp.DockerClientFlags()...)
  34. app.Subcommands = append(serviceSubCommands(),
  35. command.BuildCommand(factory),
  36. command.CreateCommand(factory),
  37. command.UpCommand(factory),
  38. command.StartCommand(factory),
  39. command.LogsCommand(factory),
  40. command.RestartCommand(factory),
  41. command.StopCommand(factory),
  42. command.ScaleCommand(factory),
  43. command.RmCommand(factory),
  44. command.PullCommand(factory),
  45. command.KillCommand(factory),
  46. command.PortCommand(factory),
  47. command.PsCommand(factory),
  48. )
  49. return app
  50. }
  51. func serviceSubCommands() []cli.Command {
  52. return []cli.Command{
  53. {
  54. Name: "enable",
  55. Usage: "turn on an service",
  56. Action: enable,
  57. },
  58. {
  59. Name: "disable",
  60. Usage: "turn off an service",
  61. Action: disable,
  62. },
  63. {
  64. Name: "list",
  65. Usage: "list services and state",
  66. Action: list,
  67. },
  68. {
  69. Name: "delete",
  70. Usage: "delete a service",
  71. Action: del,
  72. },
  73. }
  74. }
  75. func updateIncludedServices(cfg *config.CloudConfig) error {
  76. return config.Set("rancher.services_include", cfg.Rancher.ServicesInclude)
  77. }
  78. func disable(c *cli.Context) error {
  79. changed := false
  80. cfg := config.LoadConfig()
  81. for _, service := range c.Args() {
  82. if _, ok := cfg.Rancher.ServicesInclude[service]; !ok {
  83. continue
  84. }
  85. cfg.Rancher.ServicesInclude[service] = false
  86. changed = true
  87. }
  88. if changed {
  89. if err := updateIncludedServices(cfg); err != nil {
  90. logrus.Fatal(err)
  91. }
  92. }
  93. return nil
  94. }
  95. func del(c *cli.Context) error {
  96. changed := false
  97. cfg := config.LoadConfig()
  98. for _, service := range c.Args() {
  99. if _, ok := cfg.Rancher.ServicesInclude[service]; !ok {
  100. continue
  101. }
  102. delete(cfg.Rancher.ServicesInclude, service)
  103. changed = true
  104. }
  105. if changed {
  106. if err := updateIncludedServices(cfg); err != nil {
  107. logrus.Fatal(err)
  108. }
  109. }
  110. return nil
  111. }
  112. func enable(c *cli.Context) error {
  113. cfg := config.LoadConfig()
  114. var enabledServices []string
  115. for _, service := range c.Args() {
  116. if val, ok := cfg.Rancher.ServicesInclude[service]; !ok || !val {
  117. if strings.HasPrefix(service, "/") && !strings.HasPrefix(service, "/var/lib/rancher/conf") {
  118. logrus.Fatalf("ERROR: Service should be in path /var/lib/rancher/conf")
  119. }
  120. cfg.Rancher.ServicesInclude[service] = true
  121. enabledServices = append(enabledServices, service)
  122. }
  123. }
  124. if len(enabledServices) > 0 {
  125. if err := compose.StageServices(cfg, enabledServices...); err != nil {
  126. logrus.Fatal(err)
  127. }
  128. if err := updateIncludedServices(cfg); err != nil {
  129. logrus.Fatal(err)
  130. }
  131. }
  132. return nil
  133. }
  134. func list(c *cli.Context) error {
  135. cfg := config.LoadConfig()
  136. clone := make(map[string]bool)
  137. for service, enabled := range cfg.Rancher.ServicesInclude {
  138. clone[service] = enabled
  139. }
  140. services, err := network.GetServices(cfg.Rancher.Repositories.ToArray())
  141. if err != nil {
  142. logrus.Fatalf("Failed to get services: %v", err)
  143. }
  144. for _, service := range services {
  145. if enabled, ok := clone[service]; ok {
  146. delete(clone, service)
  147. if enabled {
  148. fmt.Printf("enabled %s\n", service)
  149. } else {
  150. fmt.Printf("disabled %s\n", service)
  151. }
  152. } else {
  153. fmt.Printf("disabled %s\n", service)
  154. }
  155. }
  156. for service, enabled := range clone {
  157. if enabled {
  158. fmt.Printf("enabled %s\n", service)
  159. } else {
  160. fmt.Printf("disabled %s\n", service)
  161. }
  162. }
  163. return nil
  164. }