|
| 1 | +package external |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | + "syscall" |
| 11 | + "text/template" |
| 12 | + |
| 13 | + "github.com/openshift/source-to-image/pkg/api" |
| 14 | + "github.com/openshift/source-to-image/pkg/build/strategies/dockerfile" |
| 15 | + "github.com/openshift/source-to-image/pkg/util/fs" |
| 16 | + utillog "github.com/openshift/source-to-image/pkg/util/log" |
| 17 | +) |
| 18 | + |
| 19 | +// External represents the shell out for external build commands, therefore s2i based build can |
| 20 | +// execute the generation of |
| 21 | +type External struct { |
| 22 | + dockerfile *dockerfile.Dockerfile |
| 23 | +} |
| 24 | + |
| 25 | +// s2iDockerfile Dockerfile default filename. |
| 26 | +const s2iDockerfile = "Dockerfile.s2i" |
| 27 | + |
| 28 | +var ( |
| 29 | + // local logger |
| 30 | + log = utillog.StderrLog |
| 31 | + // supported external commands, template is based on api.Config instance |
| 32 | + commands = map[string]string{ |
| 33 | + "buildah": `buildah bud --tag {{ .Tag }} --file {{ .AsDockerfile }} {{ or .WorkingDir "." }}`, |
| 34 | + "podman": `podman build --tag {{ .Tag }} --file {{ .AsDockerfile }} {{ or .WorkingDir "." }}`, |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +// GetBuilders returns a list of command names, based global commands map. |
| 39 | +func GetBuilders() []string { |
| 40 | + builders := []string{} |
| 41 | + for k := range commands { |
| 42 | + builders = append(builders, k) |
| 43 | + } |
| 44 | + return builders |
| 45 | +} |
| 46 | + |
| 47 | +// ValidBuilderName returns a boolean based in keys of global commands map. |
| 48 | +func ValidBuilderName(name string) bool { |
| 49 | + _, exists := commands[name] |
| 50 | + return exists |
| 51 | +} |
| 52 | + |
| 53 | +// renderCommand render a shell command based in api.Config instance. Attribute WithBuilder |
| 54 | +// wll determine external builder name, and api.Config feeds command's template variables. It can |
| 55 | +// return error in case of template parsing or evaluation issues. |
| 56 | +func (e *External) renderCommand(config *api.Config) (string, error) { |
| 57 | + commandTemplate, exists := commands[config.WithBuilder] |
| 58 | + if !exists { |
| 59 | + return "", fmt.Errorf("cannot find command '%s' in dictionary: '%#v'", |
| 60 | + config.WithBuilder, commands) |
| 61 | + } |
| 62 | + |
| 63 | + t, err := template.New("external-command").Parse(commandTemplate) |
| 64 | + if err != nil { |
| 65 | + return "", err |
| 66 | + } |
| 67 | + var output bytes.Buffer |
| 68 | + if err = t.Execute(&output, config); err != nil { |
| 69 | + return "", err |
| 70 | + } |
| 71 | + return output.String(), nil |
| 72 | +} |
| 73 | + |
| 74 | +// execute the given external command using "os/exec". Returns the outcomes as api.Result, making |
| 75 | +// sure it only marks result as success when exit-code is zero. Therefore, it returns errors based |
| 76 | +// in external command errors, so "s2i build" also fails. |
| 77 | +func (e *External) execute(externalCommand string) (*api.Result, error) { |
| 78 | + log.V(0).Infof("Executing external build command: '%s'", externalCommand) |
| 79 | + |
| 80 | + externalCommandSlice := strings.Split(externalCommand, " ") |
| 81 | + cmd := exec.Command(externalCommandSlice[0], externalCommandSlice[1:]...) |
| 82 | + cmd.Stdout = os.Stdout |
| 83 | + cmd.Stderr = os.Stderr |
| 84 | + |
| 85 | + res := &api.Result{Success: false} |
| 86 | + res.Messages = append(res.Messages, fmt.Sprintf("Running command: '%s'", externalCommand)) |
| 87 | + err := cmd.Start() |
| 88 | + if err != nil { |
| 89 | + res.Messages = append(res.Messages, err.Error()) |
| 90 | + return res, err |
| 91 | + } |
| 92 | + |
| 93 | + if err := cmd.Wait(); err != nil { |
| 94 | + if exitErr, okay := err.(*exec.ExitError); okay { |
| 95 | + if status, okay := exitErr.Sys().(syscall.WaitStatus); okay { |
| 96 | + exitCode := status.ExitStatus() |
| 97 | + log.V(0).Infof("External command return-code: %d", exitCode) |
| 98 | + res.Messages = append(res.Messages, fmt.Sprintf("exit-code: %d", exitCode)) |
| 99 | + if exitCode == 0 { |
| 100 | + res.Success = true |
| 101 | + } else { |
| 102 | + return res, exitErr |
| 103 | + } |
| 104 | + } |
| 105 | + } else { |
| 106 | + return res, err |
| 107 | + } |
| 108 | + } |
| 109 | + return res, nil |
| 110 | +} |
| 111 | + |
| 112 | +// asDockerfile inspect config, if user has already informed `--as-dockerfile` option, that's simply |
| 113 | +// returned, otherwise, considering `--working-dir` option first before using artificial name. |
| 114 | +func (e *External) asDockerfile(config *api.Config) string { |
| 115 | + if len(config.AsDockerfile) > 0 { |
| 116 | + return config.AsDockerfile |
| 117 | + } |
| 118 | + |
| 119 | + if len(config.WorkingDir) > 0 { |
| 120 | + return path.Join(config.WorkingDir, s2iDockerfile) |
| 121 | + } |
| 122 | + return s2iDockerfile |
| 123 | +} |
| 124 | + |
| 125 | +// Build triggers the build of a "strategy/dockerfile" to obtain "AsDockerfile" first, and then |
| 126 | +// proceed to execute the external command. |
| 127 | +func (e *External) Build(config *api.Config) (*api.Result, error) { |
| 128 | + config.AsDockerfile = e.asDockerfile(config) |
| 129 | + |
| 130 | + externalCommand, err := e.renderCommand(config) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + // generating dockerfile following AsDockerfile directive |
| 136 | + err = e.dockerfile.CreateDockerfile(config) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + return e.execute(externalCommand) |
| 142 | +} |
| 143 | + |
| 144 | +// New instance of External command strategy. |
| 145 | +func New(config *api.Config, fs fs.FileSystem) (*External, error) { |
| 146 | + df, err := dockerfile.New(config, fs) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + return &External{dockerfile: df}, nil |
| 151 | +} |
0 commit comments