github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/simplestreams"
    14  	"github.com/juju/juju/state/storage"
    15  )
    16  
    17  const (
    18  	storageDataSourceId          = "model storage"
    19  	storageDataSourceDescription = storageDataSourceId
    20  	metadataBasePath             = "imagemetadata"
    21  )
    22  
    23  // environmentStorageDataSource is a simplestreams.DataSource that
    24  // retrieves simplestreams metadata from environment storage.
    25  type environmentStorageDataSource struct {
    26  	stor          storage.Storage
    27  	priority      int
    28  	requireSigned bool
    29  }
    30  
    31  // NewModelStorageDataSource returns a new datasource that retrieves
    32  // metadata from environment storage.
    33  func NewModelStorageDataSource(stor storage.Storage, priority int, requireSigned bool) simplestreams.DataSource {
    34  	return environmentStorageDataSource{stor, priority, requireSigned}
    35  }
    36  
    37  // Description is defined in simplestreams.DataSource.
    38  func (d environmentStorageDataSource) Description() string {
    39  	return storageDataSourceDescription
    40  }
    41  
    42  // Fetch is defined in simplestreams.DataSource.
    43  func (d environmentStorageDataSource) Fetch(file string) (io.ReadCloser, string, error) {
    44  	logger.Debugf("fetching %q", file)
    45  
    46  	r, _, err := d.stor.Get(path.Join(metadataBasePath, file))
    47  	if err != nil {
    48  		return nil, "", err
    49  	}
    50  	data, err := ioutil.ReadAll(r)
    51  	if err != nil {
    52  		return nil, "", err
    53  	}
    54  
    55  	url, _ := d.URL(file)
    56  	return ioutil.NopCloser(bytes.NewReader(data)), url, nil
    57  }
    58  
    59  // URL is defined in simplestreams.DataSource.
    60  func (d environmentStorageDataSource) URL(file string) (string, error) {
    61  	path := path.Join(metadataBasePath, file)
    62  	return fmt.Sprintf("model-storage://%s", path), nil
    63  }
    64  
    65  // PublicSigningKey is defined in simplestreams.DataSource.
    66  func (d environmentStorageDataSource) PublicSigningKey() string {
    67  	return ""
    68  }
    69  
    70  // Defined in simplestreams.DataSource.
    71  func (d environmentStorageDataSource) SetAllowRetry(allow bool) {
    72  }
    73  
    74  // Priority is defined in simplestreams.DataSource.
    75  func (d environmentStorageDataSource) Priority() int {
    76  	return d.priority
    77  }
    78  
    79  // RequireSigned is defined in simplestreams.DataSource.
    80  func (d environmentStorageDataSource) RequireSigned() bool {
    81  	return d.requireSigned
    82  }