Use pre-commit with black + yamllint

* Added pre-commit config
  * Added yamllint config
  * Updated example YAML files to match the yamllint config
  * black'd existing Python code
This commit is contained in:
Emmanuel BENOîT 2022-09-18 17:32:23 +02:00
parent 7ea55e277e
commit 4a56566c58
No known key found for this signature in database
GPG key ID: 2356DC6956CF54EF
6 changed files with 100 additions and 34 deletions

26
.pre-commit-config.yaml Normal file
View file

@ -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' ]

47
.yamllint Normal file
View file

@ -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' ] }

View file

@ -1,7 +1,7 @@
all: all:
hosts: hosts:
localhost: localhost: {}
# All of this should obviously come from some other inventory plugin. # All of this should obviously come from some other inventory plugin.

View file

@ -18,12 +18,12 @@ instructions:
block: block:
- when: inventory_hostname.startswith( 'evil' ) - when: inventory_hostname.startswith( 'evil' )
action: fail action: fail
msg: "{{ inventory_hostname }} is obviously evil, skipping." msg: '{{ inventory_hostname }} is obviously evil, skipping.'
rescue: rescue:
# Do not crash on redeemably evil VMs, but still skip them. # Do not crash on redeemably evil VMs, but still skip them.
- when: inv__data.unredeemable is defined - when: inv__data.unredeemable is defined
action: fail action: fail
msg: "{{ reconstructed_error }}" msg: '{{ reconstructed_error }}'
- action: create_group - action: create_group
group: reedmably_evil group: reedmably_evil
add_host: true add_host: true
@ -35,9 +35,9 @@ instructions:
block: block:
- action: create_group - action: create_group
group: managed 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 action: create_group
group: "{{ item }}" group: '{{ item }}'
parent: managed parent: managed
# Copy inv__data fields to separate inv__ variables # Copy inv__data fields to separate inv__ variables
@ -51,8 +51,8 @@ instructions:
- subcomponent - subcomponent
when: inv__data[item] is defined when: inv__data[item] is defined
action: set_fact action: set_fact
name: "inv__{{ item }}" name: inv__{{ item }}
value: "{{ inv__data[ item ] }}" value: '{{ inv__data[ item ] }}'
# Environment variable and groups # Environment variable and groups
- action: set_fact - action: set_fact
@ -64,7 +64,7 @@ instructions:
) )
}} }}
- action: create_group - action: create_group
group: "env_{{ inv__environment }}" group: env_{{ inv__environment }}
parent: by_environment parent: by_environment
add_host: true add_host: true
@ -80,33 +80,33 @@ instructions:
# Network group # Network group
- action: create_group - action: create_group
group: "net_{{ inv__network }}" group: net_{{ inv__network }}
parent: by_network parent: by_network
add_host: true add_host: true
# Service group # Service group
- action: set_var - action: set_var
name: service_group name: service_group
value: "svc_{{ inv__service }}" value: svc_{{ inv__service }}
- action: create_group - action: create_group
group: "{{ service_group }}" group: '{{ service_group }}'
parent: by_service parent: by_service
# Component group. We add the host directly if there is no subcomponent. # Component group. We add the host directly if there is no subcomponent.
- when: inv__component is defined - when: inv__component is defined
action: block action: block
vars: vars:
comp_group: "svcm_{{ inv__service }}_{{ inv__component }}" comp_group: svcm_{{ inv__service }}_{{ inv__component }}
block: block:
- action: create_group - action: create_group
group: "{{ comp_group }}" group: '{{ comp_group }}'
parent: "{{ service_group }}" parent: '{{ service_group }}'
# Subcomponent group, or lack thereof. # Subcomponent group, or lack thereof.
- when: inv__subcomponent is not defined - when: inv__subcomponent is not defined
action: add_host action: add_host
group: "{{ comp_group }}" group: '{{ comp_group }}'
- when: inv__subcomponent is defined - when: inv__subcomponent is defined
action: create_group action: create_group
group: "svcm_{{ inv__service }}_{{ inv__subcomponent }}" group: svcm_{{ inv__service }}_{{ inv__subcomponent }}
parent: "{{ comp_group }}" parent: '{{ comp_group }}'
add_host: true add_host: true

View file

@ -893,11 +893,7 @@ class RciBlock(RcInstruction):
output.extend(" " + s for s in instr.dump()) output.extend(" " + s for s in instr.dump())
def parse_action(self, record): def parse_action(self, record):
assert ( assert self._block is None and self._rescue is None and self._always is None
self._block is None
and self._rescue is None
and self._always is None
)
if "block" not in record: if "block" not in record:
raise AnsibleParserError("%s: missing 'block' field" % (self._action,)) raise AnsibleParserError("%s: missing 'block' field" % (self._action,))
self._block = self.parse_block(record, "block") self._block = self.parse_block(record, "block")
@ -938,11 +934,7 @@ class RciBlock(RcInstruction):
return instructions return instructions
def execute_action(self, host_name, variables): def execute_action(self, host_name, variables):
assert not ( assert not (self._block is None or self._rescue is None or self._always is None)
self._block is None
or self._rescue is None
or self._always is None
)
try: try:
try: try:
self._display.vvv("- running 'block' instructions") self._display.vvv("- running 'block' instructions")

View file

@ -1,2 +1,3 @@
ansible-core>=2.13 ansible-core>=2.13
pre-commit
pytest>=7.1,<7.2 pytest>=7.1,<7.2