/
copy.go
169 lines (135 loc) · 6.27 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package copy
import (
"context"
"fmt"
"os"
"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/deploy/bundles"
"github.com/werf/werf/pkg/docker_registry"
"github.com/werf/werf/pkg/werf"
"github.com/werf/werf/pkg/werf/global_warnings"
)
var cmdData struct {
// TODO(2.0): Legacy {
Repo string
Tag string
ToTag string
// TODO(2.0): } Legacy
From string
To string
}
var commonCmdData common.CmdData
func NewCmd(ctx context.Context) *cobra.Command {
ctx = common.NewContextWithCmdData(ctx, &commonCmdData)
cmd := common.SetCommandContext(ctx, &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 {
ctx := cmd.Context()
defer global_warnings.PrintGlobalWarnings(ctx)
if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return common.LogRunningTime(func() error { return runCopy(ctx) })
},
})
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)
common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
common.SetupPlatform(&commonCmdData, cmd)
commonCmdData.SetupHelmCompatibleChart(cmd, true)
commonCmdData.SetupRenameChart(cmd)
cmd.Flags().StringVarP(&cmdData.Repo, "repo", "", os.Getenv("WERF_REPO"), "Deprecated param, use --from=ADDR instead. Source address of bundle which should be copied.")
cmd.Flags().StringVarP(&cmdData.Tag, "tag", "", os.Getenv("WERF_TAG"), "Deprecated param, use --from=REPO:TAG instead. Provide from tag version of the bundle to copy ($WERF_TAG or latest by default).")
cmd.Flags().StringVarP(&cmdData.ToTag, "to-tag", "", os.Getenv("WERF_TO_TAG"), "Deprecated param, use --to=REPO:TAG instead. Provide to tag version of the bundle to copy ($WERF_TO_TAG or same as --tag by default).")
cmd.Flags().StringVarP(&cmdData.From, "from", "", os.Getenv("WERF_FROM"), "Source address of the bundle to copy, specify bundle archive using schema `archive:PATH_TO_ARCHIVE.tar.gz`, specify remote bundle with schema `[docker://]REPO:TAG` or without schema.")
cmd.Flags().StringVarP(&cmdData.To, "to", "", os.Getenv("WERF_TO"), "Destination address of the bundle to copy, specify bundle archive using schema `archive:PATH_TO_ARCHIVE.tar.gz`, specify remote bundle with schema `[docker://]REPO:TAG` or without schema.")
return cmd
}
func runCopy(ctx context.Context) error {
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
}
fromAddrRaw := cmdData.From
if fromAddrRaw != "" && cmdData.Repo != "" {
return fmt.Errorf("unable to use --repo=ADDRESS and --from=ADDRESS params at the same time: please specify only --from=ADDRESS param, --repo param has been deprecated")
} else if cmdData.Repo != "" {
logboek.Context(ctx).Warn().LogF("Please use --from=ADDRESS param instead of deprecated --repo=ADDRESS param\n")
fromAddrRaw = cmdData.Repo
}
if fromAddrRaw == "" {
return fmt.Errorf("--from=ADDRESS param required")
}
toAddrRaw := cmdData.To
if toAddrRaw == "" {
return fmt.Errorf("--to=ADDRESS param required")
}
fromAddr, err := bundles.ParseAddr(fromAddrRaw)
if err != nil {
return fmt.Errorf("invalid from addr %q: %w", fromAddrRaw, err)
}
toAddr, err := bundles.ParseAddr(toAddrRaw)
if err != nil {
return fmt.Errorf("invalid to addr %q: %w", toAddrRaw, err)
}
var fromRegistry, toRegistry docker_registry.Interface
if fromAddr.RegistryAddress != nil {
// TODO(2.0): remove legacy compatibility param
if cmdData.Tag != "" {
logboek.Context(ctx).Warn().LogF("Please use --from=REPO:TAG tag specification instead of deprecated --tag=TAG param\n")
fromAddr.RegistryAddress.Tag = cmdData.Tag
}
fromRegistry, err = common.CreateDockerRegistry(fromAddr.RegistryAddress.Repo, *commonCmdData.InsecureRegistry, *commonCmdData.SkipTlsVerifyRegistry)
if err != nil {
return err
}
}
if toAddr.RegistryAddress != nil {
// TODO(2.0): remove legacy compatibility param
if cmdData.ToTag != "" {
logboek.Context(ctx).Warn().LogF("Please use --to=REPO:TAG tag specification instead of deprecated --to-tag=TAG param\n")
toAddr.RegistryAddress.Tag = cmdData.ToTag
}
toRegistry, err = common.CreateDockerRegistry(toAddr.RegistryAddress.Repo, *commonCmdData.InsecureRegistry, *commonCmdData.SkipTlsVerifyRegistry)
if err != nil {
return err
}
}
if *commonCmdData.HelmCompatibleChart && *commonCmdData.RenameChart != "" {
return fmt.Errorf("incompatible options specified, could not use --helm-compatible-chart and --rename-chart=%q at the same time", *commonCmdData.RenameChart)
}
return logboek.Context(ctx).LogProcess("Copy bundle").DoError(func() error {
logboek.Context(ctx).LogFDetails("From: %s\n", fromAddr.String())
logboek.Context(ctx).LogFDetails("To: %s\n", toAddr.String())
return bundles.Copy(ctx, fromAddr, toAddr, bundles.CopyOptions{
BundlesRegistryClient: bundlesRegistryClient,
FromRegistryClient: fromRegistry,
ToRegistryClient: toRegistry,
HelmCompatibleChart: *commonCmdData.HelmCompatibleChart,
RenameChart: *commonCmdData.RenameChart,
})
})
}