deploy.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "sort"
  8. "strings"
  9. "time"
  10. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  11. "github.com/briandowns/spinner"
  12. "github.com/fatih/color"
  13. api "github.com/porter-dev/porter/api/client"
  14. "github.com/porter-dev/porter/api/types"
  15. "github.com/porter-dev/porter/cli/cmd/config"
  16. "github.com/porter-dev/porter/cli/cmd/deploy"
  17. "github.com/porter-dev/porter/cli/cmd/docker"
  18. "github.com/porter-dev/porter/cli/cmd/utils"
  19. templaterUtils "github.com/porter-dev/porter/internal/templater/utils"
  20. "github.com/spf13/cobra"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/client-go/util/homedir"
  23. )
  24. // updateCmd represents the "porter update" base command when called
  25. // without any subcommands
  26. var updateCmd = &cobra.Command{
  27. Use: "update",
  28. Short: "Builds and updates a specified application given by the --app flag.",
  29. Long: fmt.Sprintf(`
  30. %s
  31. Builds and updates a specified application given by the --app flag. For example:
  32. %s
  33. This command will automatically build from a local path. The path can be configured via the
  34. --path flag. You can also overwrite the tag using the --tag flag. For example, to build from the
  35. local directory ~/path-to-dir with the tag "testing":
  36. %s
  37. If the application has a remote Git repository source configured, you can specify that the remote
  38. Git repository should be used to build the new image by specifying "--source github". Porter will use
  39. the latest commit from the remote repo and branch to update an application, and will use the latest
  40. commit as the image tag.
  41. %s
  42. To add new configuration or update existing configuration, you can pass a values.yaml file in via the
  43. --values flag. For example;
  44. %s
  45. If your application is set up to use a Dockerfile by default, you can use a buildpack via the flag
  46. "--method pack". Conversely, if your application is set up to use a buildpack by default, you can
  47. use a Dockerfile by passing the flag "--method docker". You can specify the relative path to a Dockerfile
  48. in your remote Git repository. For example, if a Dockerfile is found at ./docker/prod.Dockerfile, you can
  49. specify it as follows:
  50. %s
  51. `,
  52. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update\":"),
  53. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app"),
  54. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --path ~/path-to-dir --tag testing"),
  55. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app remote-git-app --source github"),
  56. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --values my-values.yaml"),
  57. color.New(color.FgGreen, color.Bold).Sprintf("porter update --app example-app --method docker --dockerfile ./docker/prod.Dockerfile"),
  58. ),
  59. Run: func(cmd *cobra.Command, args []string) {
  60. err := checkLoginAndRun(cmd.Context(), args, updateFull)
  61. if err != nil {
  62. os.Exit(1)
  63. }
  64. },
  65. }
  66. var updateGetEnvCmd = &cobra.Command{
  67. Use: "get-env",
  68. Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
  69. Long: fmt.Sprintf(`
  70. %s
  71. Gets environment variables for a deployment for a specified application given by the --app
  72. flag. By default, env variables are printed via stdout for use in downstream commands:
  73. %s
  74. Output can also be written to a file via the --file flag, which should specify the
  75. destination path for a .env file. For example:
  76. %s
  77. `,
  78. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update get-env\":"),
  79. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app | xargs"),
  80. color.New(color.FgGreen, color.Bold).Sprintf("porter update get-env --app example-app --file .env"),
  81. ),
  82. Run: func(cmd *cobra.Command, args []string) {
  83. err := checkLoginAndRun(cmd.Context(), args, updateGetEnv)
  84. if err != nil {
  85. os.Exit(1)
  86. }
  87. },
  88. }
  89. var updateBuildCmd = &cobra.Command{
  90. Use: "build",
  91. Short: "Builds a new version of the application specified by the --app flag.",
  92. Long: fmt.Sprintf(`
  93. %s
  94. Builds a new version of the application specified by the --app flag. Depending on the
  95. configured settings, this command may work automatically or will require a specified
  96. --method flag.
  97. If you have configured the Dockerfile path and/or a build context for this application,
  98. this command will by default use those settings, so you just need to specify the --app
  99. flag:
  100. %s
  101. If you have not linked the build-time requirements for this application, the command will
  102. use a local build. By default, the cloud-native buildpacks builder will automatically be run
  103. from the current directory. If you would like to change the build method, you can do so by
  104. using the --method flag, for example:
  105. %s
  106. When using "--method docker", you can specify the path to the Dockerfile using the
  107. --dockerfile flag. This will also override the Dockerfile path that you may have linked
  108. for the application:
  109. %s
  110. `,
  111. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update build\":"),
  112. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app"),
  113. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker"),
  114. color.New(color.FgGreen, color.Bold).Sprintf("porter update build --app example-app --method docker --dockerfile ./prod.Dockerfile"),
  115. ),
  116. Run: func(cmd *cobra.Command, args []string) {
  117. err := checkLoginAndRun(cmd.Context(), args, updateBuild)
  118. if err != nil {
  119. os.Exit(1)
  120. }
  121. },
  122. }
  123. var updatePushCmd = &cobra.Command{
  124. Use: "push",
  125. Short: "Pushes an image to a Docker registry linked to your Porter project.",
  126. Args: cobra.MaximumNArgs(1),
  127. Long: fmt.Sprintf(`
  128. %s
  129. Pushes a local Docker image to a registry linked to your Porter project. This command
  130. requires the project ID to be set either by using the %s command
  131. or the --project flag. For example, to push a local nginx image:
  132. %s
  133. %s
  134. Pushes a new image for an application specified by the --app flag. This command uses
  135. the image repository saved in the application config by default. For example, if an
  136. application "nginx" was created from the image repo "gcr.io/snowflake-123456/nginx",
  137. the following command would push the image "gcr.io/snowflake-123456/nginx:new-tag":
  138. %s
  139. This command will not use your pre-saved authentication set up via "docker login," so if you
  140. are using an image registry that was created outside of Porter, make sure that you have
  141. linked it via "porter connect".
  142. `,
  143. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update push\":"),
  144. color.New(color.FgBlue).Sprintf("porter config set-project"),
  145. color.New(color.FgGreen, color.Bold).Sprintf("porter update push gcr.io/snowflake-123456/nginx:1234567"),
  146. color.New(color.Bold).Sprintf("LEGACY USAGE:"),
  147. color.New(color.FgGreen, color.Bold).Sprintf("porter update push --app nginx --tag new-tag"),
  148. ),
  149. Run: func(cmd *cobra.Command, args []string) {
  150. err := checkLoginAndRun(cmd.Context(), args, updatePush)
  151. if err != nil {
  152. os.Exit(1)
  153. }
  154. },
  155. }
  156. var updateConfigCmd = &cobra.Command{
  157. Use: "config",
  158. Short: "Updates the configuration for an application specified by the --app flag.",
  159. Long: fmt.Sprintf(`
  160. %s
  161. Updates the configuration for an application specified by the --app flag, using the configuration
  162. given by the --values flag. This will trigger a new deployment for the application with
  163. new configuration set. Note that this will merge your existing configuration with configuration
  164. specified in the --values file. For example:
  165. %s
  166. You can update the configuration with only a new tag with the --tag flag, which will only update
  167. the image that the application uses if no --values file is specified:
  168. %s
  169. `,
  170. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter update config\":"),
  171. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --values my-values.yaml"),
  172. color.New(color.FgGreen, color.Bold).Sprintf("porter update config --app example-app --tag custom-tag"),
  173. ),
  174. Run: func(cmd *cobra.Command, args []string) {
  175. err := checkLoginAndRun(cmd.Context(), args, updateUpgrade)
  176. if err != nil {
  177. os.Exit(1)
  178. }
  179. },
  180. }
  181. var updateEnvGroupCmd = &cobra.Command{
  182. Use: "env-group",
  183. Aliases: []string{"eg", "envgroup", "env-groups", "envgroups"},
  184. Short: "Updates an environment group's variables, specified by the --name flag.",
  185. Run: func(cmd *cobra.Command, args []string) {
  186. color.New(color.FgRed).Fprintln(os.Stderr, "need to specify an operation to continue")
  187. },
  188. }
  189. var updateSetEnvGroupCmd = &cobra.Command{
  190. Use: "set",
  191. Short: "Sets the desired value of an environment variable in an env group in the form VAR=VALUE.",
  192. Args: cobra.MaximumNArgs(1),
  193. Run: func(cmd *cobra.Command, args []string) {
  194. err := checkLoginAndRun(cmd.Context(), args, updateSetEnvGroup)
  195. if err != nil {
  196. os.Exit(1)
  197. }
  198. },
  199. }
  200. var updateUnsetEnvGroupCmd = &cobra.Command{
  201. Use: "unset",
  202. Short: "Removes an environment variable from an env group.",
  203. Args: cobra.MinimumNArgs(1),
  204. Run: func(cmd *cobra.Command, args []string) {
  205. err := checkLoginAndRun(cmd.Context(), args, updateUnsetEnvGroup)
  206. if err != nil {
  207. os.Exit(1)
  208. }
  209. },
  210. }
  211. var (
  212. app string
  213. getEnvFileDest string
  214. localPath string
  215. tag string
  216. dockerfile string
  217. method string
  218. stream bool
  219. buildFlagsEnv []string
  220. forcePush bool
  221. useCache bool
  222. version uint
  223. varType string
  224. normalEnvGroupVars []string
  225. secretEnvGroupVars []string
  226. waitForSuccessfulDeploy bool
  227. )
  228. func init() {
  229. buildFlagsEnv = []string{}
  230. rootCmd.AddCommand(updateCmd)
  231. updateCmd.PersistentFlags().StringVar(
  232. &app,
  233. "app",
  234. "",
  235. "Application in the Porter dashboard",
  236. )
  237. updateCmd.PersistentFlags().BoolVar(
  238. &useCache,
  239. "use-cache",
  240. false,
  241. "Whether to use cache (currently in beta)",
  242. )
  243. updateCmd.PersistentFlags().StringVar(
  244. &namespace,
  245. "namespace",
  246. "default",
  247. "Namespace of the application",
  248. )
  249. updateCmd.PersistentFlags().StringVar(
  250. &source,
  251. "source",
  252. "local",
  253. "the type of source (\"local\" or \"github\")",
  254. )
  255. updateCmd.PersistentFlags().StringVarP(
  256. &localPath,
  257. "path",
  258. "p",
  259. "",
  260. "If local build, the path to the build directory. If remote build, the relative path from the repository root to the build directory.",
  261. )
  262. updateCmd.PersistentFlags().StringVarP(
  263. &tag,
  264. "tag",
  265. "t",
  266. "",
  267. "the specified tag to use, if not \"latest\"",
  268. )
  269. updateCmd.PersistentFlags().StringVarP(
  270. &values,
  271. "values",
  272. "v",
  273. "",
  274. "Filepath to a values.yaml file",
  275. )
  276. updateCmd.PersistentFlags().StringVar(
  277. &dockerfile,
  278. "dockerfile",
  279. "",
  280. "the path to the dockerfile",
  281. )
  282. updateCmd.PersistentFlags().StringArrayVarP(
  283. &buildFlagsEnv,
  284. "env",
  285. "e",
  286. []string{},
  287. "Build-time environment variable, in the form 'VAR=VALUE'. These are not available at image runtime.",
  288. )
  289. updateCmd.PersistentFlags().StringVar(
  290. &method,
  291. "method",
  292. "",
  293. "the build method to use (\"docker\" or \"pack\")",
  294. )
  295. updateCmd.PersistentFlags().BoolVar(
  296. &stream,
  297. "stream",
  298. false,
  299. "stream update logs to porter dashboard",
  300. )
  301. updateCmd.PersistentFlags().BoolVar(
  302. &forceBuild,
  303. "force-build",
  304. false,
  305. "set this to force build an image (images tagged with \"latest\" have this set by default)",
  306. )
  307. updateCmd.PersistentFlags().BoolVar(
  308. &forcePush,
  309. "force-push",
  310. false,
  311. "set this to force push an image (images tagged with \"latest\" have this set by default)",
  312. )
  313. updateCmd.PersistentFlags().MarkDeprecated("force-build", "--force-build is now deprecated")
  314. updateCmd.PersistentFlags().MarkDeprecated("force-push", "--force-push is now deprecated")
  315. updateCmd.PersistentFlags().BoolVar(
  316. &waitForSuccessfulDeploy,
  317. "wait",
  318. false,
  319. "set this to wait and be notified when a deployment is successful, otherwise time out",
  320. )
  321. updateCmd.AddCommand(updateGetEnvCmd)
  322. updateGetEnvCmd.PersistentFlags().StringVar(
  323. &getEnvFileDest,
  324. "file",
  325. "",
  326. "file destination for .env files",
  327. )
  328. updateGetEnvCmd.MarkPersistentFlagRequired("app")
  329. updateBuildCmd.MarkPersistentFlagRequired("app")
  330. updateConfigCmd.MarkPersistentFlagRequired("app")
  331. updateEnvGroupCmd.PersistentFlags().StringVar(
  332. &name,
  333. "name",
  334. "",
  335. "the name of the environment group",
  336. )
  337. updateEnvGroupCmd.PersistentFlags().UintVar(
  338. &version,
  339. "version",
  340. 0,
  341. "the version of the environment group",
  342. )
  343. updateEnvGroupCmd.MarkPersistentFlagRequired("name")
  344. updateSetEnvGroupCmd.PersistentFlags().StringVar(
  345. &varType,
  346. "type",
  347. "normal",
  348. "the type of environment variable (either \"normal\" or \"secret\")",
  349. )
  350. updateSetEnvGroupCmd.PersistentFlags().StringArrayVarP(
  351. &normalEnvGroupVars,
  352. "normal",
  353. "n",
  354. []string{},
  355. "list of variables to set, in the form VAR=VALUE",
  356. )
  357. updateSetEnvGroupCmd.PersistentFlags().StringArrayVarP(
  358. &secretEnvGroupVars,
  359. "secret",
  360. "s",
  361. []string{},
  362. "list of secret variables to set, in the form VAR=VALUE",
  363. )
  364. updateEnvGroupCmd.AddCommand(updateSetEnvGroupCmd)
  365. updateEnvGroupCmd.AddCommand(updateUnsetEnvGroupCmd)
  366. updateCmd.AddCommand(updateBuildCmd)
  367. updateCmd.AddCommand(updatePushCmd)
  368. updateCmd.AddCommand(updateConfigCmd)
  369. updateCmd.AddCommand(updateEnvGroupCmd)
  370. }
  371. func updateFull(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  372. project, err := client.GetProject(ctx, cliConf.Project)
  373. if err != nil {
  374. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  375. }
  376. if project.ValidateApplyV2 {
  377. err = v2.UpdateFull(ctx)
  378. if err != nil {
  379. return err
  380. }
  381. return nil
  382. }
  383. fullPath, err := filepath.Abs(localPath)
  384. if err != nil {
  385. return err
  386. }
  387. if os.Getenv("GITHUB_ACTIONS") == "" && source == "local" && fullPath == homedir.HomeDir() {
  388. proceed, err := utils.PromptConfirm("You are deploying your home directory. Do you want to continue?", false)
  389. if err != nil {
  390. return err
  391. }
  392. if !proceed {
  393. return nil
  394. }
  395. }
  396. color.New(color.FgGreen).Println("Deploying app:", app)
  397. updateAgent, err := updateGetAgent(ctx, client, cliConf)
  398. if err != nil {
  399. return err
  400. }
  401. err = updateBuildWithAgent(ctx, updateAgent)
  402. if err != nil {
  403. return err
  404. }
  405. err = updatePushWithAgent(ctx, updateAgent)
  406. if err != nil {
  407. return err
  408. }
  409. err = updateUpgradeWithAgent(ctx, updateAgent)
  410. if err != nil {
  411. return err
  412. }
  413. if waitForSuccessfulDeploy {
  414. // solves timing issue where replicasets were not on the cluster, before our initial check
  415. time.Sleep(10 * time.Second)
  416. err := checkDeploymentStatus(ctx, client, cliConf)
  417. if err != nil {
  418. return err
  419. }
  420. }
  421. return nil
  422. }
  423. func updateGetEnv(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  424. updateAgent, err := updateGetAgent(ctx, client, cliConf)
  425. if err != nil {
  426. return err
  427. }
  428. buildEnv, err := updateAgent.GetBuildEnv(ctx, &deploy.GetBuildEnvOpts{
  429. UseNewConfig: false,
  430. })
  431. if err != nil {
  432. return err
  433. }
  434. // set the environment variables in the process
  435. err = updateAgent.SetBuildEnv(buildEnv)
  436. if err != nil {
  437. return err
  438. }
  439. // write the environment variables to either a file or stdout (stdout by default)
  440. return updateAgent.WriteBuildEnv(getEnvFileDest)
  441. }
  442. func updateBuild(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  443. project, err := client.GetProject(ctx, cliConf.Project)
  444. if err != nil {
  445. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  446. }
  447. if project.ValidateApplyV2 {
  448. err = v2.UpdateBuild(ctx)
  449. if err != nil {
  450. return err
  451. }
  452. return nil
  453. }
  454. updateAgent, err := updateGetAgent(ctx, client, cliConf)
  455. if err != nil {
  456. return err
  457. }
  458. return updateBuildWithAgent(ctx, updateAgent)
  459. }
  460. func updatePush(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  461. if app == "" {
  462. if len(args) == 0 {
  463. return fmt.Errorf("please provide the docker image name")
  464. }
  465. image := args[0]
  466. registries, err := client.ListRegistries(ctx, cliConf.Project)
  467. if err != nil {
  468. return err
  469. }
  470. regs := *registries
  471. regID := uint(0)
  472. for _, reg := range regs {
  473. if strings.Contains(image, reg.URL) {
  474. regID = reg.ID
  475. break
  476. }
  477. }
  478. if regID == 0 {
  479. return fmt.Errorf("could not find registry for image: %s", image)
  480. }
  481. err = client.CreateRepository(ctx, cliConf.Project, regID,
  482. &types.CreateRegistryRepositoryRequest{
  483. ImageRepoURI: strings.Split(image, ":")[0],
  484. },
  485. )
  486. if err != nil {
  487. return err
  488. }
  489. agent, err := docker.NewAgentWithAuthGetter(ctx, client, cliConf.Project)
  490. if err != nil {
  491. return err
  492. }
  493. err = agent.PushImage(ctx, image)
  494. if err != nil {
  495. return err
  496. }
  497. return nil
  498. }
  499. updateAgent, err := updateGetAgent(ctx, client, cliConf)
  500. if err != nil {
  501. return err
  502. }
  503. return updatePushWithAgent(ctx, updateAgent)
  504. }
  505. func updateUpgrade(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  506. project, err := client.GetProject(ctx, cliConf.Project)
  507. if err != nil {
  508. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  509. }
  510. if project.ValidateApplyV2 {
  511. err = v2.UpdateUpgrade(ctx)
  512. if err != nil {
  513. return err
  514. }
  515. return nil
  516. }
  517. updateAgent, err := updateGetAgent(ctx, client, cliConf)
  518. if err != nil {
  519. return err
  520. }
  521. err = updateUpgradeWithAgent(ctx, updateAgent)
  522. if err != nil {
  523. return err
  524. }
  525. if waitForSuccessfulDeploy {
  526. // solves timing issue where replicasets were not on the cluster, before our initial check
  527. time.Sleep(10 * time.Second)
  528. err := checkDeploymentStatus(ctx, client, cliConf)
  529. if err != nil {
  530. return err
  531. }
  532. }
  533. return nil
  534. }
  535. func updateSetEnvGroup(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  536. if len(normalEnvGroupVars) == 0 && len(secretEnvGroupVars) == 0 && len(args) == 0 {
  537. return fmt.Errorf("please provide one or more variables to update")
  538. }
  539. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  540. s.Color("cyan")
  541. s.Suffix = fmt.Sprintf(" Fetching env group '%s' in namespace '%s'", name, namespace)
  542. s.Start()
  543. envGroupResp, err := client.GetEnvGroup(ctx, cliConf.Project, cliConf.Cluster, namespace,
  544. &types.GetEnvGroupRequest{
  545. Name: name, Version: version,
  546. },
  547. )
  548. s.Stop()
  549. if err != nil {
  550. return err
  551. }
  552. newEnvGroup := &types.CreateEnvGroupRequest{
  553. Name: envGroupResp.Name,
  554. Variables: make(map[string]string),
  555. }
  556. for k, v := range envGroupResp.Variables {
  557. newEnvGroup.Variables[k] = v
  558. }
  559. // first check for multiple variables being set using the -e or -s flags
  560. if len(normalEnvGroupVars) > 0 || len(secretEnvGroupVars) > 0 {
  561. for _, v := range normalEnvGroupVars {
  562. delete(newEnvGroup.Variables, v)
  563. key, value, err := validateVarValue(v)
  564. if err != nil {
  565. return err
  566. }
  567. newEnvGroup.Variables[key] = value
  568. }
  569. if len(secretEnvGroupVars) > 0 {
  570. newEnvGroup.SecretVariables = make(map[string]string)
  571. }
  572. for _, v := range secretEnvGroupVars {
  573. delete(newEnvGroup.Variables, v)
  574. key, value, err := validateVarValue(v)
  575. if err != nil {
  576. return err
  577. }
  578. newEnvGroup.SecretVariables[key] = value
  579. }
  580. s.Suffix = fmt.Sprintf(" Updating env group '%s' in namespace '%s'", name, namespace)
  581. } else { // legacy usage
  582. key, value, err := validateVarValue(args[0])
  583. if err != nil {
  584. return err
  585. }
  586. delete(newEnvGroup.Variables, key)
  587. if varType == "secret" {
  588. newEnvGroup.SecretVariables = make(map[string]string)
  589. newEnvGroup.SecretVariables[key] = value
  590. s.Suffix = fmt.Sprintf(" Adding new secret variable '%s' to env group '%s' in namespace '%s'", key, name, namespace)
  591. } else {
  592. newEnvGroup.Variables[key] = value
  593. s.Suffix = fmt.Sprintf(" Adding new variable '%s' to env group '%s' in namespace '%s'", key, name, namespace)
  594. }
  595. }
  596. s.Start()
  597. _, err = client.CreateEnvGroup(
  598. ctx, cliConf.Project, cliConf.Cluster, namespace, newEnvGroup,
  599. )
  600. s.Stop()
  601. if err != nil {
  602. return err
  603. }
  604. color.New(color.FgGreen).Println("env group successfully updated")
  605. return nil
  606. }
  607. func validateVarValue(in string) (string, string, error) {
  608. key, value, found := strings.Cut(in, "=")
  609. if !found {
  610. return "", "", fmt.Errorf("%s is not in the form of VAR=VALUE", in)
  611. }
  612. return key, value, nil
  613. }
  614. func updateUnsetEnvGroup(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  615. if len(args) == 0 {
  616. return fmt.Errorf("required variable name")
  617. }
  618. s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
  619. s.Color("cyan")
  620. s.Suffix = fmt.Sprintf(" Fetching env group '%s' in namespace '%s'", name, namespace)
  621. s.Start()
  622. envGroupResp, err := client.GetEnvGroup(ctx, cliConf.Project, cliConf.Cluster, namespace,
  623. &types.GetEnvGroupRequest{
  624. Name: name, Version: version,
  625. },
  626. )
  627. s.Stop()
  628. if err != nil {
  629. return err
  630. }
  631. newEnvGroup := &types.CreateEnvGroupRequest{
  632. Name: envGroupResp.Name,
  633. Variables: envGroupResp.Variables,
  634. }
  635. for _, v := range args {
  636. delete(newEnvGroup.Variables, v)
  637. }
  638. s.Suffix = fmt.Sprintf(" Removing variables from env group '%s' in namespace '%s'", name, namespace)
  639. s.Start()
  640. _, err = client.CreateEnvGroup(
  641. ctx, cliConf.Project, cliConf.Cluster, namespace, newEnvGroup,
  642. )
  643. s.Stop()
  644. if err != nil {
  645. return err
  646. }
  647. color.New(color.FgGreen).Println("env group successfully updated")
  648. return nil
  649. }
  650. // HELPER METHODS
  651. func updateGetAgent(ctx context.Context, client api.Client, cliConf config.CLIConfig) (*deploy.DeployAgent, error) {
  652. var buildMethod deploy.DeployBuildType
  653. if method != "" {
  654. buildMethod = deploy.DeployBuildType(method)
  655. }
  656. // add additional env, if they exist
  657. additionalEnv := make(map[string]string)
  658. for _, buildEnv := range buildFlagsEnv {
  659. if strSplArr := strings.SplitN(buildEnv, "=", 2); len(strSplArr) >= 2 {
  660. additionalEnv[strSplArr[0]] = strSplArr[1]
  661. }
  662. }
  663. // initialize the update agent
  664. return deploy.NewDeployAgent(ctx, client, app, &deploy.DeployOpts{
  665. SharedOpts: &deploy.SharedOpts{
  666. ProjectID: cliConf.Project,
  667. ClusterID: cliConf.Cluster,
  668. Namespace: namespace,
  669. LocalPath: localPath,
  670. LocalDockerfile: dockerfile,
  671. OverrideTag: tag,
  672. Method: buildMethod,
  673. AdditionalEnv: additionalEnv,
  674. UseCache: useCache,
  675. },
  676. Local: source != "github",
  677. })
  678. }
  679. func updateBuildWithAgent(ctx context.Context, updateAgent *deploy.DeployAgent) error {
  680. // build the deployment
  681. color.New(color.FgGreen).Println("Building docker image for", app)
  682. if stream {
  683. _ = updateAgent.StreamEvent(ctx, types.SubEvent{
  684. EventID: "build",
  685. Name: "Build",
  686. Index: 100,
  687. Status: types.EventStatusInProgress,
  688. Info: "",
  689. }) //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  690. }
  691. if useCache {
  692. err := config.SetDockerConfig(ctx, updateAgent.Client, updateAgent.Opts.ProjectID)
  693. if err != nil {
  694. return err
  695. }
  696. }
  697. // read the values if necessary
  698. valuesObj, err := readValuesFile()
  699. if err != nil {
  700. return err
  701. }
  702. buildEnv, err := updateAgent.GetBuildEnv(ctx, &deploy.GetBuildEnvOpts{
  703. UseNewConfig: true,
  704. NewConfig: valuesObj,
  705. })
  706. if err != nil {
  707. if stream {
  708. // another concern: is it safe to ignore the error here?
  709. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  710. EventID: "build",
  711. Name: "Build",
  712. Index: 110,
  713. Status: types.EventStatusFailed,
  714. Info: err.Error(),
  715. })
  716. }
  717. return err
  718. }
  719. // set the environment variables in the process
  720. err = updateAgent.SetBuildEnv(buildEnv)
  721. if err != nil {
  722. if stream {
  723. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  724. EventID: "build",
  725. Name: "Build",
  726. Index: 120,
  727. Status: types.EventStatusFailed,
  728. Info: err.Error(),
  729. })
  730. }
  731. return err
  732. }
  733. if err := updateAgent.Build(ctx, nil); err != nil {
  734. if stream {
  735. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  736. EventID: "build",
  737. Name: "Build",
  738. Index: 130,
  739. Status: types.EventStatusFailed,
  740. Info: err.Error(),
  741. })
  742. }
  743. return err
  744. }
  745. if stream {
  746. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  747. EventID: "build",
  748. Name: "Build",
  749. Index: 140,
  750. Status: types.EventStatusSuccess,
  751. Info: "",
  752. })
  753. }
  754. return nil
  755. }
  756. func updatePushWithAgent(ctx context.Context, updateAgent *deploy.DeployAgent) error {
  757. if useCache {
  758. color.New(color.FgGreen).Println("Skipping image push for", app, "as use-cache is set")
  759. return nil
  760. }
  761. // push the deployment
  762. color.New(color.FgGreen).Println("Pushing new image for", app)
  763. if stream {
  764. updateAgent.StreamEvent( //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  765. ctx, types.SubEvent{
  766. EventID: "push",
  767. Name: "Push",
  768. Index: 200,
  769. Status: types.EventStatusInProgress,
  770. Info: "",
  771. })
  772. }
  773. if err := updateAgent.Push(ctx); err != nil {
  774. if stream {
  775. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  776. EventID: "push",
  777. Name: "Push",
  778. Index: 210,
  779. Status: types.EventStatusFailed,
  780. Info: err.Error(),
  781. })
  782. }
  783. return err
  784. }
  785. if stream {
  786. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  787. EventID: "push",
  788. Name: "Push",
  789. Index: 220,
  790. Status: types.EventStatusSuccess,
  791. Info: "",
  792. })
  793. }
  794. return nil
  795. }
  796. func updateUpgradeWithAgent(ctx context.Context, updateAgent *deploy.DeployAgent) error {
  797. // push the deployment
  798. color.New(color.FgGreen).Println("Upgrading configuration for", app)
  799. if stream {
  800. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  801. EventID: "upgrade",
  802. Name: "Upgrade",
  803. Index: 300,
  804. Status: types.EventStatusInProgress,
  805. Info: "",
  806. })
  807. }
  808. var err error
  809. // read the values if necessary
  810. valuesObj, err := readValuesFile()
  811. if err != nil {
  812. return err
  813. }
  814. if err != nil {
  815. if stream {
  816. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  817. EventID: "upgrade",
  818. Name: "Upgrade",
  819. Index: 310,
  820. Status: types.EventStatusFailed,
  821. Info: err.Error(),
  822. })
  823. }
  824. return err
  825. }
  826. if len(updateAgent.Opts.AdditionalEnv) > 0 {
  827. syncedEnv, err := deploy.GetSyncedEnv(
  828. ctx,
  829. updateAgent.Client,
  830. updateAgent.Release.Config,
  831. updateAgent.Opts.ProjectID,
  832. updateAgent.Opts.ClusterID,
  833. updateAgent.Opts.Namespace,
  834. false,
  835. )
  836. if err != nil {
  837. return err
  838. }
  839. for k := range updateAgent.Opts.AdditionalEnv {
  840. if _, ok := syncedEnv[k]; ok {
  841. return fmt.Errorf("environment variable %s already exists as part of a synced environment group", k)
  842. }
  843. }
  844. normalEnv, err := deploy.GetNormalEnv(
  845. updateAgent.Client,
  846. updateAgent.Release.Config,
  847. updateAgent.Opts.ProjectID,
  848. updateAgent.Opts.ClusterID,
  849. updateAgent.Opts.Namespace,
  850. false,
  851. )
  852. if err != nil {
  853. return err
  854. }
  855. // add the additional environment variables to container.env.normal
  856. for k, v := range updateAgent.Opts.AdditionalEnv {
  857. normalEnv[k] = v
  858. }
  859. valuesObj = templaterUtils.CoalesceValues(valuesObj, map[string]interface{}{
  860. "container": map[string]interface{}{
  861. "env": map[string]interface{}{
  862. "normal": normalEnv,
  863. },
  864. },
  865. })
  866. }
  867. err = updateAgent.UpdateImageAndValues(ctx, valuesObj)
  868. if err != nil {
  869. if stream {
  870. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  871. EventID: "upgrade",
  872. Name: "Upgrade",
  873. Index: 320,
  874. Status: types.EventStatusFailed,
  875. Info: err.Error(),
  876. })
  877. }
  878. return err
  879. }
  880. if stream {
  881. updateAgent.StreamEvent(ctx, types.SubEvent{ //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  882. EventID: "upgrade",
  883. Name: "Upgrade",
  884. Index: 330,
  885. Status: types.EventStatusSuccess,
  886. Info: "",
  887. })
  888. }
  889. color.New(color.FgGreen).Println("Successfully updated", app)
  890. return nil
  891. }
  892. func checkDeploymentStatus(ctx context.Context, client api.Client, cliConfig config.CLIConfig) error {
  893. color.New(color.FgBlue).Println("waiting for deployment to be ready, this may take a few minutes and will time out if it takes longer than 30 minutes")
  894. sharedConf := &PorterRunSharedConfig{
  895. Client: client,
  896. CLIConfig: cliConfig,
  897. }
  898. err := sharedConf.setSharedConfig(ctx)
  899. if err != nil {
  900. return fmt.Errorf("could not retrieve kubernetes credentials: %w", err)
  901. }
  902. prevRefresh := time.Now()
  903. timeWait := prevRefresh.Add(30 * time.Minute)
  904. success := false
  905. depls, err := sharedConf.Clientset.AppsV1().Deployments(namespace).List(
  906. ctx,
  907. metav1.ListOptions{
  908. LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", app),
  909. },
  910. )
  911. if err != nil {
  912. return fmt.Errorf("could not get deployments for app %s: %w", app, err)
  913. }
  914. if len(depls.Items) == 0 {
  915. return fmt.Errorf("could not find any deployments for app %s", app)
  916. }
  917. sort.Slice(depls.Items, func(i, j int) bool {
  918. return depls.Items[i].CreationTimestamp.After(depls.Items[j].CreationTimestamp.Time)
  919. })
  920. depl := depls.Items[0]
  921. // determine if the deployment has an appropriate number of ready replicas
  922. minAvailable := *(depl.Spec.Replicas) - getMaxUnavailable(depl)
  923. var revision string
  924. for k, v := range depl.Spec.Template.ObjectMeta.Annotations {
  925. if k == "helm.sh/revision" {
  926. revision = v
  927. break
  928. }
  929. }
  930. if revision == "" {
  931. return fmt.Errorf("could not find revision for deployment")
  932. }
  933. pods, err := sharedConf.Clientset.CoreV1().Pods(namespace).List(
  934. ctx, metav1.ListOptions{
  935. LabelSelector: fmt.Sprintf("app.kubernetes.io/instance=%s", app),
  936. },
  937. )
  938. if err != nil {
  939. return fmt.Errorf("error fetching pods for app %s: %w", app, err)
  940. }
  941. if len(pods.Items) == 0 {
  942. return fmt.Errorf("could not find any pods for app %s", app)
  943. }
  944. var rsName string
  945. for _, pod := range pods.Items {
  946. if pod.ObjectMeta.Annotations["helm.sh/revision"] == revision {
  947. for _, ref := range pod.OwnerReferences {
  948. if ref.Kind == "ReplicaSet" {
  949. rs, err := sharedConf.Clientset.AppsV1().ReplicaSets(namespace).Get(
  950. ctx,
  951. ref.Name,
  952. metav1.GetOptions{},
  953. )
  954. if err != nil {
  955. return fmt.Errorf("error fetching new replicaset: %w", err)
  956. }
  957. rsName = rs.Name
  958. break
  959. }
  960. }
  961. if rsName != "" {
  962. break
  963. }
  964. }
  965. }
  966. if rsName == "" {
  967. return fmt.Errorf("could not find replicaset for app %s", app)
  968. }
  969. for time.Now().Before(timeWait) {
  970. // refresh the client every 10 minutes
  971. if time.Now().After(prevRefresh.Add(10 * time.Minute)) {
  972. err = sharedConf.setSharedConfig(ctx)
  973. if err != nil {
  974. return fmt.Errorf("could not retrieve kube credentials: %s", err.Error())
  975. }
  976. prevRefresh = time.Now()
  977. }
  978. rs, err := sharedConf.Clientset.AppsV1().ReplicaSets(namespace).Get(
  979. ctx,
  980. rsName,
  981. metav1.GetOptions{},
  982. )
  983. if err != nil {
  984. return fmt.Errorf("error fetching new replicaset: %w", err)
  985. }
  986. if minAvailable <= rs.Status.ReadyReplicas {
  987. success = true
  988. }
  989. if success {
  990. break
  991. }
  992. time.Sleep(2 * time.Second)
  993. }
  994. if success {
  995. color.New(color.FgGreen).Printf("%s has been successfully deployed on the cluster\n", app)
  996. } else {
  997. return fmt.Errorf("timed out waiting for deployment to be ready, please check the Porter dashboard for more information")
  998. }
  999. return nil
  1000. }