server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/porter-dev/porter/cli/cmd/github"
  7. "github.com/fatih/color"
  8. "github.com/porter-dev/porter/cli/cmd/docker"
  9. "github.com/spf13/cobra"
  10. )
  11. type startOps struct {
  12. imageTag string `form:"required"`
  13. db string `form:"oneof=sqlite postgres"`
  14. port *int `form:"required"`
  15. }
  16. var opts = &startOps{}
  17. var serverCmd = &cobra.Command{
  18. Use: "server",
  19. Short: "Commands to control a local Porter server",
  20. }
  21. var testCmd = &cobra.Command{
  22. Use: "test",
  23. Short: "Testing",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. porterDir := filepath.Join(home, ".porter")
  26. err := github.DownloadLatestServerRelease(porterDir)
  27. if err != nil {
  28. color.New(color.FgRed).Println("Failed:", err.Error())
  29. os.Exit(1)
  30. }
  31. },
  32. }
  33. // startCmd represents the start command
  34. var startCmd = &cobra.Command{
  35. Use: "start",
  36. Short: "Starts a Porter instance using the Docker engine",
  37. Run: func(cmd *cobra.Command, args []string) {
  38. err := start(
  39. opts.imageTag,
  40. opts.db,
  41. *opts.port,
  42. )
  43. if err != nil {
  44. red := color.New(color.FgRed)
  45. red.Println("Error running start:", err.Error())
  46. red.Println("Shutting down...")
  47. err = stop()
  48. if err != nil {
  49. red.Println("Shutdown unsuccessful:", err.Error())
  50. }
  51. os.Exit(1)
  52. }
  53. },
  54. }
  55. var stopCmd = &cobra.Command{
  56. Use: "stop",
  57. Short: "Stops a Porter instance running on the Docker engine",
  58. Run: func(cmd *cobra.Command, args []string) {
  59. if err := stop(); err != nil {
  60. color.New(color.FgRed).Println("Shutdown unsuccessful:", err.Error())
  61. os.Exit(1)
  62. }
  63. },
  64. }
  65. func init() {
  66. rootCmd.AddCommand(testCmd)
  67. rootCmd.AddCommand(serverCmd)
  68. serverCmd.AddCommand(startCmd)
  69. serverCmd.AddCommand(stopCmd)
  70. startCmd.PersistentFlags().StringVar(
  71. &opts.db,
  72. "db",
  73. "sqlite",
  74. "the db to use, one of sqlite or postgres",
  75. )
  76. startCmd.PersistentFlags().StringVar(
  77. &opts.imageTag,
  78. "image-tag",
  79. "latest",
  80. "the Porter image tag to use",
  81. )
  82. opts.port = startCmd.PersistentFlags().IntP(
  83. "port",
  84. "p",
  85. 8080,
  86. "the host port to run the server on",
  87. )
  88. }
  89. func start(
  90. imageTag string,
  91. db string,
  92. port int,
  93. ) error {
  94. env := []string{
  95. "NODE_ENV=production",
  96. "FULLSTORY_ORG_ID=VXNSS",
  97. }
  98. var porterDB docker.PorterDB
  99. switch db {
  100. case "postgres":
  101. porterDB = docker.Postgres
  102. case "sqlite":
  103. porterDB = docker.SQLite
  104. }
  105. startOpts := &docker.PorterStartOpts{
  106. ProcessID: "main",
  107. ServerImageTag: imageTag,
  108. ServerPort: port,
  109. DB: porterDB,
  110. Env: env,
  111. }
  112. _, _, err := docker.StartPorter(startOpts)
  113. if err != nil {
  114. return err
  115. }
  116. green := color.New(color.FgGreen)
  117. green.Printf("Server ready: listening on localhost:%d\n", port)
  118. return setHost(fmt.Sprintf("http://localhost:%d", port))
  119. }
  120. func stop() error {
  121. agent, err := docker.NewAgentFromEnv()
  122. if err != nil {
  123. return err
  124. }
  125. err = agent.StopPorterContainersWithProcessID("main", false)
  126. if err != nil {
  127. return err
  128. }
  129. green := color.New(color.FgGreen)
  130. green.Println("Successfully stopped the Porter server.")
  131. return nil
  132. }