-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgeorchestraconfig.py
More file actions
145 lines (136 loc) · 6.21 KB
/
Copy pathgeorchestraconfig.py
File metadata and controls
145 lines (136 loc) · 6.21 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
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
#!/bin/env python3
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 et
from configparser import ConfigParser
from itertools import chain
from os import getenv, getcwd, path
import json
import re
import yaml
class GeorchestraConfig:
def __init__(self):
self.sections = dict()
self.datadirpath = getenv("georchestradatadir", "/etc/georchestra")
parser = ConfigParser()
with open(f"{self.datadirpath}/default.properties") as lines:
lines = chain(("[section]",), lines) # This line does the trick.
parser.read_file(lines)
self.sections["default"] = parser["section"]
self.sections["default"]["datadirpath"] = self.datadirpath
with open(f"{self.datadirpath}/mapstore/geostore.properties") as lines:
lines = chain(("[section]",), lines) # This line does the trick.
parser.read_file(lines)
self.sections["mapstoregeostore"] = parser["section"]
self.read_app_routes()
self.sections["urls"] = dict()
with open(f"{self.datadirpath}/mapstore/configs/localConfig.json") as file:
s = file.read()
localconfig = json.loads(s)
# used to find geonetwork entry in sec-proxy targets
try:
localentry = localconfig["initialState"]["defaultState"]["catalog"][
"default"
]["services"]["local"]
self.sections["urls"]["localgn"] = localentry["url"].split("/")[1]
except:
# safe default value
self.sections["urls"]["localgn"] = "geonetwork"
try:
localentry = localconfig["initialState"]["defaultState"]["catalog"][
"default"
]["services"]["localgs"]
self.sections["urls"]["localgs"] = localentry["url"].split("/")[1]
except:
# safe default value
self.sections["urls"]["localgs"] = "geoserver"
with open(f"{self.datadirpath}/geonetwork/geonetwork.properties") as lines:
lines = chain(("[section]",), lines) # This line does the trick.
parser.read_file(lines)
self.sections["geonetwork"] = parser["section"]
# read current commit from .git/HEAD which might lead to the branch tip
prefix = getcwd() + "/.git/"
self.sections["gaia"] = {"commit": None}
try:
with open(prefix + "HEAD", "r") as head:
branchref = head.read()
# we're on a tag that's a git sha
if re.match("^[0-9a-f]{32,}$", branchref):
self.sections["gaia"] = {"commit": branchref[0:8]}
# else we're on a branch
elif re.match("^ref: refs/heads/.*$", branchref):
with open(prefix + branchref.split(" ")[1].strip(), "r") as branch:
self.sections["gaia"] = {"commit": branch.read().strip()[0:8]}
except OSError:
# failed to read .git/HEAD or .git/refs/heads/* ?
pass
def read_app_routes(self) -> None:
parser = ConfigParser()
file_app_routes = (
f"{self.datadirpath}/security-proxy/targets-mapping.properties"
)
if path.exists(file_app_routes):
with open(file_app_routes) as lines:
lines = chain(("[section]",), lines) # This line does the trick.
parser.read_file(lines)
self.sections["app_routes"] = parser["section"]
self.sections["app_routes"]["entrance"] = "security-proxy"
else:
file_app_routes = f"{self.datadirpath}/gateway/routes.yaml"
with open(file_app_routes) as lines:
self.sections["app_routes"] = dict()
lines2 = yaml.safe_load(lines)
# only get the targets lines https://github.com/georchestra/datadir/blob/docker-master/gateway/routes.yaml#L76
for service_target in lines2["georchestra.gateway.services"]:
self.sections["app_routes"][service_target.split(".")[0]] = lines2[
"georchestra.gateway.services"
][service_target]
self.sections["app_routes"]["entrance"] = "gateway"
def tostr(self):
str = ""
for key in self.sections:
str += key + ":\r\n<br>"
for key2 in self.sections[key]:
str += " \t " + key2 + " : "
if self.sections[key][key2] == self.get(key2, section=key):
str += " \t " + self.sections[key][key2] + "\r\n<br> "
else:
str += (
" \t "
+ self.sections[key][key2]
+ " = "
+ self.get(key2, section=key)
+ "\r\n<br> "
)
return str
def get(self, key, section="default"):
if section not in self.sections:
return None
value = self.sections[section].get(key, None)
if value:
# this is to catch ${ENV_VAR}
search_env = re.match(r"^\${(.*)}$", value)
# this is for url using env var http://${ENV_VAR}/geonetwork/..etc?params
search_env2 = re.match(r"(.*)\${(.*)}(.*)", value)
# catching url in this form http://${CONSOLE_HOST:console}:8080/console/
search_env3 = re.match(r"(.*)\${(.*):(.*)}(.*)", value)
if search_env:
if getenv(search_env.group(1)):
value = getenv(search_env.group(1))
elif search_env3:
env_value = getenv(search_env3.group(2))
if env_value:
value = search_env3.group(1) + env_value + search_env3.group(4)
else:
value = (
search_env3.group(1)
+ search_env3.group(3)
+ search_env3.group(4)
)
elif search_env2:
if getenv(search_env2.group(2)):
value = (
search_env2.group(1)
+ getenv(search_env2.group(2))
+ search_env2.group(3)
)
return value