forked from quay/quay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_cloud_config.py
112 lines (98 loc) · 3.51 KB
/
container_cloud_config.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
"""
Provides helper methods and templates for generating cloud config for running containers.
Originally from https://github.com/DevTable/container-cloud-config
"""
import json
import logging
import os
from urllib.parse import quote as urlquote
from jinja2 import Environment, FileSystemLoader, StrictUndefined
logger = logging.getLogger(__name__)
_SUPPORTED_CONTAINER_RUNTIMES = ("docker", "podman")
class CloudConfigContext(object):
"""Context object for easy generating of cloud config."""
def populate_jinja_environment(self, env):
"""Populates the given jinja environment with the methods defined in this context."""
env.filters["registry"] = self.registry
env.filters["dataurl"] = self.data_url
env.filters["jsonify"] = json.dumps
env.globals["dockersystemd"] = self._dockersystemd_template
def _dockersystemd_template(
self,
name,
container,
container_runtime="docker",
username="",
password="",
tag="latest",
extra_args="",
after_units=[],
exec_start_post=[],
exec_stop_post=[],
restart_policy="always",
oneshot=False,
env_file=None,
onfailure_units=[],
requires_units=[],
wants_units=[],
timeout_start_sec=600,
timeout_stop_sec=2000,
autostart=True,
):
assert container_runtime in _SUPPORTED_CONTAINER_RUNTIMES
try:
timeout_start_sec = int(timeout_start_sec)
timeout_stop_sec = int(timeout_stop_sec)
except (ValueError, TypeError):
logger.error(
"Invalid timeouts (%s, %s): values should be integers",
timeout_start_sec,
timeout_stop_sec,
)
raise
path = os.path.join(os.path.dirname(__file__), "templates")
env = Environment(
loader=FileSystemLoader(path), undefined=StrictUndefined, trim_blocks=True
)
self.populate_jinja_environment(env)
template = env.get_template("dockersystemd.json")
return template.render(
name=name,
container=container,
container_runtime=container_runtime,
username=username,
password=password,
tag=tag,
extra_args=extra_args,
after_units=after_units,
requires_units=requires_units,
wants_units=wants_units,
onfailure_units=onfailure_units,
exec_start_post=exec_start_post,
exec_stop_post=exec_stop_post,
restart_policy=restart_policy,
oneshot=oneshot,
autostart=autostart,
timeout_start_sec=timeout_start_sec,
timeout_stop_sec=timeout_stop_sec,
env_file=env_file,
)
def data_url(self, content):
"""Encodes the content of an ignition file using RFC 2397."""
data = "," + urlquote(content)
return "data:" + data
def registry(self, container_name):
"""Parse the registry from repositories of the following formats:
quay.io/quay/quay:tagname -> quay.io
localhost:5000/quay/quay:tagname -> localhost:5000
localhost:5000/quay/quay -> localhost:5000
quay/quay:latest -> ''
quay/quay -> ''
mysql:latest -> ''
mysql -> ''
"""
num_slashes = container_name.count("/")
if num_slashes == 2:
return container_name[: container_name.find("/")]
else:
return ""