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
|
#!/usr/bin/env python3
from dialog import Dialog
from threading import Thread
import subprocess
import sys, os
import contextlib
import time
#http://blog.thelinuxkid.com/2013/06/get-python-subprocess-output-without.html
def read_stderr_realtime(proc, stream='stderr'):
newlines = ['\n', '\r\n', '\r']
stream = getattr(proc, stream)
with contextlib.closing(stream):
while True:
out = []
last = stream.read(1)
# Don't loop forever
if last == '' and proc.poll() is not None:
break
while last not in newlines:
# Don't loop forever
if last == '' and proc.poll() is not None:
break
out.append(last)
last = stream.read(1)
out = ''.join(out)
yield out
def check_environment(d):
d.set_background_title("Checking your environment")
try:
subprocess.check_call(["ping", "8.8.8.8", "-c1", "-W5"])
except subprocess.CalledProcessError:
d.set_background_title("Fatal: Not Connected To Network")
d.msgbox("Please check your network connection and restart")
sys.exit(1)
try:
subprocess.check_call(["ping", "192.168.4.200", "-c1", "-W5"])
except subprocess.CalledProcessError:
d.set_background_title("Fatal: Can not find depot")
d.msgbox("Please check your network connection and restart")
sys.exit(1)
subprocess.call(["umount","/mnt/images"])
try:
subprocess.check_call(["mount","depot:/home/public/images","/mnt/images"])
except subprocess.CalledProcessError:
d.set_background_title("Fatal: Can not mount images directory")
d.msgbox("Please contact your system administrator")
sys.exit(1)
def main_menu(d):
d.set_background_title("OSTC Image Loader")
code, tag = d.menu("Select Action Below",
choices=[("load","Load new disk image from depot"),
("archive","Archive disk image to depot"),
("exit","Quit")]
)
return tag
def select_file_for_read(d,directory):
while True:
code, selection = d.fselect(directory,10,70)
if code==d.OK:
if os.path.isfile(selection) and os.access(selection,os.R_OK):
return code, selection
else:
d.msgbox("Invalid Selection: Can't open image file")
else:
return code, selection
def load_image(d):
d.set_background_title("Select file to load.....")
code, selection = select_file_for_read(d,'/mnt/images/')
if code == d.OK :
disk_loaded=False
cmd = ['pv', '-n', selection ]
try:
output=open('/dev/sda','w')
except:
d.set_background_title("Fatal: Can not open target disk")
d.msgbox("Please check your system")
sys.exit(1)
try:
proc = subprocess.Popen(
cmd,
stdout=output,
stderr=subprocess.PIPE,
universal_newlines=True,
)
d.set_background_title("loading:" +selection)
d.gauge_start("Progress",10,70,0)
for line in read_stderr_realtime(proc):
if line.strip() == '':
continue
if "No such file" in line.strip():
break
d.gauge_update(int(line.strip()))
if int(line.strip()) == 100:
d.gauge_update(100, "Finished!")
disk_loaded=True
break
except IOError:
d.set_background_title("Fatal: Could Not Complete Task")
d.msgbox("Sorry")
sys.exit(1)
code = d.gauge_stop()
if disk_loaded:
d.set_background_title("Success: Disk has been reimaged!")
code = d.pause("Please remove boot media, rebooting in 5 seconds", 10,70, 5)
if code==d.OK:
subprocess.check_call(["reboot"])
else:
d.set_background_title("Reboot Cancelled")
d.msgbox("Have A Nice Day")
sys.exit(2)
else:
d.set_background_title("Cancelled")
d.msgbox("Have A Nice Day")
sys.exit(2)
def select_file_for_write(d,directory):
while True:
code, selection = d.fselect(directory,10,70)
if code==d.OK:
if ( not os.path.isdir(selection) ) and os.access(os.path.dirname(selection),os.W_OK):
return code, selection
else:
d.msgbox("Invalid Selection: Can't write image file: "+selection)
else:
return code, selection
def archive_image(d):
d.set_background_title("Select archive image.....")
code, selection = select_file_for_write(d,'/mnt/images/archive/')
if code == d.OK :
disk_archived=False
cmd=['dd','if=/dev/sda','bs=16M','count=256','status=none|pv','-n','-s','4g']
#cmd = ['pv', '-n', '/dev/sda' ]
try:
output=open(selection,'w')
except:
d.set_background_title("Fatal: Can not open archive file")
d.msgbox("Please check your system")
sys.exit(1)
try:
proc = subprocess.Popen(
cmd,
stdout=output,
stderr=subprocess.PIPE,
universal_newlines=True,
)
d.set_background_title("archiving to:" +selection)
d.gauge_start("Progress",10,70,0)
for line in read_stderr_realtime(proc):
if line.strip() == '':
continue
if "No such file" in line.strip():
#break
continue
d.gauge_update(int(line.strip()))
if int(line.strip()) == 100:
d.gauge_update(100, "Finished!")
disk_archived=True
break
except IOError:
d.set_background_title("Fatal: Could Not Complete Task")
d.msgbox("Sorry")
sys.exit(1)
code = d.gauge_stop()
if disk_archived:
d.set_background_title("Success: Disk has been reimaged!")
code = d.pause("Please remove boot media, rebooting in 5 seconds", 10,70, 5)
if code==d.OK:
subprocess.check_call(["reboot"])
else:
d.set_background_title("Reboot Cancelled")
d.msgbox("Have A Nice Day")
sys.exit(2)
else:
d.set_background_title("Cancelled")
d.msgbox("Have A Nice Day")
sys.exit(2)
d = Dialog(dialog="dialog")
check_environment(d)
while True:
choice=main_menu(d)
if choice=="load":
load_image(d)
if choice=="archive":
archive_image(d)
else:
d.set_background_title("OK....")
d.msgbox("Have A Nice Day")
sys.exit(2)
|