-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
204 lines (183 loc) · 6.12 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""See the docstring to main()."""
from pathlib import Path
import click
from monitoring.processor import run_app as run_monitoring
from consumer.consumer import run_app as run_consumer
from consumer.migrations.init import run as run_migration
CURRENT_DIR = Path(__file__).parent
@click.group()
def main():
"""
Usage: ./main.py COMMAND [ARG...]
Commands:
monitoring Start a monitoring service\n
--source-file filepath to the source file \n
--kafka_servers kafka bootstrap_servers \n
--kafka_topic kafka topic \n
--kafka_ssl_cafile CA certificate \n
--kafka_ssl_certfile access certificate \n
--kafka_ssl_keyfile access key \n
--debug run application in the debug mode \n
consumer Service for storing monitoring data \n
--kafka_servers kafka bootstrap_servers \n
--kafka_topic kafka topic \n
--kafka_ssl_cafile CA certificate \n
--kafka_ssl_certfile access certificate \n
--kafka_ssl_keyfile access key \n
--postgres_host PostgreSQL hostname \n
--postgres_port PostgreSQL port \n
--postgres_db PostgreSQL database \n
--postgres_user PostgreSQL user \n
--postgres_password PostgreSQL password \n
--postgres_ssl use PostgreSQL ssl connection \n
--debug run application in the debug mode \n
init-migration runs an init migration \n
--postgres_host PostgreSQL hostname \n
--postgres_port PostgreSQL port \n
--postgres_db PostgreSQL database \n
--postgres_user PostgreSQL user \n
--postgres_password PostgreSQL password \n
--postgres_ssl use PostgreSQL ssl connection \n
Example:\n
>>> ./main.py monitoring --debug \n
>>> ./main.py consumer --debug \n
>>> ./main.py init-migration\n
"""
@click.command()
@click.option(
"--source-file",
help="Read websites from this file",
default=str(CURRENT_DIR / "monitoring-sites.json"),
type=click.Path(exists=True, file_okay=True, dir_okay=False),
show_default=True,
)
@click.option("--kafka_servers", default="kafka:9093")
@click.option("--kafka_topic", default="monitoring")
@click.option(
"--kafka_ssl_cafile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--kafka_ssl_certfile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--kafka_ssl_keyfile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option("--debug", default=False, show_default=True, is_flag=True)
def monitoring(
source_file: str,
kafka_servers: str,
kafka_topic: str,
kafka_ssl_cafile: str,
kafka_ssl_certfile: str,
kafka_ssl_keyfile: str,
debug: bool,
) -> None:
"""
Service checks sites and sends result to Kafka
The input file is a JSON-format file that you may produce and modify with
any convenient text editor. The file should have the following format:
{
"url": <url>,
"validator": <regexp>
}
"""
click.echo("Starting monitoring service ...")
run_monitoring(
source_file,
kafka_servers=kafka_servers,
kafka_topic=kafka_topic,
kafka_ssl_cafile=kafka_ssl_cafile,
kafka_ssl_certfile=kafka_ssl_certfile,
kafka_ssl_keyfile=kafka_ssl_keyfile,
debug=debug,
)
@click.command()
@click.option("--kafka_servers", default="kafka:9093")
@click.option("--kafka_topic", default="monitoring")
@click.option(
"--kafka_ssl_cafile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--kafka_ssl_certfile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option(
"--kafka_ssl_keyfile",
type=click.Path(exists=False, file_okay=True, dir_okay=False),
)
@click.option("--postgres_host", default="postgres")
@click.option("--postgres_port", default=5432)
@click.option("--postgres_db", default="monitoring")
@click.option("--postgres_user", default="demo")
@click.option("--postgres_password", default="demopassword")
@click.option("--postgres_ssl", default=False, show_default=True, is_flag=True)
@click.option("--debug", default=False, show_default=True, is_flag=True)
def consumer(
kafka_servers: str,
kafka_topic: str,
kafka_ssl_cafile: str,
kafka_ssl_certfile: str,
kafka_ssl_keyfile: str,
postgres_host: str,
postgres_port: int,
postgres_db: str,
postgres_user: str,
postgres_password: str,
postgres_ssl: bool,
debug: bool,
) -> None:
"""
Service reads data from Kafka and writes to the database
"""
click.echo("Starting consumer service ...")
run_consumer(
kafka_servers=kafka_servers,
kafka_topic=kafka_topic,
kafka_ssl_cafile=kafka_ssl_cafile,
kafka_ssl_certfile=kafka_ssl_certfile,
kafka_ssl_keyfile=kafka_ssl_keyfile,
postgres_host=postgres_host,
postgres_port=postgres_port,
postgres_db=postgres_db,
postgres_user=postgres_user,
postgres_password=postgres_password,
postgres_ssl=postgres_ssl,
debug=debug,
)
@click.command()
@click.option("--postgres_host", default="postgres")
@click.option("--postgres_port", default=5432)
@click.option("--postgres_db", default="monitoring")
@click.option("--postgres_user", default="demo")
@click.option("--postgres_password", default="demopassword")
@click.option("--postgres_ssl", default=False, show_default=True, is_flag=True)
def init_migration(
postgres_host: str,
postgres_port: int,
postgres_db: str,
postgres_user: str,
postgres_password: str,
postgres_ssl: bool,
) -> None:
"""
runs init migration
"""
click.echo("runs a migration ...")
run_migration(
postgres_host,
postgres_port,
postgres_db,
postgres_user,
postgres_password,
postgres_ssl,
)
main.add_command(monitoring)
main.add_command(consumer)
main.add_command(init_migration)
if __name__ == "__main__":
main()