app.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package app
  2. import (
  3. "fmt"
  4. "os"
  5. "os/signal"
  6. "strings"
  7. "syscall"
  8. "github.com/rancher/os/log"
  9. "golang.org/x/net/context"
  10. "github.com/codegangsta/cli"
  11. "github.com/docker/libcompose/project"
  12. "github.com/docker/libcompose/project/options"
  13. )
  14. func ProjectPs(p project.APIProject, c *cli.Context) error {
  15. qFlag := c.Bool("q")
  16. allInfo, err := p.Ps(context.Background(), qFlag, c.Args()...)
  17. if err != nil {
  18. return cli.NewExitError(err.Error(), 1)
  19. }
  20. os.Stdout.WriteString(allInfo.String(!qFlag))
  21. return nil
  22. }
  23. func ProjectStop(p project.APIProject, c *cli.Context) error {
  24. err := p.Stop(context.Background(), c.Int("timeout"), c.Args()...)
  25. if err != nil {
  26. return cli.NewExitError(err.Error(), 1)
  27. }
  28. return nil
  29. }
  30. func ProjectDown(p project.APIProject, c *cli.Context) error {
  31. options := options.Down{
  32. RemoveVolume: c.Bool("volumes"),
  33. RemoveImages: options.ImageType(c.String("rmi")),
  34. RemoveOrphans: c.Bool("remove-orphans"),
  35. }
  36. err := p.Down(context.Background(), options, c.Args()...)
  37. if err != nil {
  38. return cli.NewExitError(err.Error(), 1)
  39. }
  40. return nil
  41. }
  42. func ProjectBuild(p project.APIProject, c *cli.Context) error {
  43. config := options.Build{
  44. NoCache: c.Bool("no-cache"),
  45. ForceRemove: c.Bool("force-rm"),
  46. Pull: c.Bool("pull"),
  47. }
  48. err := p.Build(context.Background(), config, c.Args()...)
  49. if err != nil {
  50. return cli.NewExitError(err.Error(), 1)
  51. }
  52. return nil
  53. }
  54. func ProjectCreate(p project.APIProject, c *cli.Context) error {
  55. options := options.Create{
  56. NoRecreate: c.Bool("no-recreate"),
  57. ForceRecreate: c.Bool("force-recreate"),
  58. NoBuild: c.Bool("no-build"),
  59. }
  60. err := p.Create(context.Background(), options, c.Args()...)
  61. if err != nil {
  62. return cli.NewExitError(err.Error(), 1)
  63. }
  64. return nil
  65. }
  66. func ProjectUp(p project.APIProject, c *cli.Context) error {
  67. options := options.Up{
  68. Create: options.Create{
  69. NoRecreate: c.Bool("no-recreate"),
  70. ForceRecreate: c.Bool("force-recreate"),
  71. NoBuild: c.Bool("no-build"),
  72. },
  73. }
  74. ctx, cancelFun := context.WithCancel(context.Background())
  75. err := p.Up(ctx, options, c.Args()...)
  76. if err != nil {
  77. return cli.NewExitError(err.Error(), 1)
  78. }
  79. if c.Bool("foreground") {
  80. signalChan := make(chan os.Signal, 1)
  81. cleanupDone := make(chan bool)
  82. signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
  83. errChan := make(chan error)
  84. go func() {
  85. errChan <- p.Log(ctx, true, c.Args()...)
  86. }()
  87. go func() {
  88. select {
  89. case <-signalChan:
  90. fmt.Printf("\nGracefully stopping...\n")
  91. cancelFun()
  92. ProjectStop(p, c)
  93. cleanupDone <- true
  94. case err := <-errChan:
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. cleanupDone <- true
  99. }
  100. }()
  101. <-cleanupDone
  102. return nil
  103. }
  104. return nil
  105. }
  106. func ProjectStart(p project.APIProject, c *cli.Context) error {
  107. err := p.Start(context.Background(), c.Args()...)
  108. if err != nil {
  109. return cli.NewExitError(err.Error(), 1)
  110. }
  111. return nil
  112. }
  113. func ProjectRestart(p project.APIProject, c *cli.Context) error {
  114. err := p.Restart(context.Background(), c.Int("timeout"), c.Args()...)
  115. if err != nil {
  116. return cli.NewExitError(err.Error(), 1)
  117. }
  118. return nil
  119. }
  120. func ProjectLog(p project.APIProject, c *cli.Context) error {
  121. err := p.Log(context.Background(), c.Bool("follow"), c.Args()...)
  122. if err != nil {
  123. return cli.NewExitError(err.Error(), 1)
  124. }
  125. return nil
  126. }
  127. func ProjectPull(p project.APIProject, c *cli.Context) error {
  128. err := p.Pull(context.Background(), c.Args()...)
  129. if err != nil && !c.Bool("ignore-pull-failures") {
  130. return cli.NewExitError(err.Error(), 1)
  131. }
  132. return nil
  133. }
  134. func ProjectDelete(p project.APIProject, c *cli.Context) error {
  135. options := options.Delete{
  136. RemoveVolume: c.Bool("v"),
  137. }
  138. if !c.Bool("force") {
  139. options.BeforeDeleteCallback = func(stoppedContainers []string) bool {
  140. fmt.Printf("Going to remove %v\nAre you sure? [yN]\n", strings.Join(stoppedContainers, ", "))
  141. var answer string
  142. _, err := fmt.Scanln(&answer)
  143. if err != nil {
  144. log.Error(err)
  145. return false
  146. }
  147. if answer != "y" && answer != "Y" {
  148. return false
  149. }
  150. return true
  151. }
  152. }
  153. err := p.Delete(context.Background(), options, c.Args()...)
  154. if err != nil {
  155. return cli.NewExitError(err.Error(), 1)
  156. }
  157. return nil
  158. }
  159. func ProjectKill(p project.APIProject, c *cli.Context) error {
  160. err := p.Kill(context.Background(), c.String("signal"), c.Args()...)
  161. if err != nil {
  162. return cli.NewExitError(err.Error(), 1)
  163. }
  164. return nil
  165. }