OLD | NEW |
1 import os | 1 import os |
2 | 2 |
3 import yaml | 3 import yaml |
4 | 4 |
5 from twisted.internet.defer import inlineCallbacks | 5 from twisted.internet.defer import inlineCallbacks |
6 | 6 |
7 from juju.control.utils import get_environment, expand_path | 7 from juju.control import legacy |
| 8 from juju.control.utils import ( |
| 9 expand_constraints, expand_path, get_environment, sync_environment_state) |
8 | 10 |
9 from juju.charm.errors import ServiceConfigValueError | 11 from juju.charm.errors import ServiceConfigValueError |
10 from juju.charm.publisher import CharmPublisher | 12 from juju.charm.publisher import CharmPublisher |
11 from juju.charm.repository import resolve | 13 from juju.charm.repository import resolve |
12 from juju.errors import CharmError | 14 from juju.errors import CharmError |
13 from juju.machine.constraints import Constraints | |
14 from juju.state.endpoint import RelationEndpoint | 15 from juju.state.endpoint import RelationEndpoint |
15 from juju.state.environment import EnvironmentStateManager | |
16 from juju.state.placement import place_unit | 16 from juju.state.placement import place_unit |
17 from juju.state.relation import RelationStateManager | 17 from juju.state.relation import RelationStateManager |
18 from juju.state.service import ServiceStateManager | 18 from juju.state.service import ServiceStateManager |
19 | 19 |
20 | 20 |
21 def _expand_constraints(s): | |
22 if s: | |
23 return s.split(" ") | |
24 return [] | |
25 | |
26 | |
27 def configure_subparser(subparsers): | 21 def configure_subparser(subparsers): |
28 sub_parser = subparsers.add_parser("deploy", help=command.__doc__, | 22 sub_parser = subparsers.add_parser("deploy", help=command.__doc__, |
29 description=deploy.__doc__) | 23 description=deploy.__doc__) |
30 | 24 |
31 sub_parser.add_argument( | 25 sub_parser.add_argument( |
32 "--environment", "-e", | 26 "--environment", "-e", |
33 help="Environment to deploy the charm in.") | 27 help="Environment to deploy the charm in.") |
34 | 28 |
35 sub_parser.add_argument( | 29 sub_parser.add_argument( |
36 "--num-units", "-n", default=1, type=int, metavar="NUM", | 30 "--num-units", "-n", default=1, type=int, metavar="NUM", |
37 help="Number of service units to deploy.") | 31 help="Number of service units to deploy.") |
38 | 32 |
39 sub_parser.add_argument( | 33 sub_parser.add_argument( |
40 "-u", "--upgrade", default=False, action="store_true", | 34 "-u", "--upgrade", default=False, action="store_true", |
41 help="Deploy the charm on disk, increments revision if needed") | 35 help="Deploy the charm on disk, increments revision if needed") |
42 | 36 |
43 sub_parser.add_argument( | 37 sub_parser.add_argument( |
44 "--repository", | 38 "--repository", |
45 help="Directory for charm lookup and retrieval", | 39 help="Directory for charm lookup and retrieval", |
46 default=os.environ.get('JUJU_REPOSITORY'), | 40 default=os.environ.get("JUJU_REPOSITORY"), |
47 type=expand_path) | 41 type=expand_path) |
48 | 42 |
49 sub_parser.add_argument( | 43 sub_parser.add_argument( |
50 "--constraints", | 44 "--constraints", |
51 help="Hardware constraints for the service", | 45 help="Hardware constraints for the service", |
52 default=[], | 46 default=[], |
53 type=_expand_constraints) | 47 type=expand_constraints) |
54 | 48 |
55 sub_parser.add_argument( | 49 sub_parser.add_argument( |
56 "charm", nargs=None, | 50 "charm", nargs=None, |
57 help="Charm name") | 51 help="Charm name") |
58 | 52 |
59 sub_parser.add_argument( | 53 sub_parser.add_argument( |
60 "service_name", nargs="?", | 54 "service_name", nargs="?", |
61 help="Service name of deployed charm") | 55 help="Service name of deployed charm") |
62 | 56 |
63 sub_parser.add_argument( | 57 sub_parser.add_argument( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 121 |
128 charm_id = str(charm_url.with_revision(charm.get_revision())) | 122 charm_id = str(charm_url.with_revision(charm.get_revision())) |
129 | 123 |
130 # Validate config options prior to deployment attempt | 124 # Validate config options prior to deployment attempt |
131 service_options = {} | 125 service_options = {} |
132 service_name = service_name or charm_url.name | 126 service_name = service_name or charm_url.name |
133 if config_file: | 127 if config_file: |
134 service_options = parse_config_options( | 128 service_options = parse_config_options( |
135 config_file, service_name, charm) | 129 config_file, service_name, charm) |
136 | 130 |
137 constraints = Constraints.from_strs(environment.type, constraint_strs) | |
138 | |
139 charm = yield repo.find(charm_url) | 131 charm = yield repo.find(charm_url) |
140 charm_id = str(charm_url.with_revision(charm.get_revision())) | 132 charm_id = str(charm_url.with_revision(charm.get_revision())) |
141 | 133 |
142 provider = environment.get_machine_provider() | 134 provider = environment.get_machine_provider() |
143 placement_policy = provider.get_placement_policy() | 135 placement_policy = provider.get_placement_policy() |
| 136 constraint_set = yield provider.get_constraint_set() |
| 137 constraints = constraint_set.parse(constraint_strs) |
144 client = yield provider.connect() | 138 client = yield provider.connect() |
145 | 139 |
146 try: | 140 try: |
147 storage = yield provider.get_file_storage() | 141 yield legacy.check_constraints(client, constraint_strs) |
148 service_manager = ServiceStateManager(client) | 142 yield legacy.check_environment( |
149 environment_state_manager = EnvironmentStateManager(client) | 143 client, provider.get_legacy_config_keys()) |
150 yield environment_state_manager.set_config_state( | 144 yield sync_environment_state(client, env_config, environment.name) |
151 env_config, environment.name) | |
152 | 145 |
153 # Publish the charm to juju | 146 # Publish the charm to juju |
| 147 storage = yield provider.get_file_storage() |
154 publisher = CharmPublisher(client, storage) | 148 publisher = CharmPublisher(client, storage) |
155 yield publisher.add_charm(charm_id, charm) | 149 yield publisher.add_charm(charm_id, charm) |
156 result = yield publisher.publish() | 150 result = yield publisher.publish() |
157 | 151 |
158 # In future we might have multiple charms be published at | 152 # In future we might have multiple charms be published at |
159 # the same time. For now, extract the charm_state from the | 153 # the same time. For now, extract the charm_state from the |
160 # list. | 154 # list. |
161 charm_state = result[0] | 155 charm_state = result[0] |
162 | 156 |
163 # Create the service state | 157 # Create the service state |
| 158 service_manager = ServiceStateManager(client) |
164 service_state = yield service_manager.add_service_state( | 159 service_state = yield service_manager.add_service_state( |
165 service_name, charm_state, constraints) | 160 service_name, charm_state, constraints) |
166 | 161 |
167 # Use the charm's ConfigOptions instance to validate service | 162 # Use the charm's ConfigOptions instance to validate service |
168 # options.. Invalid options passed will thrown an exception | 163 # options.. Invalid options passed will thrown an exception |
169 # and prevent the deploy. | 164 # and prevent the deploy. |
170 state = yield service_state.get_config() | 165 state = yield service_state.get_config() |
171 charm_config = yield charm_state.get_config() | 166 charm_config = yield charm_state.get_config() |
172 # return the validated options with the defaults included | 167 # return the validated options with the defaults included |
173 service_options = charm_config.validate(service_options) | 168 service_options = charm_config.validate(service_options) |
174 state.update(service_options) | 169 state.update(service_options) |
175 yield state.write() | 170 yield state.write() |
176 | 171 |
177 # Create desired number of service units | 172 # Create desired number of service units |
178 for i in xrange(num_units): | 173 if (yield service_state.is_subordinate()): |
179 unit_state = yield service_state.add_unit_state() | 174 log.info("Subordinate %r awaiting relationship " |
180 yield place_unit(client, placement_policy, unit_state) | 175 "to principal for deployment.", service_name) |
| 176 else: |
| 177 for i in xrange(num_units): |
| 178 unit_state = yield service_state.add_unit_state() |
| 179 yield place_unit(client, placement_policy, unit_state) |
181 | 180 |
182 # Check if we have any peer relations to establish | 181 # Check if we have any peer relations to establish |
183 if charm.metadata.peers: | 182 if charm.metadata.peers: |
184 relation_manager = RelationStateManager(client) | 183 relation_manager = RelationStateManager(client) |
185 for peer_name, peer_info in charm.metadata.peers.items(): | 184 for peer_name, peer_info in charm.metadata.peers.items(): |
186 yield relation_manager.add_relation_state( | 185 yield relation_manager.add_relation_state( |
187 RelationEndpoint(service_name, | 186 RelationEndpoint(service_name, |
188 peer_info["interface"], | 187 peer_info["interface"], |
189 peer_name, | 188 peer_name, |
190 "peer")) | 189 "peer")) |
191 | 190 |
192 log.info("Charm deployed as service: %r", service_name) | 191 log.info("Charm deployed as service: %r", service_name) |
193 finally: | 192 finally: |
194 yield client.close() | 193 yield client.close() |
OLD | NEW |