deploy.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. var local bool
  55. var localPath string
  56. var tag string
  57. func init() {
  58. rootCmd.AddCommand(deployCmd)
  59. deployCmd.PersistentFlags().StringVar(
  60. &app,
  61. "app",
  62. "",
  63. "Application in the Porter dashboard",
  64. )
  65. deployCmd.PersistentFlags().StringVar(
  66. &namespace,
  67. "namespace",
  68. "default",
  69. "Namespace of the application",
  70. )
  71. deployCmd.PersistentFlags().BoolVar(
  72. &local,
  73. "local",
  74. false,
  75. "Whether local context should be used for build",
  76. )
  77. deployCmd.PersistentFlags().StringVarP(
  78. &localPath,
  79. "path",
  80. "p",
  81. ".",
  82. "If local build, the path to the build directory",
  83. )
  84. deployCmd.PersistentFlags().StringVarP(
  85. &tag,
  86. "tag",
  87. "t",
  88. "",
  89. "If local build, the specified tag to use, if not \"latest\"",
  90. )
  91. deployCmd.AddCommand(deployGetEnvCmd)
  92. deployGetEnvCmd.PersistentFlags().StringVar(
  93. &getEnvFileDest,
  94. "file",
  95. "",
  96. "file destination for .env files",
  97. )
  98. deployCmd.AddCommand(deployBuildCmd)
  99. }
  100. func deployFull(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  101. color.New(color.FgGreen).Println("Deploying app:", app)
  102. // initialize the deploy agent
  103. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  104. ProjectID: config.Project,
  105. ClusterID: config.Cluster,
  106. Namespace: namespace,
  107. Local: local,
  108. LocalPath: localPath,
  109. OverrideTag: tag,
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. buildEnv, err := deployAgent.GetBuildEnv()
  115. if err != nil {
  116. return err
  117. }
  118. // set the environment variables in the process
  119. err = deployAgent.SetBuildEnv(buildEnv)
  120. if err != nil {
  121. return err
  122. }
  123. // build the deployment
  124. color.New(color.FgGreen).Println("Building docker image for", app)
  125. err = deployAgent.Build()
  126. if err != nil {
  127. return err
  128. }
  129. // push the deployment
  130. color.New(color.FgGreen).Println("Deploying new application for", app)
  131. err = deployAgent.Deploy()
  132. if err != nil {
  133. return err
  134. }
  135. color.New(color.FgGreen).Println("Successfully deployed", app)
  136. return nil
  137. }
  138. func deployGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  139. // initialize the deploy agent
  140. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  141. ProjectID: config.Project,
  142. ClusterID: config.Cluster,
  143. Namespace: namespace,
  144. })
  145. if err != nil {
  146. return err
  147. }
  148. buildEnv, err := deployAgent.GetBuildEnv()
  149. if err != nil {
  150. return err
  151. }
  152. // set the environment variables in the process
  153. err = deployAgent.SetBuildEnv(buildEnv)
  154. if err != nil {
  155. return err
  156. }
  157. // write the environment variables to either a file or stdout (stdout by default)
  158. return deployAgent.WriteBuildEnv(getEnvFileDest)
  159. }
  160. func deployBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  161. color.New(color.FgGreen).Println("Building app:", app)
  162. // initialize the deploy agent
  163. deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  164. ProjectID: config.Project,
  165. ClusterID: config.Cluster,
  166. Namespace: namespace,
  167. })
  168. if err != nil {
  169. return err
  170. }
  171. buildEnv, err := deployAgent.GetBuildEnv()
  172. if err != nil {
  173. return err
  174. }
  175. // set the environment variables in the process
  176. err = deployAgent.SetBuildEnv(buildEnv)
  177. if err != nil {
  178. return err
  179. }
  180. // build the deployment
  181. color.New(color.FgGreen).Println("Building docker image for", app)
  182. return deployAgent.Build()
  183. }