Repository navigation
Expand file tree
/
Copy pathcloudbuild.go
More file actions
183 lines (155 loc) · 4.68 KB
/
Copy pathcloudbuild.go
File metadata and controls
183 lines (155 loc) · 4.68 KB
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
package forge
import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/frantjc/forge/cloudbuild"
"github.com/frantjc/forge/envconv"
"github.com/frantjc/forge/internal/bin"
"github.com/frantjc/forge/internal/featureflags"
xtar "github.com/frantjc/x/archive/tar"
xos "github.com/frantjc/x/os"
xslices "github.com/frantjc/x/slices"
)
type CloudBuild struct {
cloudbuild.Step
}
func (o *CloudBuild) Run(ctx context.Context, containerRuntime ContainerRuntime, opts ...RunOpt) error {
opt := runOptsWithDefaults(opts...)
image, err := containerRuntime.PullImage(ctx, o.Name)
if err != nil {
return err
}
home := "/root"
if config, err := image.Config(); err == nil {
for _, env := range config.Env {
if strings.HasPrefix(env, "HOME=") {
home = strings.TrimPrefix(env, "HOME=")
break
}
}
}
containerConfig, script, err := stepToContainerConfigAndScript(&o.Step, home, image, opt)
if err != nil {
return err
}
containerConfig.Mounts = overrideMounts(containerConfig.Mounts, opt.Mounts...)
container, err := createSleepingContainer(ctx, containerRuntime, image, containerConfig, opt)
if err != nil {
return err
}
defer container.Stop(ctx) //nolint:errcheck
defer container.Remove(ctx) //nolint:errcheck
if !featureflags.BindMounts {
_home := os.Getenv("HOME")
if _home == "" {
if u, err := user.Current(); err == nil {
_home = u.HomeDir
}
}
source := filepath.Join(_home, ".config/gcloud")
if fi, err := os.Stat(source); err == nil && fi.IsDir() {
rc := xtar.Compress(source)
defer rc.Close()
if err = container.CopyTo(ctx, filepath.Join(home, ".config"), rc); err != nil {
return fmt.Errorf("copy gcloud config to container: %w", err)
}
}
}
if err = copyScriptToContainer(ctx, container, script, opt); err != nil {
return err
}
if exitCode, err := container.Exec(ctx, containerConfig, opt.Streams); err != nil {
return err
} else if exitCode > 0 {
return xos.NewExitCodeError(ErrContainerExitedWithNonzeroExitCode, exitCode)
}
return nil
}
func copyScriptToContainer(ctx context.Context, container Container, script string, opt *RunOpts) error {
if !bin.HasShebang(script) {
script = fmt.Sprintf("#!/usr/bin/env sh\nset -eo pipefail\n%s", script)
}
if err := container.CopyTo(ctx, opt.WorkingDir, bin.NewScriptTarArchive(script, ScriptName)); err != nil {
return err
}
return nil
}
func stepToContainerConfigAndScript(step *cloudbuild.Step, home string, image Image, opt *RunOpts) (*ContainerConfig, string, error) {
var (
containerConfig = &ContainerConfig{
Entrypoint: []string{},
Env: step.Env,
WorkingDir: CloudBuildWorkingDir(opt.WorkingDir),
}
substitutions = step.Substitutions
mapping = func(s string) string {
if substitution, ok := substitutions[s]; ok {
return substitution
}
return fmt.Sprintf("$%s", s)
}
_home = os.Getenv("HOME")
)
if _home == "" {
if u, err := user.Current(); err == nil {
_home = u.HomeDir
}
}
source := filepath.Join(_home, ".config/gcloud")
if fi, err := os.Stat(source); err == nil && fi.IsDir() {
containerConfig.Mounts = []Mount{
{
Destination: filepath.Join(home, ".config/gcloud"),
},
}
if featureflags.BindMounts {
containerConfig.Mounts[0].Source = source
}
}
if step.Script != "" {
if step.Entrypoint != "" {
return nil, "", fmt.Errorf("cannot specify entrypoint with script")
}
containerConfig.Entrypoint = append([]string{filepath.Join(opt.WorkingDir, ScriptName)}, step.Args...)
} else {
if lenArgs := len(step.Args); step.Entrypoint == "" || lenArgs == 0 {
config, err := image.Config()
if err != nil {
return nil, "", err
}
if step.Entrypoint != "" {
containerConfig.Entrypoint = []string{step.Entrypoint}
} else {
containerConfig.Entrypoint = config.Entrypoint
}
if lenArgs == 0 {
containerConfig.Entrypoint = append(containerConfig.Entrypoint, config.Cmd...)
} else {
containerConfig.Entrypoint = append(containerConfig.Entrypoint, step.Args...)
}
} else {
containerConfig.Entrypoint = append([]string{step.Entrypoint}, step.Args...)
}
}
if step.DynamicSubstitutions {
for range []byte{0, 0} {
for k, v := range substitutions {
substitutions[k] = os.Expand(v, mapping)
}
}
}
containerConfig.Entrypoint = xslices.Map(containerConfig.Entrypoint, func(s string, _ int) string {
return os.Expand(s, mapping)
})
containerConfig.Env = xslices.Map(containerConfig.Env, func(s string, _ int) string {
return os.Expand(s, mapping)
})
if step.AutomapSubstitutions {
containerConfig.Env = append(containerConfig.Env, envconv.MapToArr(substitutions)...)
}
return containerConfig, os.Expand(step.Script, mapping), nil
}