@@ -19,24 +19,19 @@ package util
1919import (
2020 "archive/tar"
2121 "compress/gzip"
22- "context"
2322 "encoding/json"
2423 "errors"
2524 "io/ioutil"
2625 "os"
2726 "path/filepath"
28- "regexp"
2927 "strings"
3028
31- "github.com/containers/image/docker"
32- "github.com/containers/image/docker/tarfile"
33- "github.com/docker/docker/client"
34-
29+ "github.com/containers/image/types"
3530 "github.com/golang/glog"
3631)
3732
3833var sourceToPrepMap = map [string ]func (ip ImagePrepper ) Prepper {
39- "ID" : func (ip ImagePrepper ) Prepper { return IDPrepper {ImagePrepper : ip } },
34+ "ID" : func (ip ImagePrepper ) Prepper { return DaemonPrepper {ImagePrepper : ip } },
4035 "URL" : func (ip ImagePrepper ) Prepper { return CloudPrepper {ImagePrepper : ip } },
4136 "tar" : func (ip ImagePrepper ) Prepper { return TarPrepper {ImagePrepper : ip } },
4237}
@@ -66,79 +61,22 @@ type ConfigSchema struct {
6661 History []ImageHistoryItem `json:"history"`
6762}
6863
69- type ImagePrepper struct {
70- Source string
71- Client * client.Client
72- }
73-
74- type Prepper interface {
75- getFileSystem () (string , error )
76- getConfig () (ConfigSchema , error )
77- }
78-
79- func (p ImagePrepper ) GetImage () (Image , error ) {
80- glog .Infof ("Starting prep for image %s" , p .Source )
81- img := p .Source
82-
83- var prepper Prepper
84- for source , check := range sourceCheckMap {
85- if check (img ) {
86- prepper = sourceToPrepMap [source ](p )
87- break
88- }
89- }
90- if prepper == nil {
91- return Image {}, errors .New ("Could not retrieve image from source" )
92- }
93-
94- imgPath , err := prepper .getFileSystem ()
95- if err != nil {
96- return Image {}, err
97- }
98-
99- config , err := prepper .getConfig ()
100- if err != nil {
101- glog .Error ("Error retrieving History: " , err )
102- }
103-
104- glog .Infof ("Finished prepping image %s" , p .Source )
105- return Image {
106- Source : img ,
107- FSPath : imgPath ,
108- Config : config ,
109- }, nil
110- }
111-
11264func getImageFromTar (tarPath string ) (string , error ) {
11365 glog .Info ("Extracting image tar to obtain image file system" )
11466 path := strings .TrimSuffix (tarPath , filepath .Ext (tarPath ))
11567 err := unpackDockerSave (tarPath , path )
11668 return path , err
11769}
11870
119- // CloudPrepper prepares images sourced from a Cloud registry
120- type CloudPrepper struct {
121- ImagePrepper
122- }
123-
124- func (p CloudPrepper ) getFileSystem () (string , error ) {
125- // The regexp when passed a string creates a list of the form
126- // [repourl/image:tag, image:tag, tag] (the tag may or may not be present)
127- URLPattern := regexp .MustCompile ("^.+/(.+(:.+){0,1})$" )
128- URLMatch := URLPattern .FindStringSubmatch (p .Source )
129- // Removing the ":" so that the image path name can be <image><tag>
130- sanitizedName := strings .Replace (URLMatch [1 ], ":" , "" , - 1 )
71+ func getFileSystemFromReference (ref types.ImageReference , imageName string ) (string , error ) {
72+ sanitizedName := strings .Replace (imageName , ":" , "" , - 1 )
73+ sanitizedName = strings .Replace (sanitizedName , "/" , "" , - 1 )
13174
13275 path , err := ioutil .TempDir ("" , sanitizedName )
13376 if err != nil {
13477 return "" , err
13578 }
13679
137- ref , err := docker .ParseReference ("//" + p .Source )
138- if err != nil {
139- return "" , err
140- }
141-
14280 img , err := ref .NewImage (nil )
14381 if err != nil {
14482 glog .Error (err )
@@ -170,121 +108,26 @@ func (p CloudPrepper) getFileSystem() (string, error) {
170108 return path , nil
171109}
172110
173- func (p CloudPrepper ) getConfig () (ConfigSchema , error ) {
174- ref , err := docker .ParseReference ("//" + p .Source )
175- if err != nil {
176- return ConfigSchema {}, err
177- }
178-
111+ func getConfigFromReference (ref types.ImageReference , source string ) (ConfigSchema , error ) {
179112 img , err := ref .NewImage (nil )
180113 if err != nil {
181- glog .Errorf ("Error referencing image %s from registry: %s" , p . Source , err )
114+ glog .Errorf ("Error referencing image %s from registry: %s" , source , err )
182115 return ConfigSchema {}, errors .New ("Could not obtain image config" )
183116 }
184117 defer img .Close ()
185118
186119 configBlob , err := img .ConfigBlob ()
187120 if err != nil {
188- glog .Errorf ("Error obtaining config blob for image %s from registry: %s" , p . Source , err )
121+ glog .Errorf ("Error obtaining config blob for image %s from registry: %s" , source , err )
189122 return ConfigSchema {}, errors .New ("Could not obtain image config" )
190123 }
191124
192125 var config ConfigSchema
193126 err = json .Unmarshal (configBlob , & config )
194127 if err != nil {
195- glog .Errorf ("Error with config file struct for image %s: %s" , p .Source , err )
196- return ConfigSchema {}, errors .New ("Could not obtain image config" )
197- }
198- return config , nil
199- }
200-
201- type IDPrepper struct {
202- ImagePrepper
203- }
204-
205- func (p IDPrepper ) getFileSystem () (string , error ) {
206- tarPath , err := saveImageToTar (p .Client , p .Source , p .Source )
207- if err != nil {
208- return "" , err
209- }
210-
211- defer os .Remove (tarPath )
212- return getImageFromTar (tarPath )
213- }
214-
215- func (p IDPrepper ) getConfig () (ConfigSchema , error ) {
216- inspect , _ , err := p .Client .ImageInspectWithRaw (context .Background (), p .Source )
217- if err != nil {
218- return ConfigSchema {}, err
219- }
220-
221- config := ConfigObject {
222- Env : inspect .Config .Env ,
223- }
224- history := p .getHistory ()
225- return ConfigSchema {
226- Config : config ,
227- History : history ,
228- }, nil
229- }
230-
231- func (p IDPrepper ) getHistory () []ImageHistoryItem {
232- history , err := p .Client .ImageHistory (context .Background (), p .Source )
233- if err != nil {
234- glog .Error ("Could not obtain image history for %s: %s" , p .Source , err )
235- }
236- historyItems := []ImageHistoryItem {}
237- for _ , item := range history {
238- historyItems = append (historyItems , ImageHistoryItem {CreatedBy : item .CreatedBy })
239- }
240- return historyItems
241- }
242-
243- type TarPrepper struct {
244- ImagePrepper
245- }
246-
247- func (p TarPrepper ) getFileSystem () (string , error ) {
248- return getImageFromTar (p .Source )
249- }
250-
251- func (p TarPrepper ) getConfig () (ConfigSchema , error ) {
252- tempDir := strings .TrimSuffix (p .Source , filepath .Ext (p .Source )) + "-config"
253- defer os .RemoveAll (tempDir )
254- err := UnTar (p .Source , tempDir )
255- if err != nil {
256- return ConfigSchema {}, err
257- }
258-
259- var config ConfigSchema
260- // First open the manifest, then find the referenced config.
261- manifestPath := filepath .Join (tempDir , "manifest.json" )
262- contents , err := ioutil .ReadFile (manifestPath )
263- if err != nil {
264- return ConfigSchema {}, err
265- }
266-
267- manifests := []tarfile.ManifestItem {}
268- if err := json .Unmarshal (contents , & manifests ); err != nil {
269- return ConfigSchema {}, err
270- }
271-
272- if len (manifests ) != 1 {
273- return ConfigSchema {}, errors .New ("specified tar file contains multiple images" )
274- }
275-
276- cfgFilename := filepath .Join (tempDir , manifests [0 ].Config )
277- file , err := ioutil .ReadFile (cfgFilename )
278- if err != nil {
279- glog .Errorf ("Could not read config file %s: %s" , cfgFilename , err )
128+ glog .Errorf ("Error with config file struct for image %s: %s" , source , err )
280129 return ConfigSchema {}, errors .New ("Could not obtain image config" )
281130 }
282- err = json .Unmarshal (file , & config )
283- if err != nil {
284- glog .Errorf ("Could not marshal config file %s: %s" , cfgFilename , err )
285- return ConfigSchema {}, errors .New ("Could not obtain image config" )
286- }
287-
288131 return config , nil
289132}
290133
0 commit comments