-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandles_lib.py
222 lines (172 loc) · 5.16 KB
/
handles_lib.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import urllib.request
import json
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
handle_server = os.getenv('HANDLE_SERVER')
handle_client_path = os.getenv('HANDLE_CLIENT_PATH')
handle_log_path = os.getenv('HANDLE_LOG_PATH')
handle_key = os.getenv('HANDLE_KEY')
handle_passphrase = os.getenv('HANDLE_PASSPHRASE')
handle_user = os.getenv('HANDLE_USER')
handle_id = os.getenv('HANDLE_ID')
handle_target = os.getenv('HANDLE_TARGET')
handle_commands_path = os.getenv('HANDLE_COMMANDS_PATH')
def construct_auth():
"""
Constructs auth command
:return: String
"""
auth_command = 'AUTHENTICATE PUBKEY:' + handle_user + handle_id + '\n'
auth_command += handle_key + '|' + handle_passphrase + '\n'
return auth_command
def get_handle(uuid):
"""
Gets handle by uuid
:param uuid:
:return:
"""
try:
request = handle_server + handle_id + '/' + uuid
response = urllib.request.urlopen(request).read()
return json.loads(response)
except urllib.error.HTTPError as e:
print('Handle server response error ' + e)
return e
except urllib.error.URLError as e:
print('Handle server is offline ' + e)
return e
def create_handle(uuid):
"""
Creates handle
@param uuid
@returns Boolean
"""
command = construct_auth()
command += '\n'
command += 'CREATE ' + handle_id + '/' + uuid + '\n'
command += '2 URL 86400 1110 UTF8 ' + handle_target + uuid + '\n'
command += '\n'
command_file = create_command_file('create', command)
log_file = create_log_file('create', uuid)
if command_file is False or log_file is False:
return False
command = handle_commands_path + command_file
log = handle_log_path + log_file
if log is False:
return False
result = execute_command(command, log)
if result is False:
return False
delete_command_file(command_file)
return True
def update_handle(uuid, new_handle_target):
"""
Updates handle
@param uuid
@param new_handle_target
@returns log
"""
command = construct_auth()
command += '\n'
command += 'MODIFY ' + handle_id + '/' + uuid + '\n'
command += '2 URL 86400 1110 UTF8 ' + new_handle_target + uuid + '\n'
command += '\n'
command_file = create_command_file('update', command)
log_file = create_log_file('update', uuid)
if command_file is False or log_file is False:
return False
command = handle_commands_path + command_file
log = handle_log_path + log_file
if log is False:
return False
result = execute_command(command, log)
if result is False:
return False
delete_command_file(command_file)
return True
def delete_handle(uuid):
"""
Deletes handle
@param uuid
@returns log
"""
command = construct_auth()
command += '\n'
command += 'DELETE ' + handle_id + '/' + uuid + '\n'
command += '\n'
command_file = create_command_file('delete', command)
log_file = create_log_file('delete', uuid)
if command_file is False or log_file is False:
return False
command = handle_commands_path + command_file
log = handle_log_path + log_file
if log is False:
return False
result = execute_command(command, log)
if result is False:
return False
delete_command_file(command_file)
return True
def execute_command(command, log):
"""
Executes handle client command
@param command
@param log
:return: Boolean
"""
try:
handle_client_exec = handle_client_path + 'bin/hdl-genericbatch '
handle_cmd_exec = 'sh ' + handle_client_exec + ' ' + command + ' ' + log + ' -verbose'
os.system(handle_cmd_exec)
return True
except Exception as e:
print(e)
return False
def create_command_file(action, command):
"""
Creates the command file used by the handle client
@param action
@param command
:return: Boolean
"""
try:
file = 'du_' + action + '_handle.txt'
handle_command = open(handle_commands_path + file, 'a+')
handle_command.write(command)
return file
except Exception as e:
print(e)
print('ERROR: Unable to create handle command file')
return False
def create_log_file(action, uuid):
"""
Creates the log file used by the handle client
@param action
@param uuid
:return: String
"""
try:
file = 'du_' + action + '_handle_' + uuid + '.log'
f = open(handle_log_path + file, 'a+')
return file
except Exception as e:
print(e)
print('ERROR: Unable to create handle log file')
return False
def delete_command_file(command_file):
"""
Deletes command file
@param command_file
:return: Boolean
"""
try:
if os.path.exists(handle_commands_path + command_file):
os.remove(handle_commands_path + command_file)
return True
except Exception as e:
print(e)
print('ERROR: Unable to delete handle command file')
return False