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
|
import configparser
import validators
import os
import sys
from var import LOG
"""
Validate the config of a Docker run (environment variables)
"""
def validate_docker_config():
errors = 0
# Validate BASE_URL
try:
if not os.environ["BASE_URL"]:
LOG.error("BASE_URL is not set")
errors += 1
elif not validators.url(os.environ["BASE_URL"]):
LOG.error("BASE_URL is not a valid URL")
errors += 1
except KeyError:
LOG.critical("BASE_URL does not exist!")
errors += 1
# Validate IP_TO_LOCATION
try:
if not os.environ["IP_TO_LOCATION"]:
LOG.error("IP_TO_LOCATION is not set")
errors += 1
elif os.environ["IP_TO_LOCATION"].upper() not in ["TRUE", "FALSE", "T", "F"]:
LOG.error("IP_TO_LOCATION is not set to TRUE or FALSE")
errors += 1
else:
iptolocation = (
True if os.environ["IP_TO_LOCATION"].upper() in ["TRUE", "T"] else False
)
# Validate API_KEY if IP_TO_LOCATION is set to TRUE
if iptolocation:
try:
if not os.environ["API_KEY"]:
LOG.error("API_KEY is not set")
errors += 1
except KeyError:
LOG.critical("API_KEY does not exist!")
errors += 1
except KeyError:
LOG.critical("IP_TO_LOCATION does not exist!")
errors += 1
if errors > 0:
LOG.critical(f"{errors} error(s) found in environment variables")
sys.exit()
"""
Validate the config of a bare metal run (config.ini file)
"""
def validate_bare_metal_config(file_contents):
config = configparser.ConfigParser()
config.read_string(file_contents)
errors = 0
# Validate BASE_URL
try:
if not config["CONFIG"]["BASE_URL"]:
LOG.error("BASE_URL is not set")
errors += 1
elif not validators.url(config["CONFIG"]["BASE_URL"]):
LOG.error("BASE_URL is not a valid URL")
errors += 1
except ValueError:
LOG.critical("BASE_URL does not exist!")
errors += 1
# Validate IP_TO_LOCATION
try:
if not config["CONFIG"]["IP_TO_LOCATION"]:
LOG.error("IP_TO_LOCATION is not set")
errors += 1
elif config["CONFIG"]["IP_TO_LOCATION"].upper() not in [
"TRUE",
"FALSE",
"T",
"F",
]:
LOG.error("IP_TO_LOCATION is not set to TRUE or FALSE")
errors += 1
else:
iptolocation = (
True
if config["CONFIG"]["IP_TO_LOCATION"].upper() in ["TRUE", "T"]
else False
)
# Validate API_KEY if IP_TO_LOCATION is set to TRUE
if iptolocation:
try:
if not config["CONFIG"]["API_KEY"]:
LOG.error("API_KEY is not set")
errors += 1
except ValueError:
LOG.critical("API_KEY does not exist!")
errors += 1
except ValueError:
LOG.critical("IP_TO_LOCATION does not exist!")
errors += 1
if errors > 0:
LOG.critical(f"{errors} error(s) found in `config.ini`")
sys.exit()
def validate_config():
# If the app is running in Docker
if "BASE_URL" in os.environ or "IP_TO_LOCATION" in os.environ:
return validate_docker_config()
# Otherwise, the app is running on bare metal
try:
with open("config.ini", "r") as f:
file_contents = f.read()
return validate_bare_metal_config(file_contents)
except FileNotFoundError:
config = configparser.ConfigParser()
config["CONFIG"] = {"BASE_URL": "", "IP_TO_LOCATION": "", "API_KEY": ""}
with open("config.ini", "w") as configfile:
config.write(configfile)
LOG.error(
"`config.ini` has been created. Fill out the necessary information then re-run."
)
sys.exit()
|