|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: UTF-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
# Isomer - The distributed application framework |
|
5
|
|
|
# ============================================== |
|
6
|
|
|
# Copyright (C) 2011-2020 Heiko 'riot' Weinen <[email protected]> and others. |
|
7
|
|
|
# |
|
8
|
|
|
# This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
# it under the terms of the GNU Affero General Public License as published by |
|
10
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
# (at your option) any later version. |
|
12
|
|
|
# |
|
13
|
|
|
# This program is distributed in the hope that it will be useful, |
|
14
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
# GNU Affero General Public License for more details. |
|
17
|
|
|
# |
|
18
|
|
|
# You should have received a copy of the GNU Affero General Public License |
|
19
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
|
|
21
|
|
|
""" |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
|
|
import os.path |
|
25
|
|
|
from typing import Union |
|
26
|
|
|
|
|
27
|
|
|
from isomer.tool import log, warn, debug |
|
28
|
|
|
|
|
29
|
|
|
ETC_BASE_PATH = "/etc/isomer" |
|
30
|
|
|
|
|
31
|
|
|
ETC_INSTANCE_PATH = os.path.join(ETC_BASE_PATH, "instances") |
|
32
|
|
|
ETC_REMOTE_PATH = os.path.join(ETC_BASE_PATH, "remotes") |
|
33
|
|
|
ETC_REMOTE_KEYS_PATH = os.path.join(ETC_BASE_PATH, "keys") |
|
34
|
|
|
|
|
35
|
|
|
INSTANCE: str = "" |
|
36
|
|
|
ENVIRONMENT: Union[str, None] = None |
|
37
|
|
|
PREFIX: str = "" |
|
38
|
|
|
|
|
39
|
|
|
locations = { |
|
40
|
|
|
"cache": "/var/cache/isomer/%s", |
|
41
|
|
|
"local": "/var/local/isomer/%s", |
|
42
|
|
|
"lib": "/var/lib/isomer/%s", |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def set_etc_path(path): |
|
47
|
|
|
"""Override the base path - dangerous! Only use for testing.""" |
|
48
|
|
|
global ETC_BASE_PATH |
|
49
|
|
|
global ETC_INSTANCE_PATH |
|
50
|
|
|
global ETC_REMOTE_PATH |
|
51
|
|
|
global ETC_REMOTE_KEYS_PATH |
|
52
|
|
|
|
|
53
|
|
|
ETC_BASE_PATH = path |
|
54
|
|
|
|
|
55
|
|
|
ETC_INSTANCE_PATH = os.path.join(ETC_BASE_PATH, "instances") |
|
56
|
|
|
ETC_REMOTE_PATH = os.path.join(ETC_BASE_PATH, "remotes") |
|
57
|
|
|
ETC_REMOTE_KEYS_PATH = os.path.join(ETC_BASE_PATH, "keys") |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def get_etc_path(): |
|
61
|
|
|
"""Get currently set configuration base path""" |
|
62
|
|
|
return ETC_BASE_PATH |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def get_etc_instance_path(): |
|
66
|
|
|
"""Get currently set instance configurations base path, e.g. |
|
67
|
|
|
`/etc/isomer/instances/`""" |
|
68
|
|
|
return ETC_INSTANCE_PATH |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def get_etc_remote_path(): |
|
72
|
|
|
"""Get currently set remote configurations base path""" |
|
73
|
|
|
return ETC_REMOTE_PATH |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
def get_etc_remote_keys_path(): |
|
77
|
|
|
"""Get currently set remote keys base path""" |
|
78
|
|
|
return ETC_REMOTE_KEYS_PATH |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
def get_log_path(): |
|
82
|
|
|
"""Get currently set logging base path""" |
|
83
|
|
|
if PREFIX not in (None, ""): |
|
84
|
|
|
path = os.path.join(PREFIX, "var", "log", "isomer") |
|
85
|
|
|
else: |
|
86
|
|
|
path = "/var/log/isomer" |
|
87
|
|
|
return path |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def set_prefix_path(prefix): |
|
91
|
|
|
"""Set a new base prefix (Caution!)""" |
|
92
|
|
|
global PREFIX |
|
93
|
|
|
|
|
94
|
|
|
PREFIX = prefix |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
def get_prefix_path(): |
|
98
|
|
|
"""Get the base prefix""" |
|
99
|
|
|
|
|
100
|
|
|
return PREFIX |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
def set_instance(instance, environment, prefix=None): |
|
104
|
|
|
"""Sets the global instance and environment""" |
|
105
|
|
|
|
|
106
|
|
|
global INSTANCE |
|
107
|
|
|
global ENVIRONMENT |
|
108
|
|
|
global PREFIX |
|
109
|
|
|
|
|
110
|
|
|
INSTANCE = instance |
|
111
|
|
|
ENVIRONMENT = environment |
|
112
|
|
|
if prefix is not None: |
|
113
|
|
|
PREFIX = prefix |
|
114
|
|
|
log("Warning! Prefix is set:", PREFIX, lvl=warn) |
|
115
|
|
|
|
|
116
|
|
|
log("Setting Instance: %s and Environment: %s" % (INSTANCE, ENVIRONMENT), lvl=debug) |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
def get_path(location: str, subfolder: str, ensure: bool = False, instance: str = "", |
|
120
|
|
|
environment: str = ""): |
|
121
|
|
|
"""Return a normalized path for the running instance and environment |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
:param location: Either cache, local or lib - all reside in /var |
|
125
|
|
|
:param subfolder: Subfolder inside location |
|
126
|
|
|
:param ensure: Create the folder, if it doesn't exist and this parameter is True |
|
127
|
|
|
:param instance: Temporarily override to get at another instance's folder |
|
128
|
|
|
:param environment: Temporarily override to pick a specific environment |
|
129
|
|
|
""" |
|
130
|
|
|
|
|
131
|
|
|
if instance != "": |
|
132
|
|
|
instance_name = instance |
|
133
|
|
|
else: |
|
134
|
|
|
instance_name = INSTANCE |
|
135
|
|
|
|
|
136
|
|
|
if environment != "": |
|
137
|
|
|
environment_name = environment |
|
138
|
|
|
else: |
|
139
|
|
|
environment_name = ENVIRONMENT |
|
140
|
|
|
|
|
141
|
|
|
if PREFIX not in (None, ""): |
|
142
|
|
|
path = os.path.join(PREFIX, locations[location].lstrip("/") % instance_name) |
|
143
|
|
|
else: |
|
144
|
|
|
path = locations[location] % instance_name |
|
145
|
|
|
|
|
146
|
|
|
if environment_name is not None: |
|
147
|
|
|
path = os.path.join(path, str(environment_name)) |
|
148
|
|
|
|
|
149
|
|
|
path = os.path.join(path, subfolder) |
|
150
|
|
|
path = path.rstrip("/") |
|
151
|
|
|
|
|
152
|
|
|
if ensure and not os.path.exists(path): |
|
153
|
|
|
os.makedirs(path) |
|
154
|
|
|
|
|
155
|
|
|
return path |
|
156
|
|
|
|