forked from openshift/source-to-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
396 lines (346 loc) · 13.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openshift/source-to-image/pkg/api"
"github.com/openshift/source-to-image/pkg/api/describe"
"github.com/openshift/source-to-image/pkg/api/validation"
"github.com/openshift/source-to-image/pkg/build"
"github.com/openshift/source-to-image/pkg/build/strategies"
"github.com/openshift/source-to-image/pkg/build/strategies/sti"
cmdutil "github.com/openshift/source-to-image/pkg/cmd"
"github.com/openshift/source-to-image/pkg/config"
"github.com/openshift/source-to-image/pkg/create"
"github.com/openshift/source-to-image/pkg/docker"
"github.com/openshift/source-to-image/pkg/errors"
"github.com/openshift/source-to-image/pkg/run"
"github.com/openshift/source-to-image/pkg/tar"
"github.com/openshift/source-to-image/pkg/util"
"github.com/openshift/source-to-image/pkg/version"
)
func newCmdVersion() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Display version",
Long: "Display version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("s2i %v\n", version.Get())
},
}
}
func newCmdBuild(cfg *api.Config) *cobra.Command {
useConfig := false
oldScriptsFlag := ""
oldDestination := ""
buildCmd := &cobra.Command{
Use: "build <source> <image> [<tag>]",
Short: "Build a new image",
Long: "Build a new Docker image named <tag> (if provided) from a source repository and base image.",
Example: `
# Build an application Docker image from a Git repository
$ s2i build git://github.com/openshift/ruby-hello-world centos/ruby-22-centos7 hello-world-app
# Build from a local directory
$ s2i build . centos/ruby-22-centos7 hello-world-app
`,
Run: func(cmd *cobra.Command, args []string) {
if glog.V(1) {
glog.Infof("Running S2I version %q\n", version.Get())
}
// Attempt to restore the build command from the configuration file
if useConfig {
config.Restore(cfg, cmd)
}
// If user specifies the arguments, then we override the stored ones
if len(args) >= 2 {
cfg.Source = args[0]
cfg.BuilderImage = args[1]
if len(args) >= 3 {
cfg.Tag = args[2]
}
}
if cfg.ForcePull {
glog.Warning("DEPRECATED: The '--force-pull' option is deprecated. Use '--pull-policy' instead")
}
if len(cfg.BuilderPullPolicy) == 0 {
cfg.BuilderPullPolicy = api.DefaultBuilderPullPolicy
}
if len(cfg.PreviousImagePullPolicy) == 0 {
cfg.PreviousImagePullPolicy = api.DefaultPreviousImagePullPolicy
}
if errs := validation.ValidateConfig(cfg); len(errs) > 0 {
for _, e := range errs {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", e)
}
fmt.Println()
cmd.Help()
os.Exit(1)
}
// Persists the current command line options and config into .s2ifile
if useConfig {
config.Save(cfg, cmd)
}
// Attempt to read the .dockercfg and extract the authentication for
// docker pull
if r, err := os.Open(cfg.DockerCfgPath); err == nil {
defer r.Close()
auths := docker.LoadImageRegistryAuth(r)
cfg.PullAuthentication = docker.GetImageRegistryAuth(auths, cfg.BuilderImage)
if cfg.Incremental {
cfg.IncrementalAuthentication = docker.GetImageRegistryAuth(auths, cfg.Tag)
}
}
if len(cfg.EnvironmentFile) > 0 {
result, err := util.ReadEnvironmentFile(cfg.EnvironmentFile)
if err != nil {
glog.Warningf("Unable to read environment file %q: %v", cfg.EnvironmentFile, err)
} else {
for name, value := range result {
cfg.Environment = append(cfg.Environment, api.EnvironmentSpec{Name: name, Value: value})
}
}
}
if len(oldScriptsFlag) != 0 {
glog.Warning("DEPRECATED: Flag --scripts is deprecated, use --scripts-url instead")
cfg.ScriptsURL = oldScriptsFlag
}
if len(oldDestination) != 0 {
glog.Warning("DEPRECATED: Flag --location is deprecated, use --destination instead")
cfg.Destination = oldDestination
}
if glog.V(2) {
fmt.Printf("\n%s\n", describe.DescribeConfig(cfg))
}
if !docker.IsReachable(cfg) {
glog.Fatalf("Unable to connect to Docker daemon. Please set the DOCKER_HOST or make sure the Docker socket %q exists", cfg.DockerConfig.Endpoint)
}
builder, err := strategies.GetStrategy(cfg)
checkErr(err)
result, err := builder.Build(cfg)
checkErr(err)
for _, message := range result.Messages {
glog.V(1).Infof(message)
}
if cfg.RunImage {
runner, err := run.New(cfg)
checkErr(err)
err = runner.Run(cfg)
checkErr(err)
}
},
}
cmdutil.AddCommonFlags(buildCmd, cfg)
buildCmd.Flags().BoolVar(&(cfg.RunImage), "run", false, "Run resulting image as part of invocation of this command")
buildCmd.Flags().BoolVar(&(cfg.DisableRecursive), "recursive", true, "Fetch all git submodules when cloning application repository")
buildCmd.Flags().VarP(&(cfg.Environment), "env", "e", "Specify an single environment variable in NAME=VALUE format")
buildCmd.Flags().StringVarP(&(cfg.Ref), "ref", "r", "", "Specify a ref to check-out")
buildCmd.Flags().StringVarP(&(cfg.AssembleUser), "assemble-user", "", "", "Specify the user to run assemble with")
buildCmd.Flags().StringVarP(&(cfg.ContextDir), "context-dir", "", "", "Specify the sub-directory inside the repository with the application sources")
buildCmd.Flags().StringVarP(&(cfg.ExcludeRegExp), "exclude", "", tar.DefaultExclusionPattern.String(), "Regular expression for selecting files from the source tree to exclude from the build, where the default excludes the '.git' directory (see https://golang.org/pkg/regexp for syntax, but note that \"\" will be interpreted as allow all files and exclude no files)")
buildCmd.Flags().StringVarP(&(cfg.ScriptsURL), "scripts-url", "s", "", "Specify a URL for the assemble and run scripts")
buildCmd.Flags().StringVar(&(oldScriptsFlag), "scripts", "", "DEPRECATED: Specify a URL for the assemble and run scripts")
buildCmd.Flags().BoolVar(&(useConfig), "use-config", false, "Store command line options to .stifile")
buildCmd.Flags().StringVarP(&(cfg.EnvironmentFile), "environment-file", "E", "", "Specify the path to the file with environment")
buildCmd.Flags().StringVarP(&(cfg.DisplayName), "application-name", "n", "", "Specify the display name for the application (default: output image name)")
buildCmd.Flags().StringVarP(&(cfg.Description), "description", "", "", "Specify the description of the application")
buildCmd.Flags().VarP(&(cfg.AllowedUIDs), "allowed-uids", "u", "Specify a range of allowed user ids for the builder image")
buildCmd.Flags().VarP(&(cfg.Injections), "inject", "i", "Specify a directory to inject into the assemble container")
buildCmd.Flags().VarP(&(cfg.BuildVolumes), "volume", "v", "Specify a volume to mount into the assemble container")
buildCmd.Flags().StringSliceVar(&(cfg.DropCapabilities), "cap-drop", []string{}, "Specify a comma-separated list of capabilities to drop when running Docker containers")
buildCmd.Flags().StringVarP(&(oldDestination), "location", "l", "",
"DEPRECATED: Specify a destination location for untar operation")
buildCmd.Flags().BoolVarP(&(cfg.ForceCopy), "copy", "c", false, "Use local file system copy instead of git cloning the source url")
return buildCmd
}
func newCmdRebuild(cfg *api.Config) *cobra.Command {
buildCmd := &cobra.Command{
Use: "rebuild <image> [<new-tag>]",
Short: "Rebuild an existing image",
Long: "Rebuild an existing application image that was built by S2I previously.",
Run: func(cmd *cobra.Command, args []string) {
// If user specifies the arguments, then we override the stored ones
if len(args) >= 1 {
cfg.Tag = args[0]
} else {
cmd.Help()
os.Exit(1)
}
if r, err := os.Open(cfg.DockerCfgPath); err == nil {
defer r.Close()
cfg.PullAuthentication = docker.LoadAndGetImageRegistryAuth(r, cfg.Tag)
}
err := build.GenerateConfigFromLabels(cfg)
checkErr(err)
if len(args) >= 2 {
cfg.Tag = args[1]
}
// Attempt to read the .dockercfg and extract the authentication for
// docker pull
if r, err := os.Open(cfg.DockerCfgPath); err == nil {
defer r.Close()
cfg.PullAuthentication = docker.LoadAndGetImageRegistryAuth(r, cfg.BuilderImage)
}
if len(cfg.BuilderPullPolicy) == 0 {
cfg.BuilderPullPolicy = api.DefaultBuilderPullPolicy
}
if len(cfg.PreviousImagePullPolicy) == 0 {
cfg.PreviousImagePullPolicy = api.DefaultPreviousImagePullPolicy
}
if glog.V(2) {
fmt.Printf("\n%s\n", describe.DescribeConfig(cfg))
}
builder, err := strategies.GetStrategy(cfg)
checkErr(err)
result, err := builder.Build(cfg)
checkErr(err)
for _, message := range result.Messages {
glog.V(1).Infof(message)
}
},
}
cmdutil.AddCommonFlags(buildCmd, cfg)
return buildCmd
}
func newCmdCreate() *cobra.Command {
return &cobra.Command{
Use: "create <imageName> <destination>",
Short: "Bootstrap a new S2I image repository",
Long: "Bootstrap a new S2I image with given imageName inside the destination directory",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
cmd.Help()
os.Exit(1)
}
b := create.New(args[0], args[1])
b.AddSTIScripts()
b.AddDockerfile()
b.AddTests()
},
}
}
func newCmdGenBashCompletion(root *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "genbashcompletion",
Short: "Generate Bash completion for the s2i command",
Long: "Generate Bash completion for the s2i command into standard output",
Run: func(cmd *cobra.Command, args []string) {
// TODO: The version of cobra we vendor takes a
// *bytes.Buffer, while newer versions take an
// io.Writer. The code below could be simplified to a
// single line `root.GenBashCompletion(os.Stdout)` when
// we update cobra.
var out bytes.Buffer
root.GenBashCompletion(&out)
fmt.Print(out.String())
},
}
}
func newCmdUsage(cfg *api.Config) *cobra.Command {
oldScriptsFlag := ""
oldDestination := ""
usageCmd := &cobra.Command{
Use: "usage <image>",
Short: "Print usage of the assemble script associated with the image",
Long: "Create and start a container from the image and invoke its usage script.",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
os.Exit(1)
}
cfg.Usage = true
cfg.BuilderImage = args[0]
if len(oldScriptsFlag) != 0 {
glog.Warning("DEPRECATED: Flag --scripts is deprecated, use --scripts-url instead")
cfg.ScriptsURL = oldScriptsFlag
}
if len(cfg.BuilderPullPolicy) == 0 {
cfg.BuilderPullPolicy = api.DefaultBuilderPullPolicy
}
if len(cfg.PreviousImagePullPolicy) == 0 {
cfg.PreviousImagePullPolicy = api.DefaultPreviousImagePullPolicy
}
uh, err := sti.NewUsage(cfg)
checkErr(err)
err = uh.Show()
checkErr(err)
},
}
usageCmd.Flags().StringVarP(&(oldDestination), "location", "l", "",
"Specify a destination location for untar operation")
cmdutil.AddCommonFlags(usageCmd, cfg)
return usageCmd
}
func setupGlog(flags *pflag.FlagSet) {
from := flag.CommandLine
if fflag := from.Lookup("v"); fflag != nil {
level := fflag.Value.(*glog.Level)
levelPtr := (*int32)(level)
flags.Int32Var(levelPtr, "loglevel", 0, "Set the level of log output (0-5)")
}
// FIXME currently glog has only option to redirect output to stderr
// the preferred for S2I would be to redirect to stdout
flag.CommandLine.Set("logtostderr", "true")
}
func checkErr(err error) {
if err == nil {
return
}
if e, ok := err.(errors.Error); ok {
glog.Errorf("An error occurred: %v", e)
glog.Errorf("Suggested solution: %v", e.Suggestion)
if e.Details != nil {
glog.V(1).Infof("Details: %v", e.Details)
}
glog.Error("If the problem persists consult the docs at https://github.com/openshift/source-to-image/tree/master/docs. " +
"Eventually reach us on freenode #openshift or file an issue at https://github.com/openshift/source-to-image/issues " +
"providing us with a log from your build using --loglevel=3")
os.Exit(e.ErrorCode)
} else {
glog.Errorf("An error occurred: %v", err)
os.Exit(1)
}
}
func main() {
cfg := &api.Config{}
stiCmd := &cobra.Command{
Use: "s2i",
Long: "Source-to-image (S2I) is a tool for building repeatable docker images.\n\n" +
"A command line interface that injects and assembles source code into a docker image.\n" +
"Complete documentation is available at http://github.com/openshift/source-to-image",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
cfg.DockerConfig = docker.GetDefaultDockerConfig()
stiCmd.PersistentFlags().StringVarP(&(cfg.DockerConfig.Endpoint), "url", "U", cfg.DockerConfig.Endpoint, "Set the url of the docker socket to use")
stiCmd.PersistentFlags().StringVar(&(cfg.DockerConfig.CertFile), "cert", cfg.DockerConfig.CertFile, "Set the path of the docker TLS certificate file")
stiCmd.PersistentFlags().StringVar(&(cfg.DockerConfig.KeyFile), "key", cfg.DockerConfig.KeyFile, "Set the path of the docker TLS key file")
stiCmd.PersistentFlags().StringVar(&(cfg.DockerConfig.CAFile), "ca", cfg.DockerConfig.CAFile, "Set the path of the docker TLS ca file")
stiCmd.AddCommand(newCmdVersion())
stiCmd.AddCommand(newCmdBuild(cfg))
stiCmd.AddCommand(newCmdRebuild(cfg))
stiCmd.AddCommand(newCmdUsage(cfg))
stiCmd.AddCommand(newCmdCreate())
setupGlog(stiCmd.PersistentFlags())
basename := filepath.Base(os.Args[0])
// Make case-insensitive and strip executable suffix if present
if runtime.GOOS == "windows" {
basename = strings.ToLower(basename)
basename = strings.TrimSuffix(basename, ".exe")
}
if basename == "sti" {
glog.Warning("sti binary is deprecated, use s2i instead")
}
stiCmd.AddCommand(newCmdGenBashCompletion(stiCmd))
err := stiCmd.Execute()
if err != nil {
os.Exit(1)
}
}