Improved enumeration of actions

This commit is contained in:
Chris Houseknecht 2016-10-05 00:45:27 -04:00 committed by Matt Clay
parent 1e4b8c13a3
commit 63b6672ea5

View file

@ -484,7 +484,7 @@ AUTH_PARAM_MAPPING = {
u'tls_verify': u'--tlsverify' u'tls_verify': u'--tlsverify'
} }
class ContainerManager(DockerBaseClass): class ContainerManager(DockerBaseClass):
def __init__(self, client): def __init__(self, client):
@ -598,11 +598,11 @@ class ContainerManager(DockerBaseClass):
return options return options
def cmd_up(self): def cmd_up(self):
start_deps = self.dependencies start_deps = self.dependencies
service_names = self.services service_names = self.services
detached = True detached = True
result = dict(changed=False, actions=dict(), ansible_facts=dict()) result = dict(changed=False, actions=[], ansible_facts=dict())
up_options = { up_options = {
u'--no-recreate': False, u'--no-recreate': False,
@ -624,26 +624,29 @@ class ContainerManager(DockerBaseClass):
self.log("convergence strategy: %s" % converge) self.log("convergence strategy: %s" % converge)
if self.pull: if self.pull:
result.update(self.cmd_pull()) pull_output = self.cmd_pull()
result['changed'] = pull_output['changed']
result['actions'] += pull_output['actions']
if self.build: if self.build:
result.update(self.cmd_build()) build_output = self.cmd_build()
result['changed'] = build_output['changed']
result['actions'] += build_output['actions']
for service in self.project.services: for service in self.project.services:
if not service_names or service.name in service_names: if not service_names or service.name in service_names:
plan = service.convergence_plan(strategy=converge) plan = service.convergence_plan(strategy=converge)
if plan.action != 'noop': if plan.action != 'noop':
result['changed'] = True result['changed'] = True
if self.debug or self.check_mode: result_action = dict(service=service.name)
if not result['actions'].get(service.name): result_action[plan.action] = []
result['actions'][service.name] = dict()
result['actions'][service.name][plan.action] = []
for container in plan.containers: for container in plan.containers:
result['actions'][service.name][plan.action].append(dict( result_action[plan.action].append(dict(
id=container.id, id=container.id,
name=container.name, name=container.name,
short_id=container.short_id, short_id=container.short_id,
)) ))
result['actions'].append(result_action)
if not self.check_mode and result['changed']: if not self.check_mode and result['changed']:
try: try:
@ -661,13 +664,19 @@ class ContainerManager(DockerBaseClass):
self.client.fail("Error starting project - %s" % str(exc)) self.client.fail("Error starting project - %s" % str(exc))
if self.stopped: if self.stopped:
result.update(self.cmd_stop(service_names)) stop_output = self.cmd_stop(service_names)
result['changed'] = stop_output['changed']
result['actions'] += stop_output['actions']
if self.restarted: if self.restarted:
result.update(self.cmd_restart(service_names)) restart_output = self.cmd_restart(service_names)
result['changed'] = restart_output['changed']
result['actions'] += restart_output['actions']
if self.scale: if self.scale:
result.update(self.cmd_scale()) scale_output = self.cmd_scale()
result['changed'] = scale_output['changed']
result['actions'] += scale_output['actions']
for service in self.project.services: for service in self.project.services:
result['ansible_facts'][service.name] = dict() result['ansible_facts'][service.name] = dict()
@ -729,11 +738,14 @@ class ContainerManager(DockerBaseClass):
def cmd_pull(self): def cmd_pull(self):
result = dict( result = dict(
changed=False, changed=False,
actions=dict(), actions=[],
) )
if not self.check_mode: if not self.check_mode:
for service in self.project.get_services(self.services, include_deps=False): for service in self.project.get_services(self.services, include_deps=False):
if 'image' not in service.options:
continue
self.log('Pulling image for service %s' % service.name) self.log('Pulling image for service %s' % service.name)
# store the existing image ID # store the existing image ID
old_image_id = '' old_image_id = ''
@ -764,22 +776,24 @@ class ContainerManager(DockerBaseClass):
if new_image_id != old_image_id: if new_image_id != old_image_id:
# if a new image was pulled # if a new image was pulled
result['changed'] = True result['changed'] = True
result['actions'][service.name] = dict() result['actions'].append(dict(
result['actions'][service.name]['pulled_image'] = dict( service=service.name,
name=service.image_name, pulled_image=dict(
id=new_image_id name=service.image_name,
) id=new_image_id
)
))
return result return result
def cmd_build(self): def cmd_build(self):
result = dict( result = dict(
changed=False, changed=False,
actions=dict(), actions=[]
) )
if not self.check_mode: if not self.check_mode:
for service in self.project.get_services(self.services, include_deps=False): for service in self.project.get_services(self.services, include_deps=False):
self.log('Building image for service %s' % service.name)
if service.can_be_built(): if service.can_be_built():
self.log('Building image for service %s' % service.name)
# store the existing image ID # store the existing image ID
old_image_id = '' old_image_id = ''
try: try:
@ -800,53 +814,55 @@ class ContainerManager(DockerBaseClass):
if new_image_id not in old_image_id: if new_image_id not in old_image_id:
# if a new image was built # if a new image was built
result['changed'] = True result['changed'] = True
result['actions'][service.name] = dict() result['actions'].append(dict(
result['actions'][service.name]['built_image'] = dict( service=service.name,
name=service.image_name, built_image=dict(
id=new_image_id name=service.image_name,
) id=new_image_id
)
))
return result return result
def cmd_down(self): def cmd_down(self):
result = dict( result = dict(
changed=False, changed=False,
actions=dict(), actions=[]
) )
for service in self.project.services: for service in self.project.services:
containers = service.containers(stopped=True) containers = service.containers(stopped=True)
if len(containers): if len(containers):
result['changed'] = True result['changed'] = True
if self.debug or self.check_mode: result['actions'].append(dict(
result['actions'][service.name] = dict() service=service.name,
result['actions'][service.name]['deleted'] = [container.name for container in containers] deleted=[container.name for container in containers]
))
if not self.check_mode and result['changed']: if not self.check_mode and result['changed']:
image_type = image_type_from_opt('--rmi', self.remove_images) image_type = image_type_from_opt('--rmi', self.remove_images)
try: try:
self.project.down(image_type, self.remove_volumes, self.remove_orphans) self.project.down(image_type, self.remove_volumes, self.remove_orphans)
except Exception as exc: except Exception as exc:
self.client.fail("Error stopping project - %s" % str(exc)) self.client.fail("Error stopping project - %s" % str(exc))
return result return result
def cmd_stop(self, service_names): def cmd_stop(self, service_names):
result = dict( result = dict(
changed=False, changed=False,
actions=dict() actions=[]
) )
for service in self.project.services: for service in self.project.services:
if not service_names or service.name in service_names: if not service_names or service.name in service_names:
result['actions'][service.name] = dict() service_res = dict(
result['actions'][service.name]['stop'] = [] service=service.name,
stop=[]
)
for container in service.containers(stopped=False): for container in service.containers(stopped=False):
result['changed'] = True result['changed'] = True
if self.debug: service_res['stop'].append(dict(
result['actions'][service.name]['stop'].append(dict( id=container.id,
id=container.id, name=container.name,
name=container.name, short_id=container.short_id
short_id=container.short_id, ))
)) result['actions'].append(service_res)
if not self.check_mode and result['changed']: if not self.check_mode and result['changed']:
try: try:
@ -859,22 +875,24 @@ class ContainerManager(DockerBaseClass):
def cmd_restart(self, service_names): def cmd_restart(self, service_names):
result = dict( result = dict(
changed=False, changed=False,
actions=dict() actions=[]
) )
for service in self.project.services: for service in self.project.services:
if not service_names or service.name in service_names: if not service_names or service.name in service_names:
result['actions'][service.name] = dict() service_res = dict(
result['actions'][service.name]['restart'] = [] service=service.name,
restart=[]
)
for container in service.containers(stopped=True): for container in service.containers(stopped=True):
result['changed'] = True result['changed'] = True
if self.debug or self.check_mode: service_res['restart'].append(dict(
result['actions'][service.name]['restart'].append(dict( id=container.id,
id=container.id, name=container.name,
name=container.name, short_id=container.short_id
short_id=container.short_id, ))
)) result['actions'].append(service_res)
if not self.check_mode and result['changed']: if not self.check_mode and result['changed']:
try: try:
self.project.restart(service_names=service_names, timeout=self.timeout) self.project.restart(service_names=service_names, timeout=self.timeout)
@ -886,22 +904,25 @@ class ContainerManager(DockerBaseClass):
def cmd_scale(self): def cmd_scale(self):
result = dict( result = dict(
changed=False, changed=False,
actions=dict() actions=[]
) )
for service in self.project.services: for service in self.project.services:
if service.name in self.scale: if service.name in self.scale:
result['actions'][service.name] = dict() service_res = dict(
service=service.name,
scale=0
)
containers = service.containers(stopped=True) containers = service.containers(stopped=True)
if len(containers) != self.scale[service.name]: if len(containers) != self.scale[service.name]:
result['changed'] = True result['changed'] = True
if self.debug or self.check_mode: service_res['scale'] = self.scale[service.name] - len(containers)
result['actions'][service.name]['scale'] = self.scale[service.name] - len(containers)
if not self.check_mode: if not self.check_mode:
try: try:
service.scale(int(self.scale[service.name])) service.scale(int(self.scale[service.name]))
except Exception as exc: except Exception as exc:
self.client.fail("Error scaling %s - %s" % (service.name, str(exc))) self.client.fail("Error scaling %s - %s" % (service.name, str(exc)))
result['actions'].append(service_res)
return result return result