server.go 2.5 KB

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