job.go 2.1 KB

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