1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"launchpad.net/juju-core/agent"
13
// TODO(axw) 2013-09-25 bug #1230131
14
// Move these variables out of agent when we can do upgrades in
15
// the right place. In this case, the local provider should do
16
// the envvar-to-agent.conf migration.
17
StorageDir = agent.StorageDir
18
StorageAddr = agent.StorageAddr
19
SharedStorageDir = agent.SharedStorageDir
20
SharedStorageAddr = agent.SharedStorageAddr
21
StorageCACert = "StorageCACert"
22
StorageCAKey = "StorageCAKey"
23
StorageHostnames = "StorageHostnames"
24
StorageAuthKey = "StorageAuthKey"
27
// LocalStorageConfig is an interface that, if implemented, may be used
28
// to configure a machine agent for use with the localstorage worker in
30
type LocalStorageConfig interface {
33
SharedStorageDir() string
34
SharedStorageAddr() string
37
// LocalTLSStorageConfig is an interface that extends LocalStorageConfig
38
// to support serving storage over TLS.
39
type LocalTLSStorageConfig interface {
42
// StorageCACert is the CA certificate in PEM format.
43
StorageCACert() []byte
45
// StorageCAKey is the CA private key in PEM format.
48
// StorageHostnames is the set of hostnames that will
49
// be assigned to the storage server's certificate.
50
StorageHostnames() []string
52
// StorageAuthKey is the key that clients must present
53
// to perform modifying operations.
54
StorageAuthKey() string
60
sharedStorageDir string
61
sharedStorageAddr string
68
// StoreConfig takes a LocalStorageConfig (or derivative interface),
69
// and stores it in a map[string]string suitable for updating an
70
// agent.Config's key/value map.
71
func StoreConfig(storageConfig LocalStorageConfig) (map[string]string, error) {
72
kv := make(map[string]string)
73
kv[StorageDir] = storageConfig.StorageDir()
74
kv[StorageAddr] = storageConfig.StorageAddr()
75
kv[SharedStorageDir] = storageConfig.SharedStorageDir()
76
kv[SharedStorageAddr] = storageConfig.SharedStorageAddr()
77
if tlsConfig, ok := storageConfig.(LocalTLSStorageConfig); ok {
78
if authkey := tlsConfig.StorageAuthKey(); authkey != "" {
79
kv[StorageAuthKey] = authkey
81
if cert := tlsConfig.StorageCACert(); cert != nil {
82
kv[StorageCACert] = string(cert)
84
if key := tlsConfig.StorageCAKey(); key != nil {
85
kv[StorageCAKey] = string(key)
87
if hostnames := tlsConfig.StorageHostnames(); len(hostnames) > 0 {
88
data, err := goyaml.Marshal(hostnames)
92
kv[StorageHostnames] = string(data)
98
func loadConfig(agentConfig agent.Config) (*config, error) {
100
storageDir: agentConfig.Value(StorageDir),
101
storageAddr: agentConfig.Value(StorageAddr),
102
sharedStorageDir: agentConfig.Value(SharedStorageDir),
103
sharedStorageAddr: agentConfig.Value(SharedStorageAddr),
104
authkey: agentConfig.Value(StorageAuthKey),
107
caCertPEM := agentConfig.Value(StorageCACert)
108
if len(caCertPEM) > 0 {
109
config.caCertPEM = []byte(caCertPEM)
112
caKeyPEM := agentConfig.Value(StorageCAKey)
113
if len(caKeyPEM) > 0 {
114
config.caKeyPEM = []byte(caKeyPEM)
117
hostnames := agentConfig.Value(StorageHostnames)
118
if len(hostnames) > 0 {
119
err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames)