-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplestreams.go
69 lines (57 loc) · 1.96 KB
/
simplestreams.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/simplestreams"
"github.com/juju/juju/environs/storage"
"github.com/juju/juju/state"
)
// environmentStorageDataSource is a simplestreams.DataSource that
// retrieves simplestreams metadata from environment storage.
type environmentStorageDataSource struct {
stor state.Storage
}
// NewEnvironmentStorageDataSource returns a new datasource that retrieves
// metadata from environment storage.
func NewEnvironmentStorageDataSource(stor state.Storage) simplestreams.DataSource {
return environmentStorageDataSource{stor}
}
// Description is defined in simplestreams.DataSource.
func (d environmentStorageDataSource) Description() string {
return "environment storage"
}
// Fetch is defined in simplestreams.DataSource.
func (d environmentStorageDataSource) Fetch(file string) (io.ReadCloser, string, error) {
logger.Debugf("fetching %q", file)
r, _, err := d.stor.Get(path.Join(storage.BaseImagesPath, file))
if err != nil {
return nil, "", err
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, "", err
}
url, _ := d.URL(file)
return ioutil.NopCloser(bytes.NewReader(data)), url, nil
}
// URL is defined in simplestreams.DataSource.
func (d environmentStorageDataSource) URL(file string) (string, error) {
path := path.Join(storage.BaseImagesPath, file)
return fmt.Sprintf("environment-storage://%s", path), nil
}
// Defined in simplestreams.DataSource.
func (d environmentStorageDataSource) SetAllowRetry(allow bool) {
}
// registerSimplestreamsDataSource registers a environmentStorageDataSource.
func registerSimplestreamsDataSource(stor state.Storage) {
ds := NewEnvironmentStorageDataSource(stor)
environs.RegisterImageDataSourceFunc(ds.Description(), func(environs.Environ) (simplestreams.DataSource, error) {
return ds, nil
})
}