1 | # npackage example https://svn.lliurex.net/pandora/n4d-ldap/trunk |
---|
2 | # jinja2 http://jinja.pocoo.org/docs/templates |
---|
3 | |
---|
4 | from jinja2 import Environment |
---|
5 | from jinja2.loaders import FileSystemLoader |
---|
6 | from jinja2 import Template |
---|
7 | import tempfile |
---|
8 | import shutil |
---|
9 | import os |
---|
10 | import subprocess |
---|
11 | class NetinstallManager: |
---|
12 | |
---|
13 | def __init__(self): |
---|
14 | #Load template file |
---|
15 | self.tpl_env = Environment(loader=FileSystemLoader('/usr/share/n4d/templates/netinstall')) |
---|
16 | self.imagepath="/etc/ltsp/bootopts/" |
---|
17 | pass |
---|
18 | #def init |
---|
19 | |
---|
20 | def startup(self,options): |
---|
21 | # executed when launching n4d |
---|
22 | pass |
---|
23 | |
---|
24 | #def startup |
---|
25 | |
---|
26 | def apt(self): |
---|
27 | # executed after apt operations |
---|
28 | pass |
---|
29 | |
---|
30 | #def apt |
---|
31 | |
---|
32 | # service test and backup functions # |
---|
33 | |
---|
34 | def test(self): |
---|
35 | |
---|
36 | pass |
---|
37 | |
---|
38 | #def test |
---|
39 | |
---|
40 | def backup(self): |
---|
41 | |
---|
42 | pass |
---|
43 | |
---|
44 | #def test |
---|
45 | |
---|
46 | def restore(self): |
---|
47 | |
---|
48 | pass |
---|
49 | |
---|
50 | #def test |
---|
51 | |
---|
52 | def load_exports(self): |
---|
53 | #Get template |
---|
54 | template_cname = self.tpl_env.get_template("cname") |
---|
55 | list_variables = {} |
---|
56 | |
---|
57 | ########################### |
---|
58 | #Getting VARS |
---|
59 | ########################### |
---|
60 | |
---|
61 | #Obtains INTERNAL_DOMAIN |
---|
62 | list_variables['INTERNAL_DOMAIN'] = objects['VariablesManager'].get_variable('INTERNAL_DOMAIN') |
---|
63 | #If INTERNAL_DOMAIN is not defined returns an error |
---|
64 | if list_variables['INTERNAL_DOMAIN'] == None: |
---|
65 | return {'status':False,'msg':'Variable INTERNAL_DOMAIN not defined'} |
---|
66 | |
---|
67 | #Obtains HOSTNAME |
---|
68 | list_variables['HOSTNAME'] = objects['VariablesManager'].get_variable('HOSTNAME') |
---|
69 | #If variable SRV_IP is not defined returns an error |
---|
70 | if list_variables['HOSTNAME'] == None: |
---|
71 | return {'status':False,'msg':'Variable HOSTNAME not defined'} |
---|
72 | |
---|
73 | #Encode vars to UTF-8 |
---|
74 | string_template = template_cname.render(list_variables).encode('UTF-8') |
---|
75 | #Open template file |
---|
76 | fd, tmpfilepath = tempfile.mkstemp() |
---|
77 | new_export_file = open(tmpfilepath,'w') |
---|
78 | new_export_file.write(string_template) |
---|
79 | new_export_file.close() |
---|
80 | os.close(fd) |
---|
81 | #Write template values |
---|
82 | n4d_mv(tmpfilepath,'/var/lib/dnsmasq/config/cname-preseed',True,'root','root','0644',True ) |
---|
83 | subprocess.Popen(['/etc/init.d/dnsmasq','restart'],stdout=subprocess.PIPE).communicate() |
---|
84 | return {'status':True,'msg':'Exports written'} |
---|
85 | #def load_exports |
---|
86 | # ######################### # |
---|
87 | |
---|
88 | def getNetinstall(self): |
---|
89 | ''' |
---|
90 | Reads file /etc/ltsp/bootopts/netinstall and returns true or false |
---|
91 | ''' |
---|
92 | # 1. /opt/ltsp/name-chroot |
---|
93 | # 2. /opt/ltsp/images/name-chroot.img |
---|
94 | # 3. /var/lib/tftpboot/ltsp/name-chroot |
---|
95 | # if 1,2 and 3 exist -> show |
---|
96 | # if 1 but not exist 2 or/and 3 -> show with error |
---|
97 | # |
---|
98 | |
---|
99 | json_data=open(self.imagepath+"netinstall.json") |
---|
100 | data = json.load(json_data) |
---|
101 | json_data.close() |
---|
102 | if(data["netinstall_boot"].lower()=="true"): |
---|
103 | netinstall='true'; |
---|
104 | else: |
---|
105 | netinstall='false'; |
---|
106 | |
---|
107 | if(data["netinstall_unattended"].lower()=="true"): |
---|
108 | unattended='true'; |
---|
109 | else: |
---|
110 | unattended='false'; |
---|
111 | |
---|
112 | if("netinstall_stats" not in data.keys() or data["netinstall_stats"].lower()!="false"): |
---|
113 | do_stats='true'; |
---|
114 | else: |
---|
115 | do_stats='false'; |
---|
116 | |
---|
117 | #write the json file |
---|
118 | ret=self.setNetinstall(data["netinstall_boot"].lower(),data["netinstall_unattended"].lower(),data["netinstall_stats"].lower()) |
---|
119 | if ret['status'].lower() != 'true': |
---|
120 | raise Exception('Error setting json file calling setNetinstall') |
---|
121 | |
---|
122 | return {"netinstall":netinstall, "unattended":unattended, "stats": do_stats} |
---|
123 | |
---|
124 | |
---|
125 | # END def GetNetInstall |
---|
126 | |
---|
127 | def setNetinstall(self, status, unattended,stats): |
---|
128 | ''' |
---|
129 | receives data from admin-center form |
---|
130 | sets option for netinstall int bootopt.json (status and unattended install) |
---|
131 | ''' |
---|
132 | try: |
---|
133 | mirror_var="/var/lib/n4d/variables-dir/LLIUREXMIRROR" |
---|
134 | if os.path.isfile(mirror_var): |
---|
135 | if (status.lower()=="true" or status.lower()=="false"): |
---|
136 | path_to_write = os.path.join(self.imagepath,"netinstall.json") |
---|
137 | f = open(path_to_write,'w') |
---|
138 | data='{"netinstall_boot":"'+str(status)+'", "netinstall_unattended":"'+str(unattended)+'", "netinstall_stats":"'+str(stats)+'"}' |
---|
139 | f.writelines(data) |
---|
140 | f.close() |
---|
141 | |
---|
142 | # Enable or disable NETINSTALL on menu (the last option, but enabled) |
---|
143 | if (status.lower()=="true"): |
---|
144 | objects["LlxBootManager"].pushToBootList("netinstall") |
---|
145 | else: |
---|
146 | objects["LlxBootManager"].removeFromBootList("netinstall") |
---|
147 | |
---|
148 | # Removing user and password from preseed |
---|
149 | self.setNetinstallUnattended(status, "", "", "") |
---|
150 | |
---|
151 | return {"status":"true", "msg":"all ok"} |
---|
152 | |
---|
153 | else: |
---|
154 | return {"status":"false", "msg":"option not valid"} |
---|
155 | else: |
---|
156 | return {"status":"false", "msg":"mirror is not available"} |
---|
157 | |
---|
158 | #return data; |
---|
159 | except Exception as e: |
---|
160 | return {"status":"false", "msg":str(e)}; |
---|
161 | |
---|
162 | # END def getListTemplate(self, image) |
---|
163 | |
---|
164 | def set_force_classroom_stats(self,stats): |
---|
165 | var_name='STATS_ENABLED' |
---|
166 | manager = objects['VariablesManager'] |
---|
167 | stats_value = manager.get_variable(var_name) |
---|
168 | |
---|
169 | if stats_value == None: |
---|
170 | # Register new variable |
---|
171 | ret,msg=manager.add_variable(var_name,'0','','Stats enabled','lliurex-statistics') |
---|
172 | # if ret==True: |
---|
173 | # ret,msg=manager.init_variable(var_name,'0'); |
---|
174 | # if ret!=True: |
---|
175 | # raise Exception('Error initalizing variable') |
---|
176 | # else: |
---|
177 | # raise Exception('Error adding variable') |
---|
178 | if stats.lower() == 'true' or stats == '1': |
---|
179 | # Enable |
---|
180 | ret,msg=manager.set_variable(var_name,'1') |
---|
181 | else: |
---|
182 | # Disable |
---|
183 | ret,msg=manager.set_variable(var_name,'0') |
---|
184 | if ret != True: |
---|
185 | raise Exception('Error setting variable') |
---|
186 | else: |
---|
187 | return True |
---|
188 | #END def set_force_classroom_stats(self,status) |
---|
189 | |
---|
190 | def get_force_classroom_stats(self): |
---|
191 | var_name='STATS_ENABLED' |
---|
192 | manager = objects['VariablesManager'] |
---|
193 | stats_value = manager.get_variable(var_name) |
---|
194 | |
---|
195 | if stats_value == None: |
---|
196 | # Register new variable |
---|
197 | ret,msg=manager.add_variable(var_name,'0','','Stats enabled','lliurex-statistics') |
---|
198 | stats_value = manager.get_variable(var_name) |
---|
199 | return str(stats_value) |
---|
200 | # if ret==True: |
---|
201 | # ret,msg=manager.init_variable(var_name,'0'); |
---|
202 | # if ret!=True: |
---|
203 | # raise Exception('Error initalizing variable'+str(ret)+' '+str(msg)) |
---|
204 | # else: |
---|
205 | # raise Exception('Error adding variable') |
---|
206 | else: |
---|
207 | return str(stats_value) |
---|
208 | |
---|
209 | |
---|
210 | #END def get_force_classroom_stats(self): |
---|
211 | |
---|
212 | def setNetinstallUnattended(self, status, username, password, rootpassword): |
---|
213 | ''' |
---|
214 | Writing in presseed username and password |
---|
215 | ''' |
---|
216 | if not username or not password or not rootpassword: |
---|
217 | return {"status":"false", "msg": "Usernames or Passwords can't be an empty string"} |
---|
218 | try: |
---|
219 | filedir="/var/www/preseed" |
---|
220 | filepath="/var/www/preseed/unattended.cfg" |
---|
221 | filepartman="/var/www/preseed/partman_sda.cfg" |
---|
222 | |
---|
223 | if not os.path.exists(filedir): |
---|
224 | os.makedirs(filedir) |
---|
225 | |
---|
226 | preseed=open(filepath,'w') |
---|
227 | preseed.write("# LMD Created user account\n") |
---|
228 | salt="sw9.tfRI" |
---|
229 | userpassencrypted=crypt.crypt(str(password),"$1$"+salt+"$") |
---|
230 | rootpassencrypted=crypt.crypt(str(rootpassword),"$1$"+salt+"$") |
---|
231 | |
---|
232 | if(status==True): |
---|
233 | # Saving file |
---|
234 | |
---|
235 | userfullline="d-i passwd/user-fullname string "+str(username)+"\n"; |
---|
236 | userline="d-i passwd/username string "+str(username)+"\n"; |
---|
237 | passline="d-i passwd/user-password-crypted password "+str(userpassencrypted)+"\n" |
---|
238 | |
---|
239 | if (len(rootpassword) > 0): |
---|
240 | rootpassline = "d-i passwd/root-password-crypted password "+str(rootpassencrypted) + "\n" |
---|
241 | else: |
---|
242 | rootpassline = "# d-i passwd/root-password-crypted password "+str(rootpassencrypted) + "\n" |
---|
243 | |
---|
244 | # Partition preseed |
---|
245 | try: |
---|
246 | partman = open(filepartman,'r') |
---|
247 | preseed.writelines(partman.readlines()) |
---|
248 | preseed.write("\n") |
---|
249 | partman.close() |
---|
250 | except Exception as e: |
---|
251 | return str(e) |
---|
252 | |
---|
253 | else: |
---|
254 | userfullline="#d-i passwd/user-fullname string \n" |
---|
255 | userline="#d-i passwd/username string \n" |
---|
256 | passline="# d-i passwd/user-password-crypted password \n" |
---|
257 | rootpassline = "# d-i passwd/root-password-crypted password \n" |
---|
258 | |
---|
259 | preseed.write("# Normal user name\n") |
---|
260 | preseed.write(userfullline) |
---|
261 | preseed.write(userline) |
---|
262 | preseed.write("# Normal user's password, either in clear text\n") |
---|
263 | preseed.write("#d-i passwd/user-password password insecure\n") |
---|
264 | preseed.write("# Normal user's password encrypted using an MD5 hash.\n") |
---|
265 | preseed.write(passline) |
---|
266 | preseed.write(rootpassline) |
---|
267 | |
---|
268 | # Allow weak passwords |
---|
269 | preseed.write("d-i user-setup/allow-password-weak boolean true\n") |
---|
270 | |
---|
271 | preseed.close() |
---|
272 | |
---|
273 | return {"status":"true", "msg":"all ok"} |
---|
274 | |
---|
275 | except Exception as e: |
---|
276 | return {"status":"false", "msg":str(e)} |
---|
277 | |
---|
278 | # END def getListTemplate(self, image) |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | #class N4dProxy |
---|