forked from target/strelka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strelka_user_client.py
143 lines (127 loc) · 5.82 KB
/
strelka_user_client.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
#!/usr/bin/env python3
"""
strelka_user_client.py
Command line utility for sending files from a client to a Strelka cluster.
This is intended to be used for ad-hoc file requests and should not be
expected to perform long-lived or fully automated file transfers.
"""
import argparse
import json
import logging
import os
import socket
from client import lib
def send_request(client, path=None, location=None,
hostname=None, timeout=None):
"""Sends file requests to a Strelka cluster.
Args:
client: Previously configured instance of lib.Client().
path: Path to the file to send to the cluster.
location: Dictionary containing values related to the location
of the remote file to retrieve.
hostname: Hostname of the client sending the file to the cluster.
timeout: Amount of time (in seconds) to wait until a file transfer
times out.
"""
protobuf_request = None
if path is not None:
with open(path, "rb") as fin:
protobuf_request = lib.request_to_protobuf(file=fin.read(),
filename=path,
source=hostname)
elif location is not None:
protobuf_request = lib.request_to_protobuf(location=location,
source=hostname)
if protobuf_request is not None:
logging.debug(f"Sending {path or location} with timeout {timeout}")
result = client.send(protobuf_request, timeout=timeout)
if result:
logging.debug(f"Successfully sent {path or location}")
else:
logging.debug(f"Failed to send {path or location}")
else:
logging.error("No file or location provided!")
def main():
parser = argparse.ArgumentParser(prog="strelka_user_client.py",
description="sends ad-hoc file requests to"
" a Strelka cluster.",
usage="%(prog)s [options]")
parser.add_argument("-d", "--debug",
action="store_true",
dest="debug",
help="enable debug messages to the console")
parser.add_argument("-b", "--broker", required=True,
dest="broker", type=str,
help="network address and network port of the broker"
" (e.g. 127.0.0.1:5558)")
parser.add_argument("-p", "--path",
dest="path", type=str,
help="path to the file or directory of files to send"
" to the broker")
parser.add_argument("-l", "--location",
dest="location", type=str,
help="JSON representation of a location for the"
" cluster to retrieve files from")
parser.add_argument("-t", "--timeout",
dest="timeout", type=int,
help="amount of time (in seconds) to wait until a"
" file transfer times out")
parser.add_argument("-bpk", "--broker-public-key",
action="store",
dest="broker_public_key",
help="location of the broker Curve public key"
" certificate (this option enables curve"
" encryption and must be used if the broker"
" has curve enabled)")
parser.add_argument("-csk", "--client-secret-key",
action="store",
dest="client_secret_key",
help="location of the client Curve secret key"
" certificate (this option enables curve"
" encryption and must be used if the broker"
" has curve enabled)")
parser.add_argument("-ug", "--use-green",
action="store_true",
dest="use_green",
help="determines if PyZMQ green should be used, which"
" can increase performance at the risk of"
" message loss")
args = parser.parse_args()
if args.debug:
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
else:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S")
path = args.path or ""
location = {}
if args.location:
location = json.loads(args.location)
timeout = args.timeout or 60
broker_public_key = args.broker_public_key or None
client_secret_key = args.client_secret_key or None
use_green = args.use_green
hostname = socket.gethostname()
client = lib.Client(f"tcp://{args.broker}", use_green=use_green,
broker_public_key=broker_public_key,
client_secret_key=client_secret_key)
if path:
if os.path.isdir(path):
with os.scandir(path) as sd:
for entry in sd:
if not entry.name.startswith(".") and entry.is_file():
file_path = os.path.join(path, entry.name)
send_request(client, path=file_path,
hostname=hostname, timeout=timeout)
else:
send_request(client, path=path,
hostname=hostname, timeout=timeout)
elif location:
send_request(client, location=location,
hostname=hostname, timeout=timeout)
if __name__ == "__main__":
main()