deploy.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/config"
  11. "github.com/porter-dev/porter/cli/cmd/deploy"
  12. "github.com/porter-dev/porter/cli/cmd/utils"
  13. templaterUtils "github.com/porter-dev/porter/internal/templater/utils"
  14. "github.com/spf13/cobra"
  15. "k8s.io/client-go/util/homedir"
  16. )
  17. // updateCmd represents the "porter update" base command when called
  18. // without any subcommands
  19. var updateCmd = &cobra.Command{
  20. Use: "update",
  21. Short: "Builds and updates a specified application given by the --app flag.",
  22. Long: fmt.Sprintf(`
  23. %s
  24. Builds and updates a specified application given by the --app flag. For example:
  25. %s
  26. This command will automatically build from a local path. The path can be configured via the
  27. --path flag. You can also overwrite the tag using the --tag flag. For example, to build from the
  28. local directory ~/path-to-dir with the tag "testing":
  29. %s
  30. If the application has a remote Git repository source configured, you can specify that the remote
  31. Git repository should be used to build the new image by specifying "--source github". Porter will use
  32. the latest commit from the remote repo and branch to update an application, and will use the latest
  33. commit as the image tag.
  34. %s
  35. To add new configuration or update existing configuration, you can pass a values.yaml file in via the
  36. --values flag. For example;
  37. %s
  38. If your application is set up to use a Dockerfile by default, you can use a buildpack via the flag
  39. "--method pack". Conversely, if your application is set up to use a buildpack by default, you can
  40. use a Dockerfile by passing the flag "--method docker". You can specify the relative path to a Dockerfile
  41. in your remote Git repository. For example, if a Dockerfile is found at ./docker/prod.Dockerfile, you can
  42. specify it as follows:
  43. %s
  44. `,
  45. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update\":"),
  46. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app"),
  47. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --path ~/path-to-dir --tag testing"),
  48. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app remote-git-app --source github"),
  49. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --values my-values.yaml"),
  50. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --method docker --dockerfile ./docker/prod.Dockerfile"),
  51. ),
  52. Run: func(cmd *cobra.Command, args []string) {
  53. err := checkLoginAndRun(args, updateFull)
  54. if err != nil {
  55. os.Exit(1)
  56. }
  57. },
  58. }
  59. var updateGetEnvCmd = &cobra.Command{
  60. Use: "get-env",
  61. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  62. Long: fmt.Sprintf(`
  63. %s
  64. Gets environment variables for a deployment for a specified application given by the --app
  65. flag. By default, env variables are printed via stdout for use in downstream commands:
  66. %s
  67. Output can also be written to a file via the --file flag, which should specify the
  68. destination path for a .env file. For example:
  69. %s
  70. `,
  71. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update get-env\":"),
  72. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app | xargs"),
  73. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app --file .env"),
  74. ),
  75. Run: func(cmd *cobra.Command, args []string) {
  76. err := checkLoginAndRun(args, updateGetEnv)
  77. if err != nil {
  78. os.Exit(1)
  79. }
  80. },
  81. }
  82. var updateBuildCmd = &cobra.Command{
  83. Use: "build",
  84. Short: "Builds a new version of the application specified by the --app flag.",
  85. Long: fmt.Sprintf(`
  86. %s
  87. Builds a new version of the application specified by the --app flag. Depending on the
  88. configured settings, this command may work automatically or will require a specified
  89. --method flag.
  90. If you have configured the Dockerfile path and/or a build context for this application,
  91. this command will by default use those settings, so you just need to specify the --app
  92. flag:
  93. %s
  94. If you have not linked the build-time requirements for this application, the command will
  95. use a local build. By default, the cloud-native buildpacks builder will automatically be run
  96. from the current directory. If you would like to change the build method, you can do so by
  97. using the --method flag, for example:
  98. %s
  99. When using "--method docker", you can specify the path to the Dockerfile using the
  100. --dockerfile flag. This will also override the Dockerfile path that you may have linked
  101. for the application:
  102. %s
  103. `,
  104. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update build\":"),
  105. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app"),
  106. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker"),
  107. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker --dockerfile ./prod.Dockerfile"),
  108. ),
  109. Run: func(cmd *cobra.Command, args []string) {
  110. err := checkLoginAndRun(args, updateBuild)
  111. if err != nil {
  112. os.Exit(1)
  113. }
  114. },
  115. }
  116. var updatePushCmd = &cobra.Command{
  117. Use: "push",
  118. Short: "Pushes a new image for an application specified by the --app flag.",
  119. Long: fmt.Sprintf(`
  120. %s
  121. Pushes a new image for an application specified by the --app flag. This command uses
  122. the image repository saved in the application config by default. For example, if an
  123. application "nginx" was created from the image repo "gcr.io/snowflake-123456/nginx",
  124. the following command would push the image "gcr.io/snowflake-123456/nginx:new-tag":
  125. %s
  126. This command will not use your pre-saved authentication set up via "docker login," so if you
  127. are using an image registry that was created outside of Porter, make sure that you have
  128. linked it via "porter connect".
  129. `,
  130. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update push\":"),
  131. color.New(color.FgGreen, color.Bold).Sprintf("porter update push --app nginx --tag new-tag"),
  132. ),
  133. Run: func(cmd *cobra.Command, args []string) {
  134. err := checkLoginAndRun(args, updatePush)
  135. if err != nil {
  136. os.Exit(1)
  137. }
  138. },
  139. }
  140. var updateConfigCmd = &cobra.Command{
  141. Use: "config",
  142. Short: "Updates the configuration for an application specified by the --app flag.",
  143. Long: fmt.Sprintf(`
  144. %s
  145. Updates the configuration for an application specified by the --app flag, using the configuration
  146. given by the --values flag. This will trigger a new deployment for the application with
  147. new configuration set. Note that this will merge your existing configuration with configuration
  148. specified in the --values file. For example:
  149. %s
  150. You can update the configuration with only a new tag with the --tag flag, which will only update
  151. the image that the application uses if no --values file is specified:
  152. %s
  153. `,
  154. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update config\":"),
  155. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --values my-values.yaml"),
  156. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --tag custom-tag"),
  157. ),
  158. Run: func(cmd *cobra.Command, args []string) {
  159. err := checkLoginAndRun(args, updateUpgrade)
  160. if err != nil {
  161. os.Exit(1)
  162. }
  163. },
  164. }
  165. var app string
  166. var getEnvFileDest string
  167. var localPath string
  168. var tag string
  169. var dockerfile string
  170. var method string
  171. var stream bool
  172. var buildFlagsEnv []string
  173. var forcePush bool
  174. var useCache bool
  175. func init() {
  176. buildFlagsEnv = []string{}
  177. rootCmd.AddCommand(updateCmd)
  178. updateCmd.PersistentFlags().StringVar(
  179. &app,
  180. "app",
  181. "",
  182. "Application in the Porter dashboard",
  183. )
  184. updateCmd.MarkPersistentFlagRequired("app")
  185. updateCmd.PersistentFlags().BoolVar(
  186. &useCache,
  187. "use-cache",
  188. false,
  189. "Whether to use cache (currently in beta)",
  190. )
  191. updateCmd.PersistentFlags().StringVar(
  192. &namespace,
  193. "namespace",
  194. "default",
  195. "Namespace of the application",
  196. )
  197. updateCmd.PersistentFlags().StringVar(
  198. &source,
  199. "source",
  200. "local",
  201. "the type of source (\"local\" or \"github\")",
  202. )
  203. updateCmd.PersistentFlags().StringVarP(
  204. &localPath,
  205. "path",
  206. "p",
  207. "",
  208. "If local build, the path to the build directory. If remote build, the relative path from the repository root to the build directory.",
  209. )
  210. updateCmd.PersistentFlags().StringVarP(
  211. &tag,
  212. "tag",
  213. "t",
  214. "",
  215. "the specified tag to use, if not \"latest\"",
  216. )
  217. updateCmd.PersistentFlags().StringVarP(
  218. &values,
  219. "values",
  220. "v",
  221. "",
  222. "Filepath to a values.yaml file",
  223. )
  224. updateCmd.PersistentFlags().StringVar(
  225. &dockerfile,
  226. "dockerfile",
  227. "",
  228. "the path to the dockerfile",
  229. )
  230. updateCmd.PersistentFlags().StringArrayVarP(
  231. &buildFlagsEnv,
  232. "env",
  233. "e",
  234. []string{},
  235. "Build-time environment variable, in the form 'VAR=VALUE'. These are not available at image runtime.",
  236. )
  237. updateCmd.PersistentFlags().StringVar(
  238. &method,
  239. "method",
  240. "",
  241. "the build method to use (\"docker\" or \"pack\")",
  242. )
  243. updateCmd.PersistentFlags().BoolVar(
  244. &stream,
  245. "stream",
  246. false,
  247. "stream update logs to porter dashboard",
  248. )
  249. updateCmd.PersistentFlags().BoolVar(
  250. &forceBuild,
  251. "force-build",
  252. false,
  253. "set this to force build an image (images tagged with \"latest\" have this set by default)",
  254. )
  255. updateCmd.PersistentFlags().BoolVar(
  256. &forcePush,
  257. "force-push",
  258. false,
  259. "set this to force push an image (images tagged with \"latest\" have this set by default)",
  260. )
  261. updateCmd.PersistentFlags().MarkDeprecated("force-build", "--force-build is now deprecated")
  262. updateCmd.PersistentFlags().MarkDeprecated("force-push", "--force-push is now deprecated")
  263. updateCmd.AddCommand(updateGetEnvCmd)
  264. updateGetEnvCmd.PersistentFlags().StringVar(
  265. &getEnvFileDest,
  266. "file",
  267. "",
  268. "file destination for .env files",
  269. )
  270. updateCmd.AddCommand(updateBuildCmd)
  271. updateCmd.AddCommand(updatePushCmd)
  272. updateCmd.AddCommand(updateConfigCmd)
  273. }
  274. func updateFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  275. fullPath, err := filepath.Abs(localPath)
  276. if err != nil {
  277. return err
  278. }
  279. if os.Getenv("GITHUB_ACTIONS") == "" && source == "local" && fullPath == homedir.HomeDir() {
  280. proceed, err := utils.PromptConfirm("You are deploying your home directory. Do you want to continue?", false)
  281. if err != nil {
  282. return err
  283. }
  284. if !proceed {
  285. return nil
  286. }
  287. }
  288. color.New(color.FgGreen).Println("Deploying app:", app)
  289. updateAgent, err := updateGetAgent(client)
  290. if err != nil {
  291. return err
  292. }
  293. err = updateBuildWithAgent(updateAgent)
  294. if err != nil {
  295. return err
  296. }
  297. err = updatePushWithAgent(updateAgent)
  298. if err != nil {
  299. return err
  300. }
  301. err = updateUpgradeWithAgent(updateAgent)
  302. if err != nil {
  303. return err
  304. }
  305. return nil
  306. }
  307. func updateGetEnv(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  308. updateAgent, err := updateGetAgent(client)
  309. if err != nil {
  310. return err
  311. }
  312. buildEnv, err := updateAgent.GetBuildEnv(&deploy.GetBuildEnvOpts{
  313. UseNewConfig: false,
  314. })
  315. if err != nil {
  316. return err
  317. }
  318. // set the environment variables in the process
  319. err = updateAgent.SetBuildEnv(buildEnv)
  320. if err != nil {
  321. return err
  322. }
  323. // write the environment variables to either a file or stdout (stdout by default)
  324. return updateAgent.WriteBuildEnv(getEnvFileDest)
  325. }
  326. func updateBuild(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  327. updateAgent, err := updateGetAgent(client)
  328. if err != nil {
  329. return err
  330. }
  331. return updateBuildWithAgent(updateAgent)
  332. }
  333. func updatePush(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  334. updateAgent, err := updateGetAgent(client)
  335. if err != nil {
  336. return err
  337. }
  338. return updatePushWithAgent(updateAgent)
  339. }
  340. func updateUpgrade(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  341. updateAgent, err := updateGetAgent(client)
  342. if err != nil {
  343. return err
  344. }
  345. return updateUpgradeWithAgent(updateAgent)
  346. }
  347. // HELPER METHODS
  348. func updateGetAgent(client *api.Client) (*deploy.DeployAgent, error) {
  349. var buildMethod deploy.DeployBuildType
  350. if method != "" {
  351. buildMethod = deploy.DeployBuildType(method)
  352. }
  353. // add additional env, if they exist
  354. additionalEnv := make(map[string]string)
  355. for _, buildEnv := range buildFlagsEnv {
  356. if strSplArr := strings.SplitN(buildEnv, "=", 2); len(strSplArr) >= 2 {
  357. additionalEnv[strSplArr[0]] = strSplArr[1]
  358. }
  359. }
  360. // initialize the update agent
  361. return deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
  362. SharedOpts: &deploy.SharedOpts{
  363. ProjectID: cliConf.Project,
  364. ClusterID: cliConf.Cluster,
  365. Namespace: namespace,
  366. LocalPath: localPath,
  367. LocalDockerfile: dockerfile,
  368. OverrideTag: tag,
  369. Method: buildMethod,
  370. AdditionalEnv: additionalEnv,
  371. UseCache: useCache,
  372. },
  373. Local: source != "github",
  374. })
  375. }
  376. func updateBuildWithAgent(updateAgent *deploy.DeployAgent) error {
  377. // build the deployment
  378. color.New(color.FgGreen).Println("Building docker image for", app)
  379. if stream {
  380. updateAgent.StreamEvent(types.SubEvent{
  381. EventID: "build",
  382. Name: "Build",
  383. Index: 100,
  384. Status: types.EventStatusInProgress,
  385. Info: "",
  386. })
  387. }
  388. if useCache {
  389. err := config.SetDockerConfig(updateAgent.Client)
  390. if err != nil {
  391. return err
  392. }
  393. }
  394. // read the values if necessary
  395. valuesObj, err := readValuesFile()
  396. if err != nil {
  397. return err
  398. }
  399. buildEnv, err := updateAgent.GetBuildEnv(&deploy.GetBuildEnvOpts{
  400. UseNewConfig: true,
  401. NewConfig: valuesObj,
  402. })
  403. if err != nil {
  404. if stream {
  405. // another concern: is it safe to ignore the error here?
  406. updateAgent.StreamEvent(types.SubEvent{
  407. EventID: "build",
  408. Name: "Build",
  409. Index: 110,
  410. Status: types.EventStatusFailed,
  411. Info: err.Error(),
  412. })
  413. }
  414. return err
  415. }
  416. // set the environment variables in the process
  417. err = updateAgent.SetBuildEnv(buildEnv)
  418. if err != nil {
  419. if stream {
  420. updateAgent.StreamEvent(types.SubEvent{
  421. EventID: "build",
  422. Name: "Build",
  423. Index: 120,
  424. Status: types.EventStatusFailed,
  425. Info: err.Error(),
  426. })
  427. }
  428. return err
  429. }
  430. if err := updateAgent.Build(nil); err != nil {
  431. if stream {
  432. updateAgent.StreamEvent(types.SubEvent{
  433. EventID: "build",
  434. Name: "Build",
  435. Index: 130,
  436. Status: types.EventStatusFailed,
  437. Info: err.Error(),
  438. })
  439. }
  440. return err
  441. }
  442. if stream {
  443. updateAgent.StreamEvent(types.SubEvent{
  444. EventID: "build",
  445. Name: "Build",
  446. Index: 140,
  447. Status: types.EventStatusSuccess,
  448. Info: "",
  449. })
  450. }
  451. return nil
  452. }
  453. func updatePushWithAgent(updateAgent *deploy.DeployAgent) error {
  454. if useCache {
  455. color.New(color.FgGreen).Println("Skipping image push for", app, "as use-cache is set")
  456. return nil
  457. }
  458. // push the deployment
  459. color.New(color.FgGreen).Println("Pushing new image for", app)
  460. if stream {
  461. updateAgent.StreamEvent(types.SubEvent{
  462. EventID: "push",
  463. Name: "Push",
  464. Index: 200,
  465. Status: types.EventStatusInProgress,
  466. Info: "",
  467. })
  468. }
  469. if err := updateAgent.Push(); err != nil {
  470. if stream {
  471. updateAgent.StreamEvent(types.SubEvent{
  472. EventID: "push",
  473. Name: "Push",
  474. Index: 210,
  475. Status: types.EventStatusFailed,
  476. Info: err.Error(),
  477. })
  478. }
  479. return err
  480. }
  481. if stream {
  482. updateAgent.StreamEvent(types.SubEvent{
  483. EventID: "push",
  484. Name: "Push",
  485. Index: 220,
  486. Status: types.EventStatusSuccess,
  487. Info: "",
  488. })
  489. }
  490. return nil
  491. }
  492. func updateUpgradeWithAgent(updateAgent *deploy.DeployAgent) error {
  493. // push the deployment
  494. color.New(color.FgGreen).Println("Upgrading configuration for", app)
  495. if stream {
  496. updateAgent.StreamEvent(types.SubEvent{
  497. EventID: "upgrade",
  498. Name: "Upgrade",
  499. Index: 300,
  500. Status: types.EventStatusInProgress,
  501. Info: "",
  502. })
  503. }
  504. var err error
  505. // read the values if necessary
  506. valuesObj, err := readValuesFile()
  507. if err != nil {
  508. return err
  509. }
  510. if err != nil {
  511. if stream {
  512. updateAgent.StreamEvent(types.SubEvent{
  513. EventID: "upgrade",
  514. Name: "Upgrade",
  515. Index: 310,
  516. Status: types.EventStatusFailed,
  517. Info: err.Error(),
  518. })
  519. }
  520. return err
  521. }
  522. if len(updateAgent.Opts.AdditionalEnv) > 0 {
  523. syncedEnv, err := deploy.GetSyncedEnv(
  524. updateAgent.Client,
  525. updateAgent.Release.Config,
  526. updateAgent.Opts.ProjectID,
  527. updateAgent.Opts.ClusterID,
  528. updateAgent.Opts.Namespace,
  529. false,
  530. )
  531. if err != nil {
  532. return err
  533. }
  534. for k := range updateAgent.Opts.AdditionalEnv {
  535. if _, ok := syncedEnv[k]; ok {
  536. return fmt.Errorf("environment variable %s already exists as part of a synced environment group", k)
  537. }
  538. }
  539. normalEnv, err := deploy.GetNormalEnv(
  540. updateAgent.Client,
  541. updateAgent.Release.Config,
  542. updateAgent.Opts.ProjectID,
  543. updateAgent.Opts.ClusterID,
  544. updateAgent.Opts.Namespace,
  545. false,
  546. )
  547. if err != nil {
  548. return err
  549. }
  550. // add the additional environment variables to container.env.normal
  551. for k, v := range updateAgent.Opts.AdditionalEnv {
  552. normalEnv[k] = v
  553. }
  554. valuesObj = templaterUtils.CoalesceValues(valuesObj, map[string]interface{}{
  555. "container": map[string]interface{}{
  556. "env": map[string]interface{}{
  557. "normal": normalEnv,
  558. },
  559. },
  560. })
  561. }
  562. err = updateAgent.UpdateImageAndValues(valuesObj)
  563. if err != nil {
  564. if stream {
  565. updateAgent.StreamEvent(types.SubEvent{
  566. EventID: "upgrade",
  567. Name: "Upgrade",
  568. Index: 320,
  569. Status: types.EventStatusFailed,
  570. Info: err.Error(),
  571. })
  572. }
  573. return err
  574. }
  575. if stream {
  576. updateAgent.StreamEvent(types.SubEvent{
  577. EventID: "upgrade",
  578. Name: "Upgrade",
  579. Index: 330,
  580. Status: types.EventStatusSuccess,
  581. Info: "",
  582. })
  583. }
  584. color.New(color.FgGreen).Println("Successfully updated", app)
  585. return nil
  586. }