/
main.go
220 lines (173 loc) · 6.87 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package cmd_factory
import (
"fmt"
"github.com/spf13/cobra"
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
"github.com/werf/werf/pkg/tmp_manager"
"github.com/werf/werf/pkg/true_git"
"github.com/werf/werf/pkg/werf"
)
func NewCmdWithData(commonCmdData *common.CmdData) *cobra.Command {
cmd := &cobra.Command{
Use: "publish [IMAGE_NAME...]",
Short: "Build final images from stages and push into images repo",
Long: common.GetLongCommandDescription(`Build final images using each specified tag with the tagging strategy and push into images repo.
New docker layer with service info about tagging strategy will be built for each tag of each image from werf.yaml. Images will be pushed into docker repo with the names IMAGES_REPO/IMAGE_NAME:TAG.
If one or more IMAGE_NAME parameters specified, werf will publish only these images from werf.yaml.`),
Example: ` # Publish images into myregistry.mydomain.com/myproject images repo using 'mybranch' tag and git-branch tagging strategy
$ werf images publish --stages-storage :local --images-repo myregistry.mydomain.com/myproject --tag-git-branch mybranch`,
DisableFlagsInUseLine: true,
Annotations: map[string]string{},
RunE: func(cmd *cobra.Command, args []string) error {
defer werf.PrintGlobalWarnings(common.BackgroundContext())
return common.LogRunningTime(func() error {
if err := common.ProcessLogOptions(commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return runImagesPublish(commonCmdData, args)
})
},
}
common.SetupDir(commonCmdData, cmd)
common.SetupConfigPath(commonCmdData, cmd)
common.SetupConfigTemplatesDir(commonCmdData, cmd)
common.SetupTmpDir(commonCmdData, cmd)
common.SetupHomeDir(commonCmdData, cmd)
common.SetupSSHKey(commonCmdData, cmd)
common.SetupTag(commonCmdData, cmd)
common.SetupStagesStorageOptions(commonCmdData, cmd)
common.SetupImagesRepoOptions(commonCmdData, cmd)
common.SetupDockerConfig(commonCmdData, cmd, "Command needs granted permissions to read and pull images from the specified stages storage and push images into images repo")
common.SetupInsecureRegistry(commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(commonCmdData, cmd)
common.SetupLogOptions(commonCmdData, cmd)
common.SetupLogProjectDir(commonCmdData, cmd)
common.SetupSynchronization(commonCmdData, cmd)
common.SetupKubeConfig(commonCmdData, cmd)
common.SetupKubeConfigBase64(commonCmdData, cmd)
common.SetupKubeContext(commonCmdData, cmd)
common.SetupPublishReportPath(commonCmdData, cmd)
common.SetupPublishReportFormat(commonCmdData, cmd)
common.SetupVirtualMerge(commonCmdData, cmd)
common.SetupVirtualMergeFromCommit(commonCmdData, cmd)
common.SetupVirtualMergeIntoCommit(commonCmdData, cmd)
common.SetupGitUnshallow(commonCmdData, cmd)
common.SetupAllowGitShallowClone(commonCmdData, cmd)
return cmd
}
func runImagesPublish(commonCmdData *common.CmdData, imagesToProcess []string) error {
ctx := common.BackgroundContext()
werf.PostponeMultiwerfNotUpToDateWarning()
werf.PostponeWerf11DeprecationWarning()
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
if err := image.Init(); err != nil {
return err
}
if err := lrumeta.Init(); err != nil {
return err
}
if err := true_git.Init(true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}
if err := common.DockerRegistryInit(commonCmdData); err != nil {
return err
}
if err := docker.Init(ctx, *commonCmdData.DockerConfig, *commonCmdData.LogVerbose, *commonCmdData.LogDebug); err != nil {
return err
}
ctxWithDockerCli, err := docker.NewContext(ctx)
if err != nil {
return err
}
ctx = ctxWithDockerCli
projectDir, err := common.GetProjectDir(commonCmdData)
if err != nil {
return fmt.Errorf("getting project dir failed: %s", err)
}
common.ProcessLogProjectDir(commonCmdData, projectDir)
werfConfig, err := common.GetRequiredWerfConfig(ctx, projectDir, commonCmdData, true)
if err != nil {
return fmt.Errorf("unable to load werf config: %s", err)
}
logboek.LogOptionalLn()
for _, imageToProcess := range imagesToProcess {
if !werfConfig.HasImage(imageToProcess) {
return fmt.Errorf("specified image %s is not defined in werf.yaml", logging.ImageLogName(imageToProcess, false))
}
}
projectName := werfConfig.Meta.Project
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
defer tmp_manager.ReleaseProjectDir(projectTmpDir)
containerRuntime := &container_runtime.LocalDockerServerRuntime{} // TODO
stagesStorage, err := common.GetStagesStorage(containerRuntime, commonCmdData)
if err != nil {
return err
}
synchronization, err := common.GetSynchronization(ctx, commonCmdData, projectName, stagesStorage)
if err != nil {
return err
}
stagesStorageCache, err := common.GetStagesStorageCache(synchronization)
if err != nil {
return err
}
storageLockManager, err := common.GetStorageLockManager(ctx, synchronization)
if err != nil {
return err
}
storageManager := manager.NewStorageManager(projectName, storageLockManager, stagesStorageCache)
if err := storageManager.UseStagesStorage(ctx, stagesStorage); err != nil {
return err
}
imagesRepo, err := common.GetImagesRepo(ctx, projectName, commonCmdData)
if err != nil {
return err
}
tagOpts, err := common.GetTagOptions(commonCmdData, common.TagOptionsGetterOptions{})
if err != nil {
return err
}
if err := ssh_agent.Init(ctx, common.GetSSHKey(commonCmdData)); err != nil {
return fmt.Errorf("cannot initialize ssh agent: %s", err)
}
defer func() {
err := ssh_agent.Terminate()
if err != nil {
logboek.Warn().LogF("WARNING: ssh agent termination failed: %s\n", err)
}
}()
publishReportFormat, err := common.GetPublishReportFormat(commonCmdData)
if err != nil {
return err
}
opts := build.PublishImagesOptions{
ImagesToPublish: imagesToProcess,
TagOptions: tagOpts,
PublishReportPath: *commonCmdData.PublishReportPath,
PublishReportFormat: publishReportFormat,
}
conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, imagesToProcess, projectDir, projectTmpDir, ssh_agent.SSHAuthSock, containerRuntime, storageManager, imagesRepo, storageLockManager, common.GetConveyorOptions(commonCmdData))
defer conveyorWithRetry.Terminate()
if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
return c.PublishImages(ctx, opts)
}); err != nil {
return err
}
return nil
}