1 | import json |
---|
2 | import os |
---|
3 | import crypt |
---|
4 | |
---|
5 | class LmdBootManager: |
---|
6 | |
---|
7 | |
---|
8 | def __init__(self): |
---|
9 | self.imagepath="/etc/ltsp/bootopts/" |
---|
10 | |
---|
11 | |
---|
12 | pass |
---|
13 | #def __init__ |
---|
14 | |
---|
15 | def getNetinstall(self): |
---|
16 | ''' |
---|
17 | Reads file /etc/ltsp/bootopts/netinstall and returns true or false |
---|
18 | ''' |
---|
19 | # 1. /opt/ltsp/name-chroot |
---|
20 | # 2. /opt/ltsp/images/name-chroot.img |
---|
21 | # 3. /var/lib/tftpboot/ltsp/name-chroot |
---|
22 | # if 1,2 and 3 exist -> show |
---|
23 | # if 1 but not exist 2 or/and 3 -> show with error |
---|
24 | # |
---|
25 | |
---|
26 | json_data=open(self.imagepath+"netinstall.json") |
---|
27 | data = json.load(json_data) |
---|
28 | json_data.close() |
---|
29 | if(data["netinstall_boot"].lower()=="true"): |
---|
30 | netinstall=True; |
---|
31 | else: |
---|
32 | netinstall=False; |
---|
33 | |
---|
34 | if(data["netinstall_unattended"].lower()=="true"): |
---|
35 | unattended=True; |
---|
36 | else: |
---|
37 | unattended=False; |
---|
38 | |
---|
39 | return {"netinstall":netinstall, "unattended":unattended} |
---|
40 | |
---|
41 | return json.dumps(imagelist) |
---|
42 | |
---|
43 | |
---|
44 | # END def GetListImages(self) |
---|
45 | |
---|
46 | def setNetinstall(self, status, unattended): |
---|
47 | ''' |
---|
48 | sets option for netinstall int bootopt.json (status and unattended install) |
---|
49 | ''' |
---|
50 | try: |
---|
51 | mirror_var="/var/lib/n4d/variables-dir/LLIUREXMIRROR" |
---|
52 | if os.path.isfile(mirror_var): |
---|
53 | if (status.lower()=="true" or status.lower()=="false"): |
---|
54 | path_to_write = os.path.join(self.imagepath,"netinstall.json") |
---|
55 | f = open(path_to_write,'w') |
---|
56 | data='{"netinstall_boot":"'+str(status)+'", "netinstall_unattended":"'+str(unattended)+'"}' |
---|
57 | f.writelines(data) |
---|
58 | f.close() |
---|
59 | |
---|
60 | # Enable NETINSTALL on menu (the last option, but enabled) |
---|
61 | objects["LlxBootManager"].pushToBootList("netinstall") |
---|
62 | |
---|
63 | # Removing user and password from preseed |
---|
64 | self.setNetinstallUnattended(status, "", "", "") |
---|
65 | |
---|
66 | return {"status":"true", "msg":"all ok"} |
---|
67 | |
---|
68 | else: |
---|
69 | return {"status":"false", "msg":"option not valid"} |
---|
70 | else: |
---|
71 | return {"status":"false", "msg":"mirror is not available"} |
---|
72 | |
---|
73 | #return data; |
---|
74 | except Exception as e: |
---|
75 | return str(e); |
---|
76 | |
---|
77 | # END def getListTemplate(self, image) |
---|
78 | |
---|
79 | def setNetinstallUnattended(self, status, username, password, rootpassword): |
---|
80 | ''' |
---|
81 | Writing in presseed username and password |
---|
82 | ''' |
---|
83 | try: |
---|
84 | filedir="/var/www/preseed" |
---|
85 | filepath="/var/www/preseed/unattended.cfg" |
---|
86 | filepartman="/var/www/preseed/partman_sda.cfg" |
---|
87 | |
---|
88 | if not os.path.exists(filedir): |
---|
89 | os.makedirs(filedir) |
---|
90 | |
---|
91 | preseed=open(filepath,'w') |
---|
92 | preseed.write("# LMD Created user account\n") |
---|
93 | salt="sw9.tfRI" |
---|
94 | userpassencrypted=crypt.crypt(str(password),"$1$"+salt+"$") |
---|
95 | rootpassencrypted=crypt.crypt(str(rootpassword),"$1$"+salt+"$") |
---|
96 | |
---|
97 | if(status==True): |
---|
98 | # Saving file |
---|
99 | |
---|
100 | userfullline="d-i passwd/user-fullname string "+str(username)+"\n"; |
---|
101 | userline="d-i passwd/username string "+str(username)+"\n"; |
---|
102 | passline="d-i passwd/user-password-crypted password "+str(userpassencrypted)+"\n" |
---|
103 | |
---|
104 | if (len(rootpassword) > 0): |
---|
105 | rootpassline = "d-i passwd/root-password-crypted password "+str(rootpassencrypted) + "\n" |
---|
106 | else: |
---|
107 | rootpassline = "# d-i passwd/root-password-crypted password "+str(rootpassencrypted) + "\n" |
---|
108 | |
---|
109 | # Partition preseed |
---|
110 | try: |
---|
111 | partman = open(filepartman,'r') |
---|
112 | preseed.writelines(partman.readlines()) |
---|
113 | preseed.write("\n") |
---|
114 | partman.close() |
---|
115 | except Exception as e: |
---|
116 | return str(e) |
---|
117 | |
---|
118 | else: |
---|
119 | userfullline="#d-i passwd/user-fullname string \n" |
---|
120 | userline="#d-i passwd/username string \n" |
---|
121 | passline="# d-i passwd/user-password-crypted password \n" |
---|
122 | rootpassline = "# d-i passwd/root-password-crypted password \n" |
---|
123 | |
---|
124 | preseed.write("# Normal user name\n") |
---|
125 | preseed.write(userfullline) |
---|
126 | preseed.write(userline) |
---|
127 | preseed.write("# Normal user's password, either in clear text\n") |
---|
128 | preseed.write("#d-i passwd/user-password password insecure\n") |
---|
129 | preseed.write("# Normal user's password encrypted using an MD5 hash.\n") |
---|
130 | preseed.write(passline) |
---|
131 | preseed.write(rootpassline) |
---|
132 | |
---|
133 | # Allow weak passwords |
---|
134 | preseed.write("d-i user-setup/allow-password-weak boolean true\n") |
---|
135 | |
---|
136 | preseed.close() |
---|
137 | |
---|
138 | except Exception as e: |
---|
139 | return str(e) |
---|
140 | |
---|
141 | # END def getListTemplate(self, image) |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | ''' |
---|
146 | Methods to work with lts.conf file |
---|
147 | ''' |
---|
148 | |
---|
149 | ''' def getTemplate(self, template): |
---|
150 | '' ' |
---|
151 | Reads the file template from /etc/ltsp/templates |
---|
152 | Returna a JSON string with the config options |
---|
153 | '' ' |
---|
154 | try: |
---|
155 | config = StringIO.StringIO() |
---|
156 | config.write('[meta_inf]\n') |
---|
157 | config.write('name="'+template+'"\n') |
---|
158 | |
---|
159 | |
---|
160 | config.write('[default]\n') |
---|
161 | config.write(open(str(self.templatepath)+"/"+str(template)).read()) |
---|
162 | config.seek(0, os.SEEK_SET) |
---|
163 | cp = ConfigParser.ConfigParser() |
---|
164 | cp.readfp(config) |
---|
165 | aux = cp._sections |
---|
166 | for x in aux.keys(): |
---|
167 | for y in aux[x]: |
---|
168 | if aux[x][y][0] == '"' and aux[x][y][-1] == '"': |
---|
169 | aux[x][y] = aux[x][y][1:-1] |
---|
170 | |
---|
171 | return json.dumps(aux) |
---|
172 | #return json.dumps(cp.items('default')); |
---|
173 | |
---|
174 | |
---|
175 | except Exception as e: |
---|
176 | return "Exception: "+str(e) |
---|
177 | # END def getListTemplate(self, template) |
---|
178 | ''' |
---|
179 | |
---|
180 | def getLtsConf(self): |
---|
181 | # Reads $tftpboot/ltsp/i386/lts.conf and returns it in json format |
---|
182 | |
---|
183 | try: |
---|
184 | conf_file="/var/lib/tftpboot/ltsp/i386/lts.conf" |
---|
185 | cp = ConfigParser.ConfigParser() |
---|
186 | cp.optionxform=str |
---|
187 | cp.read(conf_file) |
---|
188 | aux = cp._sections |
---|
189 | print "**",aux,"$$$" |
---|
190 | for x in aux.keys(): |
---|
191 | for y in aux[x]: |
---|
192 | if aux[x][y][0] == '"' and aux[x][y][-1] == '"': |
---|
193 | aux[x][y] = aux[x][y][1:-1] |
---|
194 | |
---|
195 | print "@@",aux,"##" |
---|
196 | return json.dumps(aux) |
---|
197 | pass |
---|
198 | #return json.dumps(cp.items('default')); |
---|
199 | |
---|
200 | except Exception as e: |
---|
201 | return "Exception: "+str(e) |
---|
202 | # END def getListTemplate(self, template) |
---|
203 | |
---|
204 | pass |
---|
205 | |
---|
206 | def mergeLtsConf(self): |
---|
207 | |
---|
208 | # Creates lts.conf file merging lts.default.conf and lts.clients.conf |
---|
209 | conf_file="/var/lib/tftpboot/ltsp/i386/lts.conf" |
---|
210 | default_file="/var/lib/tftpboot/ltsp/i386/lts.default.conf" |
---|
211 | clients_file="/var/lib/tftpboot/ltsp/i386/lts.clients.conf" |
---|
212 | filenames = [default_file, clients_file] |
---|
213 | |
---|
214 | with open(conf_file, 'w') as outfile: |
---|
215 | for fname in filenames: |
---|
216 | with open(fname) as infile: |
---|
217 | for line in infile: |
---|
218 | outfile.write(line) |
---|
219 | return True; |
---|
220 | |
---|
221 | def setLtsClientsConf(self): |
---|
222 | # Writes json config in $tftpboot/ltsp/i386/lts.conf |
---|
223 | try: |
---|
224 | conf_file="/var/lib/tftpboot/ltsp/i386/lts.clients.conf" |
---|
225 | clientpath="/etc/ltsp/bootopts/clients/" |
---|
226 | cfgfile = open(conf_file,'w') |
---|
227 | |
---|
228 | # Autologin by mac... |
---|
229 | try: |
---|
230 | for i in os.listdir(clientpath): |
---|
231 | if '.json' in i: |
---|
232 | json_data=open(os.path.join(clientpath,i)) |
---|
233 | data = json.load(json_data) |
---|
234 | json_data.close() |
---|
235 | cfgfile.write("\n["+data["mac"]+"]\n"); |
---|
236 | if ("autologin" in data) and ("user" in data) and ("pass" in data): |
---|
237 | if (data["autologin"].lower()=="true"): |
---|
238 | cfgfile.write("LDM_AUTOLOGIN = True\n"); |
---|
239 | cfgfile.write("LDM_USERNAME = "+data["user"]+"\n"); |
---|
240 | cfgfile.write("LDM_PASSWORD = "+data["pass"]+"\n"); |
---|
241 | |
---|
242 | if ("forceThin" in data) and (data["forceThin"].lower()=="true"): |
---|
243 | cfgfile.write("LTSP_FATCLIENT = False\n"); |
---|
244 | |
---|
245 | if ("extraOptions" in data) and (data["extraOptions"]!=""): |
---|
246 | optList=data["extraOptions"].split("\n"); |
---|
247 | for opt in optList: |
---|
248 | cfgfile.write(opt+"\n"); |
---|
249 | |
---|
250 | except Exception as e: |
---|
251 | print "[LMDBootManager] Exception "+str(e) |
---|
252 | |
---|
253 | cfgfile.close() |
---|
254 | |
---|
255 | # Finally merge both conf files: |
---|
256 | self.mergeLtsConf(); |
---|
257 | except Exception as e: |
---|
258 | return "Exception: "+str(e) |
---|
259 | |
---|
260 | |
---|
261 | pass |
---|
262 | |
---|
263 | |
---|
264 | def setLtsConf(self, config): |
---|
265 | # Writes json config in $tftpboot/ltsp/i386/lts.conf |
---|
266 | try: |
---|
267 | conf_file="/var/lib/tftpboot/ltsp/i386/lts.default.conf" |
---|
268 | cfgfile = open(conf_file,'w') |
---|
269 | |
---|
270 | #cp = ConfigParser.ConfigParser() |
---|
271 | conf=json.loads(config) |
---|
272 | |
---|
273 | for (section) in conf: |
---|
274 | cfgfile.write("["+section+"]\n"); |
---|
275 | for i in conf[section]: |
---|
276 | if i.lower()=="__name__": |
---|
277 | pass |
---|
278 | else: |
---|
279 | if (conf[section][i]).find(' ')!=-1: # ound spaces, adding " |
---|
280 | cfgfile.write("{0}=\"{1}\"\n".format(i, (conf[section][i]))) |
---|
281 | else: |
---|
282 | cfgfile.write("{0}={1}\n".format(i, (conf[section][i]))) |
---|
283 | cfgfile.close() |
---|
284 | |
---|
285 | # Finally merge both conf files: |
---|
286 | self.mergeLtsConf(); |
---|
287 | |
---|
288 | except Exception as e: |
---|
289 | return "Exception: "+str(e) |
---|
290 | |
---|
291 | |
---|
292 | def getBootOpts(self): |
---|
293 | ''' |
---|
294 | Reads file /etc/ltsp/bootopts/netinstall and returns true or false |
---|
295 | ''' |
---|
296 | # 1. /opt/ltsp/name-chroot |
---|
297 | # 2. /opt/ltsp/images/name-chroot.img |
---|
298 | # 3. /var/lib/tftpboot/ltsp/name-chroot |
---|
299 | # if 1,2 and 3 exist -> show |
---|
300 | # if 1 but not exist 2 or/and 3 -> show with error |
---|
301 | # |
---|
302 | json_data=open(self.imagepath+"netinstall.json") |
---|
303 | data = json.load(json_data) |
---|
304 | json_data.close() |
---|
305 | if(data["netinstall_boot"].lower()=="true"): |
---|
306 | return True |
---|
307 | else: |
---|
308 | return False |
---|
309 | |
---|
310 | return json.dumps(imagelist) |
---|
311 | |
---|
312 | |
---|
313 | # END def GetListImages(self) |
---|
314 | |
---|
315 | def setBootOpts(self, status): |
---|
316 | ''' |
---|
317 | sets option for netinstall int bootopt.json |
---|
318 | ''' |
---|
319 | try: |
---|
320 | mirror_var="/var/lib/n4d/variables-dir/LLIUREXMIRROR" |
---|
321 | if os.path.isfile(mirror_var): |
---|
322 | if (status.lower()=="true" or status.lower()=="false"): |
---|
323 | path_to_write = os.path.join(self.imagepath,"netinstall.json") |
---|
324 | f = open(path_to_write,'w') |
---|
325 | data='{"netinstall_boot":"'+str(status)+'"}' |
---|
326 | f.writelines(data) |
---|
327 | f.close() |
---|
328 | return {"status":"true", "msg":"all ok"} |
---|
329 | |
---|
330 | else: |
---|
331 | return {"status":"false", "msg":"option not valid"} |
---|
332 | else: |
---|
333 | return {"status":"false", "msg":"mirror is not available"} |
---|
334 | |
---|
335 | #return data; |
---|
336 | except Exception as e: |
---|
337 | return str(e); |
---|
338 | |
---|
339 | def ReadNBDSwapSize(self): |
---|
340 | nbd_swap_file="/etc/ltsp/nbdswapd.conf" |
---|
341 | f1 = open(nbd_swap_file,'r') |
---|
342 | data=f1.readlines() |
---|
343 | size=data[0][5:]; |
---|
344 | f1.close() |
---|
345 | return size |
---|
346 | |
---|
347 | |
---|
348 | def SaveNBDSwapSize(self, swap_size): |
---|
349 | ''' |
---|
350 | Saves properly options to set swap size |
---|
351 | ''' |
---|
352 | try: |
---|
353 | # Saving nbdswapd.conf |
---|
354 | nbd_swap_file="/etc/ltsp/nbdswapd.conf" |
---|
355 | f1 = open(nbd_swap_file,'w') |
---|
356 | data='SIZE='+str(swap_size)+'\n'; |
---|
357 | f1.writelines(data) |
---|
358 | f1.close() |
---|
359 | |
---|
360 | # Saving swap.conf |
---|
361 | swap_conf_file="/etc/nbd-server/conf.d/swap.conf" |
---|
362 | f2 = open(swap_conf_file,'w') |
---|
363 | f2.writelines("[swap]\n") |
---|
364 | f2.writelines("exportname = /tmp/nbd-swap/%s\n") |
---|
365 | f2.writelines("prerun = nbdswapd %s\n") |
---|
366 | f2.writelines("postrun = rm -f %s\n") |
---|
367 | f2.writelines("authfile = /etc/ltsp/nbd-server.allow\n") |
---|
368 | f2.close() |
---|
369 | |
---|
370 | # Checking /etc/hosts.allow |
---|
371 | needs_nbdswap_line=True |
---|
372 | |
---|
373 | if os.path.isfile("/etc/hosts.allow"): |
---|
374 | # if file exists, read it |
---|
375 | f3=open("/etc/hosts.allow", 'r') |
---|
376 | for line in f3.readlines(): |
---|
377 | print line |
---|
378 | if (line=="nbdswapd: ALL: keepalive\n"): |
---|
379 | needs_nbdswap_line=False |
---|
380 | f3.close(); |
---|
381 | |
---|
382 | # If file does not exists or has no line for nbdwap, write it |
---|
383 | if (needs_nbdswap_line): |
---|
384 | f4=open("/etc/hosts.allow", 'a') |
---|
385 | f4.writelines("nbdswapd: ALL: keepalive\n") |
---|
386 | f4.close() |
---|
387 | |
---|
388 | return {"status":True, "msg":"finished"} |
---|
389 | |
---|
390 | except Exception as e: |
---|
391 | print "exc,"+str(e) |
---|
392 | return {"status":False, "msg":str(e)} |
---|
393 | |
---|
394 | def getDefaultBootImage(self): |
---|
395 | try: |
---|
396 | if os.path.exists(self.imagepath+"defaultboot.json"): |
---|
397 | json_data=open(self.imagepath+"defaultboot.json") |
---|
398 | data = json.load(json_data) |
---|
399 | json_data.close() |
---|
400 | return data |
---|
401 | else: |
---|
402 | return {"default_boot":""} |
---|
403 | except Exception as e: |
---|
404 | return {"default_boot":""} |
---|
405 | |
---|
406 | def setDefaultBootImage(self, image): |
---|
407 | try: |
---|
408 | path_to_write = os.path.join(self.imagepath,"defaultboot.json") |
---|
409 | f = open(path_to_write,'w') |
---|
410 | data='{"default_boot":"'+str(image)+'"}' |
---|
411 | f.writelines(data) |
---|
412 | f.close() |
---|
413 | return {"status":"true", "msg":"all ok"} |
---|
414 | except Exception as e: |
---|
415 | return {"status":False, "msg":str(e)} |
---|
416 | |
---|
417 | |
---|
418 | def getMenuTimeout(self): |
---|
419 | try: |
---|
420 | if os.path.exists(self.imagepath+"menutimeout.json"): |
---|
421 | json_data=open(self.imagepath+"menutimeout.json") |
---|
422 | data = json.load(json_data) |
---|
423 | json_data.close() |
---|
424 | return data |
---|
425 | else: |
---|
426 | return {"timeout":""} |
---|
427 | except Exception as e: |
---|
428 | return {"timeout":""} |
---|
429 | |
---|
430 | def setMenuTimeout(self, time): |
---|
431 | try: |
---|
432 | path_to_write = os.path.join(self.imagepath,"menutimeout.json") |
---|
433 | f = open(path_to_write,'w') |
---|
434 | data='{"timeout":"'+str(time)+'"}' |
---|
435 | f.writelines(data) |
---|
436 | f.close() |
---|
437 | return {"status":"true", "msg":"all ok"} |
---|
438 | except Exception as e: |
---|
439 | return {"status":False, "msg":str(e)} |
---|