Merge lp:~niedbalski/juju-deployer/fix-lp1454720 into lp:juju-deployer

Proposed by Jorge Niedbalski
Status: Merged
Merged at revision: 145
Proposed branch: lp:~niedbalski/juju-deployer/fix-lp1454720
Merge into: lp:juju-deployer
Diff against target: 187 lines (+110/-12)
4 files modified
deployer/action/importer.py (+40/-11)
deployer/deployment.py (+7/-1)
deployer/tests/test_data/v4/container-existing-machine.yaml (+42/-0)
deployer/tests/test_importer.py (+21/-0)
To merge this branch: bzr merge lp:~niedbalski/juju-deployer/fix-lp1454720
Reviewer Review Type Date Requested Status
Tim Van Steenburgh (community) Approve
Review via email: mp+259002@code.launchpad.net

Description of the change

Not forcefully create a machine if already exists on the environment.

To post a comment you must log in.
147. By Jorge Niedbalski

Added docstrings

Revision history for this message
Tim Van Steenburgh (tvansteenburgh) wrote :

LGTM, thanks Jorge! One inline comment for cleanup.

review: Approve
148. By Jorge Niedbalski

Addressed @tvan review

Revision history for this message
Jorge Niedbalski (niedbalski) wrote :

@tvansteenburgh

Thanks for reviewing. I just Re-submitted using your recommendations

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'deployer/action/importer.py'
--- deployer/action/importer.py 2015-03-24 17:38:56 +0000
+++ deployer/action/importer.py 2015-05-13 19:46:10 +0000
@@ -56,6 +56,11 @@
56 else:56 else:
57 self.env.add_units(svc.name, abs(delta))57 self.env.add_units(svc.name, abs(delta))
5858
59 def machine_exists(self, id):
60 """Checks if the given id exists on the current environment."""
61 return int(id) in map(int,
62 self.env.status().get('machines', {}).keys())
63
59 def create_machines(self):64 def create_machines(self):
60 """Create machines as specified in the machine spec in the bundle.65 """Create machines as specified in the machine spec in the bundle.
6166
@@ -70,26 +75,42 @@
70 foo: bar75 foo: bar
71 1:76 1:
72 constraints: "mem=4G"77 constraints: "mem=4G"
73 78
74 This method first attempts to create any machines in the 'machines'79 This method first attempts to create any machines in the 'machines'
75 section of the bundle specification with the given constraints and80 section of the bundle specification with the given constraints and
76 annotations. Then, if there are any machines requested for containers81 annotations. Then, if there are any machines requested for containers
77 in the style of <container type>:new, it creates those machines and82 in the style of <container type>:new, it creates those machines and
78 adds them to the machines map."""83 adds them to the machines map.
84
85 If the machine already exists on the environment, then the new machine
86 creation is skipped.
87 """
79 machines = self.deployment.get_machines()88 machines = self.deployment.get_machines()
89
80 if machines:90 if machines:
81 self.log.info("Creating machines...")91 self.log.info("Creating machines...")
82 for machine_name, spec in machines.items():92 for machine_name, spec in machines.items():
83 self.machines[machine_name] = self.env.add_machine(93 if self.machine_exists(machine_name):
84 series=spec.get('series', 94 self.log.info(
95 """Machine %s already exists on environment, """
96 """skipping creation""" % machine_name)
97 self.machines[machine_name] = self.deployment.get_machine(
98 machine_name)
99 else:
100 self.log.info("Machine %s will be created" % machine_name)
101 self.machines[machine_name] = self.env.add_machine(
102 series=spec.get('series',
85 self.deployment.data['series']),103 self.deployment.data['series']),
86 constraints=spec.get('constraints'))104 constraints=spec.get('constraints'))
87 annotations = spec.get('annotations')105
88 if annotations:106 if isinstance(spec, dict):
89 self.env.set_annotation(107 annotations = spec.get('annotations')
90 self.machines[machine_name],108 if annotations:
91 annotations,109 self.env.set_annotation(
92 entity_type='machine')110 self.machines[machine_name],
111 annotations,
112 entity_type='machine')
113
93 # In the case of <container type>:new, we need to create a machine114 # In the case of <container type>:new, we need to create a machine
94 # before creating the container on which the service will be115 # before creating the container on which the service will be
95 # deployed. This is stored in the machines map which will be used116 # deployed. This is stored in the machines map which will be used
@@ -97,7 +118,15 @@
97 for service in self.deployment.get_services():118 for service in self.deployment.get_services():
98 placement = self.deployment.get_unit_placement(service, None)119 placement = self.deployment.get_unit_placement(service, None)
99 for container_host in placement.get_new_machines_for_containers():120 for container_host in placement.get_new_machines_for_containers():
100 self.machines[container_host] = self.env.add_machine()121 if self.machine_exists(machine_name):
122 self.log.info("Machine %s already exists,"
123 "skipping creation" % machine_name)
124 self.machines[container_host] = self.deployment.get_machine(
125 container_host)
126 else:
127 self.log.info("A new container will be created"
128 "on machine: %s" % container_host)
129 self.machines[container_host] = self.env.add_machine()
101 self.deployment.set_machines(machines)130 self.deployment.set_machines(machines)
102131
103 def get_charms(self):132 def get_charms(self):
104133
=== modified file 'deployer/deployment.py'
--- deployer/deployment.py 2015-03-18 18:19:43 +0000
+++ deployer/deployment.py 2015-05-13 19:46:10 +0000
@@ -59,6 +59,12 @@
59 """59 """
60 self.machines = machines60 self.machines = machines
6161
62 def get_machine(self, id):
63 """Return a dict containing machine options
64 None is returned if the machine doesn't exists in
65 the bundle YAML."""
66 return self.get_machines().get(id, None)
67
62 def get_machines(self):68 def get_machines(self):
63 """Return a dict mapping machine names to machine options.69 """Return a dict mapping machine names to machine options.
6470
@@ -77,7 +83,7 @@
77 @staticmethod83 @staticmethod
78 def _machines_placement_sort(svc_a, svc_b):84 def _machines_placement_sort(svc_a, svc_b):
79 """Sort machines with machine placement in mind.85 """Sort machines with machine placement in mind.
80 86
81 If svc_a is colocated alongside svc_b, svc_b needs to be deployed87 If svc_a is colocated alongside svc_b, svc_b needs to be deployed
82 first, so that it exists by the time svc_a is deployed, and vice88 first, so that it exists by the time svc_a is deployed, and vice
83 versa; this sorts first based on this fact, then secondly based on89 versa; this sorts first based on this fact, then secondly based on
8490
=== added file 'deployer/tests/test_data/v4/container-existing-machine.yaml'
--- deployer/tests/test_data/v4/container-existing-machine.yaml 1970-01-01 00:00:00 +0000
+++ deployer/tests/test_data/v4/container-existing-machine.yaml 2015-05-13 19:46:10 +0000
@@ -0,0 +1,42 @@
1services:
2 mediawiki:
3 charm: cs:precise/mediawiki-10
4 num_units: 1
5 options:
6 debug: false
7 name: Please set name of wiki
8 skin: vector
9 annotations:
10 gui-x: "609"
11 gui-y: "-15"
12 to:
13 - "1"
14 mysql:
15 charm: cs:precise/mysql-28
16 num_units: 1
17 options:
18 binlog-format: MIXED
19 block-size: 5
20 dataset-size: 80%
21 flavor: distro
22 ha-bindiface: eth0
23 ha-mcastport: 5411
24 max-connections: -1
25 preferred-storage-engine: InnoDB
26 query-cache-size: -1
27 query-cache-type: "OFF"
28 rbd-name: mysql1
29 tuning-level: safest
30 vip_cidr: 24
31 vip_iface: eth0
32 annotations:
33 gui-x: "610"
34 gui-y: "255"
35 to:
36 - "lxc:1"
37series: precise
38relations:
39- - mediawiki:db
40 - mysql:db
41machines:
42 1: 1
043
=== modified file 'deployer/tests/test_importer.py'
--- deployer/tests/test_importer.py 2015-03-24 17:38:56 +0000
+++ deployer/tests/test_importer.py 2015-05-13 19:46:10 +0000
@@ -94,3 +94,24 @@
94 self.assertEqual(94 self.assertEqual(
95 env.add_machine.call_args_list[1][1],95 env.add_machine.call_args_list[1][1],
96 {'series': 'trusty', 'constraints': 'mem=512M'})96 {'series': 'trusty', 'constraints': 'mem=512M'})
97
98 @skip_if_offline
99 @mock.patch('deployer.action.importer.time')
100 def test_importer_existing_machine(self, mock_time):
101 self.options.configs = [
102 os.path.join(self.test_data_dir, 'v4',
103 'container-existing-machine.yaml')]
104 stack = ConfigStack(self.options.configs)
105 deploy = stack.get(self.options.configs[0])
106 env = mock.MagicMock()
107
108 config = {'status.return_value.get.return_value': {
109 1: {}
110 }}
111
112 env.configure_mock(**config)
113
114 importer = Importer(env, deploy, self.options)
115 importer.run()
116
117 env.add_machine.assert_not_called()

Subscribers

People subscribed via source and target branches