job.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/fatih/color"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/cli/cmd/deploy/wait"
  10. "github.com/spf13/cobra"
  11. )
  12. var jobCmd = &cobra.Command{
  13. Use: "job",
  14. }
  15. var batchImageUpdateCmd = &cobra.Command{
  16. Use: "update-images",
  17. Short: "Updates the image tag of all jobs in a namespace which use a specific image.",
  18. Long: fmt.Sprintf(`
  19. %s
  20. Updates the image tag of all jobs in a namespace which use a specific image. Note that for all
  21. jobs with version <= v0.4.0, this will trigger a new run of a manual job. However, for versions
  22. >= v0.5.0, this will not create a new run of the job.
  23. Example commands:
  24. %s
  25. This command is namespace-scoped and uses the default namespace. To specify a different namespace,
  26. use the --namespace flag:
  27. %s
  28. `,
  29. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter job update-images\":"),
  30. color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --image-repo-uri my-image.registry.io --tag newtag"),
  31. color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --namespace custom-namespace --image-repo-uri my-image.registry.io --tag newtag"),
  32. ),
  33. Run: func(cmd *cobra.Command, args []string) {
  34. err := checkLoginAndRun(args, batchImageUpdate)
  35. if err != nil {
  36. os.Exit(1)
  37. }
  38. },
  39. }
  40. var waitCmd = &cobra.Command{
  41. Use: "wait",
  42. Short: "Waits for a job to complete.",
  43. Long: fmt.Sprintf(`
  44. %s
  45. Waits for a job with a given name and namespace to complete a run. If the job completes successfully,
  46. this command exits with exit code 0. Otherwise, this command exits with exit code 1.
  47. Example commands:
  48. %s
  49. This command is namespace-scoped and uses the default namespace. To specify a different namespace,
  50. use the --namespace flag:
  51. %s
  52. `,
  53. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter job wait\":"),
  54. color.New(color.FgGreen, color.Bold).Sprintf("porter job wait --name job-example"),
  55. color.New(color.FgGreen, color.Bold).Sprintf("porter job wait --name job-example --namespace custom-namespace"),
  56. ),
  57. Run: func(cmd *cobra.Command, args []string) {
  58. err := checkLoginAndRun(args, waitForJob)
  59. if err != nil {
  60. os.Exit(1)
  61. }
  62. },
  63. }
  64. var imageRepoURI string
  65. func init() {
  66. rootCmd.AddCommand(jobCmd)
  67. jobCmd.AddCommand(batchImageUpdateCmd)
  68. jobCmd.AddCommand(waitCmd)
  69. batchImageUpdateCmd.PersistentFlags().StringVar(
  70. &tag,
  71. "tag",
  72. "",
  73. "The new image tag to use.",
  74. )
  75. batchImageUpdateCmd.PersistentFlags().StringVar(
  76. &namespace,
  77. "namespace",
  78. "",
  79. "The namespace of the jobs.",
  80. )
  81. batchImageUpdateCmd.PersistentFlags().StringVarP(
  82. &imageRepoURI,
  83. "image-repo-uri",
  84. "i",
  85. "",
  86. "Image repo uri",
  87. )
  88. batchImageUpdateCmd.MarkPersistentFlagRequired("image-repo-uri")
  89. batchImageUpdateCmd.MarkPersistentFlagRequired("tag")
  90. waitCmd.PersistentFlags().StringVar(
  91. &namespace,
  92. "namespace",
  93. "",
  94. "The namespace of the jobs.",
  95. )
  96. waitCmd.PersistentFlags().StringVar(
  97. &name,
  98. "name",
  99. "",
  100. "The name of the jobs.",
  101. )
  102. waitCmd.MarkPersistentFlagRequired("name")
  103. }
  104. func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  105. color.New(color.FgGreen).Println("Updating all jobs which use the image:", imageRepoURI)
  106. return client.UpdateBatchImage(
  107. context.TODO(),
  108. cliConf.Project,
  109. cliConf.Cluster,
  110. namespace,
  111. &types.UpdateImageBatchRequest{
  112. ImageRepoURI: imageRepoURI,
  113. Tag: tag,
  114. },
  115. )
  116. }
  117. // waits for a job with a given name/namespace
  118. func waitForJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  119. return wait.WaitForJob(client, &wait.WaitOpts{
  120. ProjectID: cliConf.Project,
  121. ClusterID: cliConf.Cluster,
  122. Namespace: namespace,
  123. Name: name,
  124. })
  125. }