forked from TrellixVulnTeam/Python-Automation_KTIW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAT_Spark_Checkpoints.py
More file actions
234 lines (207 loc) · 8.35 KB
/
Copy pathAT_Spark_Checkpoints.py
File metadata and controls
234 lines (207 loc) · 8.35 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
#RETRIEVING CHECKPOINT SCRIPT
import time
import paramiko
import sys
import re
import datetime
import requests
import tkinter as tk
from tkinter import ttk
from pprint import pprint
from tkinter.messagebox import askyesno
class Alert(tk.Tk):
def __init__(self,channel, checkpoints):
super().__init__()
self.channel = channel
self.checkpoints = checkpoints
self.title('Clear Checkpoint Dialog')
self.geometry('300x150')
# Confirm button
confirm_button = ttk.Button(self, text='Clear Checkpoint', command=self.confirm)
confirm_button.pack(expand=True)
def confirm(self):
answer = askyesno(title='Confirmation',
message='Are you sure that you want to clear the checkpoints?')
if answer:
self.destroy()
remove_checkpoints(self.channel, self.checkpoints)
def get_command_results(channel,command,isCheckPointPathRequired,checkPointsReceived):
print(f"{'#' * 50} Command Executing... {'#' * 50}")
try:
interval = 0.1
maxseconds = 10
maxcount = maxseconds / interval
bufsize = 1024
input_idx = 0
timeout_flag = False
start = datetime.datetime.now()
start_secs = time.mktime(start.timetuple())
output = ''
channel.setblocking(0)
if not command.endswith("\n"):
command += "\n"
channel.send(command)
while True:
if channel.recv_ready():
data = channel.recv(bufsize).decode('ascii')
output += data
if channel.exit_status_ready():
break
# Timeout check
now = datetime.datetime.now()
now_secs = time.mktime(now.timetuple())
et_secs = now_secs - start_secs
if et_secs > maxseconds:
timeout_flag = True
break
rbuffer = output.rstrip(' ')
if len(rbuffer) > 0 and (rbuffer[-1] == '#' or rbuffer[-1] == '>'):
break
time.sleep(0.200)
if channel.recv_ready():
data = channel.recv(bufsize)
output += data.decode('ascii')
print('Command Result :',output)
commandOutputList = output.split('\n')
if isCheckPointPathRequired:
try:
path=getCheckPointPath(commandOutputList)
return path
except:
print ("Error while getting the checkpoints path")
if checkPointsReceived:
try:
check_points_list=get_spark_checkpoints_list(commandOutputList)
return check_points_list
except:
print ("Error while getting the checkpoints")
print(f"{'#' * 50} Command Executed Successfully {'#' * 50}")
except:
print('Error while executing the command.Please try again')
exit()
def getConfiguration():
while True:
try:
spark_job_name = input("Enter the spark job name :-")
if not spark_job_name:
raise ValueError('Spark Job name is not defined')
response = requests.get("http://connectivity.odh.lgi.io/marathon/v2/apps//spark/{0}".format(spark_job_name),
auth=('odhuser', 'password'))
data = response.json()
if data:
responseData = data['app']['cmd']
hdfs_path = responseData.split(' ')[-2]
return hdfs_path
break
except ValueError as e:
print("Sorry, I didn't understand that.", e)
continue
except:
print("Something went wrong while getting the response")
def getCheckpointPathCommand(channel):
try:
check_point_path = getConfiguration()
cmd = f'docker exec -it hdfs-datanode hdfs dfs -cat {check_point_path}'
check_point_cmd = get_command_results(channel,cmd,True,False)
except:
print("Something went wrong while getting the checkpoint path command")
return check_point_cmd
def getCheckpointsCommand(channel,checkPointPath):
cmd = f'docker exec -it hdfs-datanode hdfs dfs -ls {checkPointPath}'
result = get_command_results(channel,cmd,False,True)
return result
def getCheckPointPath(commandOutputList):
for checkpoint in commandOutputList:
if "checkpoint:" in checkpoint or "checkpointLocation:" in checkpoint :
checkpoint_path_retrieved = checkpoint[checkpoint.find("hdfs"):]
else:
pass
return checkpoint_path_retrieved
def remove_checkpoints(channel,checkpoints):
for checkpoint in checkpoints:
cmd = f'docker exec -it hdfs-datanode hdfs dfs -rm -r {checkpoint}'
get_command_results(channel,cmd,False,False)
def get_spark_checkpoints_list(commandOutputList):
checkpointlist = []
for checkpoints in commandOutputList:
if "spark-checkpoints" in checkpoints:
hdfsPath = checkpoints[checkpoints.find("hdfs"):-1]
checkpointlist.append(hdfsPath)
else:
pass
return checkpointlist
def ssh_conn():
print(f"{'#' * 50} Python Script For Clearing Spark CheckPoint {'#' * 50}")
while True:
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
hostname = input("Enter the hostname:-")
if not hostname:
raise ValueError('Hostname is not defined')
else:
pattern = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
ip = pattern.match(hostname)
if not ip:
print("Unacceptable IP address")
continue
sshpath = input("Enter the path to your ssh key:-")
if not sshpath:
raise ValueError('SSH Path is not defined')
user = input("Enter the username:-")
if not user:
raise ValueError('Username is not defined')
k = paramiko.RSAKey.from_private_key_file(sshpath)
print(f"{'#' * 50} Connecting to the Device {'#' * 50}")
client.connect(hostname,port =22, username=user, pkey=k)
channel = client.get_transport().open_session()
channel.get_pty()
print(f"{'#' * 50} Connecting Established with {'#' * 50}")
commands_to_exec = 'sudo -i'
channel.invoke_shell()
get_command_results(channel,commands_to_exec,False,False)
#checkpoint
checkPointPath = getCheckpointPathCommand(channel)
print(f"{'#' * 50} Retrieving the checkpoints... {'#' * 50}")
if checkPointPath:
checkpoints = getCheckpointsCommand(channel,checkPointPath)
else:
print("Something went wrong while retrieving checkpoints.Please try again...")
print(f"{'#' * 50} Deleting the checkpoints... {'#' * 50}")
try:
if len(checkpoints) == 0:
print("Checkpoints not found for this job")
else:
alert = Alert(channel, checkpoints)
alert.mainloop()
except:
print("Error while deleting the checkpoints")
client.close()
break
except IOError as e:
print("Invalid input", e)
continue
except ValueError as e:
print("Sorry, I didn't understand that.", e)
continue
except IndexError as e:
print("Something went wrong with the index", e)
continue
except paramiko.SSHException as sshException:
print("Unable to establish SSH connection: %s" % sshException)
continue
except paramiko.AuthenticationException:
print("Authentication Error!!")
continue
except:
print("Unexpected error:", sys.exc_info()[0])
raise
ssh_conn()
sys.exit()
#Example
#C:/Users/gaurav.sikka01/.ssh/opensshkey --please use your local private key
#172.31.101.116 --odhat12e
#hdfs:///processing-conf/spark_eosdtv_eosstb_eosstb_streamparser_nl-conf.yml-e1bbecf6a60aebc1ccce65d237cf05ee.yml
#hdfs:///processing-conf/spark_eosdtv_eosstb_eosstb_streamparser_gb-conf.yml-4b6b6403eae7cbef72b4361df4cebd22.yml
#container restart scenario --> need to validate