-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_shares.py
244 lines (187 loc) · 7.15 KB
/
check_shares.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 12 16:40:47 2018
@author: erick
"""
import subprocess
import shlex
def get_shares(IP):
command = "smbclient -L "+ IP + " -A /home/erick/smb_credentials.txt"
# print(command)
result = subprocess.check_output(shlex.split(command) )
result = result.split()
shares=[]
started = 0
for i in range(len(result)):
if (result[i]=="Disk"):
shares.append(result[i-1])
started = 1
if (result[i]=="IPC" and started == 1):
break
return shares
def mount_share(IP, share):
command = "sudo mount -t cifs //"+IP+"/"+share+" /home/erick/mnt -o credentials=/home/erick/smb_credentials.txt,vers=1.0,rw,dir_mode=0777,file_mode=0777,defaults,noperm"
# print(command)
proc = subprocess.Popen(shlex.split(command) ,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,err=proc.communicate()
proc.wait()
# if err:
# print(err)
def mount_imported(IP):
command = "sudo mount -t cifs //"+IP+"/imported"+" /home/erick/mnt_imported -o credentials=/home/erick/smb_credentials.txt,vers=1.0,rw,dir_mode=0777,file_mode=0777,defaults,noperm"
# print(command)
proc = subprocess.Popen(shlex.split(command) ,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,err=proc.communicate()
proc.wait()
def get_username():
command = "cat /home/erick/mnt/.username.txt"
# print(command)
proc = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
username = out.rstrip()
return username, err
def get_password():
command = "cat /home/erick/passfile_omero.txt"
# print(command)
proc=subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
passw = out.rstrip()
return passw
def get_list_files():
command = "find /home/erick/mnt/ -type f \( -mmin -43200 -a -mmin +1 \)"
# print(command)
proc=subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
files = out.split()
return files
def unmount():
command = "sudo umount /home/erick/mnt"
print(command)
proc = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
# if err:
# print(err)
proc.wait()
def unmount_imported():
command = "sudo umount /home/erick/mnt_imported"
print(command)
proc = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
# if err:
# print(err)
proc.wait()
def import_file(curr_file,username,passw, f):
proc = subprocess.Popen(
'nice -n 15 /home/erick/OMERO.server/bin/omero '+'import '+
curr_file+
# ' -f '+
' -T '+ "\"regex:^.*mnt/(?<Container1>.*?)\" "+
'-u '+ username+
' --sudo '+ 'root '+
'-s '+ 'camdu.warwick.ac.uk '+
'-w '+ passw , shell=True, stdout=subprocess.PIPE,stderr=f)
# print("after import")
out,err=proc.communicate()
return out,err
#
def create_user(username, password):
command = "/home/erick/OMERO.server/bin/omero login -s camdu.warwick.ac.uk -u root -w "+password
# print(command)
proc=subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out,err)
command = "/home/erick/OMERO.server/bin/omero ldap create "+username
# print(command)
proc=subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out,err)
def move_file(currfile, share):
command = "mkdir /home/erick/mnt_imported/"+share
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
file_wo_dir,final_dir = recreate_folder(currfile,"/home/erick/mnt","/home/erick/mnt_imported/"+share)
command = "mv " + currfile + " " + final_dir +"/"+ file_wo_dir
print(command)
proc = subprocess.Popen(shlex.split(command))
out, err = proc.communicate()
def parse_log():
log = open("log.log", "r")
lines = log.readlines()
lastsuccess = 0
currline = 0
allfiles = []
for line in lines:
if "IMPORT_DONE" in line:
this_import = lines[lastsuccess:currline]
lastsuccess = currline
files = parse_import(this_import)
allfiles.extend(files)
currline += 1
print(allfiles)
return allfiles
def parse_import(this_import):
files = []
for line in this_import:
if "FILE_UPLOAD_COMPLETE" in line:
thisfile = line.split(" ")[-1].rstrip()
files.append(thisfile)
return files
def recreate_folder(filename,base,output):
file_split = filename.split("/")
base_split = base.split("/")
i = len(base_split)
fold_string = ""
while i < len(file_split)-1:
folder = file_split[i]
fold_string = fold_string + "/" + folder
command = "mkdir "+ output + "/"+fold_string
proc = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
i += 1
return file_split[-1],output + "/"+fold_string
def from_dvs(IP):
shares = get_shares(IP)
# shares = ['ratamero']
print(shares)
mount_imported(IP)
for share in shares:
if share == "imported":
print("IMPORTED SHARE - SKIPPING")
continue
print("share="+share)
mount_share(IP, share)
####### importing code goes here #########
command = "/home/erick/OMERO.server/bin/omero sessions clear"
# print(command)
proc = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out, err = proc.communicate()
username, err = get_username()
if err:
print(err)
else:
print(username)
passw = get_password()
create_user(username, passw)
#
###### find all files older than an hour, newer than a month, save list to text file
files = get_list_files()
print(files)
# read text file and loop over files importing and moving
for curr_file in files:
f = open('log.log', 'w')
print("importing "+curr_file)
out,err=import_file(curr_file,username,passw, f)
f.close()
filestomove = parse_log()
for curr_file in filestomove:
move_file(curr_file, share)
# print("test:",passw, username)
#
#
unmount()
unmount_imported()
if __name__=="__main__":
IP="192.168.10.122"
from_dvs(IP)