/
main.go
168 lines (133 loc) · 5.24 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
package cmd_factory
import (
"fmt"
"github.com/spf13/cobra"
"github.com/flant/logboek"
"github.com/flant/werf/cmd/werf/common"
"github.com/flant/werf/pkg/build"
"github.com/flant/werf/pkg/docker"
"github.com/flant/werf/pkg/docker_registry"
"github.com/flant/werf/pkg/image"
"github.com/flant/werf/pkg/lock"
"github.com/flant/werf/pkg/logging"
"github.com/flant/werf/pkg/ssh_agent"
"github.com/flant/werf/pkg/tmp_manager"
"github.com/flant/werf/pkg/true_git"
"github.com/flant/werf/pkg/werf"
)
type CmdData struct {
PullUsername string
PullPassword string
IntrospectBeforeError bool
IntrospectAfterError bool
}
func NewCmdWithData(cmdData *CmdData, commonCmdData *common.CmdData) *cobra.Command {
cmd := &cobra.Command{
Use: "build [IMAGE_NAME...]",
Short: "Build stages",
Example: ` # Build stages of all images from werf.yaml, built stages will be placed locally
$ werf stages build --stages-storage :local
# Build stages of image 'backend' from werf.yaml
$ werf stages build --stages-storage :local backend
# Build and enable drop-in shell session in the failed assembly container in the case when an error occurred
$ werf build --stages-storage :local --introspect-error
# Set --stages-storage default value using $WERF_STAGES_STORAGE param
$ export WERF_STAGES_STORAGE=:local
$ werf build`,
Long: common.GetLongCommandDescription(`Build stages for images described in the werf.yaml.
The result of build command are built stages pushed into the specified stages storage (or locally in the case when --stages-storage=:local).
If one or more IMAGE_NAME parameters specified, werf will build only these images stages from werf.yaml`),
DisableFlagsInUseLine: true,
Annotations: map[string]string{
common.CmdEnvAnno: common.EnvsDescription(common.WerfDebugAnsibleArgs),
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := common.ProcessLogOptions(commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return common.LogRunningTime(func() error {
return runStagesBuild(cmdData, commonCmdData, args)
})
},
}
common.SetupDir(commonCmdData, cmd)
common.SetupTmpDir(commonCmdData, cmd)
common.SetupHomeDir(commonCmdData, cmd)
common.SetupSSHKey(commonCmdData, cmd)
common.SetupStagesStorage(commonCmdData, cmd)
common.SetupDockerConfig(commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified stages storage, to pull base images")
common.SetupInsecureRepo(commonCmdData, cmd)
common.SetupIntrospectStage(commonCmdData, cmd)
common.SetupLogOptions(commonCmdData, cmd)
common.SetupLogProjectDir(commonCmdData, cmd)
cmd.Flags().BoolVarP(&cmdData.IntrospectAfterError, "introspect-error", "", false, "Introspect failed stage in the state, right after running failed assembly instruction")
cmd.Flags().BoolVarP(&cmdData.IntrospectBeforeError, "introspect-before-error", "", false, "Introspect failed stage in the clean state, before running all assembly instructions of the stage")
return cmd
}
func runStagesBuild(cmdData *CmdData, commonCmdData *common.CmdData, imagesToProcess []string) error {
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
if err := lock.Init(); err != nil {
return err
}
if err := true_git.Init(true_git.Options{Out: logboek.GetOutStream(), Err: logboek.GetErrStream()}); err != nil {
return err
}
if err := docker_registry.Init(docker_registry.Options{AllowInsecureRepo: *commonCmdData.InsecureRepo}); err != nil {
return err
}
if err := docker.Init(*commonCmdData.DockerConfig); err != nil {
return err
}
projectDir, err := common.GetProjectDir(commonCmdData)
if err != nil {
return fmt.Errorf("getting project dir failed: %s", err)
}
common.ProcessLogProjectDir(commonCmdData, projectDir)
werfConfig, err := common.GetWerfConfig(projectDir)
if err != nil {
return fmt.Errorf("bad config: %s", err)
}
for _, imageToProcess := range imagesToProcess {
if !werfConfig.HasImage(imageToProcess) {
return fmt.Errorf("specified image %s is not defined in werf.yaml", logging.ImageLogName(imageToProcess, false))
}
}
projectTmpDir, err := tmp_manager.CreateProjectDir()
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
defer tmp_manager.ReleaseProjectDir(projectTmpDir)
stagesRepo, err := common.GetStagesRepo(commonCmdData)
if err != nil {
return err
}
if err := ssh_agent.Init(*commonCmdData.SSHKeys); err != nil {
return fmt.Errorf("cannot initialize ssh agent: %s", err)
}
defer func() {
err := ssh_agent.Terminate()
if err != nil {
logboek.LogErrorF("WARNING: ssh agent termination failed: %s\n", err)
}
}()
introspectOptions, err := common.GetIntrospectOptions(commonCmdData, werfConfig)
if err != nil {
return err
}
opts := build.BuildStagesOptions{
ImageBuildOptions: image.BuildOptions{
IntrospectAfterError: cmdData.IntrospectAfterError,
IntrospectBeforeError: cmdData.IntrospectBeforeError,
},
IntrospectOptions: introspectOptions,
}
c := build.NewConveyor(werfConfig, imagesToProcess, projectDir, projectTmpDir, ssh_agent.SSHAuthSock)
if err = c.BuildStages(stagesRepo, opts); err != nil {
return err
}
return nil
}