This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
192 lines (155 loc) · 4.59 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
package resource
import (
"os"
gopath "path"
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
fingerprinter Fingerprinter
compressor boshcmd.Compressor
sha1calc bicrypto.SHA1Calculator
cmdRunner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewArchiveImpl(
files []File,
prepFiles []File,
additionalChunks []string,
releaseDirPath string,
fingerprinter Fingerprinter,
compressor boshcmd.Compressor,
sha1calc bicrypto.SHA1Calculator,
cmdRunner boshsys.CmdRunner,
fs boshsys.FileSystem,
) ArchiveImpl {
return ArchiveImpl{
files: files,
prepFiles: prepFiles,
additionalChunks: additionalChunks,
releaseDirPath: releaseDirPath,
fingerprinter: fingerprinter,
compressor: compressor,
sha1calc: sha1calc,
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")
}
archiveSHA1, err := a.sha1calc.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(gopath.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 := gopath.Join(stagingDir, sourceFile.RelativePath)
dstDir := gopath.Dir(dstPath)
err := a.fs.MkdirAll(dstDir, os.ModePerm)
if err != nil {
return err
}
sourceDirStat, err := a.fs.Lstat(gopath.Dir(sourceFile.Path))
if err != nil {
return err
}
err = a.fs.Chmod(dstDir, sourceDirStat.Mode())
if err != nil {
return err
}
sourceFileStat, err := a.fs.Lstat(sourceFile.Path)
if err != nil {
return err
}
if sourceFileStat.Mode()&os.ModeSymlink != 0 {
symlinkTarget, err := a.fs.Readlink(sourceFile.Path)
if err != nil {
return err
}
return a.fs.Symlink(symlinkTarget, dstPath)
} else {
err = a.fs.CopyFile(sourceFile.Path, dstPath)
if err != nil {
return err
}
// Be very explicit about changing permissions for copied file
return a.fs.Chmod(dstPath, sourceFileStat.Mode())
}
}
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(stagingFiles, nil, a.additionalChunks, "", a.fingerprinter, nil, nil, nil, nil)
}