~hazmat/pyjuju/proposed-support

« back to all changes in this revision

Viewing changes to juju/control/constraints_set.py

  • Committer: kapil.thangavelu at canonical
  • Date: 2012-05-22 22:08:15 UTC
  • mfrom: (484.1.53 trunk)
  • Revision ID: kapil.thangavelu@canonical.com-20120522220815-acyt8m89i9ybe0w1
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
from twisted.internet.defer import inlineCallbacks
4
4
 
5
 
from juju.control.utils import get_environment
6
 
from juju.machine.constraints import Constraints
 
5
from juju.control import legacy
 
6
from juju.control.utils import get_environment, sync_environment_state
 
7
from juju.state.environment import EnvironmentStateManager
7
8
from juju.state.service import ServiceStateManager
8
9
 
9
10
 
34
35
    """Set machine constraints for the environment, or for a named service.
35
36
    """
36
37
    environment = get_environment(options)
 
38
    env_config = options.environments
37
39
    return constraints_set(
38
 
        environment, options.service, options.constraints)
 
40
        env_config, environment, options.service, options.constraints)
39
41
 
40
42
 
41
43
@inlineCallbacks
42
 
def constraints_set(environment, service_name, constraint_strs):
 
44
def constraints_set(env_config, environment, service_name, constraint_strs):
43
45
    """
44
46
    Machine constraints allow you to pick the hardware to which your services
45
47
    will be deployed. Examples:
46
48
 
47
49
    $ juju set-constraints --service-name mysql mem=8G cpu=4
48
50
 
49
 
    $ juju set-constraints ec2-instance-type=t1.micro
50
 
 
51
 
    "arch", "cpu" and "mem" are always available; other constraints are
52
 
    provider-specific, and will be ignored if specified in an environment of
53
 
    the wrong kind. The recognised constraints are currently:
54
 
 
55
 
    * arch (CPU architecture: x86/amd64/arm; unset by default)
56
 
    * cpu (processing power in Amazon ECU; 1 by default)
57
 
    * mem (memory in [MGT]iB; 512M by default)
58
 
    * ec2-region (us-east-1 by default)
59
 
    * ec2-zone (unset by default)
60
 
    * ec2-instance-type (unset by default)
61
 
    * orchestra-classes (unset by default)
62
 
    * orchestra-name (unset by default)
 
51
    $ juju set-constraints instance-type=t1.micro
 
52
 
 
53
    Available constraints vary by provider type, and will be ignored if not
 
54
    understood by the current environment's provider. The current set of
 
55
    available constraints across all providers is:
 
56
 
 
57
    On Amazon EC2:
 
58
 
 
59
        * arch (CPU architecture: i386/amd64/arm; amd64 by default)
 
60
        * cpu (processing power in Amazon ECU; 1 by default)
 
61
        * mem (memory in [MGT]iB; 512M by default)
 
62
        * instance-type (unset by default)
 
63
        * ec2-zone (unset by default)
 
64
 
 
65
    On Orchestra:
 
66
 
 
67
        * orchestra-classes (unset by default)
 
68
 
 
69
    On MAAS:
 
70
 
 
71
        * maas-name (unset by default)
63
72
 
64
73
    Service settings, if specified, will override environment settings, which
65
 
    will in turn override the juju defaults of mem=512M, cpu=1,
66
 
    ec2-region=us-east-1.
 
74
    will in turn override the juju defaults of mem=512M, cpu=1, arch=amd64.
67
75
 
68
76
    New constraints set on an entity will completely replace that entity's
69
77
    pre-existing constraints.
72
80
    service constraints, just specify "name=" (rather than just not specifying
73
81
    the constraint at all, which will cause it to inherit the environment's
74
82
    value).
 
83
 
 
84
    To entirely unset a constraint, specify "name=any".
75
85
    """
76
 
    constraints = Constraints.from_strs(environment.type, constraint_strs)
77
 
 
78
 
    if service_name is None:
79
 
        raise NotImplementedError("Environment constraints not implemented")
80
 
 
81
86
    provider = environment.get_machine_provider()
 
87
    constraint_set = yield provider.get_constraint_set()
 
88
    constraints = constraint_set.parse(constraint_strs)
82
89
    client = yield provider.connect()
83
90
    try:
84
 
        service_state_manager = ServiceStateManager(client)
85
 
        service = yield service_state_manager.get_service_state(service_name)
86
 
        yield service.set_constraints(constraints)
 
91
        yield legacy.check_constraints(client, constraint_strs)
 
92
        yield sync_environment_state(client, env_config, environment.name)
 
93
        if service_name is None:
 
94
            esm = EnvironmentStateManager(client)
 
95
            yield esm.set_constraints(constraints)
 
96
        else:
 
97
            ssm = ServiceStateManager(client)
 
98
            service = yield ssm.get_service_state(service_name)
 
99
            yield service.set_constraints(constraints)
87
100
    finally:
88
101
        yield client.close()