forked from containers/build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
286 lines (246 loc) · 7.18 KB
/
common.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
// Copyright 2015 The appc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lib
import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/containers/build/lib/appc"
"github.com/containers/build/lib/oci"
"github.com/containers/build/util"
)
const OCISchemaVersion = 2
const defaultWorkPath = ".acbuild"
type OCILayout struct {
imageLayoutVersion string `json:"imageLayoutVersion"`
}
var OCILayoutValue = OCILayout{"1.0.0"}
// BuildMode represents which image spec is being followed during a build, AppC
// or OCI
type BuildMode string
const (
BuildModeAppC = BuildMode("appc")
BuildModeOCI = BuildMode("oci")
)
var (
errNoBuildInProgress = fmt.Errorf("no build in progress in this working dir - try \"acbuild begin\"")
)
// ACBuild contains all the information for a current build. Once an ACBuild
// has been created, the functions available on it will perform different
// actions in the build, like updating a dependency or writing a finished ACI.
type ACBuild struct {
ContextPath string
LockPath string
CurrentImagePath string
DepStoreTarPath string
DepStoreExpandedPath string
OverlayTargetPath string
OverlayWorkPath string
BuildModePath string
OCIExpandedBlobsPath string
Debug bool
Mode BuildMode
man Manifest
lockFile *os.File
}
// NewACBuild returns a new ACBuild struct with sane defaults for all of the
// different paths
func NewACBuild(cwd string, debug bool, buildMode BuildMode) (*ACBuild, error) {
a := &ACBuild{
ContextPath: path.Join(cwd, defaultWorkPath),
LockPath: path.Join(cwd, defaultWorkPath, "lock"),
CurrentImagePath: path.Join(cwd, defaultWorkPath, "currentaci"),
DepStoreTarPath: path.Join(cwd, defaultWorkPath, "depstore-tar"),
DepStoreExpandedPath: path.Join(cwd, defaultWorkPath, "depstore-expanded"),
OverlayTargetPath: path.Join(cwd, defaultWorkPath, "target"),
OverlayWorkPath: path.Join(cwd, defaultWorkPath, "work"),
BuildModePath: path.Join(cwd, defaultWorkPath, "buildMode"),
OCIExpandedBlobsPath: path.Join(cwd, defaultWorkPath, "ociblobs"),
Debug: debug,
Mode: buildMode,
}
// This might fail, and that's ok (maybe the build hasn't started yet)
a.loadManifest()
return a, nil
}
func (a *ACBuild) loadManifest() error {
var err error
switch a.Mode {
case BuildModeAppC:
a.man, err = appc.LoadManifest(a.CurrentImagePath)
case BuildModeOCI:
a.man, err = oci.LoadImage(a.CurrentImagePath)
}
if err != nil {
_, serr := os.Stat(a.ContextPath)
if !os.IsNotExist(serr) {
// If the context path exists, then this build was started and we
// shouldn't have failed. Let's error.
return fmt.Errorf("error loading manifest: %v\n", err)
}
}
return nil
}
func (a *ACBuild) lock() error {
_, err := os.Stat(a.ContextPath)
switch {
case os.IsNotExist(err):
return errNoBuildInProgress
case err != nil:
return err
}
if a.lockFile != nil {
return fmt.Errorf("lock already held by this ACBuild")
}
a.lockFile, err = os.OpenFile(a.LockPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
switch err1 := err.(type) {
case *os.PathError:
switch err2 := err1.Err.(type) {
case syscall.Errno:
if err2 == syscall.EACCES {
err = fmt.Errorf("permission denied: please run this as a user with appropriate privileges\n")
}
}
}
return err
}
err = syscall.Flock(int(a.lockFile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
if err == syscall.EWOULDBLOCK {
return fmt.Errorf("lock already held - is another acbuild running in this working dir?")
}
return err
}
return nil
}
func (a *ACBuild) unlock() error {
if a.lockFile == nil {
return fmt.Errorf("lock isn't held by this ACBuild")
}
err := syscall.Flock(int(a.lockFile.Fd()), syscall.LOCK_UN)
if err != nil {
return err
}
err = a.lockFile.Close()
if err != nil {
return err
}
a.lockFile = nil
err = os.Remove(a.LockPath)
if err != nil {
return err
}
return nil
}
func GetBuildMode(cwd string) (BuildMode, error) {
mode, err := ioutil.ReadFile(path.Join(cwd, defaultWorkPath, "buildMode"))
if err != nil {
return "", err
}
return BuildMode(mode), nil
}
func (a *ACBuild) rehashAndStoreOCIBlob(targetPath string, newLayer bool) error {
layerDigestWriter := sha256.New()
finishedWriting := false
tmpFile, err := ioutil.TempFile(a.ContextPath, "acbuild-layer-rehashing")
if err != nil {
return err
}
defer func() {
if !finishedWriting {
tmpFile.Close()
}
}()
combinedWriter := io.MultiWriter(layerDigestWriter, tmpFile)
gzipWriter := gzip.NewWriter(combinedWriter)
defer func() {
if !finishedWriting {
gzipWriter.Close()
}
}()
diffIdWriter := sha256.New()
tarWriter := tar.NewWriter(io.MultiWriter(diffIdWriter, gzipWriter))
defer func() {
if !finishedWriting {
tarWriter.Close()
}
}()
err = filepath.Walk(targetPath, util.PathWalker(tarWriter, targetPath))
if err != nil {
return err
}
tarWriter.Close()
gzipWriter.Close()
tmpFile.Close()
finfo, err := os.Stat(tmpFile.Name())
if err != nil {
return err
}
fsize := finfo.Size()
finishedWriting = true
// See https://github.com/opencontainers/image-spec/blob/master/config.md for the difference between layer
// digest and DiffID.
layerDigest := hex.EncodeToString(layerDigestWriter.Sum(nil))
diffId := hex.EncodeToString(diffIdWriter.Sum(nil))
err = os.MkdirAll(path.Join(a.CurrentImagePath, "blobs", "sha256"), 0755)
if err != nil {
return err
}
err = os.Rename(tmpFile.Name(), path.Join(a.CurrentImagePath, "blobs", "sha256", layerDigest))
if err != nil {
return err
}
blobStorePath := path.Dir(path.Dir(targetPath))
err = os.Rename(targetPath, path.Join(blobStorePath, "sha256", layerDigest))
if err != nil {
return err
}
var oldTopLayerHash string
switch ociMan := a.man.(type) {
case *oci.Image:
if newLayer {
// add a new top layer to the config/manifest
err = ociMan.NewTopLayer("sha256", layerDigest, diffId, fsize)
if err != nil {
return err
}
} else {
// update the top layer hash in the config/manifest, and remove the old
// top layer
oldTopLayerHash, err = ociMan.UpdateTopLayer("sha256", layerDigest, diffId, fsize)
if err != nil {
return err
}
}
default:
return fmt.Errorf("mismatch between build mode and manifest type?!")
}
if !newLayer && oldTopLayerHash != "" {
err = os.Remove(path.Join(a.CurrentImagePath, "blobs", strings.Replace(oldTopLayerHash, ":", "/", -1)))
if err != nil {
fmt.Fprintf(os.Stderr, "error removing old top layer, hash %s: %v", oldTopLayerHash, err)
}
}
return nil
}