-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathspilo_commons.py
More file actions
85 lines (62 loc) · 2.58 KB
/
spilo_commons.py
File metadata and controls
85 lines (62 loc) · 2.58 KB
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
import logging
import os
import subprocess
import re
import yaml
logger = logging.getLogger('__name__')
RW_DIR = os.environ.get('RW_DIR', '/run')
PATRONI_CONFIG_FILE = os.path.join(RW_DIR, 'postgres.yml')
LIB_DIR = '/usr/lib/postgresql'
# (min_version, max_version, shared_preload_libraries, extwlist.extensions)
extensions = {
'timescaledb': (9.6, 18, True, True),
'pg_cron': (9.5, 18, True, False),
'pg_stat_kcache': (9.4, 18, True, False),
'pg_partman': (9.4, 18, False, True)
}
if os.environ.get('ENABLE_PG_MON') == 'true':
extensions['pg_mon'] = (11, 18, True, False)
def adjust_extensions(old, version, extwlist=False):
ret = []
for name in old.split(','):
name = name.strip()
value = extensions.get(name)
if name not in ret and value is None or value[0] <= version <= value[1] and (not extwlist or value[3]):
ret.append(name)
return ','.join(ret)
def append_extensions(old, version, extwlist=False):
extwlist = 3 if extwlist else 2
ret = []
def maybe_append(name):
value = extensions.get(name)
if name not in ret and (value is None or value[0] <= version <= value[1] and value[extwlist]):
ret.append(name)
for name in old.split(','):
maybe_append(name.strip())
for name in extensions.keys():
maybe_append(name)
return ','.join(ret)
def get_binary_version(bin_dir):
postgres = os.path.join(bin_dir or '', 'postgres')
version = subprocess.check_output([postgres, '--version']).decode()
version = re.match(r'^[^\s]+ [^\s]+ (\d+)(\.(\d+))?', version)
return '.'.join([version.group(1), version.group(3)]) if int(version.group(1)) < 10 else version.group(1)
def get_bin_dir(version):
return '{0}/{1}/bin'.format(LIB_DIR, version)
def is_valid_pg_version(version):
bin_dir = get_bin_dir(version)
postgres = os.path.join(bin_dir, 'postgres')
# check that there is postgres binary inside
return os.path.isfile(postgres) and os.access(postgres, os.X_OK)
def write_file(config, filename, overwrite):
if not overwrite and os.path.exists(filename):
logger.warning('File %s already exists, not overwriting. (Use option --force if necessary)', filename)
else:
with open(filename, 'w') as f:
logger.info('Writing to file %s', filename)
f.write(config)
def get_patroni_config():
with open(PATRONI_CONFIG_FILE) as f:
return yaml.safe_load(f)
def write_patroni_config(config, force):
write_file(yaml.dump(config, default_flow_style=False, width=120), PATRONI_CONFIG_FILE, force)