-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsender_cli.py
More file actions
368 lines (333 loc) · 11.6 KB
/
Copy pathsender_cli.py
File metadata and controls
368 lines (333 loc) · 11.6 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""CLI for use Devo Sender from shell command line."""
import sys
from pathlib import Path
try:
import click
except ImportError as import_error:
print(
str(import_error),
"- Use 'pip install click' or install this package with [click] option",
)
sys.exit(1)
import os
from devo.__version__ import __version__
from devo.common import Configuration
from devo.sender.data import DevoSenderException, Sender, open_file
from devo.sender.lookup import Lookup
# Groups
# ------------------------------------------------------------------------------
@click.group(invoke_without_command=True)
@click.option("--version", "-v", is_flag=True, default=False)
def cli(version):
"""Devo Sender Command-line Interface"""
if version:
pkg_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
)
)
click.echo(
"devo-sdk {!s} from {!s} (python {!s})".format(__version__, pkg_dir, sys.version[:3])
)
# Commands
# ------------------------------------------------------------------------------
@cli.command()
@click.option(
"--config",
"-c",
type=click.Path(exists=True, readable=True),
help="Optional JSON/Yaml File with configuration info.",
)
@click.option("--address", "-a", help="Devo relay address")
@click.option("--port", "-p", help="Devo relay address port")
@click.option(
"--key",
help="Devo user key cert file.",
type=click.Path(exists=True, readable=True),
)
@click.option("--cert", help="Devo user cert file.", type=click.Path(exists=True, readable=True))
@click.option("--chain", help="Devo chain.crt file.", type=click.Path(exists=True, readable=True))
@click.option("--sec_level", help="Sec level for opensslsocket. Default: None", type=int)
@click.option(
"--verify_mode",
help="Verify mode for SSL Socket. "
"Default: SSL default."
'You need use int "0" (CERT_NONE), '
'"1" (CERT_OPTIONAL) or '
'"2" (CERT_REQUIRED)',
type=int,
)
@click.option("--check_hostname", help="Verify cert hostname. Default: True", type=bool)
@click.option(
"--multiline/--no-multiline",
help="Flag for multiline (With break-line in msg). Default False",
default=False,
)
@click.option("--type", help="Connection type: SSL or TCP", default="SSL")
@click.option(
"--tag",
"-t",
help="Tag / Table to which the data will be sent in Devo.",
default="test.drop.ltsender",
)
@click.option(
"--line",
"-l",
help="For shipments of only one line, the text you want to send.",
default="David Hasselhoff",
)
@click.option(
"--file",
"-f",
help="The file that you want to send to Devo, which will be sent line by line.",
type=click.Path(exists=True, readable=True),
)
@click.option(
"--header",
"-h",
help="This option is used to indicate if the file has headers or not, not to send them.",
default=False,
type=bool,
)
@click.option("--raw", is_flag=True, help="Send raw events from a file when using --file")
@click.option("--debug/--no-debug", help="For testing purposes", default=False)
@click.option("--zip/--no-zip", help="For testing purposes", default=False, type=bool)
@click.option("--buffer", help="Buffer size for zipped data.", type=int)
@click.option(
"--compression_level",
help="Compression level for zipped data. Read readme for more info",
type=int,
)
@click.option(
"--no-verify-certificates",
help="Do not Verify certificates credentials before connection",
type=bool,
is_flag=True,
)
@click.option("--env", "-e", help="Use env vars for configuration", default=False, type=bool)
@click.option(
"--default",
"-d",
help="Use default file for configuration",
default=False,
type=bool,
)
def data(**kwargs):
"""Send data to devo"""
config = configure(kwargs)
sended = 0
try:
con = Sender(config=config)
if config.get("buffer", None) is not None:
con.buffer_size(size=config.get("buffer"))
if config.get("compression_level", None) is not None:
con.compression_level(cl=config.get("compression_level"))
if config.get("file", False):
if not Path(config["file"]).is_file():
print_error(str("File '%s' does not found" % Path(config["file"]).absolute()))
return
if config["multiline"]:
with open_file(config["file"], mode="r") as file:
content = file.read()
if not config["raw"]:
sended += con.send(
tag=config["tag"],
msg=content,
multiline=config["multiline"],
zip=config.get("zip", False),
)
else:
sended += con.send_raw(
content,
multiline=config["multiline"],
zip=config.get("zip", False),
)
else:
with open_file(config["file"], mode="r") as file:
if config["header"]:
next(file)
for line in file:
if config["raw"]:
sended += con.send_raw(line)
else:
sended += con.send(
tag=config["tag"],
msg=line,
zip=config.get("zip", False),
)
else:
sended += con.send(tag=config["tag"], msg=config["line"], zip=config.get("zip", False))
con.close()
if config.get("debug", False):
click.echo("Sended: %s" % str(sended))
except DevoSenderException as error:
print_error(str(error))
if config.get("debug", False):
raise DevoSenderException(str(error)) from error
exit()
except Exception as error:
print_error(str(error))
@cli.command()
@click.option(
"--config",
"-c",
type=click.Path(exists=True, readable=True),
help="Optional JSON/Yaml File with configuration info.",
)
@click.option("--env", "-e", help="Use env vars for configuration", default=False, type=bool)
@click.option(
"--default",
"-d",
help="Use default file for configuration",
default=False,
type=bool,
)
@click.option("--url", "--address", "-a", help="Devo relay address")
@click.option("--port", "-p", default=443, help="Devo relay address port")
@click.option("--key", help="Devo user key cert file.", type=click.Path(exists=True))
@click.option("--cert", help="Devo user cert file.", type=click.Path(exists=True))
@click.option("--chain", help="Devo chain.crt file.", type=click.Path(exists=True))
@click.option("--sec_level", help="Sec level for opensslsocket. Default: None", type=int)
@click.option(
"--verify_mode",
help="Verify mode for SSL Socket. "
"Default: SSL default."
'You need use int "0" (CERT_NONE), '
'"1" (CERT_OPTIONAL) or '
'"2" (CERT_REQUIRED)',
type=int,
)
@click.option("--check_hostname", help="Verify cert hostname. Default: True", type=bool)
@click.option("--type", help="Connection type: SSL or TCP", default="SSL")
@click.option("--name", "-n", help="Name for Lookup.")
@click.option("--action", "-ac", help="INC or FULL.", default="FULL")
@click.option(
"--file",
"-f",
help="The file that you want to send to Devo, which will be sent line by line.",
type=click.Path(exists=True, readable=True),
)
@click.option(
"--lkey",
"-lk",
help="Name of the column that contains the "
"Lookup key. It has to be the exact name "
"that appears in the header.",
)
@click.option(
"--dkey",
"-dk",
help="Name of the column that contains the "
'action/delete key with "add" or "delete".'
"It has to be the exact name "
"that appears in the header.",
)
@click.option(
"--detect-types/--no-detect-types",
"-dt/-ndt",
help="Detect types of fields. Default: False",
default=False,
)
@click.option("--delimiter", help="CSV Delimiter char.", default=",")
@click.option("--quotechar", "-qc", help="CSV Quote char.", default='"')
@click.option(
"--escapequotes",
"-eq",
is_flag=True,
help="Escape Quotes. Default: False",
default=False,
)
@click.option(
"--escapenewline",
"-enl",
is_flag=True,
help="Escape New Line char. Default: False",
default=False,
)
@click.option(
"--no-verify-certificates",
help="Do not Verify certificates credentials before connection",
type=bool,
is_flag=True,
)
@click.option("--debug/--no-debug", help="For testing purposes", default=False)
def lookup(**kwargs):
"""Send csv lookups to devo"""
config = configure_lookup(kwargs)
warning_st = 0
# Exit errors by https://tldp.org/LDP/abs/html/exitcodes.html
status_msg = {
64: "Some field contains double quotes in this file."
"If you do not use -eq or --escapequotes it might not work."
}
con = Sender(config=config)
lookup = Lookup(
name=config["name"],
historic_tag=None,
con=con,
escape_quotes=config["escapequotes"],
escape_newline=config["escapenewline"]
)
if lookup.check_quotes(config["file"]):
warning_st = 64
lookup.send_csv(
config["file"],
delimiter=config["delimiter"],
quotechar=config["quotechar"],
has_header=True,
# headers=line.rstrip().split(config['delimiter']),
key=config["lkey"],
action=config.get("action", "FULL"),
delete_field=config.get("dkey", None),
types=config.get(("lookup", "types"), config.get("types", None)),
detect_types=config.get("detect_types", False),
)
if warning_st != 0:
print_warning(status_msg[warning_st])
exit(warning_st)
def configure(args):
"""Configuration of Sender CLI"""
return init_conf(args)
def init_conf(args):
"""Generic configuration of CLI, from config file and cli arguments"""
config = Configuration()
if args.get("config"):
config.load_config(args.get("config"), "sender")
config["verify_config"] = not args.get("no_verify_certificates", False)
if args.get("env"):
config.set(
"address",
os.environ.get("DEVO_SENDER_ADDRESS", os.environ.get("DEVO_SENDER_URL", None)),
)
config.set("port", os.environ.get("DEVO_SENDER_PORT", None))
config.set("key", os.environ.get("DEVO_SENDER_KEY", None))
config.set("cert", os.environ.get("DEVO_SENDER_CERT", None))
config.set("chain", os.environ.get("DEVO_SENDER_CHAIN", None))
if args.get("default"):
config.load_default_config(section="sender")
config.mix(dict(args))
return config
def configure_lookup(args):
"""Configuration of Lookup for CLI"""
config = init_conf(args)
if args.get("config"):
config.load_config(args.get("config"), "lookup")
if "url" in config.keys():
config.set("address", config.get("url"))
return config
def print_error(error, show_help=False):
"""Class for print error in shell when exception"""
if show_help:
click.echo("")
click.echo(click.get_current_context().get_help())
click.echo(click.style(error, fg="red"), err=True)
def print_warning(warning, show_help=False):
"""Class for print warning in shell"""
if show_help:
click.echo("")
click.echo(click.get_current_context().get_help())
click.echo(click.style(warning, fg="yellow"), err=True)