From 4199634e98b8e7962981e1215e389d6668140534 Mon Sep 17 00:00:00 2001 From: SamYaple Date: Mon, 21 Dec 2015 17:04:09 +0000 Subject: [PATCH] pep8 the ansible modules By ignoring the appropriate tests that pep8 does we can properly run *most* of the pep8 tests on all of our modules allowing for a more consistent coding style. Closes-Bug: #1528431 Change-Id: I33f27a250d06d4f044267aa3ad189e092789b8df --- ansible/action_plugins/merge_configs.py | 50 +++++++++++-------- ansible/library/bslurp.py | 23 +++++---- docker/kolla-ansible/find_disks.py | 10 ++-- .../kolla-ansible/kolla_keystone_service.py | 46 +++++++++-------- docker/kolla-ansible/kolla_keystone_user.py | 33 ++++++------ docker/kolla-ansible/kolla_sanity.py | 4 +- docker/kolla-ansible/kolla_zookeeper.py | 3 +- tox.ini | 2 +- 8 files changed, 93 insertions(+), 78 deletions(-) diff --git a/ansible/action_plugins/merge_configs.py b/ansible/action_plugins/merge_configs.py index 622f6f91d2..2d2b6545bc 100644 --- a/ansible/action_plugins/merge_configs.py +++ b/ansible/action_plugins/merge_configs.py @@ -18,9 +18,9 @@ from ConfigParser import ConfigParser from cStringIO import StringIO import os +from ansible.runner.return_data import ReturnData from ansible import utils from ansible.utils import template -from ansible.runner.return_data import ReturnData class ActionModule(object): @@ -30,7 +30,20 @@ class ActionModule(object): def __init__(self, runner): self.runner = runner - def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs): + def read_config(self, source, inject, config): + # Only use config if present + if os.access(source, os.R_OK): + # template the source data locally & get ready to transfer + resultant = template.template_from_file(self.runner.basedir, + source, inject) + + # Read in new results and merge this with the existing config + fakefile = StringIO(resultant) + config.readfp(fakefile) + fakefile.close() + + def run(self, conn, tmp, module_name, module_args, inject, + complex_args=None, **kwargs): args = {} if complex_args: args.update(complex_args) @@ -47,7 +60,6 @@ class ActionModule(object): else: inject.update(utils.parse_kv(extra_vars)) - # Catch the case where sources is a str() if not isinstance(sources, list): sources = [sources] @@ -58,33 +70,29 @@ class ActionModule(object): # template the source string source = template.template(self.runner.basedir, source, inject) - # Only use config if present - if os.access(source, os.R_OK): - # template the source data locally & get ready to transfer - try: - resultant = template.template_from_file(self.runner.basedir, source, inject) - except Exception as e: - return ReturnData(conn=conn, comm_ok=False, result={'failed': True, 'msg': str(e)}) - - # Read in new results and merge this with the existing config - fakefile = StringIO(resultant) - config.readfp(fakefile) - fakefile.close() + try: + self.read_config(source, inject, config) + except Exception as e: + return ReturnData(conn=conn, comm_ok=False, + result={'failed': True, 'msg': str(e)}) # Dump configparser to string via an emulated file fakefile = StringIO() config.write(fakefile) # Template the file to fill out any variables - content = template.template(self.runner.basedir, fakefile.getvalue(), inject) + content = template.template(self.runner.basedir, fakefile.getvalue(), + inject) fakefile.close() # Ship this content over to a new file for use with the copy module xfered = self.runner._transfer_str(conn, tmp, 'source', content) copy_module_args = dict( - src=xfered, - dest=dest, - original_basename=os.path.basename(source), - follow=True, + src=xfered, + dest=dest, + original_basename=os.path.basename(source), + follow=True, ) - return self.runner._execute_module(conn, tmp, 'copy', '', inject=inject, complex_args=copy_module_args) + return self.runner._execute_module(conn, tmp, 'copy', '', + inject=inject, + complex_args=copy_module_args) diff --git a/ansible/library/bslurp.py b/ansible/library/bslurp.py index 485b4c3b46..a4479c2442 100644 --- a/ansible/library/bslurp.py +++ b/ansible/library/bslurp.py @@ -109,9 +109,9 @@ Distribute a to file many host without compression; Change permissions on dest: import base64 import hashlib import os -import sys import zlib + def copy_from_host(module): compress = module.params.get('compress') src = module.params.get('src') @@ -121,7 +121,7 @@ def copy_from_host(module): if not os.access(src, os.R_OK): module.fail_json(msg="file is not readable: {}".format(src)) - mode = oct(os.stat(src).st_mode & 0777) + mode = oct(os.stat(src).st_mode & 0o777) with open(src, 'rb') as f: raw_data = f.read() @@ -162,16 +162,16 @@ def copy_to_host(module): module.exit_json(changed=True) + def main(): - module = AnsibleModule( - argument_spec = dict( - compress = dict(default=True, type='bool'), - dest = dict(type='str'), - mode = dict(default='0644', type='str'), - sha1 = dict(default=None, type='str'), - src = dict(required=True, type='str') - ) + argument_spec = dict( + compress=dict(default=True, type='bool'), + dest=dict(type='str'), + mode=dict(default='0644', type='str'), + sha1=dict(default=None, type='str'), + src=dict(required=True, type='str') ) + module = AnsibleModule(argument_spec) dest = module.params.get('dest') @@ -183,7 +183,8 @@ def main(): except Exception as e: module.exit_json(failed=True, changed=True, msg=repr(e)) + # import module snippets -from ansible.module_utils.basic import * +from ansible.module_utils.basic import * # noqa if __name__ == '__main__': main() diff --git a/docker/kolla-ansible/find_disks.py b/docker/kolla-ansible/find_disks.py index 7b9324b2ce..d5761ceb0c 100644 --- a/docker/kolla-ansible/find_disks.py +++ b/docker/kolla-ansible/find_disks.py @@ -45,12 +45,12 @@ EXAMPLES = ''' import json import pyudev + def main(): - module = AnsibleModule( - argument_spec = dict( - partition_name = dict(required=True, type='str') - ) + argument_spec = dict( + partition_name=dict(required=True, type='str') ) + module = AnsibleModule(argument_spec) partition_name = module.params.get('partition_name') try: @@ -68,6 +68,6 @@ def main(): module.exit_json(failed=True, msg=repr(e)) # import module snippets -from ansible.module_utils.basic import * +from ansible.module_utils.basic import * # noqa if __name__ == '__main__': main() diff --git a/docker/kolla-ansible/kolla_keystone_service.py b/docker/kolla-ansible/kolla_keystone_service.py index addd14a923..7c03d00e13 100644 --- a/docker/kolla-ansible/kolla_keystone_service.py +++ b/docker/kolla-ansible/kolla_keystone_service.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This file is a barebones file needed to file a gap until Ansible 2.0. No error -# checking, no deletions, no updates. Idempotent creation only. +# This file is a barebones file needed to file a gap until Ansible 2.0. No +# error checking, no deletions, no updates. Idempotent creation only. # If you look closely, you will see we arent _really_ using the shade module # we just use it to slightly abstract the authentication model. As patches land @@ -24,18 +24,18 @@ import shade + def main(): - module = AnsibleModule( - argument_spec = openstack_full_argument_spec( - description = dict(required=True, type='str'), - service_name = dict(required=True, type='str'), - service_type = dict(required=True, type='str'), - admin_url = dict(required=True, type='str'), - internal_url = dict(required=True, type='str'), - public_url = dict(required=True, type='str'), - endpoint_region = dict(required=True, type='str') - ) + argument_spec = openstack_full_argument_spec( + description=dict(required=True, type='str'), + service_name=dict(required=True, type='str'), + service_type=dict(required=True, type='str'), + admin_url=dict(required=True, type='str'), + internal_url=dict(required=True, type='str'), + public_url=dict(required=True, type='str'), + endpoint_region=dict(required=True, type='str') ) + module = AnsibleModule(argument_spec) try: description = module.params.pop('description') @@ -61,24 +61,26 @@ def main(): if _endpoint.service_id == service.id: endpoint = _endpoint else: - service = cloud.keystone_client.services.create(name=service_name, - service_type=service_type, - description=description) + service = cloud.keystone_client.services.create( + name=service_name, + service_type=service_type, + description=description) if endpoint is None: changed = True - cloud.keystone_client.endpoints.create(service_id=service.id, - adminurl=admin_url, - internalurl=internal_url, - publicurl=public_url, - region=endpoint_region) + cloud.keystone_client.endpoints.create( + service_id=service.id, + adminurl=admin_url, + internalurl=internal_url, + publicurl=public_url, + region=endpoint_region) module.exit_json(changed=changed) except Exception as e: module.exit_json(failed=True, changed=True, msg=e) # import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.openstack import * +from ansible.module_utils.basic import * # noqa +from ansible.module_utils.openstack import * # noqa if __name__ == '__main__': main() diff --git a/docker/kolla-ansible/kolla_keystone_user.py b/docker/kolla-ansible/kolla_keystone_user.py index 5d295c1e27..2d1a739c5c 100644 --- a/docker/kolla-ansible/kolla_keystone_user.py +++ b/docker/kolla-ansible/kolla_keystone_user.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This file is a barebones file needed to file a gap until Ansible 2.0. No error -# checking, no deletions, no updates. Idempotent creation only. +# This file is a barebones file needed to file a gap until Ansible 2.0. No +# error checking, no deletions, no updates. Idempotent creation only. # If you look closely, you will see we arent _really_ using the shade module # we just use it to slightly abstract the authentication model. As patches land @@ -24,15 +24,15 @@ import shade + def main(): - module = AnsibleModule( - argument_spec = openstack_full_argument_spec( - password = dict(required=True, type='str'), - project = dict(required=True, type='str'), - role = dict(required=True, type='str'), - user = dict(required=True, type='str') - ) + argument_spec = openstack_full_argument_spec( + password=dict(required=True, type='str'), + project=dict(required=True, type='str'), + role=dict(required=True, type='str'), + user=dict(required=True, type='str') ) + module = AnsibleModule(argument_spec) try: password = module.params.pop('password') @@ -61,7 +61,8 @@ def main(): if not project: changed = True - project = cloud.keystone_client.tenants.create(tenant_name=project_name) + project = cloud.keystone_client.tenants.create( + tenant_name=project_name) if not role: changed = True @@ -69,15 +70,19 @@ def main(): if not user: changed = True - user = cloud.keystone_client.users.create(name=user_name, password=password, tenant_id=project.id) - cloud.keystone_client.roles.add_user_role(role=role.id, user=user.id, tenant=project.id) + user = cloud.keystone_client.users.create(name=user_name, + password=password, + tenant_id=project.id) + cloud.keystone_client.roles.add_user_role(role=role.id, + user=user.id, + tenant=project.id) module.exit_json(changed=changed) except Exception as e: module.exit_json(failed=True, changed=True, msg=e) # import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.openstack import * +from ansible.module_utils.basic import * # noqa +from ansible.module_utils.openstack import * # noqa if __name__ == '__main__': main() diff --git a/docker/kolla-ansible/kolla_sanity.py b/docker/kolla-ansible/kolla_sanity.py index d579a1093a..7f4eea9fc8 100644 --- a/docker/kolla-ansible/kolla_sanity.py +++ b/docker/kolla-ansible/kolla_sanity.py @@ -57,7 +57,7 @@ def main(): module.exit_json(failed=True, changed=True, msg=e) # import module snippets -from ansible.module_utils.basic import * -from ansible.module_utils.openstack import * +from ansible.module_utils.basic import * # noqa +from ansible.module_utils.openstack import * # noqa if __name__ == '__main__': main() diff --git a/docker/kolla-ansible/kolla_zookeeper.py b/docker/kolla-ansible/kolla_zookeeper.py index 5362000762..d6326f46a8 100644 --- a/docker/kolla-ansible/kolla_zookeeper.py +++ b/docker/kolla-ansible/kolla_zookeeper.py @@ -61,7 +61,6 @@ def main(): # import module snippets -from ansible.module_utils.basic import * - +from ansible.module_utils.basic import * # noqa if __name__ == '__main__': main() diff --git a/tox.ini b/tox.ini index ec7eca5598..9a579f24a7 100644 --- a/tox.ini +++ b/tox.ini @@ -104,4 +104,4 @@ commands = [flake8] show-source = True -exclude=.eggs,.git,.tox,doc,ansible/library,ansible/action_plugins,docker/kolla-ansible +exclude=.eggs,.git,.tox,doc