-
Notifications
You must be signed in to change notification settings - Fork 162
/
archive.go
227 lines (185 loc) · 5.53 KB
/
archive.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
package resource
import (
"os"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshcmd "github.com/cloudfoundry/bosh-utils/fileutil"
boshsys "github.com/cloudfoundry/bosh-utils/system"
bicrypto "github.com/cloudfoundry/bosh-cli/crypto"
)
type ArchiveImpl struct {
files []File
prepFiles []File
additionalChunks []string
releaseDirPath string
followSymlinks bool
fingerprinter Fingerprinter
compressor boshcmd.Compressor
digestCalculator bicrypto.DigestCalculator
cmdRunner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewArchiveImpl(
args ArchiveFactoryArgs,
releaseDirPath string,
fingerprinter Fingerprinter,
compressor boshcmd.Compressor,
digestCalculator bicrypto.DigestCalculator,
cmdRunner boshsys.CmdRunner,
fs boshsys.FileSystem,
) ArchiveImpl {
return ArchiveImpl{
files: args.Files,
prepFiles: args.PrepFiles,
additionalChunks: args.Chunks,
followSymlinks: args.FollowSymlinks,
releaseDirPath: releaseDirPath,
fingerprinter: fingerprinter,
compressor: compressor,
digestCalculator: digestCalculator,
cmdRunner: cmdRunner,
fs: fs,
}
}
func (a ArchiveImpl) Fingerprint() (string, error) {
fp, err := a.fingerprinter.Calculate(a.files, a.additionalChunks)
if err != nil {
return "", bosherr.WrapErrorf(err, "Fingerprinting source files")
}
return fp, nil
}
func (a ArchiveImpl) Build(expectedFp string) (string, string, error) {
stagingDir, err := a.fs.TempDir("bosh-resource-archive")
if err != nil {
return "", "", bosherr.WrapError(err, "Creating staging directory")
}
defer func() {
_ = a.fs.RemoveAll(stagingDir)
}()
for _, file := range a.files {
err := a.copyFile(file, stagingDir)
if err != nil {
return "", "", bosherr.WrapError(err, "Copying into staging directory")
}
}
stagingFp, err := a.buildStagingArchive(stagingDir).Fingerprint()
if err != nil {
return "", "", bosherr.WrapError(err, "Fingerprinting staged files")
}
if expectedFp != stagingFp {
return "", "", bosherr.Errorf(
"Expected source ('%s') and staging ('%s') fingerprints to match", expectedFp, stagingFp)
}
err = a.runPrepScripts(stagingDir)
if err != nil {
return "", "", bosherr.WrapError(err, "Running prep scripts")
}
archivePath, err := a.compressor.CompressFilesInDir(stagingDir)
if err != nil {
return "", "", bosherr.WrapError(err, "Compressing staging directory")
}
//generation of digest string
archiveSHA1, err := a.digestCalculator.Calculate(archivePath)
if err != nil {
_ = a.compressor.CleanUp(archivePath)
return "", "", bosherr.WrapError(err, "Calculating archive SHA1")
}
return archivePath, archiveSHA1, nil
}
func (a ArchiveImpl) runPrepScripts(stagingDir string) error {
for _, prepFile := range a.prepFiles {
// No need to copy into staging dir since we expect prep script to be in files
cmd := boshsys.Command{
Name: "bash",
Args: []string{"-x", prepFile.Path},
WorkingDir: stagingDir,
UseIsolatedEnv: false,
Env: map[string]string{
"BUILD_DIR": stagingDir,
"RELEASE_DIR": a.releaseDirPath,
},
}
_, _, _, err := a.cmdRunner.RunComplexCommand(cmd)
if err != nil {
return err
}
// Arguably we should not remove the prep script
err = a.fs.RemoveAll(filepath.Join(stagingDir, prepFile.RelativePath))
if err != nil {
return bosherr.WrapError(
err, "Removing prep scrpt from staging directory")
}
}
return nil
}
func (a ArchiveImpl) copyFile(sourceFile File, stagingDir string) error {
dstPath := filepath.Join(stagingDir, sourceFile.RelativePath)
dstDir := filepath.Dir(dstPath)
err := a.fs.MkdirAll(dstDir, os.ModePerm)
if err != nil {
return err
}
sourceDirStat, err := a.fs.Lstat(filepath.Dir(sourceFile.Path))
if err != nil {
return err
}
sourceDirMode := sourceDirStat.Mode()
isSourceDirSymlink := sourceDirStat.Mode()&os.ModeSymlink != 0
if isSourceDirSymlink && a.followSymlinks {
symlinkTarget, err := filepath.EvalSymlinks(filepath.Dir(sourceFile.Path))
if err != nil {
return err
}
statResult, err := a.fs.Lstat(symlinkTarget)
if err != nil {
return err
}
sourceDirMode = statResult.Mode()
}
err = a.fs.Chmod(dstDir, sourceDirMode)
if err != nil {
return err
}
sourceFileStat, err := a.fs.Lstat(sourceFile.Path)
if err != nil {
return err
}
isSymlink := sourceFileStat.Mode()&os.ModeSymlink != 0
sourceFilePath := sourceFile.Path
if isSymlink {
if !a.followSymlinks {
symlinkTarget, err := a.fs.Readlink(sourceFilePath)
if err != nil {
return err
}
return a.fs.Symlink(symlinkTarget, dstPath)
}
symlinkTarget, err := a.fs.ReadAndFollowLink(sourceFilePath)
if err != nil {
return err
}
sourceFilePath = symlinkTarget
}
err = a.fs.CopyFile(sourceFilePath, dstPath)
if err != nil {
return err
}
// Be very explicit about changing permissions for copied file
// Only pay attention to whether the source file is executable
return a.fs.Chmod(dstPath, getFilePerms(sourceFileStat))
}
func getFilePerms(stat os.FileInfo) os.FileMode {
if (stat.Mode() | 0100) == stat.Mode() {
return 0755
} else {
return 0644
}
}
func (a ArchiveImpl) buildStagingArchive(stagingDir string) Archive {
var stagingFiles []File
for _, file := range a.files {
stagingFiles = append(stagingFiles, file.WithNewDir(stagingDir))
}
// Initialize with bare minimum deps so that fingerprinting can be performed
return NewArchiveImpl(ArchiveFactoryArgs{Files: stagingFiles, Chunks: a.additionalChunks}, "", a.fingerprinter, nil, nil, nil, nil)
}