-
Notifications
You must be signed in to change notification settings - Fork 13
/
artifact.go
237 lines (202 loc) · 6.66 KB
/
artifact.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
package camel
import (
"bytes"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/unarchiver"
"github.com/ActiveState/cli/pkg/platform/runtime/envdef"
"github.com/ActiveState/cli/pkg/platform/runtime/store"
"github.com/go-openapi/strfmt"
"github.com/thoas/go-funk"
)
type ErrNotExecutable struct{ *locale.LocalizedError }
type ErrNoPrefixes struct{ *locale.LocalizedError }
type ArtifactSetup struct {
artifactID strfmt.UUID
store *store.Store
}
func NewArtifactSetup(artifactID strfmt.UUID, store *store.Store) *ArtifactSetup {
return &ArtifactSetup{artifactID, store}
}
func (as *ArtifactSetup) EnvDef(tmpDir string) (*envdef.EnvironmentDefinition, error) {
// camel archives are structured like this
// <archiveName>/
// <relInstallDir>/
// artifact contents ...
// metadata.json
// First: We need to identify the values for <archiveName> and <relInstallDir>
var archiveName string
fs, err := os.ReadDir(tmpDir)
if err != nil {
return nil, errs.Wrap(err, "Could not read temporary installation directory %s", tmpDir)
}
for _, f := range fs {
if f.IsDir() {
archiveName = f.Name()
}
}
if archiveName == "" {
return nil, errs.New("Expected sub-directory in extracted artifact tarball.")
}
tmpBaseDir := filepath.Join(tmpDir, archiveName)
// parse the legacy metadata
md, err := InitMetaData(tmpBaseDir)
if err != nil {
return nil, errs.Wrap(err, "Could not load meta data definitions for camel artifact.")
}
// convert file relocation commands into an envdef.FileTransform slice
transforms, err := convertToFileTransforms(tmpBaseDir, md.InstallDir, md)
if err != nil {
return nil, errs.Wrap(err, "Could not determine file transformations")
}
// convert environment variables into an envdef.EnvironmentVariable slice
vars := convertToEnvVars(md)
ed := &envdef.EnvironmentDefinition{
InstallDir: filepath.Join(archiveName, md.InstallDir),
Transforms: transforms,
Env: vars,
}
return ed, nil
}
func convertToEnvVars(metadata *MetaData) []envdef.EnvironmentVariable {
var res []envdef.EnvironmentVariable
if metadata.AffectedEnv != "" {
res = append(res, envdef.EnvironmentVariable{
Name: metadata.AffectedEnv,
Values: []string{},
Inherit: false,
Join: envdef.Disallowed,
})
}
for k, v := range metadata.Env {
res = append(res, envdef.EnvironmentVariable{
Name: k,
Values: []string{v},
Inherit: false,
})
}
for k, v := range metadata.PathListEnv {
res = append(res, envdef.EnvironmentVariable{
Name: k,
Values: []string{v},
Join: envdef.Prepend,
Separator: string(os.PathListSeparator),
Inherit: true,
})
}
var binPaths []string
// set up PATH according to binary locations
for _, v := range metadata.BinaryLocations {
path := v.Path
if v.Relative {
path = filepath.Join("${INSTALLDIR}", path)
}
binPaths = append(binPaths, path)
}
// Add DLL dir to PATH on Windows
if runtime.GOOS == "windows" && metadata.RelocationTargetBinaries != "" {
binPaths = append(binPaths, filepath.Join("${INSTALLDIR}", metadata.RelocationTargetBinaries))
}
res = append(res, envdef.EnvironmentVariable{
Name: "PATH",
Values: funk.ReverseStrings(binPaths),
Inherit: true,
Join: envdef.Prepend,
Separator: string(os.PathListSeparator),
})
return res
}
func paddingForBinaryFile(isBinary bool) *string {
if !isBinary {
return nil
}
pad := "\000"
return &pad
}
func convertToFileTransforms(tmpBaseDir string, relInstDir string, metadata *MetaData) ([]envdef.FileTransform, error) {
var res []envdef.FileTransform
instDir := filepath.Join(tmpBaseDir, relInstDir)
for _, tr := range metadata.TargetedRelocations {
// walk through files in tr.InDir and find files that need replacements. For those we create a FileTransform element
trans, err := fileTransformsInDir(instDir, filepath.Join(instDir, tr.InDir), tr.SearchString, tr.Replacement, func(_ string, _ bool) bool { return true })
if err != nil {
return res, errs.Wrap(err, "Failed convert targeted relocations")
}
res = append(res, trans...)
}
// metadata.RelocationDir is the string to search for and replace with ${INSTALLDIR}
if metadata.RelocationDir == "" {
return res, nil
}
binariesSeparate := runtime.GOOS == "linux" && metadata.RelocationTargetBinaries != ""
relocFilePath := filepath.Join(tmpBaseDir, "support", "reloc.txt")
relocMap := map[string]bool{}
if fileutils.FileExists(relocFilePath) {
relocMap = loadRelocationFile(relocFilePath)
}
trans, err := fileTransformsInDir(instDir, instDir, metadata.RelocationDir, "${INSTALLDIR}", func(path string, isBinary bool) bool {
return relocMap[path] || !binariesSeparate || !isBinary
})
if err != nil {
return res, errs.Wrap(err, "Could not determine transformations in installation directory")
}
res = append(res, trans...)
if binariesSeparate {
trans, err := fileTransformsInDir(instDir, instDir, metadata.RelocationDir, "${INSTALLDIR}", func(_ string, isBinary bool) bool {
return isBinary
})
if err != nil {
return res, errs.Wrap(err, "Could not determine separate binary transformations in installation directory")
}
res = append(res, trans...)
}
return res, nil
}
// fileTransformsInDir walks through all the files in searchDir and creates a FileTransform item for files that contain searchString and pass the filter function
func fileTransformsInDir(instDir string, searchDir string, searchString string, replacement string, filter func(string, bool) bool) ([]envdef.FileTransform, error) {
var res []envdef.FileTransform
err := filepath.Walk(searchDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// skip symlinks
if (info.Mode() & fs.ModeSymlink) == fs.ModeSymlink {
return nil
}
if info.IsDir() {
return nil
}
b, err := os.ReadFile(path)
if err != nil {
return errs.Wrap(err, "Could not read file path %s", path)
}
// relativePath is the path relative to the installation directory
relativePath := strings.TrimPrefix(path, instDir)
isBinary := fileutils.IsBinary(b)
if !filter(relativePath, isBinary) {
return nil
}
if bytes.Contains(b, []byte(searchString)) {
res = append(res, envdef.FileTransform{
In: []string{relativePath},
Pattern: searchString,
With: replacement,
PadWith: paddingForBinaryFile(isBinary),
})
}
return nil
})
return res, err
}
func (as *ArtifactSetup) Unarchiver() unarchiver.Unarchiver {
if runtime.GOOS == "windows" {
return unarchiver.NewZip()
}
return unarchiver.NewTarGz()
}