2
0

deploy.go 22 KB

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