deploy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/fatih/color"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/porter-dev/porter/cli/cmd/deploy"
  8. "github.com/spf13/cobra"
  9. )
  10. // deployCmd represents the "porter deploy" base command when called
  11. // without any subcommands
  12. var deployCmd = &cobra.Command{
  13. Use: "deploy",
  14. Short: "Builds and deploys a specified application given by the --app flag.",
  15. Run: func(cmd *cobra.Command, args []string) {
  16. err := checkLoginAndRun(args, deployFull)
  17. if err != nil {
  18. os.Exit(1)
  19. }
  20. },
  21. }
  22. var deployGetEnvCmd = &cobra.Command{
  23. Use: "get-env",
  24. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  25. Long: fmt.Sprintf(`Gets environment variables for a deployment for a specified application given by the --app flag.
  26. By default, env variables are printed via stdout for use in downstream commands:
  27. %s
  28. Output can also be written to a dotenv file via the --file flag, which should specify the destination
  29. path for a .env file. For example:
  30. %s
  31. `,
  32. color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app>"),
  33. color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app> --file .env"),
  34. ),
  35. Run: func(cmd *cobra.Command, args []string) {
  36. err := checkLoginAndRun(args, deployGetEnv)
  37. if err != nil {
  38. os.Exit(1)
  39. }
  40. },
  41. }
  42. var deployBuildCmd = &cobra.Command{
  43. Use: "build",
  44. Short: "Builds a new version of the application specified by the --app flag.",
  45. Run: func(cmd *cobra.Command, args []string) {
  46. err := checkLoginAndRun(args, deployBuild)
  47. if err != nil {
  48. os.Exit(1)
  49. }
  50. },
  51. }
  52. var app string
  53. var getEnvFileDest string
  54. func init() {
  55. rootCmd.AddCommand(deployCmd)
  56. deployCmd.PersistentFlags().StringVar(
  57. &app,
  58. "app",
  59. "",
  60. "Application in the Porter dashboard",
  61. )
  62. deployCmd.PersistentFlags().StringVar(
  63. &namespace,
  64. "namespace",
  65. "default",
  66. "Namespace of the application",
  67. )
  68. deployCmd.AddCommand(deployGetEnvCmd)
  69. deployGetEnvCmd.PersistentFlags().StringVar(
  70. &getEnvFileDest,
  71. "file",
  72. "",
  73. "file destination for .env files",
  74. )
  75. deployCmd.AddCommand(deployBuildCmd)
  76. }
  77. func deployFull(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  78. color.New(color.FgGreen).Println("Deploying app:", app)
  79. // initialize the deploy agent
  80. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  81. ProjectID: config.Project,
  82. ClusterID: config.Cluster,
  83. Namespace: namespace,
  84. })
  85. if err != nil {
  86. return err
  87. }
  88. buildEnv, err := deployAgent.GetBuildEnv()
  89. if err != nil {
  90. return err
  91. }
  92. // set the environment variables in the process
  93. err = deployAgent.SetBuildEnv(buildEnv)
  94. if err != nil {
  95. return err
  96. }
  97. // build the deployment
  98. color.New(color.FgGreen).Println("Building docker image for", app)
  99. err = deployAgent.Build()
  100. if err != nil {
  101. return err
  102. }
  103. // push the deployment
  104. color.New(color.FgGreen).Println("Deploying new application for", app)
  105. err = deployAgent.Deploy()
  106. if err != nil {
  107. return err
  108. }
  109. color.New(color.FgGreen).Println("Successfully deployed", app)
  110. return nil
  111. }
  112. func deployGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  113. // initialize the deploy agent
  114. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  115. ProjectID: config.Project,
  116. ClusterID: config.Cluster,
  117. Namespace: namespace,
  118. })
  119. if err != nil {
  120. return err
  121. }
  122. buildEnv, err := deployAgent.GetBuildEnv()
  123. if err != nil {
  124. return err
  125. }
  126. // set the environment variables in the process
  127. err = deployAgent.SetBuildEnv(buildEnv)
  128. if err != nil {
  129. return err
  130. }
  131. // write the environment variables to either a file or stdout (stdout by default)
  132. return deployAgent.WriteBuildEnv(getEnvFileDest)
  133. }
  134. func deployBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  135. color.New(color.FgGreen).Println("Building app:", app)
  136. // initialize the deploy agent
  137. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  138. ProjectID: config.Project,
  139. ClusterID: config.Cluster,
  140. Namespace: namespace,
  141. })
  142. if err != nil {
  143. return err
  144. }
  145. buildEnv, err := deployAgent.GetBuildEnv()
  146. if err != nil {
  147. return err
  148. }
  149. // set the environment variables in the process
  150. err = deployAgent.SetBuildEnv(buildEnv)
  151. if err != nil {
  152. return err
  153. }
  154. // build the deployment
  155. color.New(color.FgGreen).Println("Building docker image for", app)
  156. return deployAgent.Build()
  157. }