|
| 1 | +package copy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + helm_v3 "helm.sh/helm/v3/cmd/helm" |
| 9 | + |
| 10 | + "github.com/werf/werf/cmd/werf/common" |
| 11 | + "github.com/werf/werf/pkg/deploy/bundles" |
| 12 | + "github.com/werf/werf/pkg/werf" |
| 13 | + "github.com/werf/werf/pkg/werf/global_warnings" |
| 14 | +) |
| 15 | + |
| 16 | +var cmdData struct { |
| 17 | + Repo *common.RepoData |
| 18 | + Tag string |
| 19 | + To *common.RepoData |
| 20 | + ToTag string |
| 21 | +} |
| 22 | + |
| 23 | +var commonCmdData common.CmdData |
| 24 | + |
| 25 | +func NewCmd() *cobra.Command { |
| 26 | + cmd := &cobra.Command{ |
| 27 | + Use: "copy", |
| 28 | + Short: "Copy published bundle into another location", |
| 29 | + Long: common.GetLongCommandDescription(`Take latest bundle from the specified container registry using specified version tag and copy it either into a different tag within the same container registry or into another container registry.`), |
| 30 | + DisableFlagsInUseLine: true, |
| 31 | + Annotations: map[string]string{ |
| 32 | + common.CmdEnvAnno: common.EnvsDescription(), |
| 33 | + }, |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + defer global_warnings.PrintGlobalWarnings(common.GetContext()) |
| 36 | + |
| 37 | + if err := common.ProcessLogOptions(&commonCmdData); err != nil { |
| 38 | + common.PrintHelp(cmd) |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + common.LogVersion() |
| 43 | + |
| 44 | + return common.LogRunningTime(runCopy) |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{}) |
| 49 | + common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{}) |
| 50 | + |
| 51 | + common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repos") |
| 52 | + common.SetupInsecureRegistry(&commonCmdData, cmd) |
| 53 | + common.SetupInsecureHelmDependencies(&commonCmdData, cmd) |
| 54 | + common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd) |
| 55 | + |
| 56 | + cmdData.Repo = common.NewRepoData("repo", common.RepoDataOptions{OnlyAddress: true}) |
| 57 | + cmdData.Repo.SetupCmd(cmd) |
| 58 | + |
| 59 | + cmdData.To = common.NewRepoData("to", common.RepoDataOptions{OnlyAddress: true}) |
| 60 | + cmdData.To.SetupCmd(cmd) |
| 61 | + |
| 62 | + common.SetupLogOptions(&commonCmdData, cmd) |
| 63 | + common.SetupLogProjectDir(&commonCmdData, cmd) |
| 64 | + common.SetupPlatform(&commonCmdData, cmd) |
| 65 | + |
| 66 | + defaultTag := os.Getenv("WERF_TAG") |
| 67 | + if defaultTag == "" { |
| 68 | + defaultTag = "latest" |
| 69 | + } |
| 70 | + cmd.Flags().StringVarP(&cmdData.Tag, "tag", "", defaultTag, "Provide from tag version of the bundle to copy ($WERF_TAG or latest by default)") |
| 71 | + cmd.Flags().StringVarP(&cmdData.ToTag, "to-tag", "", "", "Provide to tag version of the bundle to copy ($WERF_TO_TAG or same as --tag by default)") |
| 72 | + |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func runCopy() error { |
| 77 | + ctx := common.GetContext() |
| 78 | + |
| 79 | + if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil { |
| 80 | + return fmt.Errorf("initialization error: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + helm_v3.Settings.Debug = *commonCmdData.LogDebug |
| 88 | + |
| 89 | + bundlesRegistryClient, err := common.NewBundlesRegistryClient(ctx, &commonCmdData) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + if *cmdData.Repo.Address == "" { |
| 95 | + return fmt.Errorf("--repo=ADDRESS param required") |
| 96 | + } |
| 97 | + if *cmdData.To.Address == "" { |
| 98 | + return fmt.Errorf("--to=ADDRESS param required") |
| 99 | + } |
| 100 | + |
| 101 | + fromRegistry, err := cmdData.Repo.CreateDockerRegistry(*commonCmdData.InsecureRegistry, *commonCmdData.SkipTlsVerifyRegistry) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("error creating container registry accessor for repo %s: %w", *cmdData.Repo.Address, err) |
| 104 | + } |
| 105 | + |
| 106 | + fromTag := cmdData.Tag |
| 107 | + toTag := cmdData.ToTag |
| 108 | + if toTag == "" { |
| 109 | + toTag = fromTag |
| 110 | + } |
| 111 | + |
| 112 | + fromRef := fmt.Sprintf("%s:%s", *cmdData.Repo.Address, fromTag) |
| 113 | + toRef := fmt.Sprintf("%s:%s", *cmdData.To.Address, toTag) |
| 114 | + |
| 115 | + return bundles.Copy(ctx, fromRef, toRef, bundlesRegistryClient, fromRegistry) |
| 116 | +} |
0 commit comments