/
base.go
102 lines (83 loc) · 3.12 KB
/
base.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
package buildah
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/werf/werf/pkg/buildah/thirdparty"
"github.com/werf/werf/pkg/util"
)
type BaseBuildah struct {
Isolation thirdparty.Isolation
TmpDir string
InstanceTmpDir string
ConfigTmpDir string
SignaturePolicyPath string
RegistriesConfigPath string
RegistriesConfigDirPath string
Insecure bool
}
type BaseBuildahOpts struct {
Isolation thirdparty.Isolation
Insecure bool
}
func NewBaseBuildah(tmpDir string, opts BaseBuildahOpts) (*BaseBuildah, error) {
b := &BaseBuildah{
Isolation: opts.Isolation,
TmpDir: tmpDir,
Insecure: opts.Insecure,
}
if err := os.MkdirAll(b.TmpDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to create dir %q: %s", b.TmpDir, err)
}
var err error
b.InstanceTmpDir, err = ioutil.TempDir(b.TmpDir, "instance")
if err != nil {
return nil, fmt.Errorf("unable to create instance tmp dir: %s", err)
}
b.ConfigTmpDir = filepath.Join(b.InstanceTmpDir, "config")
if err := os.MkdirAll(b.ConfigTmpDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to create dir %q: %s", b.ConfigTmpDir, err)
}
b.SignaturePolicyPath = filepath.Join(b.ConfigTmpDir, "policy.json")
if err := ioutil.WriteFile(b.SignaturePolicyPath, []byte(DefaultSignaturePolicy), os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to write file %q: %s", b.SignaturePolicyPath, err)
}
b.RegistriesConfigPath = filepath.Join(b.ConfigTmpDir, "registries.conf")
if err := ioutil.WriteFile(b.RegistriesConfigPath, []byte(DefaultRegistriesConfig), os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to write file %q: %s", b.RegistriesConfigPath, err)
}
b.RegistriesConfigDirPath = filepath.Join(b.ConfigTmpDir, "registries.conf.d")
if err := os.MkdirAll(b.RegistriesConfigDirPath, os.ModePerm); err != nil {
return nil, fmt.Errorf("unable to create dir %q: %s", b.RegistriesConfigDirPath, err)
}
return b, nil
}
func (b *BaseBuildah) NewSessionTmpDir() (string, error) {
sessionTmpDir, err := ioutil.TempDir(b.TmpDir, "session")
if err != nil {
return "", fmt.Errorf("unable to create session tmp dir: %s", err)
}
return sessionTmpDir, nil
}
func (b *BaseBuildah) prepareBuildFromDockerfile(dockerfile []byte, contextTar io.Reader) (string, string, string, error) {
sessionTmpDir, err := b.NewSessionTmpDir()
if err != nil {
return "", "", "", err
}
dockerfileTmpPath := filepath.Join(sessionTmpDir, "Dockerfile")
if err := ioutil.WriteFile(dockerfileTmpPath, dockerfile, os.ModePerm); err != nil {
return "", "", "", fmt.Errorf("error writing %q: %s", dockerfileTmpPath, err)
}
contextTmpDir := filepath.Join(sessionTmpDir, "context")
if err := os.MkdirAll(contextTmpDir, os.ModePerm); err != nil {
return "", "", "", fmt.Errorf("unable to create dir %q: %s", contextTmpDir, err)
}
if contextTar != nil {
if err := util.ExtractTar(contextTar, contextTmpDir); err != nil {
return "", "", "", fmt.Errorf("unable to extract context tar to tmp context dir: %s", err)
}
}
return sessionTmpDir, contextTmpDir, dockerfileTmpPath, nil
}