1 | import json |
---|
2 | import os |
---|
3 | |
---|
4 | |
---|
5 | class LmdImageManager: |
---|
6 | |
---|
7 | |
---|
8 | def __init__(self): |
---|
9 | self.imagepath="/etc/ltsp/images/" |
---|
10 | self.tftppath="/var/lib/tftpboot/ltsp" |
---|
11 | |
---|
12 | pass |
---|
13 | #def __init__ |
---|
14 | |
---|
15 | def getImageList(self): |
---|
16 | ''' |
---|
17 | Reads the file list of templates from /etc/ltsp/images |
---|
18 | Returna a JSON List. |
---|
19 | ''' |
---|
20 | # 1. /opt/ltsp/name-chroot |
---|
21 | # 2. /opt/ltsp/images/name-chroot.img |
---|
22 | # 3. /var/lib/tftpboot/ltsp/name-chroot |
---|
23 | # if 1,2 and 3 exist -> show |
---|
24 | # if 1 but not exist 2 or/and 3 -> show with error |
---|
25 | # |
---|
26 | |
---|
27 | imagelist=[] |
---|
28 | |
---|
29 | for i in os.listdir(self.imagepath): |
---|
30 | if i.endswith('.json'): |
---|
31 | imagelist.append(str(i)) |
---|
32 | |
---|
33 | return json.dumps(imagelist) |
---|
34 | |
---|
35 | |
---|
36 | # END def GetListImages(self) |
---|
37 | |
---|
38 | def getImage(self, image): |
---|
39 | ''' |
---|
40 | Returns the metadata from certain image |
---|
41 | |
---|
42 | // WIP HERE |
---|
43 | |
---|
44 | -> mirar si accepta be el tercer parametre o cal fer el kwargs... |
---|
45 | agafa aquest taskid i comprova amb getTask si existeix i el seu estat |
---|
46 | i l'afig amb data["taskstatus"]=ret(....) |
---|
47 | |
---|
48 | Aixi al renderImage de l'ImageManager ja podem tindre disponible l'estat sense haver |
---|
49 | d'estar consultant-ho des de la gui |
---|
50 | ''' |
---|
51 | |
---|
52 | try: |
---|
53 | json_data=open(self.imagepath+image) |
---|
54 | data = json.load(json_data) |
---|
55 | json_data.close() |
---|
56 | |
---|
57 | if "taskid" in data: |
---|
58 | taskid=data["taskid"]; |
---|
59 | else: |
---|
60 | taskid="0"; |
---|
61 | |
---|
62 | # Searching task id for this image |
---|
63 | status="DONE"; |
---|
64 | ret=objects['TaskMan'].getTaskStatus(taskid); |
---|
65 | |
---|
66 | if ret["status"]==True: |
---|
67 | status=ret["taskStatus"] |
---|
68 | # if ret[status] is false the task has been done time ago |
---|
69 | data["task_status"]=status; |
---|
70 | |
---|
71 | return json.dumps(data) |
---|
72 | #return data; |
---|
73 | except Exception as e: |
---|
74 | print "[LmdImageManager]: getImage Exception "+str(e) |
---|
75 | return str(e); |
---|
76 | |
---|
77 | # END def getListTemplate(self, image) |
---|
78 | |
---|
79 | |
---|
80 | def setImage(self, image, data): |
---|
81 | ''' |
---|
82 | Saves metadata from *data to image |
---|
83 | data is unicoded string |
---|
84 | image is name |
---|
85 | ''' |
---|
86 | |
---|
87 | path_to_write = os.path.join(self.imagepath,image + ".json") |
---|
88 | f = open(path_to_write,'w') |
---|
89 | f.writelines(data) |
---|
90 | f.close() |
---|
91 | |
---|
92 | try: |
---|
93 | ''' |
---|
94 | Writing lts.conf for image |
---|
95 | ''' |
---|
96 | |
---|
97 | '''ldm_session default |
---|
98 | ltsp_fatclient undefined |
---|
99 | fat_ram_threshold default |
---|
100 | lmd_extra_params''' |
---|
101 | |
---|
102 | # Checking if we need to write file |
---|
103 | |
---|
104 | jsondata=json.loads(data) |
---|
105 | |
---|
106 | if ((jsondata['ldm_session']!="default") or (jsondata['ltsp_fatclient']!="undefined") or (jsondata['fat_ram_threshold']!="default") or (jsondata['lmd_extra_params']!="") or (jsondata['ldm_language']!="default")): |
---|
107 | tftpfilename=os.path.join(self.tftppath,image + "/lts.conf") |
---|
108 | |
---|
109 | f=open(tftpfilename,'w') |
---|
110 | f.write("[Default]\n") |
---|
111 | if (jsondata['ldm_session']!="default"): |
---|
112 | f.write("LDM_SESSION="+jsondata['ldm_session']+"\n") |
---|
113 | if (jsondata['ldm_language']!="default"): |
---|
114 | f.write("LDM_LANGUAGE="+jsondata['ldm_language']+"\n") |
---|
115 | if (jsondata['ltsp_fatclient']!="undefined"): |
---|
116 | f.write("LTSP_FATCLIENT="+jsondata['ltsp_fatclient']+"\n") |
---|
117 | if (jsondata['fat_ram_threshold']!="default"): |
---|
118 | f.write("FAT_RAM_THRESHOLD="+jsondata['fat_ram_threshold']+"\n") |
---|
119 | |
---|
120 | if (jsondata['use_local_apps']!="false"): |
---|
121 | f.write("LOCAL_APPS_EXTRAMOUNTS=/home\n") |
---|
122 | f.write("LOCAL_APPS=true\n") |
---|
123 | f.write("LOCAL_APPS_MENU=true\n") |
---|
124 | f.write("LOCAL_APPS_MENU_ITEMS="+jsondata['local_apps_text']+"\n") |
---|
125 | |
---|
126 | if (jsondata['lmd_extra_params']!=""): |
---|
127 | extra_params=jsondata['lmd_extra_params'].split("<br/>") |
---|
128 | for param in extra_params: |
---|
129 | # Checking syntax and writing |
---|
130 | if("=" not in param): continue |
---|
131 | else: |
---|
132 | f.write(param+"\n") |
---|
133 | f.close() |
---|
134 | else: |
---|
135 | # We need to check if there was an old config... |
---|
136 | tftpfilename=os.path.join(self.tftppath,image + "/lts.conf") |
---|
137 | try: |
---|
138 | os.remove(tftpfilename) |
---|
139 | except: |
---|
140 | pass |
---|
141 | |
---|
142 | return {"status":True, "msg":"Config Done"} |
---|
143 | |
---|
144 | except Exception as e: |
---|
145 | return {"status":False, "msg":str(e)} |
---|
146 | |
---|
147 | |
---|
148 | '''if(data.) |
---|
149 | path_to_write = os.path.join(self.imagepath,image + ".json") |
---|
150 | f = open(path_to_write,'w') |
---|
151 | f.writelines(data) |
---|
152 | f.close()''' |
---|
153 | |
---|
154 | |
---|
155 | ''' |
---|
156 | datafile=""; |
---|
157 | for i in data: |
---|
158 | print i |
---|
159 | print "*****" |
---|
160 | datafile=datafile+i+" "; |
---|
161 | |
---|
162 | jsondata=json.loads(datafile) |
---|
163 | print type(data) |
---|
164 | print "****************" |
---|
165 | print type(datafile) |
---|
166 | |
---|
167 | fd=open(self.imagepath+image, 'w') |
---|
168 | fd.write('{"id":"'+jsondata['id']+'",\n') |
---|
169 | fd.write('"name":"'+jsondata['name']+'",\n') |
---|
170 | fd.write('"template":"'+jsondata['template']+'",\n') |
---|
171 | fd.write('"desc":"'+(jsondata['desc']).encode('utf8')+'",\n') |
---|
172 | fd.write('"img":"'+jsondata['img']+'"}\n') |
---|
173 | fd.close() |
---|
174 | ''' |
---|
175 | # def setImage(self, image, data) |
---|
176 | |
---|
177 | |
---|
178 | def setStatusImage(self, img_id, status): |
---|
179 | |
---|
180 | json_data=open(self.imagepath+img_id+".json") |
---|
181 | data = json.load(json_data) |
---|
182 | json_data.close() |
---|
183 | |
---|
184 | # Set status |
---|
185 | data["status"]=status |
---|
186 | |
---|
187 | self.setImage(img_id, json.dumps(data)); |
---|
188 | |
---|
189 | |
---|
190 | def setNewTaskIdForImage(self, img_id, newid): |
---|
191 | |
---|
192 | json_data=open(self.imagepath+img_id+".json") |
---|
193 | data = json.load(json_data) |
---|
194 | json_data.close() |
---|
195 | |
---|
196 | # Set status |
---|
197 | data["taskid"]=newid |
---|
198 | |
---|
199 | self.setImage(img_id, json.dumps(data)); |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | def deleteImage(self, img_id): |
---|
204 | ''' |
---|
205 | N4d Method to delete an image identified by img_id |
---|
206 | ''' |
---|
207 | import shutil; |
---|
208 | |
---|
209 | try: |
---|
210 | chroot="/opt/ltsp/"+str(img_id) |
---|
211 | image="/opt/ltsp/images/"+str(img_id)+".img" |
---|
212 | json_file="/etc/ltsp/images/"+str(img_id)+".json" |
---|
213 | tftpboot="/var/lib/tftpboot/ltsp/"+str(img_id) |
---|
214 | nbd="/etc/nbd-server/conf.d/ltsp_"+str(img_id)+".conf" |
---|
215 | |
---|
216 | # Umount anything mounted under image |
---|
217 | test_chroot=self.umount_chroot(chroot); |
---|
218 | if test_chroot['status']==False: |
---|
219 | return test_chroot; |
---|
220 | |
---|
221 | # Remove chroot |
---|
222 | if (os.path.isdir(chroot)): |
---|
223 | shutil.rmtree(chroot); |
---|
224 | |
---|
225 | # Removing .img |
---|
226 | if (os.path.isfile(image)): |
---|
227 | os.remove(image); |
---|
228 | |
---|
229 | # Remove nbd |
---|
230 | if (os.path.isfile(nbd)): |
---|
231 | os.remove(nbd); |
---|
232 | |
---|
233 | # Remove /var/lib/tftpboot/... |
---|
234 | if (os.path.isdir(tftpboot)): |
---|
235 | shutil.rmtree(tftpboot); |
---|
236 | |
---|
237 | # Remove .json file |
---|
238 | if (os.path.isfile(json_file)): |
---|
239 | os.remove(json_file); |
---|
240 | |
---|
241 | return {"status":True, "msg":"Image Removed"} |
---|
242 | except Exception as e: |
---|
243 | return {"status":False, "msg":str(e)} |
---|
244 | |
---|
245 | |
---|
246 | #def setImage(self, image, data): |
---|
247 | |
---|
248 | def umount_chroot(self,chroot_dir): |
---|
249 | ''' |
---|
250 | Umount system directories with -lazy, |
---|
251 | ''' |
---|
252 | ret="" |
---|
253 | try: |
---|
254 | # Test if exists chroot |
---|
255 | if not os.path.isdir(chroot_dir): |
---|
256 | print "NO DIR CHROOT: "+chroot_dir |
---|
257 | return {'status': True, 'msg':'[LmdImageManager] Directory not exists'} |
---|
258 | else: |
---|
259 | |
---|
260 | # umount /net/mirror/llx1406 |
---|
261 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/net/mirror | wc -l"], shell=True))!='0\n': |
---|
262 | ret=subprocess.check_output(["sudo", "umount","-l",chroot_dir+"/net/mirror/llx1406"]) |
---|
263 | |
---|
264 | # umount /proc |
---|
265 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/proc | wc -l"], shell=True))!='0\n': |
---|
266 | ret=subprocess.check_output(["sudo", "umount","-l",chroot_dir+"/proc"]) |
---|
267 | |
---|
268 | # umount /sys |
---|
269 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/sys | wc -l"], shell=True))!='0\n': |
---|
270 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/sys"]) |
---|
271 | |
---|
272 | # umount /dev/pts |
---|
273 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/dev/pts | wc -l"], shell=True))!='0\n': |
---|
274 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/dev/pts"]) |
---|
275 | |
---|
276 | # Mount /dev |
---|
277 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/dev | wc -l"], shell=True))!='0\n': |
---|
278 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/dev"]) |
---|
279 | |
---|
280 | # Umount /etc |
---|
281 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/etc/hosts | wc -l"], shell=True))!='0\n': |
---|
282 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/etc/hosts"]) |
---|
283 | |
---|
284 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/etc/ld.so.conf.d | wc -l"], shell=True))!='0\n': |
---|
285 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/etc/ld.so.conf.d"]) |
---|
286 | |
---|
287 | if (subprocess.check_output(["mount | grep "+chroot_dir+"/etc/nsswitch.conf | wc -l"], shell=True))!='0\n': |
---|
288 | ret=subprocess.check_output(["sudo","umount","-l",chroot_dir+"/etc/nsswitch.conf"]) |
---|
289 | |
---|
290 | |
---|
291 | return {'status': True, 'msg':'[LmdImageManager] All is umounted'} |
---|
292 | except Exception as e: |
---|
293 | return {'status': False, 'msg':'[LmdImageManager] '+str(e)} |
---|
294 | #def umount_chroot(self,chroot_dir) |
---|