/
deploy.py
69 lines (49 loc) · 1.69 KB
/
deploy.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
"""
Deploy Jinja filters
"""
from ansible.errors import AnsibleFilterError
BLUE_GREEN_PREFIXES = ["previous", "current", "next"]
def blue_green_host(host, prefix=None):
"""
Add next/previous prefix to the base host, if prefix is empty or equals
to 'current', the host is left unchanged.
Example usage:
{{ "foo.bar.com" | blue_green_host("previous") }}
# Should return: previous.foo.bar.com
{{ "foo.bar.com" | blue_green_host() }}
# Should return: foo.bar.com
{{ "foo.bar.com" | blue_green_host("current") }}
# Should return: foo.bar.com
"""
if prefix is None or not prefix:
return host
if prefix not in BLUE_GREEN_PREFIXES:
raise AnsibleFilterError(
f"prefix '{prefix}' is not allowed (must be in {BLUE_GREEN_PREFIXES})"
)
return f"{prefix}.{host}" if prefix != "current" else host
def blue_green_hosts(host):
"""
Add next/current/previous prefix to the base host, and return that list
of possible hosts.
Example usage:
{{ "foo.bar.com" | blue_green_hosts }}
# Should return: previous.foo.bar.com, foo.bar.com, next.foo.bar.com
"""
if not host:
raise AnsibleFilterError("host cannot be empty")
return ",".join(
[
f"{prefix}.{host}" if prefix != "current" else host
for prefix in BLUE_GREEN_PREFIXES
]
)
# pylint: disable=no-self-use,too-few-public-methods
class FilterModule:
"""Filters used for deployments"""
def filters(self):
"""List plugin filters"""
return {
"blue_green_host": blue_green_host,
"blue_green_hosts": blue_green_hosts,
}