-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-initialize.py
184 lines (151 loc) · 5.65 KB
/
docker-initialize.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
177
178
179
180
181
182
183
184
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import re
import os
import warnings
warnings.simplefilter("always", DeprecationWarning)
class Environment(object):
"""Configure container via environment variables"""
def __init__(
self,
env=os.environ,
zope_conf="/plone/instance/parts/instance/etc/zope.conf",
custom_conf="/plone/instance/custom.cfg",
zeopack_conf="/plone/instance/bin/zeopack",
zeoserver_conf="/plone/instance/parts/zeoserver/etc/zeo.conf",
):
self.env = env
self.zope_conf = zope_conf
self.custom_conf = custom_conf
self.zeopack_conf = zeopack_conf
self.zeoserver_conf = zeoserver_conf
def zeoclient(self):
"""ZEO Client"""
server = self.env.get("ZEO_ADDRESS", None)
if not server:
return
config = ""
with open(self.zope_conf, "r") as cfile:
config = cfile.read()
# Already initialized
if "<blobstorage>" not in config:
return
read_only = self.env.get("ZEO_READ_ONLY", "false")
zeo_ro_fallback = self.env.get("ZEO_CLIENT_READ_ONLY_FALLBACK", "false")
shared_blob_dir = self.env.get("ZEO_SHARED_BLOB_DIR", "off")
zeo_storage = self.env.get("ZEO_STORAGE", "1")
zeo_client_cache_size = self.env.get("ZEO_CLIENT_CACHE_SIZE", "128MB")
zeo_conf = ZEO_TEMPLATE.format(
zeo_address=server,
read_only=read_only,
zeo_client_read_only_fallback=zeo_ro_fallback,
shared_blob_dir=shared_blob_dir,
zeo_storage=zeo_storage,
zeo_client_cache_size=zeo_client_cache_size,
)
pattern = re.compile(r"<blobstorage>.+</blobstorage>", re.DOTALL)
config = re.sub(pattern, zeo_conf, config)
pattern = re.compile(r"%define ZEOADDRESS 8100", re.DOTALL)
config = re.sub(pattern, "%define ZEOADDRESS {}".format(server), config)
with open(self.zope_conf, "w") as cfile:
cfile.write(config)
def zeopack(self):
"""ZEO Pack"""
server = self.env.get("ZEO_ADDRESS", None)
if not server:
return
if ":" in server:
host, port = server.split(":")
else:
host, port = (server, "8100")
with open(self.zeopack_conf, "r") as cfile:
text = cfile.read()
text = text.replace('address = "8100"', 'address = "%s"' % server)
text = text.replace('host = "127.0.0.1"', 'host = "%s"' % host)
text = text.replace('port = "8100"', 'port = "%s"' % port)
with open(self.zeopack_conf, "w") as cfile:
cfile.write(text)
def zeoserver(self):
"""ZEO Server"""
pack_keep_old = self.env.get("ZEO_PACK_KEEP_OLD", "")
if pack_keep_old.lower() in ("false", "no", "0", "n", "f"):
with open(self.zeoserver_conf, "r") as cfile:
text = cfile.read()
if "pack-keep-old" not in text:
text = text.replace(
"</filestorage>", " pack-keep-old false\n</filestorage>"
)
with open(self.zeoserver_conf, "w") as cfile:
cfile.write(text)
def buildout(self):
"""Buildout from environment variables"""
# Already configured
if os.path.exists(self.custom_conf):
return
eggs = self.env.get("PLONE_ADDONS", self.env.get("ADDONS", "")).strip().split()
if not eggs:
eggs = self.env.get("BUILDOUT_EGGS", "").strip().split()
if eggs:
warnings.warn(
"BUILDOUT_EGGS is deprecated. Please use "
"PLONE_ADDONS instead !!!",
DeprecationWarning,
)
zcml = self.env.get("PLONE_ZCML", self.env.get("ZCML", "")).strip().split()
if not zcml:
zcml = self.env.get("BUILDOUT_ZCML", "").strip().split()
if zcml:
warnings.warn(
"BUILDOUT_ZCML is deprecated. Please use " "PLONE_ZCML instead !!!",
DeprecationWarning,
)
develop = (
self.env.get("PLONE_DEVELOP", self.env.get("DEVELOP", "")).strip().split()
)
if not develop:
develop = self.env.get("BUILDOUT_DEVELOP", "").strip().split()
if develop:
warnings.warn(
"BUILDOUT_DEVELOP is deprecated. Please use "
"PLONE_DEVELOP instead !!!",
DeprecationWarning,
)
if not (eggs or zcml or develop):
return
buildout = BUILDOUT_TEMPLATE.format(
eggs="\n\t".join(eggs), zcml="\n\t".join(zcml), develop="\n\t".join(develop)
)
with open(self.custom_conf, "w") as cfile:
cfile.write(buildout)
def setup(self, **kwargs):
self.buildout()
self.zeoclient()
self.zeopack()
self.zeoserver()
__call__ = setup
ZEO_TEMPLATE = """
<zeoclient>
read-only {read_only}
read-only-fallback {zeo_client_read_only_fallback}
blob-dir /data/blobstorage
shared-blob-dir {shared_blob_dir}
server {zeo_address}
storage {zeo_storage}
name zeostorage
var /plone/instance/parts/instance/var
cache-size {zeo_client_cache_size}
</zeoclient>
""".strip()
BUILDOUT_TEMPLATE = """
[buildout]
extends = develop.cfg
develop += {develop}
eggs += {eggs}
zcml += {zcml}
"""
def initialize():
"""Configure Plone instance as ZEO Client"""
environment = Environment()
environment.setup()
if __name__ == "__main__":
initialize()