-
Notifications
You must be signed in to change notification settings - Fork 262
/
steampipeimage.go
262 lines (224 loc) · 7.61 KB
/
steampipeimage.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
package ociinstaller
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/containerd/containerd/remotes"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/turbot/steampipe/pkg/constants"
)
type SteampipeImage struct {
OCIDescriptor *ocispec.Descriptor
ImageRef *SteampipeImageRef
Config *config
Plugin *PluginImage
Database *DbImage
Fdw *HubImage
Assets *AssetsImage
resolver *remotes.Resolver
}
type PluginImage struct {
BinaryFile string
BinaryDigest string
BinaryArchitecture string
DocsDir string
ConfigFileDir string
LicenseFile string
}
type DbImage struct {
ArchiveDir string
ReadmeFile string
LicenseFile string
}
type HubImage struct {
BinaryFile string
ReadmeFile string
LicenseFile string
ControlFile string
SqlFile string
}
type AssetsImage struct {
ReportUI string
}
func (o *ociDownloader) newSteampipeImage() *SteampipeImage {
SteampipeImage := &SteampipeImage{
resolver: &o.resolver,
}
o.Images = append(o.Images, SteampipeImage)
return SteampipeImage
}
type ImageType string
const (
ImageTypeDatabase ImageType = "db"
ImageTypeFdw ImageType = "fdw"
ImageTypeAssets ImageType = "assets"
ImageTypePlugin ImageType = "plugin"
)
func (o *ociDownloader) Download(ctx context.Context, ref *SteampipeImageRef, imageType ImageType, destDir string) (*SteampipeImage, error) {
var mediaTypes []string
Image := o.newSteampipeImage()
Image.ImageRef = ref
mediaType, err := MediaTypeForPlatform(imageType)
if err != nil {
return nil, err
}
mediaTypes = append(mediaTypes, mediaType...)
mediaTypes = append(mediaTypes, SharedMediaTypes(imageType)...)
mediaTypes = append(mediaTypes, ConfigMediaTypes()...)
log.Println("[TRACE] ociDownloader.Download:", "downloading", ref.ActualImageRef())
// Download the files
imageDesc, _, configBytes, layers, err := o.Pull(ctx, ref.ActualImageRef(), mediaTypes, destDir)
if err != nil {
return nil, err
}
Image.OCIDescriptor = imageDesc
Image.Config, err = newSteampipeImageConfig(configBytes)
if err != nil {
return nil, errors.New("invalid image - missing $config")
}
// Get the metadata
switch imageType {
case ImageTypeDatabase:
Image.Database, err = getDBImageData(layers)
case ImageTypeFdw:
Image.Fdw, err = getFdwImageData(layers)
case ImageTypePlugin:
Image.Plugin, err = getPluginImageData(layers)
case ImageTypeAssets:
Image.Assets, err = getAssetImageData(layers)
default:
return nil, errors.New("invalid Type - image types are: plugin, db, fdw")
}
if err != nil {
return nil, err
}
return Image, nil
}
func getAssetImageData(layers []ocispec.Descriptor) (*AssetsImage, error) {
var assetImage AssetsImage
// get the report dir
foundLayers := findLayersForMediaType(layers, MediaTypeAssetReportLayer)
if len(foundLayers) > 0 {
assetImage.ReportUI = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
return &assetImage, nil
}
func getDBImageData(layers []ocispec.Descriptor) (*DbImage, error) {
res := &DbImage{}
// get the binary jar file
mediaType, err := MediaTypeForPlatform("db")
if err != nil {
return nil, err
}
foundLayers := findLayersForMediaType(layers, mediaType[0])
if len(foundLayers) != 1 {
return nil, fmt.Errorf("invalid Image - should contain 1 installation file per platform, found %d", len(foundLayers))
}
res.ArchiveDir = foundLayers[0].Annotations["org.opencontainers.image.title"]
// get the readme file info
foundLayers = findLayersForMediaType(layers, MediaTypeDbDocLayer)
if len(foundLayers) > 0 {
res.ReadmeFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
// get the license file info
foundLayers = findLayersForMediaType(layers, MediaTypeDbLicenseLayer)
if len(foundLayers) > 0 {
res.LicenseFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
return res, nil
}
func getFdwImageData(layers []ocispec.Descriptor) (*HubImage, error) {
res := &HubImage{}
// get the binary (steampipe-postgres-fdw.so) info
mediaType, err := MediaTypeForPlatform("fdw")
if err != nil {
return nil, err
}
foundLayers := findLayersForMediaType(layers, mediaType[0])
if len(foundLayers) != 1 {
return nil, fmt.Errorf("invalid image - image should contain 1 binary file per platform, found %d", len(foundLayers))
}
res.BinaryFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
//sourcePath := filepath.Join(tempDir.Path, fileName)
// get the control file info
foundLayers = findLayersForMediaType(layers, MediaTypeFdwControlLayer)
if len(foundLayers) != 1 {
return nil, fmt.Errorf("invalid image - image should contain 1 control file, found %d", len(foundLayers))
}
res.ControlFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
// get the sql file info
foundLayers = findLayersForMediaType(layers, MediaTypeFdwSqlLayer)
if len(foundLayers) != 1 {
return nil, fmt.Errorf("invalid image - image should contain 1 SQL file, found %d", len(foundLayers))
}
res.SqlFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
// get the readme file info
foundLayers = findLayersForMediaType(layers, MediaTypeFdwDocLayer)
if len(foundLayers) > 0 {
res.ReadmeFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
// get the license file info
foundLayers = findLayersForMediaType(layers, MediaTypeFdwLicenseLayer)
if len(foundLayers) > 0 {
res.LicenseFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
return res, nil
}
func getPluginImageData(layers []ocispec.Descriptor) (*PluginImage, error) {
res := &PluginImage{}
var foundLayers []ocispec.Descriptor
// get the binary plugin file info
// iterate in order of mediatypes - as given by MediaTypeForPlatform (see function docs)
mediaTypes, err := MediaTypeForPlatform("plugin")
if err != nil {
return nil, err
}
for _, mediaType := range mediaTypes {
// find out the layer with the correct media type
foundLayers = findLayersForMediaType(layers, mediaType)
if len(foundLayers) == 1 {
// when found, assign and exit
res.BinaryFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
res.BinaryDigest = string(foundLayers[0].Digest)
res.BinaryArchitecture = constants.ArchAMD64
if strings.Contains(mediaType, constants.ArchARM64) {
res.BinaryArchitecture = constants.ArchARM64
}
break
}
// loop over to the next one
log.Println("[TRACE] could not find data for", mediaType)
log.Println("[TRACE] falling back to the next one, if any")
}
if len(res.BinaryFile) == 0 {
return nil, fmt.Errorf("invalid image - should contain 1 binary file per platform, found %d", len(foundLayers))
}
// get the docs dir
foundLayers = findLayersForMediaType(layers, MediaTypePluginDocsLayer)
if len(foundLayers) > 0 {
res.DocsDir = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
// get the .spc config / connections file dir
foundLayers = findLayersForMediaType(layers, MediaTypePluginSpcLayer)
if len(foundLayers) > 0 {
res.ConfigFileDir = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
// get the license file info
foundLayers = findLayersForMediaType(layers, MediaTypePluginLicenseLayer)
if len(foundLayers) > 0 {
res.LicenseFile = foundLayers[0].Annotations["org.opencontainers.image.title"]
}
return res, nil
}
func findLayersForMediaType(layers []ocispec.Descriptor, mediaType string) []ocispec.Descriptor {
log.Println("[TRACE] looking for", mediaType)
var matchedLayers []ocispec.Descriptor
for _, layer := range layers {
if layer.MediaType == mediaType {
matchedLayers = append(matchedLayers, layer)
}
}
return matchedLayers
}