Skip to content

Commit b3c1e72

Browse files
committed
merged, but jujud/agent problems
2 parents 062f9b1 + 1142bed commit b3c1e72

File tree

454 files changed

+20820
-8724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

454 files changed

+20820
-8724
lines changed

agent/agent.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ import (
3232

3333
var logger = loggo.GetLogger("juju.agent")
3434

35+
const (
36+
// BootstrapNonce is used as a nonce for the initial controller machine.
37+
BootstrapNonce = "user-admin:bootstrap"
38+
39+
// BootstrapMachineId is the ID of the initial controller machine.
40+
BootstrapMachineId = "0"
41+
)
42+
3543
// These are base values used for the corresponding defaults.
3644
var (
3745
logDir = paths.MustSucceed(paths.LogDir(series.HostSeries()))

agent/bootstrap.go renamed to agent/agentbootstrap/bootstrap.go

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// Copyright 2013 Canonical Ltd.
22
// Licensed under the AGPLv3, see LICENCE file for details.
33

4-
package agent
4+
package agentbootstrap
55

66
import (
77
"github.com/juju/errors"
8+
"github.com/juju/loggo"
89
"github.com/juju/names"
910
"github.com/juju/utils"
1011
"github.com/juju/utils/series"
1112

13+
"github.com/juju/juju/agent"
1214
"github.com/juju/juju/apiserver/params"
1315
"github.com/juju/juju/constraints"
16+
"github.com/juju/juju/controller/modelmanager"
1417
"github.com/juju/juju/environs/config"
1518
"github.com/juju/juju/instance"
1619
"github.com/juju/juju/mongo"
@@ -19,10 +22,7 @@ import (
1922
"github.com/juju/juju/state/multiwatcher"
2023
)
2124

22-
const (
23-
// BootstrapNonce is used as a nonce for the controller machine.
24-
BootstrapNonce = "user-admin:bootstrap"
25-
)
25+
var logger = loggo.GetLogger("juju.agent.agentbootstrap")
2626

2727
// BootstrapMachineConfig holds configuration information
2828
// to attach to the bootstrap machine.
@@ -50,23 +50,29 @@ type BootstrapMachineConfig struct {
5050
SharedSecret string
5151
}
5252

53-
const BootstrapMachineId = "0"
54-
5553
// InitializeState should be called on the bootstrap machine's agent
5654
// configuration. It uses that information to create the controller, dial the
5755
// controller, and initialize it. It also generates a new password for the
5856
// bootstrap machine and calls Write to save the the configuration.
5957
//
60-
// The envCfg values will be stored in the state's ModelConfig; the
58+
// The cfg values will be stored in the state's ModelConfig; the
6159
// machineCfg values will be used to configure the bootstrap Machine,
6260
// and its constraints will be also be used for the model-level
6361
// constraints. The connection to the controller will respect the
6462
// given timeout parameter.
6563
//
6664
// InitializeState returns the newly initialized state and bootstrap
6765
// machine. If it fails, the state may well be irredeemably compromised.
68-
func InitializeState(adminUser names.UserTag, c ConfigSetter, envCfg *config.Config, machineCfg BootstrapMachineConfig, dialOpts mongo.DialOpts, policy state.Policy) (_ *state.State, _ *state.Machine, resultErr error) {
69-
if c.Tag() != names.NewMachineTag(BootstrapMachineId) {
66+
func InitializeState(
67+
adminUser names.UserTag,
68+
c agent.ConfigSetter,
69+
cfg *config.Config,
70+
hostedModelConfigAttrs map[string]interface{},
71+
machineCfg BootstrapMachineConfig,
72+
dialOpts mongo.DialOpts,
73+
policy state.Policy,
74+
) (_ *state.State, _ *state.Machine, resultErr error) {
75+
if c.Tag() != names.NewMachineTag(agent.BootstrapMachineId) {
7076
return nil, nil, errors.Errorf("InitializeState not called with bootstrap machine's configuration")
7177
}
7278
servingInfo, ok := c.StateServingInfo()
@@ -87,7 +93,7 @@ func InitializeState(adminUser names.UserTag, c ConfigSetter, envCfg *config.Con
8793
}
8894

8995
logger.Debugf("initializing address %v", info.Addrs)
90-
st, err := state.Initialize(adminUser, info, envCfg, dialOpts, policy)
96+
st, err := state.Initialize(adminUser, info, cfg, dialOpts, policy)
9197
if err != nil {
9298
return nil, nil, errors.Errorf("failed to initialize state: %v", err)
9399
}
@@ -114,6 +120,27 @@ func InitializeState(adminUser names.UserTag, c ConfigSetter, envCfg *config.Con
114120
if err != nil {
115121
return nil, nil, err
116122
}
123+
124+
// Create the initial hosted model, with the model config passed to
125+
// bootstrap, which contains the UUID, name for the hosted model,
126+
// and any user supplied config.
127+
attrs := make(map[string]interface{})
128+
for k, v := range hostedModelConfigAttrs {
129+
attrs[k] = v
130+
}
131+
hostedModelConfig, err := modelmanager.ModelConfigCreator{}.NewModelConfig(modelmanager.IsAdmin, cfg, attrs)
132+
if err != nil {
133+
return nil, nil, errors.Annotate(err, "creating hosted model config")
134+
}
135+
_, hostedModelState, err := st.NewModel(hostedModelConfig, adminUser)
136+
if err != nil {
137+
return nil, nil, errors.Annotate(err, "creating hosted model")
138+
}
139+
if err := hostedModelState.SetModelConstraints(machineCfg.ModelConstraints); err != nil {
140+
return nil, nil, errors.Annotate(err, "cannot set initial hosted model constraints")
141+
}
142+
hostedModelState.Close()
143+
117144
return st, m, nil
118145
}
119146

@@ -129,13 +156,13 @@ func paramsStateServingInfoToStateStateServingInfo(i params.StateServingInfo) st
129156
}
130157
}
131158

132-
func initConstraintsAndBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineConfig) (*state.Machine, error) {
159+
func initConstraintsAndBootstrapMachine(c agent.ConfigSetter, st *state.State, cfg BootstrapMachineConfig) (*state.Machine, error) {
133160
if err := st.SetModelConstraints(cfg.ModelConstraints); err != nil {
134-
return nil, errors.Errorf("cannot set initial environ constraints: %v", err)
161+
return nil, errors.Annotate(err, "cannot set initial model constraints")
135162
}
136163
m, err := initBootstrapMachine(c, st, cfg)
137164
if err != nil {
138-
return nil, errors.Errorf("cannot initialize bootstrap machine: %v", err)
165+
return nil, errors.Annotate(err, "cannot initialize bootstrap machine")
139166
}
140167
return m, nil
141168
}
@@ -152,7 +179,7 @@ func initMongoAdminUser(info mongo.Info, dialOpts mongo.DialOpts, password strin
152179
}
153180

154181
// initBootstrapMachine initializes the initial bootstrap machine in state.
155-
func initBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineConfig) (*state.Machine, error) {
182+
func initBootstrapMachine(c agent.ConfigSetter, st *state.State, cfg BootstrapMachineConfig) (*state.Machine, error) {
156183
logger.Infof("initialising bootstrap machine with config: %+v", cfg)
157184

158185
jobs := make([]state.MachineJob, len(cfg.Jobs))
@@ -166,7 +193,7 @@ func initBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineC
166193
m, err := st.AddOneMachine(state.MachineTemplate{
167194
Addresses: cfg.Addresses,
168195
Series: series.HostSeries(),
169-
Nonce: BootstrapNonce,
196+
Nonce: agent.BootstrapNonce,
170197
Constraints: cfg.BootstrapConstraints,
171198
InstanceId: cfg.InstanceId,
172199
HardwareCharacteristics: cfg.Characteristics,
@@ -175,7 +202,7 @@ func initBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineC
175202
if err != nil {
176203
return nil, errors.Errorf("cannot create bootstrap machine in state: %v", err)
177204
}
178-
if m.Id() != BootstrapMachineId {
205+
if m.Id() != agent.BootstrapMachineId {
179206
return nil, errors.Errorf("bootstrap machine expected id 0, got %q", m.Id())
180207
}
181208
// Read the machine agent's password and change it to
@@ -198,7 +225,7 @@ func initBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineC
198225
}
199226

200227
// initAPIHostPorts sets the initial API host/port addresses in state.
201-
func initAPIHostPorts(c ConfigSetter, st *state.State, addrs []network.Address, apiPort int) error {
228+
func initAPIHostPorts(c agent.ConfigSetter, st *state.State, addrs []network.Address, apiPort int) error {
202229
var hostPorts []network.HostPort
203230
// First try to select the correct address using the default space where all
204231
// API servers should be accessible on.

agent/bootstrap_test.go renamed to agent/agentbootstrap/bootstrap_test.go

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2012, 2013 Canonical Ltd.
22
// Licensed under the AGPLv3, see LICENCE file for details.
33

4-
package agent_test
4+
package agentbootstrap_test
55

66
import (
77
"io/ioutil"
@@ -11,10 +11,12 @@ import (
1111
"github.com/juju/names"
1212
gitjujutesting "github.com/juju/testing"
1313
jc "github.com/juju/testing/checkers"
14+
"github.com/juju/utils"
1415
"github.com/juju/utils/series"
1516
gc "gopkg.in/check.v1"
1617

1718
"github.com/juju/juju/agent"
19+
"github.com/juju/juju/agent/agentbootstrap"
1820
"github.com/juju/juju/apiserver/params"
1921
"github.com/juju/juju/constraints"
2022
"github.com/juju/juju/environs"
@@ -105,7 +107,7 @@ LXC_BRIDGE="ignored"`[1:])
105107
"10.0.3.4", // lxc bridge address filtered (-"-).
106108
"10.0.3.3", // not a lxc bridge address
107109
)
108-
mcfg := agent.BootstrapMachineConfig{
110+
mcfg := agentbootstrap.BootstrapMachineConfig{
109111
Addresses: initialAddrs,
110112
BootstrapConstraints: expectBootstrapConstraints,
111113
ModelConstraints: expectModelConstraints,
@@ -119,15 +121,31 @@ LXC_BRIDGE="ignored"`[1:])
119121
"0.1.2.3",
120122
"10.0.3.3",
121123
)
124+
125+
// Prepare bootstrap config, so we can use it in the state policy.
126+
provider, err := environs.Provider("dummy")
127+
c.Assert(err, jc.ErrorIsNil)
122128
envAttrs := dummy.SampleConfig().Delete("admin-secret").Merge(testing.Attrs{
123-
"agent-version": jujuversion.Current.String(),
124-
"state-id": "1", // needed so policy can Open config
129+
"agent-version": jujuversion.Current.String(),
130+
"not-for-hosted": "foo",
125131
})
126132
envCfg, err := config.New(config.NoDefaults, envAttrs)
127133
c.Assert(err, jc.ErrorIsNil)
134+
envCfg, err = provider.BootstrapConfig(environs.BootstrapConfigParams{Config: envCfg})
135+
c.Assert(err, jc.ErrorIsNil)
136+
defer dummy.Reset()
137+
138+
hostedModelUUID := utils.MustNewUUID().String()
139+
hostedModelConfigAttrs := map[string]interface{}{
140+
"name": "hosted",
141+
"uuid": hostedModelUUID,
142+
}
128143

129144
adminUser := names.NewLocalUserTag("agent-admin")
130-
st, m, err := agent.InitializeState(adminUser, cfg, envCfg, mcfg, mongo.DefaultDialOpts(), environs.NewStatePolicy())
145+
st, m, err := agentbootstrap.InitializeState(
146+
adminUser, cfg, envCfg, hostedModelConfigAttrs, mcfg,
147+
mongo.DefaultDialOpts(), environs.NewStatePolicy(),
148+
)
131149
c.Assert(err, jc.ErrorIsNil)
132150
defer st.Close()
133151

@@ -137,9 +155,7 @@ LXC_BRIDGE="ignored"`[1:])
137155
// Check that the environment has been set up.
138156
env, err := st.Model()
139157
c.Assert(err, jc.ErrorIsNil)
140-
uuid, ok := envCfg.UUID()
141-
c.Assert(ok, jc.IsTrue)
142-
c.Assert(env.UUID(), gc.Equals, uuid)
158+
c.Assert(env.UUID(), gc.Equals, envCfg.UUID())
143159

144160
// Check that initial admin user has been set up correctly.
145161
modelTag := env.Tag().(names.ModelTag)
@@ -148,7 +164,7 @@ LXC_BRIDGE="ignored"`[1:])
148164
c.Assert(err, jc.ErrorIsNil)
149165
c.Assert(user.PasswordValid(testing.DefaultMongoPassword), jc.IsTrue)
150166

151-
// Check that model configuration has been added, and
167+
// Check that controller model configuration has been added, and
152168
// model constraints set.
153169
newEnvCfg, err := st.ModelConfig()
154170
c.Assert(err, jc.ErrorIsNil)
@@ -157,6 +173,22 @@ LXC_BRIDGE="ignored"`[1:])
157173
c.Assert(err, jc.ErrorIsNil)
158174
c.Assert(gotModelConstraints, gc.DeepEquals, expectModelConstraints)
159175

176+
// Check that the hosted model has been added, and model constraints
177+
// set.
178+
hostedModelSt, err := st.ForModel(names.NewModelTag(hostedModelUUID))
179+
c.Assert(err, jc.ErrorIsNil)
180+
defer hostedModelSt.Close()
181+
gotModelConstraints, err = hostedModelSt.ModelConstraints()
182+
c.Assert(err, jc.ErrorIsNil)
183+
c.Assert(gotModelConstraints, gc.DeepEquals, expectModelConstraints)
184+
hostedModel, err := hostedModelSt.Model()
185+
c.Assert(err, jc.ErrorIsNil)
186+
c.Assert(hostedModel.Name(), gc.Equals, "hosted")
187+
hostedCfg, err := hostedModelSt.ModelConfig()
188+
c.Assert(err, jc.ErrorIsNil)
189+
_, hasUnexpected := hostedCfg.AllAttrs()["not-for-hosted"]
190+
c.Assert(hasUnexpected, jc.IsFalse)
191+
160192
// Check that the bootstrap machine looks correct.
161193
c.Assert(m.Id(), gc.Equals, "0")
162194
c.Assert(m.Jobs(), gc.DeepEquals, []state.MachineJob{state.JobManageModel})
@@ -197,10 +229,9 @@ LXC_BRIDGE="ignored"`[1:])
197229
newCfg, err := agent.ReadConfig(agent.ConfigPath(dataDir, machine0))
198230
c.Assert(err, jc.ErrorIsNil)
199231
c.Assert(newCfg.Tag(), gc.Equals, machine0)
200-
//c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), pwHash)
201-
c.Assert(agent.Password(newCfg), gc.Not(gc.Equals), testing.DefaultMongoPassword)
202232
info, ok := cfg.MongoInfo()
203233
c.Assert(ok, jc.IsTrue)
234+
c.Assert(info.Password, gc.Not(gc.Equals), testing.DefaultMongoPassword)
204235
st1, err := state.Open(newCfg.Model(), info, mongo.DefaultDialOpts(), environs.NewStatePolicy())
205236
c.Assert(err, jc.ErrorIsNil)
206237
defer st1.Close()
@@ -223,7 +254,7 @@ func (s *bootstrapSuite) TestInitializeStateWithStateServingInfoNotAvailable(c *
223254
c.Assert(available, jc.IsFalse)
224255

225256
adminUser := names.NewLocalUserTag("agent-admin")
226-
_, _, err = agent.InitializeState(adminUser, cfg, nil, agent.BootstrapMachineConfig{}, mongo.DefaultDialOpts(), environs.NewStatePolicy())
257+
_, _, err = agentbootstrap.InitializeState(adminUser, cfg, nil, nil, agentbootstrap.BootstrapMachineConfig{}, mongo.DefaultDialOpts(), environs.NewStatePolicy())
227258
// InitializeState will fail attempting to get the api port information
228259
c.Assert(err, gc.ErrorMatches, "state serving information not available")
229260
}
@@ -251,25 +282,32 @@ func (s *bootstrapSuite) TestInitializeStateFailsSecondTime(c *gc.C) {
251282
SystemIdentity: "qux",
252283
})
253284
expectHW := instance.MustParseHardware("mem=2048M")
254-
mcfg := agent.BootstrapMachineConfig{
285+
mcfg := agentbootstrap.BootstrapMachineConfig{
255286
BootstrapConstraints: constraints.MustParse("mem=1024M"),
256287
Jobs: []multiwatcher.MachineJob{multiwatcher.JobManageModel},
257288
InstanceId: "i-bootstrap",
258289
Characteristics: expectHW,
259290
}
260291
envAttrs := dummy.SampleConfig().Delete("admin-secret").Merge(testing.Attrs{
261292
"agent-version": jujuversion.Current.String(),
262-
"state-id": "1", // needed so policy can Open config
263293
})
264294
envCfg, err := config.New(config.NoDefaults, envAttrs)
265295
c.Assert(err, jc.ErrorIsNil)
266296

297+
hostedModelConfigAttrs := map[string]interface{}{
298+
"name": "hosted",
299+
"uuid": utils.MustNewUUID().String(),
300+
}
301+
267302
adminUser := names.NewLocalUserTag("agent-admin")
268-
st, _, err := agent.InitializeState(adminUser, cfg, envCfg, mcfg, mongo.DefaultDialOpts(), environs.NewStatePolicy())
303+
st, _, err := agentbootstrap.InitializeState(
304+
adminUser, cfg, envCfg, hostedModelConfigAttrs, mcfg,
305+
mongo.DefaultDialOpts(), state.Policy(nil),
306+
)
269307
c.Assert(err, jc.ErrorIsNil)
270308
st.Close()
271309

272-
st, _, err = agent.InitializeState(adminUser, cfg, envCfg, mcfg, mongo.DefaultDialOpts(), environs.NewStatePolicy())
310+
st, _, err = agentbootstrap.InitializeState(adminUser, cfg, envCfg, nil, mcfg, mongo.DefaultDialOpts(), environs.NewStatePolicy())
273311
if err == nil {
274312
st.Close()
275313
}
@@ -296,7 +334,7 @@ func (s *bootstrapSuite) TestMachineJobFromParams(c *gc.C) {
296334
err: `invalid machine job "invalid"`,
297335
}}
298336
for _, test := range tests {
299-
got, err := agent.MachineJobFromParams(test.name)
337+
got, err := agentbootstrap.MachineJobFromParams(test.name)
300338
if err != nil {
301339
c.Check(err, gc.ErrorMatches, test.err)
302340
}

agent/agentbootstrap/export_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2016 Canonical Ltd.
2+
// Licensed under the AGPLv3, see LICENCE file for details.
3+
4+
package agentbootstrap
5+
6+
var (
7+
MachineJobFromParams = machineJobFromParams
8+
)

agent/agentbootstrap/package_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2016 Canonical Ltd.
2+
// Licensed under the AGPLv3, see LICENCE file for details.
3+
4+
package agentbootstrap_test
5+
6+
import (
7+
stdtesting "testing"
8+
9+
"github.com/juju/juju/testing"
10+
)
11+
12+
func Test(t *stdtesting.T) {
13+
testing.MgoTestPackage(t)
14+
}

agent/export_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ import (
1010
"github.com/juju/juju/state/multiwatcher"
1111
)
1212

13-
func Password(config Config) string {
14-
c := config.(*configInternal)
15-
if c.stateDetails == nil {
16-
return c.apiDetails.password
17-
}
18-
return c.stateDetails.password
19-
}
20-
2113
func PatchConfig(config Config, fieldName string, value interface{}) error {
2214
conf := config.(*configInternal)
2315
switch fieldName {
@@ -55,10 +47,6 @@ func ConfigFileExists(config Config) bool {
5547
return err == nil
5648
}
5749

58-
var (
59-
MachineJobFromParams = machineJobFromParams
60-
)
61-
6250
func EmptyConfig() Config {
6351
return &configInternal{}
6452
}

0 commit comments

Comments
 (0)