forked from juju/juju
/
environ.go
135 lines (107 loc) · 3.5 KB
/
environ.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// +build !gccgo
package vsphere
import (
"sync"
"github.com/juju/errors"
"github.com/juju/version"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/provider/common"
)
// Note: This provider/environment does *not* implement storage.
type environ struct {
name string
cloud environs.CloudSpec
client *client
// namespace is used to create the machine and device hostnames.
namespace instance.Namespace
lock sync.Mutex // lock protects access the following fields.
ecfg *environConfig
archLock sync.Mutex
supportedArchitectures []string
}
func newEnviron(cloud environs.CloudSpec, cfg *config.Config) (*environ, error) {
ecfg, err := newValidConfig(cfg, configDefaults)
if err != nil {
return nil, errors.Annotate(err, "invalid config")
}
client, err := newClient(cloud)
if err != nil {
return nil, errors.Annotatef(err, "failed to create new client")
}
namespace, err := instance.NewNamespace(cfg.UUID())
if err != nil {
return nil, errors.Trace(err)
}
env := &environ{
name: ecfg.Name(),
cloud: cloud,
ecfg: ecfg,
client: client,
namespace: namespace,
}
return env, nil
}
// Name returns the name of the environment.
func (env *environ) Name() string {
return env.name
}
// Provider returns the environment provider that created this env.
func (*environ) Provider() environs.EnvironProvider {
return providerInstance
}
// SetConfig updates the env's configuration.
func (env *environ) SetConfig(cfg *config.Config) error {
env.lock.Lock()
defer env.lock.Unlock()
if env.ecfg == nil {
return errors.New("cannot set config on uninitialized env")
}
if err := env.ecfg.update(cfg); err != nil {
return errors.Annotate(err, "invalid config change")
}
return nil
}
// Config returns the configuration data with which the env was created.
func (env *environ) Config() *config.Config {
env.lock.Lock()
cfg := env.ecfg.Config
env.lock.Unlock()
return cfg
}
// PrepareForBootstrap implements environs.Environ.
func (env *environ) PrepareForBootstrap(ctx environs.BootstrapContext) error {
return nil
}
// Create implements environs.Environ.
func (env *environ) Create(environs.CreateParams) error {
return nil
}
//this variable is exported, because it has to be rewritten in external unit tests
var Bootstrap = common.Bootstrap
// Bootstrap creates a new instance, chosing the series and arch out of
// available tools. The series and arch are returned along with a func
// that must be called to finalize the bootstrap process by transferring
// the tools and installing the initial juju controller.
func (env *environ) Bootstrap(ctx environs.BootstrapContext, params environs.BootstrapParams) (*environs.BootstrapResult, error) {
return Bootstrap(ctx, env, params)
}
//this variable is exported, because it has to be rewritten in external unit tests
var DestroyEnv = common.Destroy
// AdoptResources is part of the Environ interface.
func (env *environ) AdoptResources(controllerUUID string, fromVersion version.Number) error {
// This provider doesn't track instance -> controller.
return nil
}
// Destroy shuts down all known machines and destroys the rest of the
// known environment.
func (env *environ) Destroy() error {
return DestroyEnv(env)
}
// DestroyController implements the Environ interface.
func (env *environ) DestroyController(controllerUUID string) error {
return env.Destroy()
}