-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
176 lines (139 loc) · 5.22 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Tasks for maintaining the project.
Execute 'invoke --list' for guidance on using Invoke
"""
import platform
from pathlib import Path
from typing import Dict, List, Optional
from invoke import call, task
from invoke.context import Context
from invoke.runners import Result
ROOT_DIR = Path(__file__).parent
PYTHON_TARGETS = [
Path(__file__),
]
PYTHON_TARGETS_STR = " ".join([str(p) for p in PYTHON_TARGETS])
MOLECULE_DIR = ROOT_DIR / "molecule"
ROLES_DIR = ROOT_DIR / "roles"
PLAYBOOKS_DIR = ROOT_DIR / "playbooks"
ANSIBLE_TARGETS = [MOLECULE_DIR, PLAYBOOKS_DIR]
ANSIBLE_TARGETS_STR = " ".join([str(t) for t in ANSIBLE_TARGETS])
SAFETY_IGNORE = [42923]
def _run(c, command, env=None):
# type: (Context, str, Optional[Dict]) -> Result
return c.run(command, pty=platform.system() != "Windows", env=env)
@task()
def clean_python(c):
# type: (Context) -> None
"""Clean up python file artifacts."""
_run(c, "find . -name '*.pyc' -exec rm -f {} +")
_run(c, "find . -name '*.pyo' -exec rm -f {} +")
_run(c, "find . -name '*~' -exec rm -f {} +")
_run(c, "find . -name '__pycache__' -exec rm -fr {} +")
@task(pre=[clean_python])
def clean(c):
# type: (Context) -> None
"""Run all clean sub-tasks."""
@task()
def install_hooks(c):
# type: (Context) -> None
"""Install pre-commit hooks."""
_run(c, "poetry run pre-commit install")
@task()
def hooks(c):
# type: (Context) -> None
"""Run pre-commit hooks."""
_run(c, "poetry run pre-commit run --all-files")
@task(
help={
"force": "Force overwriting an existing role or collection. (default: False)",
}
)
def galaxy_install(c, force=False):
# type: (Context, bool) -> None
"""Install ansible-galaxy requirements."""
install_options = ["--force"] if force else []
_run(c, f"poetry run ansible-galaxy install -r requirements.yml {' '.join(install_options)}")
@task(name="format", help={"check": "Checks if source is formatted without applying changes"})
def format_(c, check=False):
# type: (Context, bool) -> None
"""Format code."""
isort_options = ["--check-only", "--diff"] if check else []
_run(c, f"poetry run isort {' '.join(isort_options)} {PYTHON_TARGETS_STR}")
black_options = ["--diff", "--check"] if check else ["--quiet"]
_run(c, f"poetry run black {' '.join(black_options)} {PYTHON_TARGETS_STR}")
@task()
def flake8(c):
# type: (Context) -> None
"""Run flake8."""
_run(c, f"poetry run flakeheaven lint {PYTHON_TARGETS_STR}")
@task()
def safety(c):
# type: (Context) -> None
"""Run safety."""
safety_options = ["--stdin", "--full-report"]
if SAFETY_IGNORE:
safety_options += ["-i", *[str(ignore) for ignore in SAFETY_IGNORE]]
_run(
c,
"poetry export --dev --format=requirements.txt --without-hashes | "
f"poetry run safety check {' '.join(safety_options)}",
)
@task()
def yamllint(c):
# type: (Context) -> None
"""Run yamllint, a linter for YAML files."""
_run(c, f"poetry run yamllint -c {ROOT_DIR / '.yamllint'} {ROOT_DIR}")
@task(help={"fix": "Allow ansible-lint to reformat YAML files and run rule transforms"})
def ansible_lint(c, fix=False):
# type: (Context, bool) -> None
"""Run ansible linter."""
lint_options = ["--force-color", "-p", "-v", "--project-dir", str(ROOT_DIR)]
if fix:
lint_options.append("--write")
_run(c, f"poetry run ansible-lint {' '.join(lint_options)} {ANSIBLE_TARGETS_STR}")
@task(pre=[flake8, safety, call(format_, check=True), yamllint, ansible_lint])
def lint(c):
# type: (Context) -> None
"""Run all linting."""
@task()
def mypy(c):
# type: (Context) -> None
"""Run mypy."""
_run(c, f"poetry run mypy {PYTHON_TARGETS_STR}")
@task(help={"target": "Name of the scenario to target."})
def tests(c, target="default"):
# type: (Context, str) -> None
"""Run ansible molecule test."""
molecule_options = ["-s", target]
_run(c, f"poetry run molecule test {' '.join(molecule_options)}")
@task(
help={
"target": "Ansible playbook to run. (default: main)",
"tag": "Only run plays and tasks tagged with these values",
"skip_tag": "Only run plays and tasks whose tags do not match these values",
"list_tags": "List all available tags",
},
iterable=["tag", "skip_tag"],
)
def playbook(c, tag, skip_tag, list_tags=False, target="main"):
# type: (Context, List[str], List[str], bool, str) -> None
"""Run Ansible playbooks, executing the defined tasks on the targeted hosts."""
playbook_options = ["-i", "inventory"]
if tag:
playbook_options += ["--tags", f'"{ ",".join(tag) }"']
if skip_tag:
playbook_options += ["--skip-tags", f'"{ ",".join(skip_tag) }"']
if list_tags:
playbook_options.append("--list-tags")
_run(c, f"poetry run ansible-playbook playbooks/{target}.yml {' '.join(playbook_options)}")
@task(
help={
"part": "Part of the version to be bumped.",
"dry_run": "Don't write any files, just pretend. (default: False)",
}
)
def version(c, part, dry_run=False):
# type: (Context, str, bool) -> None
"""Bump version."""
bump_options = ["--dry-run"] if dry_run else []
_run(c, f"poetry run bump2version {' '.join(bump_options)} {part}")