Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(bundle): implement 'bundle copy' command
```
werf bundle copy --repo REPO [--tag latest] --to NEWREPO [--to-tag NEWTAG]
```

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Apr 27, 2022
1 parent c727c56 commit 92122e7
Show file tree
Hide file tree
Showing 32 changed files with 677 additions and 480 deletions.
4 changes: 2 additions & 2 deletions cmd/werf/build/main.go
Expand Up @@ -81,8 +81,8 @@ If one or more IMAGE_NAME parameters specified, werf will build only these image

common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd)
common.SetupCacheStagesStorageOptions(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd)
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repo, to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -61,8 +61,8 @@ func NewCmd() *cobra.Command {
common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{})

common.SetupStagesStorageOptions(&commonCmdData, cmd) // FIXME
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd) // FIXME
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repo, to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
Expand Down
116 changes: 116 additions & 0 deletions cmd/werf/bundle/copy/copy.go
@@ -0,0 +1,116 @@
package copy

import (
"fmt"
"os"

"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/bundles"
"github.com/werf/werf/pkg/werf"
"github.com/werf/werf/pkg/werf/global_warnings"
)

var cmdData struct {
Repo *common.RepoData
Tag string
To *common.RepoData
ToTag string
}

var commonCmdData common.CmdData

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "copy",
Short: "Copy published bundle into another location",
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.`),
DisableFlagsInUseLine: true,
Annotations: map[string]string{
common.CmdEnvAnno: common.EnvsDescription(),
},
RunE: func(cmd *cobra.Command, args []string) error {
defer global_warnings.PrintGlobalWarnings(common.GetContext())

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}

common.LogVersion()

return common.LogRunningTime(runCopy)
},
}

common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{})

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repos")
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupInsecureHelmDependencies(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)

cmdData.Repo = common.NewRepoData("repo", common.RepoDataOptions{OnlyAddress: true})
cmdData.Repo.SetupCmd(cmd)

cmdData.To = common.NewRepoData("to", common.RepoDataOptions{OnlyAddress: true})
cmdData.To.SetupCmd(cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
common.SetupPlatform(&commonCmdData, cmd)

defaultTag := os.Getenv("WERF_TAG")
if defaultTag == "" {
defaultTag = "latest"
}
cmd.Flags().StringVarP(&cmdData.Tag, "tag", "", defaultTag, "Provide from tag version of the bundle to copy ($WERF_TAG or latest by default)")
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)")

return cmd
}

func runCopy() error {
ctx := common.GetContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %w", err)
}

if err := common.DockerRegistryInit(ctx, &commonCmdData); err != nil {
return err
}

helm_v3.Settings.Debug = *commonCmdData.LogDebug

bundlesRegistryClient, err := common.NewBundlesRegistryClient(ctx, &commonCmdData)
if err != nil {
return err
}

if *cmdData.Repo.Address == "" {
return fmt.Errorf("--repo=ADDRESS param required")
}
if *cmdData.To.Address == "" {
return fmt.Errorf("--to=ADDRESS param required")
}

fromRegistry, err := cmdData.Repo.CreateDockerRegistry(*commonCmdData.InsecureRegistry, *commonCmdData.SkipTlsVerifyRegistry)
if err != nil {
return fmt.Errorf("error creating container registry accessor for repo %s: %w", *cmdData.Repo.Address, err)
}

fromTag := cmdData.Tag
toTag := cmdData.ToTag
if toTag == "" {
toTag = fromTag
}

fromRef := fmt.Sprintf("%s:%s", *cmdData.Repo.Address, fromTag)
toRef := fmt.Sprintf("%s:%s", *cmdData.To.Address, toTag)

return bundles.Copy(ctx, fromRef, toRef, bundlesRegistryClient, fromRegistry)
}
6 changes: 3 additions & 3 deletions cmd/werf/bundle/download/download.go
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm"
helm_v3 "helm.sh/helm/v3/cmd/helm"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/bundles"
Expand Down Expand Up @@ -50,8 +50,8 @@ func NewCmd() *cobra.Command {
common.SetupInsecureHelmDependencies(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)

common.SetupStagesStorageOptions(&commonCmdData, cmd) // FIXME
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd) // FIXME
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -82,8 +82,8 @@ func NewCmd() *cobra.Command {

common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd)
common.SetupCacheStagesStorageOptions(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd)
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repo and to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -88,8 +88,8 @@ Published into container registry bundle can be rolled out by the "werf bundle"

common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd)
common.SetupCacheStagesStorageOptions(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd)
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repo and to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
Expand Down
14 changes: 7 additions & 7 deletions cmd/werf/bundle/render/render.go
Expand Up @@ -9,7 +9,7 @@ import (

uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
Expand Down Expand Up @@ -64,8 +64,8 @@ func NewCmd() *cobra.Command {
common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{})

common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd)
common.SetupFinalRepo(&commonCmdData, cmd)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified repo, to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
Expand Down Expand Up @@ -114,17 +114,17 @@ func runRender(ctx context.Context) error {
var isLocal bool
switch {
case cmdData.BundleDir != "":
if *commonCmdData.StagesStorage != "" {
if *commonCmdData.Repo.Address != "" {
return fmt.Errorf("only one of --bundle-dir or --repo should be specified, but both provided")
}
if *commonCmdData.FinalStagesStorage != "" {
if *commonCmdData.FinalRepo.Address != "" {
return fmt.Errorf("only one of --bundle-dir or --final-repo should be specified, but both provided")
}

isLocal = true
case *commonCmdData.StagesStorage == storage.LocalStorageAddress:
case *commonCmdData.Repo.Address == storage.LocalStorageAddress:
return fmt.Errorf("--repo %s is not allowed, specify remote storage address", storage.LocalStorageAddress)
case *commonCmdData.StagesStorage != "":
case *commonCmdData.Repo.Address != "":
isLocal = false
default:
return fmt.Errorf("either --bundle-dir or --repo required")
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/cleanup/cleanup.go
Expand Up @@ -64,8 +64,8 @@ It is safe to run this command periodically (daily is enough) by automated clean

common.SetupSecondaryStagesStorageOptions(&commonCmdData, cmd)
common.SetupCacheStagesStorageOptions(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupFinalStagesStorageOptions(&commonCmdData, cmd)
common.SetupRepoOptions(&commonCmdData, cmd)
common.SetupFinalRepo(&commonCmdData, cmd)
common.SetupParallelOptions(&commonCmdData, cmd, common.DefaultCleanupParallelTasksLimit)

common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and delete images from the specified repo")
Expand Down

0 comments on commit 92122e7

Please sign in to comment.