server.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/fatih/color"
  8. "github.com/porter-dev/porter/cli/cmd/docker"
  9. "github.com/porter-dev/porter/cli/cmd/github"
  10. "github.com/spf13/cobra"
  11. )
  12. type startOps struct {
  13. imageTag string `form:"required"`
  14. db string `form:"oneof=sqlite postgres"`
  15. driver string `form:"required"`
  16. port *int `form:"required"`
  17. }
  18. var opts = &startOps{}
  19. var serverCmd = &cobra.Command{
  20. Use: "server",
  21. Aliases: []string{"svr"},
  22. Short: "Commands to control a local Porter server",
  23. }
  24. // startCmd represents the start command
  25. var startCmd = &cobra.Command{
  26. Use: "start",
  27. Short: "Starts a Porter server instance on the host",
  28. Run: func(cmd *cobra.Command, args []string) {
  29. if getDriver() == "docker" {
  30. setDriver("docker")
  31. err := startDocker(
  32. opts.imageTag,
  33. opts.db,
  34. *opts.port,
  35. )
  36. if err != nil {
  37. red := color.New(color.FgRed)
  38. red.Println("Error running start:", err.Error())
  39. red.Println("Shutting down...")
  40. err = stopDocker()
  41. if err != nil {
  42. red.Println("Shutdown unsuccessful:", err.Error())
  43. }
  44. os.Exit(1)
  45. }
  46. } else {
  47. setDriver("local")
  48. err := startLocal(
  49. opts.db,
  50. *opts.port,
  51. )
  52. if err != nil {
  53. red := color.New(color.FgRed)
  54. red.Println("Error running start:", err.Error())
  55. os.Exit(1)
  56. }
  57. }
  58. },
  59. }
  60. var stopCmd = &cobra.Command{
  61. Use: "stop",
  62. Short: "Stops a Porter instance running on the Docker engine",
  63. Run: func(cmd *cobra.Command, args []string) {
  64. if getDriver() == "docker" {
  65. if err := stopDocker(); err != nil {
  66. color.New(color.FgRed).Println("Shutdown unsuccessful:", err.Error())
  67. os.Exit(1)
  68. }
  69. }
  70. },
  71. }
  72. func init() {
  73. rootCmd.AddCommand(serverCmd)
  74. serverCmd.AddCommand(startCmd)
  75. serverCmd.AddCommand(stopCmd)
  76. startCmd.PersistentFlags().StringVar(
  77. &opts.db,
  78. "db",
  79. "sqlite",
  80. "the db to use, one of sqlite or postgres",
  81. )
  82. startCmd.PersistentFlags().StringVar(
  83. &opts.driver,
  84. "driver",
  85. "local",
  86. "the driver to use, one of \"local\" or \"docker\"",
  87. )
  88. startCmd.PersistentFlags().StringVar(
  89. &opts.imageTag,
  90. "image-tag",
  91. "latest",
  92. "the Porter image tag to use (if using docker driver)",
  93. )
  94. opts.port = startCmd.PersistentFlags().IntP(
  95. "port",
  96. "p",
  97. 8080,
  98. "the host port to run the server on",
  99. )
  100. }
  101. func startDocker(
  102. imageTag string,
  103. db string,
  104. port int,
  105. ) error {
  106. env := []string{
  107. "NODE_ENV=production",
  108. "FULLSTORY_ORG_ID=VXNSS",
  109. }
  110. var porterDB docker.PorterDB
  111. switch db {
  112. case "postgres":
  113. porterDB = docker.Postgres
  114. case "sqlite":
  115. porterDB = docker.SQLite
  116. }
  117. startOpts := &docker.PorterStartOpts{
  118. ProcessID: "main",
  119. ServerImageTag: imageTag,
  120. ServerPort: port,
  121. DB: porterDB,
  122. Env: env,
  123. }
  124. _, _, err := docker.StartPorter(startOpts)
  125. if err != nil {
  126. return err
  127. }
  128. green := color.New(color.FgGreen)
  129. green.Printf("Server ready: listening on localhost:%d\n", port)
  130. return setHost(fmt.Sprintf("http://localhost:%d", port))
  131. }
  132. func startLocal(
  133. db string,
  134. port int,
  135. ) error {
  136. if db == "postgres" {
  137. return fmt.Errorf("postgres not available for local driver, run \"porter server start --db postgres --driver docker\"")
  138. }
  139. setHost(fmt.Sprintf("http://localhost:%d", port))
  140. porterDir := filepath.Join(home, ".porter")
  141. cmdPath := filepath.Join(home, ".porter", "portersvr")
  142. sqlLitePath := filepath.Join(home, ".porter", "porter.db")
  143. staticFilePath := filepath.Join(home, ".porter", "static")
  144. if _, err := os.Stat(cmdPath); os.IsNotExist(err) {
  145. err := downloadLatestReleases(porterDir)
  146. if err != nil {
  147. color.New(color.FgRed).Println("Failed:", err.Error())
  148. os.Exit(1)
  149. }
  150. }
  151. cmdPorter := exec.Command(cmdPath)
  152. cmdPorter.Env = os.Environ()
  153. cmdPorter.Env = append(cmdPorter.Env, []string{
  154. "IS_LOCAL=true",
  155. "SQL_LITE=true",
  156. "SQL_LITE_PATH=" + sqlLitePath,
  157. "STATIC_FILE_PATH=" + staticFilePath,
  158. }...)
  159. cmdPorter.Stdout = os.Stdout
  160. cmdPorter.Stderr = os.Stderr
  161. err := cmdPorter.Run()
  162. if err != nil {
  163. color.New(color.FgRed).Println("Failed:", err.Error())
  164. os.Exit(1)
  165. }
  166. return nil
  167. }
  168. func stopDocker() error {
  169. agent, err := docker.NewAgentFromEnv()
  170. if err != nil {
  171. return err
  172. }
  173. err = agent.StopPorterContainersWithProcessID("main", false)
  174. if err != nil {
  175. return err
  176. }
  177. green := color.New(color.FgGreen)
  178. green.Println("Successfully stopped the Porter server.")
  179. return nil
  180. }
  181. func downloadLatestReleases(porterDir string) error {
  182. z := &github.ZIPReleaseGetter{
  183. AssetName: "portersvr",
  184. AssetFolderDest: porterDir,
  185. ZipFolderDest: porterDir,
  186. ZipName: "portersvr_latest.zip",
  187. EntityID: "porter-dev",
  188. RepoName: "porter",
  189. IsPlatformDependent: true,
  190. }
  191. err := z.GetLatestRelease()
  192. if err != nil {
  193. return err
  194. }
  195. zStatic := &github.ZIPReleaseGetter{
  196. AssetName: "static",
  197. AssetFolderDest: filepath.Join(porterDir, "static"),
  198. ZipFolderDest: porterDir,
  199. ZipName: "static_latest.zip",
  200. EntityID: "porter-dev",
  201. RepoName: "porter",
  202. IsPlatformDependent: false,
  203. }
  204. return zStatic.GetLatestRelease()
  205. }