OLD | NEW |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package state | 4 package state |
5 | 5 |
6 import ( | 6 import ( |
7 "fmt" | 7 "fmt" |
8 "strconv" | 8 "strconv" |
9 | 9 |
10 "labix.org/v2/mgo/bson" | 10 "labix.org/v2/mgo/bson" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 var ms []*Machine | 126 var ms []*Machine |
127 env, err := st.Environment() | 127 env, err := st.Environment() |
128 if err != nil { | 128 if err != nil { |
129 return nil, err | 129 return nil, err |
130 } else if env.Life() != Alive { | 130 } else if env.Life() != Alive { |
131 return nil, fmt.Errorf("environment is no longer alive") | 131 return nil, fmt.Errorf("environment is no longer alive") |
132 } | 132 } |
133 var ops []txn.Op | 133 var ops []txn.Op |
134 var mdocs []*machineDoc | 134 var mdocs []*machineDoc |
135 for _, template := range templates { | 135 for _, template := range templates { |
| 136 // Adding a machine without any principals is |
| 137 // only permitted unit placement is supported. |
| 138 if len(template.principals) == 0 { |
| 139 if err := st.supportsUnitPlacement(); err != nil { |
| 140 return nil, err |
| 141 } |
| 142 } |
136 mdoc, addOps, err := st.addMachineOps(template) | 143 mdoc, addOps, err := st.addMachineOps(template) |
137 if err != nil { | 144 if err != nil { |
138 return nil, err | 145 return nil, err |
139 } | 146 } |
140 mdocs = append(mdocs, mdoc) | 147 mdocs = append(mdocs, mdoc) |
141 ms = append(ms, newMachine(st, mdoc)) | 148 ms = append(ms, newMachine(st, mdoc)) |
142 ops = append(ops, addOps...) | 149 ops = append(ops, addOps...) |
143 } | 150 } |
144 ssOps, err := st.maintainStateServersOps(mdocs, nil) | 151 ssOps, err := st.maintainStateServersOps(mdocs, nil) |
145 if err != nil { | 152 if err != nil { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 if template.InstanceId != "" { | 292 if template.InstanceId != "" { |
286 return nil, nil, fmt.Errorf("cannot specify instance id for a ne
w container") | 293 return nil, nil, fmt.Errorf("cannot specify instance id for a ne
w container") |
287 } | 294 } |
288 template, err := st.effectiveMachineTemplate(template, false) | 295 template, err := st.effectiveMachineTemplate(template, false) |
289 if err != nil { | 296 if err != nil { |
290 return nil, nil, err | 297 return nil, nil, err |
291 } | 298 } |
292 if containerType == "" { | 299 if containerType == "" { |
293 return nil, nil, fmt.Errorf("no container type specified") | 300 return nil, nil, fmt.Errorf("no container type specified") |
294 } | 301 } |
| 302 // Adding a machine within a machine implies add-machine or placement. |
| 303 if err := st.supportsUnitPlacement(); err != nil { |
| 304 return nil, nil, err |
| 305 } |
295 | 306 |
296 // If a parent machine is specified, make sure it exists | 307 // If a parent machine is specified, make sure it exists |
297 // and can support the requested container type. | 308 // and can support the requested container type. |
298 parent, err := st.Machine(parentId) | 309 parent, err := st.Machine(parentId) |
299 if err != nil { | 310 if err != nil { |
300 return nil, nil, err | 311 return nil, nil, err |
301 } | 312 } |
302 if !parent.supportsContainerType(containerType) { | 313 if !parent.supportsContainerType(containerType) { |
303 return nil, nil, fmt.Errorf("machine %s cannot host %s container
s", parentId, containerType) | 314 return nil, nil, fmt.Errorf("machine %s cannot host %s container
s", parentId, containerType) |
304 } | 315 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 if err != nil { | 356 if err != nil { |
346 return nil, nil, err | 357 return nil, nil, err |
347 } | 358 } |
348 if containerType == "" { | 359 if containerType == "" { |
349 return nil, nil, fmt.Errorf("no container type specified") | 360 return nil, nil, fmt.Errorf("no container type specified") |
350 } | 361 } |
351 if parentTemplate.InstanceId == "" { | 362 if parentTemplate.InstanceId == "" { |
352 if err := st.precheckInstance(parentTemplate.Series, parentTempl
ate.Constraints); err != nil { | 363 if err := st.precheckInstance(parentTemplate.Series, parentTempl
ate.Constraints); err != nil { |
353 return nil, nil, err | 364 return nil, nil, err |
354 } | 365 } |
| 366 // Adding a machine within a machine implies add-machine or plac
ement. |
| 367 if err := st.supportsUnitPlacement(); err != nil { |
| 368 return nil, nil, err |
| 369 } |
355 } | 370 } |
356 | 371 |
357 parentDoc := machineDocForTemplate(parentTemplate, strconv.Itoa(seq)) | 372 parentDoc := machineDocForTemplate(parentTemplate, strconv.Itoa(seq)) |
358 newId, err := st.newContainerId(parentDoc.Id, containerType) | 373 newId, err := st.newContainerId(parentDoc.Id, containerType) |
359 if err != nil { | 374 if err != nil { |
360 return nil, nil, err | 375 return nil, nil, err |
361 } | 376 } |
362 template, err = st.effectiveMachineTemplate(template, false) | 377 template, err = st.effectiveMachineTemplate(template, false) |
363 if err != nil { | 378 if err != nil { |
364 return nil, nil, err | 379 return nil, nil, err |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 if err != nil { | 544 if err != nil { |
530 return fmt.Errorf("cannot prepare machine add operations: %v", e
rr) | 545 return fmt.Errorf("cannot prepare machine add operations: %v", e
rr) |
531 } | 546 } |
532 ops = append(ops, ssOps...) | 547 ops = append(ops, ssOps...) |
533 err = st.runTransaction(ops) | 548 err = st.runTransaction(ops) |
534 if err != nil { | 549 if err != nil { |
535 return fmt.Errorf("failed to create new state server machines: %
v", err) | 550 return fmt.Errorf("failed to create new state server machines: %
v", err) |
536 } | 551 } |
537 return nil | 552 return nil |
538 } | 553 } |
OLD | NEW |