diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..056015d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,26 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: check-case-conflict + - id: check-added-large-files + - id: check-merge-conflict + - id: mixed-line-ending + args: [ --fix=lf ] + - id: trailing-whitespace + args: [ --markdown-linebreak-ext=md ] + - id: debug-statements + - id: requirements-txt-fixer + args: [ requirements-dev.txt ] + - repo: https://github.com/adrienverge/yamllint + rev: v1.28.0 + hooks: + - id: yamllint + types: [ file ] + files: ^(.*\.ya?ml(\.example)?|\.yamllint)$ + - repo: https://github.com/psf/black + rev: 22.8.0 + hooks: + - id: black + language_version: python3.9 + args: [ -l , '88' ] diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..d315c31 --- /dev/null +++ b/.yamllint @@ -0,0 +1,47 @@ +rules: + braces: &braces_config + min-spaces-inside: 1 + max-spaces-inside: 1 + min-spaces-inside-empty: 0 + max-spaces-inside-empty: 1 + brackets: *braces_config + colons: + max-spaces-before: 0 + max-spaces-after: 1 + commas: + max-spaces-before: 1 + min-spaces-after: 1 + max-spaces-after: 1 + comments: + require-starting-space: true + ignore-shebangs: true + min-spaces-from-content: 4 + comments-indentation: { level: warning } + document-end: { present: false } + document-start: disable + empty-lines: + max: 2 + max-start: 0 + max-end: 0 + empty-values: + forbid-in-block-mappings: true + forbid-in-flow-mappings: true + hyphens: { max-spaces-after: 1 } + indentation: + spaces: 2 + indent-sequences: true + key-duplicates: enable + key-ordering: disable + line-length: + max: 88 + allow-non-breakable-words: true + allow-non-breakable-inline-mappings: true + new-line-at-end-of-file: enable + new-lines: + type: unix + octal-values: disable + quoted-strings: + required: only-when-needed + quote-type: single + trailing-spaces: enable + truthy: { allowed-values: [ 'true' , 'false' ] } diff --git a/example/00-data.yml b/example/00-data.yml index d8c0fc0..186f065 100644 --- a/example/00-data.yml +++ b/example/00-data.yml @@ -1,7 +1,7 @@ all: hosts: - localhost: + localhost: {} # All of this should obviously come from some other inventory plugin. diff --git a/example/01-test-reconstructed.yml b/example/01-test-reconstructed.yml index 27b7ee1..c697f6c 100644 --- a/example/01-test-reconstructed.yml +++ b/example/01-test-reconstructed.yml @@ -18,12 +18,12 @@ instructions: block: - when: inventory_hostname.startswith( 'evil' ) action: fail - msg: "{{ inventory_hostname }} is obviously evil, skipping." + msg: '{{ inventory_hostname }} is obviously evil, skipping.' rescue: # Do not crash on redeemably evil VMs, but still skip them. - when: inv__data.unredeemable is defined action: fail - msg: "{{ reconstructed_error }}" + msg: '{{ reconstructed_error }}' - action: create_group group: reedmably_evil add_host: true @@ -35,9 +35,9 @@ instructions: block: - action: create_group group: managed - - loop: [by_environment, by_network, by_failover_stack, by_service] + - loop: [ by_environment, by_network, by_failover_stack, by_service ] action: create_group - group: "{{ item }}" + group: '{{ item }}' parent: managed # Copy inv__data fields to separate inv__ variables @@ -51,8 +51,8 @@ instructions: - subcomponent when: inv__data[item] is defined action: set_fact - name: "inv__{{ item }}" - value: "{{ inv__data[ item ] }}" + name: inv__{{ item }} + value: '{{ inv__data[ item ] }}' # Environment variable and groups - action: set_fact @@ -64,7 +64,7 @@ instructions: ) }} - action: create_group - group: "env_{{ inv__environment }}" + group: env_{{ inv__environment }} parent: by_environment add_host: true @@ -80,33 +80,33 @@ instructions: # Network group - action: create_group - group: "net_{{ inv__network }}" + group: net_{{ inv__network }} parent: by_network add_host: true # Service group - action: set_var name: service_group - value: "svc_{{ inv__service }}" + value: svc_{{ inv__service }} - action: create_group - group: "{{ service_group }}" + group: '{{ service_group }}' parent: by_service # Component group. We add the host directly if there is no subcomponent. - when: inv__component is defined action: block vars: - comp_group: "svcm_{{ inv__service }}_{{ inv__component }}" + comp_group: svcm_{{ inv__service }}_{{ inv__component }} block: - - action: create_group - group: "{{ comp_group }}" - parent: "{{ service_group }}" - # Subcomponent group, or lack thereof. - - when: inv__subcomponent is not defined - action: add_host - group: "{{ comp_group }}" - - when: inv__subcomponent is defined - action: create_group - group: "svcm_{{ inv__service }}_{{ inv__subcomponent }}" - parent: "{{ comp_group }}" - add_host: true + - action: create_group + group: '{{ comp_group }}' + parent: '{{ service_group }}' + # Subcomponent group, or lack thereof. + - when: inv__subcomponent is not defined + action: add_host + group: '{{ comp_group }}' + - when: inv__subcomponent is defined + action: create_group + group: svcm_{{ inv__service }}_{{ inv__subcomponent }} + parent: '{{ comp_group }}' + add_host: true diff --git a/inventory_plugins/reconstructed.py b/inventory_plugins/reconstructed.py index ee82e47..0b4d9c9 100644 --- a/inventory_plugins/reconstructed.py +++ b/inventory_plugins/reconstructed.py @@ -893,11 +893,7 @@ class RciBlock(RcInstruction): output.extend(" " + s for s in instr.dump()) def parse_action(self, record): - assert ( - self._block is None - and self._rescue is None - and self._always is None - ) + assert self._block is None and self._rescue is None and self._always is None if "block" not in record: raise AnsibleParserError("%s: missing 'block' field" % (self._action,)) self._block = self.parse_block(record, "block") @@ -938,11 +934,7 @@ class RciBlock(RcInstruction): return instructions def execute_action(self, host_name, variables): - assert not ( - self._block is None - or self._rescue is None - or self._always is None - ) + assert not (self._block is None or self._rescue is None or self._always is None) try: try: self._display.vvv("- running 'block' instructions") diff --git a/requirements-dev.txt b/requirements-dev.txt index 39a6f50..d42f352 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ ansible-core>=2.13 +pre-commit pytest>=7.1,<7.2