deploy.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. // updateCmd represents the "porter update" base command when called
  11. // without any subcommands
  12. var updateCmd = &cobra.Command{
  13. Use: "update",
  14. Short: "Builds and updates a specified application given by the --app flag.",
  15. Long: fmt.Sprintf(`
  16. %s
  17. Builds and updates a specified application given by the --app flag. For example:
  18. %s
  19. This command will automatically build from a local path. The path can be configured via the
  20. --path flag. You can also overwrite the tag using the --tag flag. For example, to build from the
  21. local directory ~/path-to-dir with the tag "testing":
  22. %s
  23. If the application has a remote Git repository source configured, you can specify that the remote
  24. Git repository should be used to build the new image by specifying "--source github". Porter will use
  25. the latest commit from the remote repo and branch to update an application, and will use the latest
  26. commit as the image tag.
  27. %s
  28. To add new configuration or update existing configuration, you can pass a values.yaml file in via the
  29. --values flag. For example;
  30. %s
  31. If your application is set up to use a Dockerfile by default, you can use a buildpack via the flag
  32. "--method pack". Conversely, if your application is set up to use a buildpack by default, you can
  33. use a Dockerfile by passing the flag "--method docker". You can specify the relative path to a Dockerfile
  34. in your remote Git repository. For example, if a Dockerfile is found at ./docker/prod.Dockerfile, you can
  35. specify it as follows:
  36. %s
  37. `,
  38. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update\":"),
  39. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app"),
  40. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --path ~/path-to-dir --tag testing"),
  41. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app remote-git-app --source github"),
  42. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --values my-values.yaml"),
  43. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --method docker --dockerfile ./docker/prod.Dockerfile"),
  44. ),
  45. Run: func(cmd *cobra.Command, args []string) {
  46. err := checkLoginAndRun(args, updateFull)
  47. if err != nil {
  48. os.Exit(1)
  49. }
  50. },
  51. }
  52. var updateGetEnvCmd = &cobra.Command{
  53. Use: "get-env",
  54. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  55. Long: fmt.Sprintf(`
  56. %s
  57. Gets environment variables for a deployment for a specified application given by the --app
  58. flag. By default, env variables are printed via stdout for use in downstream commands:
  59. %s
  60. Output can also be written to a file via the --file flag, which should specify the
  61. destination path for a .env file. For example:
  62. %s
  63. `,
  64. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update get-env\":"),
  65. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app | xargs"),
  66. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app --file .env"),
  67. ),
  68. Run: func(cmd *cobra.Command, args []string) {
  69. err := checkLoginAndRun(args, updateGetEnv)
  70. if err != nil {
  71. os.Exit(1)
  72. }
  73. },
  74. }
  75. var updateBuildCmd = &cobra.Command{
  76. Use: "build",
  77. Short: "Builds a new version of the application specified by the --app flag.",
  78. Long: fmt.Sprintf(`
  79. %s
  80. Builds a new version of the application specified by the --app flag. Depending on the
  81. configured settings, this command may work automatically or will require a specified
  82. --method flag.
  83. If you have configured the Dockerfile path and/or a build context for this application,
  84. this command will by default use those settings, so you just need to specify the --app
  85. flag:
  86. %s
  87. If you have not linked the build-time requirements for this application, the command will
  88. use a local build. By default, the cloud-native buildpacks builder will automatically be run
  89. from the current directory. If you would like to change the build method, you can do so by
  90. using the --method flag, for example:
  91. %s
  92. When using "--method docker", you can specify the path to the Dockerfile using the
  93. --dockerfile flag. This will also override the Dockerfile path that you may have linked
  94. for the application:
  95. %s
  96. `,
  97. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update build\":"),
  98. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app"),
  99. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker"),
  100. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker --dockerfile ./prod.Dockerfile"),
  101. ),
  102. Run: func(cmd *cobra.Command, args []string) {
  103. err := checkLoginAndRun(args, updateBuild)
  104. if err != nil {
  105. os.Exit(1)
  106. }
  107. },
  108. }
  109. var updatePushCmd = &cobra.Command{
  110. Use: "push",
  111. Short: "Pushes a new image for an application specified by the --app flag.",
  112. Long: fmt.Sprintf(`
  113. %s
  114. Pushes a new image for an application specified by the --app flag. This command uses
  115. the image repository saved in the application config by default. For example, if an
  116. application "nginx" was created from the image repo "gcr.io/snowflake-123456/nginx",
  117. the following command would push the image "gcr.io/snowflake-123456/nginx:new-tag":
  118. %s
  119. This command will not use your pre-saved authentication set up via "docker login," so if you
  120. are using an image registry that was created outside of Porter, make sure that you have
  121. linked it via "porter connect".
  122. `,
  123. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update push\":"),
  124. color.New(color.FgGreen, color.Bold).Sprintf("porter update push --app nginx --tag new-tag"),
  125. ),
  126. Run: func(cmd *cobra.Command, args []string) {
  127. err := checkLoginAndRun(args, updatePush)
  128. if err != nil {
  129. os.Exit(1)
  130. }
  131. },
  132. }
  133. var updateConfigCmd = &cobra.Command{
  134. Use: "config",
  135. Short: "Updates the configuration for an application specified by the --app flag.",
  136. Long: fmt.Sprintf(`
  137. %s
  138. Updates the configuration for an application specified by the --app flag, using the configuration
  139. given by the --values flag. This will trigger a new deployment for the application with
  140. new configuration set. Note that this will merge your existing configuration with configuration
  141. specified in the --values file. For example:
  142. %s
  143. You can update the configuration with only a new tag with the --tag flag, which will only update
  144. the image that the application uses if no --values file is specified:
  145. %s
  146. `,
  147. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update config\":"),
  148. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --values my-values.yaml"),
  149. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --tag custom-tag"),
  150. ),
  151. Run: func(cmd *cobra.Command, args []string) {
  152. err := checkLoginAndRun(args, updateUpgrade)
  153. if err != nil {
  154. os.Exit(1)
  155. }
  156. },
  157. }
  158. var app string
  159. var getEnvFileDest string
  160. var localPath string
  161. var tag string
  162. var dockerfile string
  163. var method string
  164. func init() {
  165. rootCmd.AddCommand(updateCmd)
  166. updateCmd.PersistentFlags().StringVar(
  167. &app,
  168. "app",
  169. "",
  170. "Application in the Porter dashboard",
  171. )
  172. updateCmd.MarkPersistentFlagRequired("app")
  173. updateCmd.PersistentFlags().StringVar(
  174. &namespace,
  175. "namespace",
  176. "default",
  177. "Namespace of the application",
  178. )
  179. updateCmd.PersistentFlags().StringVar(
  180. &source,
  181. "source",
  182. "local",
  183. "the type of source (\"local\" or \"github\")",
  184. )
  185. updateCmd.PersistentFlags().StringVarP(
  186. &localPath,
  187. "path",
  188. "p",
  189. ".",
  190. "If local build, the path to the build directory. If remote build, the relative path from the repository root to the build directory.",
  191. )
  192. updateCmd.PersistentFlags().StringVarP(
  193. &tag,
  194. "tag",
  195. "t",
  196. "",
  197. "the specified tag to use, if not \"latest\"",
  198. )
  199. updateCmd.PersistentFlags().StringVarP(
  200. &values,
  201. "values",
  202. "v",
  203. "",
  204. "Filepath to a values.yaml file",
  205. )
  206. updateCmd.PersistentFlags().StringVar(
  207. &dockerfile,
  208. "dockerfile",
  209. "",
  210. "the path to the dockerfile",
  211. )
  212. updateCmd.PersistentFlags().StringVar(
  213. &method,
  214. "method",
  215. "",
  216. "the build method to use (\"docker\" or \"pack\")",
  217. )
  218. updateCmd.AddCommand(updateGetEnvCmd)
  219. updateGetEnvCmd.PersistentFlags().StringVar(
  220. &getEnvFileDest,
  221. "file",
  222. "",
  223. "file destination for .env files",
  224. )
  225. updateCmd.AddCommand(updateBuildCmd)
  226. updateCmd.AddCommand(updatePushCmd)
  227. updateCmd.AddCommand(updateConfigCmd)
  228. }
  229. func updateFull(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  230. color.New(color.FgGreen).Println("Deploying app:", app)
  231. updateAgent, err := updateGetAgent(client)
  232. if err != nil {
  233. return err
  234. }
  235. err = updateBuildWithAgent(updateAgent)
  236. if err != nil {
  237. return err
  238. }
  239. err = updatePushWithAgent(updateAgent)
  240. if err != nil {
  241. return err
  242. }
  243. err = updateUpgradeWithAgent(updateAgent)
  244. if err != nil {
  245. return err
  246. }
  247. return nil
  248. }
  249. func updateGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  250. updateAgent, err := updateGetAgent(client)
  251. if err != nil {
  252. return err
  253. }
  254. buildEnv, err := updateAgent.GetBuildEnv()
  255. if err != nil {
  256. return err
  257. }
  258. // set the environment variables in the process
  259. err = updateAgent.SetBuildEnv(buildEnv)
  260. if err != nil {
  261. return err
  262. }
  263. // write the environment variables to either a file or stdout (stdout by default)
  264. return updateAgent.WriteBuildEnv(getEnvFileDest)
  265. }
  266. func updateBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  267. updateAgent, err := updateGetAgent(client)
  268. if err != nil {
  269. return err
  270. }
  271. return updateBuildWithAgent(updateAgent)
  272. }
  273. func updatePush(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  274. updateAgent, err := updateGetAgent(client)
  275. if err != nil {
  276. return err
  277. }
  278. return updatePushWithAgent(updateAgent)
  279. }
  280. func updateUpgrade(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
  281. updateAgent, err := updateGetAgent(client)
  282. if err != nil {
  283. return err
  284. }
  285. return updateUpgradeWithAgent(updateAgent)
  286. }
  287. // HELPER METHODS
  288. func updateGetAgent(client *api.Client) (*deploy.DeployAgent, error) {
  289. var buildMethod deploy.DeployBuildType
  290. if method != "" {
  291. buildMethod = deploy.DeployBuildType(method)
  292. }
  293. // initialize the update agent
  294. return deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  295. SharedOpts: &deploy.SharedOpts{
  296. ProjectID: config.Project,
  297. ClusterID: config.Cluster,
  298. Namespace: namespace,
  299. LocalPath: localPath,
  300. LocalDockerfile: dockerfile,
  301. OverrideTag: tag,
  302. Method: buildMethod,
  303. },
  304. Local: source != "github",
  305. })
  306. }
  307. func updateBuildWithAgent(updateAgent *deploy.DeployAgent) error {
  308. // build the deployment
  309. color.New(color.FgGreen).Println("Building docker image for", app)
  310. buildEnv, err := updateAgent.GetBuildEnv()
  311. if err != nil {
  312. return err
  313. }
  314. // set the environment variables in the process
  315. err = updateAgent.SetBuildEnv(buildEnv)
  316. if err != nil {
  317. return err
  318. }
  319. return updateAgent.Build()
  320. }
  321. func updatePushWithAgent(updateAgent *deploy.DeployAgent) error {
  322. // push the deployment
  323. color.New(color.FgGreen).Println("Pushing new image for", app)
  324. return updateAgent.Push()
  325. }
  326. func updateUpgradeWithAgent(updateAgent *deploy.DeployAgent) error {
  327. // push the deployment
  328. color.New(color.FgGreen).Println("Calling webhook for", app)
  329. // read the values if necessary
  330. valuesObj, err := readValuesFile()
  331. if err != nil {
  332. return err
  333. }
  334. err = updateAgent.UpdateImageAndValues(valuesObj)
  335. if err != nil {
  336. return err
  337. }
  338. color.New(color.FgGreen).Println("Successfully updated", app)
  339. return nil
  340. }