job.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/spf13/cobra"
  10. )
  11. var batchImageUpdateCmd = &cobra.Command{
  12. Use: "job update-images",
  13. Short: "Updates the image tag of all jobs in a namespace which use a specific image.",
  14. Long: fmt.Sprintf(`
  15. %s
  16. Updates the image tag of all jobs in a namespace which use a specific image. Note that for all
  17. jobs with version <= v0.4.0, this will trigger a new run of a manual job. However, for versions
  18. >= v0.5.0, this will not create a new run of the job.
  19. Example commands:
  20. %s
  21. This command is namespace-scoped and uses the default namespace. To specify a different namespace,
  22. use the --namespace flag:
  23. %s
  24. `,
  25. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter job update-images\":"),
  26. color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --image-repo-uri my-image.registry.io --tag newtag"),
  27. color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --namespace custom-namespace --image-repo-uri my-image.registry.io --tag newtag"),
  28. ),
  29. Run: func(cmd *cobra.Command, args []string) {
  30. err := checkLoginAndRun(args, batchImageUpdate)
  31. if err != nil {
  32. os.Exit(1)
  33. }
  34. },
  35. }
  36. var imageRepoURI string
  37. func init() {
  38. rootCmd.AddCommand(batchImageUpdateCmd)
  39. batchImageUpdateCmd.PersistentFlags().StringVar(
  40. &tag,
  41. "tag",
  42. "",
  43. "The new image tag to use.",
  44. )
  45. batchImageUpdateCmd.PersistentFlags().StringVar(
  46. &namespace,
  47. "namespace",
  48. "",
  49. "The namespace of the jobs.",
  50. )
  51. batchImageUpdateCmd.PersistentFlags().StringVarP(
  52. &imageRepoURI,
  53. "image-repo-uri",
  54. "i",
  55. "",
  56. "Image repo uri",
  57. )
  58. batchImageUpdateCmd.MarkPersistentFlagRequired("image-repo-uri")
  59. batchImageUpdateCmd.MarkPersistentFlagRequired("tag")
  60. }
  61. func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  62. color.New(color.FgGreen).Println("Updating all jobs which use the image:", imageRepoURI)
  63. return client.UpdateBatchImage(
  64. context.TODO(),
  65. config.Project,
  66. config.Cluster,
  67. namespace,
  68. &types.UpdateImageBatchRequest{
  69. ImageRepoURI: imageRepoURI,
  70. Tag: tag,
  71. },
  72. )
  73. }