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