github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/jujud/agent/simplestreams.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package agent 5 6 import ( 7 "bytes" 8 "fmt" 9 "io" 10 "io/ioutil" 11 "path" 12 13 "github.com/juju/juju/environs" 14 "github.com/juju/juju/environs/simplestreams" 15 envstorage "github.com/juju/juju/environs/storage" 16 "github.com/juju/juju/state/storage" 17 ) 18 19 const ( 20 storageDataSourceId = "environment storage" 21 storageDataSourceDescription = storageDataSourceId 22 ) 23 24 // environmentStorageDataSource is a simplestreams.DataSource that 25 // retrieves simplestreams metadata from environment storage. 26 type environmentStorageDataSource struct { 27 stor storage.Storage 28 } 29 30 // NewEnvironmentStorageDataSource returns a new datasource that retrieves 31 // metadata from environment storage. 32 func NewEnvironmentStorageDataSource(stor storage.Storage) simplestreams.DataSource { 33 return environmentStorageDataSource{stor} 34 } 35 36 // Description is defined in simplestreams.DataSource. 37 func (d environmentStorageDataSource) Description() string { 38 return storageDataSourceDescription 39 } 40 41 // Fetch is defined in simplestreams.DataSource. 42 func (d environmentStorageDataSource) Fetch(file string) (io.ReadCloser, string, error) { 43 logger.Debugf("fetching %q", file) 44 45 r, _, err := d.stor.Get(path.Join(envstorage.BaseImagesPath, file)) 46 if err != nil { 47 return nil, "", err 48 } 49 data, err := ioutil.ReadAll(r) 50 if err != nil { 51 return nil, "", err 52 } 53 54 url, _ := d.URL(file) 55 return ioutil.NopCloser(bytes.NewReader(data)), url, nil 56 } 57 58 // URL is defined in simplestreams.DataSource. 59 func (d environmentStorageDataSource) URL(file string) (string, error) { 60 path := path.Join(envstorage.BaseImagesPath, file) 61 return fmt.Sprintf("environment-storage://%s", path), nil 62 } 63 64 // Defined in simplestreams.DataSource. 65 func (d environmentStorageDataSource) SetAllowRetry(allow bool) { 66 } 67 68 // registerSimplestreamsDataSource registers a environmentStorageDataSource. 69 func registerSimplestreamsDataSource(stor storage.Storage) { 70 ds := NewEnvironmentStorageDataSource(stor) 71 environs.RegisterUserImageDataSourceFunc(storageDataSourceId, func(environs.Environ) (simplestreams.DataSource, error) { 72 return ds, nil 73 }) 74 } 75 76 // unregisterSimplestreamsDataSource de-registers an environmentStorageDataSource. 77 func unregisterSimplestreamsDataSource() { 78 environs.UnregisterImageDataSourceFunc(storageDataSourceId) 79 }