1 | #!/usr/bin/python3 |
---|
2 | |
---|
3 | # |
---|
4 | # Examples: |
---|
5 | # Export all variables into environment:> eval export $(lliurex-detect -e -a) |
---|
6 | # Test one condition:> lliurex-detect -x thin && echo si || echo no |
---|
7 | # Test one condition:> if lliurex-detect -x ltsp; then echo si; else echo no; fi |
---|
8 | # Get flavour:> FLA=$(lliurex-detect -f) |
---|
9 | # |
---|
10 | import sys |
---|
11 | import os |
---|
12 | import pwd,grp |
---|
13 | import argparse |
---|
14 | import re |
---|
15 | # import glob # Commented: python 3.5 is not available on lliurex 15 |
---|
16 | import fnmatch # allows search recursively without glob |
---|
17 | |
---|
18 | from subprocess import check_output |
---|
19 | |
---|
20 | ltsConfFile="/etc/lts.conf" |
---|
21 | |
---|
22 | def detect_live(): |
---|
23 | test1=False |
---|
24 | test2=False |
---|
25 | test3=False |
---|
26 | |
---|
27 | if os.path.isdir('/rofs'): |
---|
28 | r1=re.compile('^tmpfs\s/cow\stmpfs') |
---|
29 | r2=re.compile('^/cow\s/\soverlayfs') |
---|
30 | r3=re.compile('^/dev/loop0\s/rofs\ssquashfs') |
---|
31 | with open('/proc/mounts') as mounts_file: |
---|
32 | for line in mounts_file.readlines(): |
---|
33 | if r1.match(line): |
---|
34 | test1=True |
---|
35 | if r2.match(line): |
---|
36 | test2=True |
---|
37 | if r3.match(line): |
---|
38 | test3=True |
---|
39 | if test1 and test2 and test3: |
---|
40 | pass |
---|
41 | else: |
---|
42 | raise Exception('Not live') |
---|
43 | |
---|
44 | #def detect_live(): |
---|
45 | |
---|
46 | def detect_flavour(): |
---|
47 | # Use with python > 3.5 with glob new features |
---|
48 | # cdd_content=[] |
---|
49 | # file_name= [ filename for filename in glob.iglob('/usr/share/lliurex-cdd/**/cddflavour', recursive=True) ] |
---|
50 | # if len(file_name) > 0: |
---|
51 | # for cddfile in file_name: |
---|
52 | # try: |
---|
53 | # with open(cddfile,'r') as fileccd: |
---|
54 | # cdd= [ line.strip() for line in fileccd ] |
---|
55 | # cdd_content.extend(cdd) |
---|
56 | # except Exception as e: |
---|
57 | # raise Exception('cddflavour error '+str(e)) |
---|
58 | # else: |
---|
59 | # file_name= [ filename for filename in glob.iglob('/usr/share/lliurex-cdd/**/cddversion', recursive=True) ] |
---|
60 | # if len(file_name) > 0: |
---|
61 | # for cddfile in file_name: |
---|
62 | # try: |
---|
63 | # with open(cddfile,'r') as fileccd: |
---|
64 | # cdd= [ line.strip() for line in fileccd ] |
---|
65 | # cdd_content.extend(cdd) |
---|
66 | # except Exception as e: |
---|
67 | # raise Exception('cddversion file error '+str(e)) |
---|
68 | # else: |
---|
69 | # raise Exception('cddflavour or cddversion file not found! ') |
---|
70 | # cdd_content=list(set(cdd_content)) |
---|
71 | # cdd_content.sort() |
---|
72 | # return cdd_content |
---|
73 | |
---|
74 | # Code for python < 3.5 without glob |
---|
75 | file_name=[] |
---|
76 | cdd_content=[] |
---|
77 | for root,dirnames,filenames in os.walk('/usr/share/lliurex-cdd'): |
---|
78 | for filename in fnmatch.filter(filenames,'cddflavour'): |
---|
79 | file_name.append(os.path.join(root,filename)) |
---|
80 | if len(file_name) > 0: |
---|
81 | for cddfile in file_name: |
---|
82 | try: |
---|
83 | with open(cddfile,'r') as fileccd: |
---|
84 | cdd= [ line.strip() for line in fileccd ] |
---|
85 | cdd_content.extend(cdd) |
---|
86 | except Exception as e: |
---|
87 | raise Exception('cddflavour file error '+str(e)) |
---|
88 | else: |
---|
89 | for root,dirnames,filenames in os.walk('/usr/share/lliurex-cdd'): |
---|
90 | for filename in fnmatch.filter(filenames,'cddversion'): |
---|
91 | file_name.append(os.path.join(root,filename)) |
---|
92 | if len(file_name) > 0: |
---|
93 | for cddfile in file_name: |
---|
94 | try: |
---|
95 | with open(cddfile,'r') as fileccd: |
---|
96 | cdd= [ line.strip() for line in fileccd ] |
---|
97 | cdd_content.extend(cdd) |
---|
98 | except Exception as e: |
---|
99 | raise Exception('cddversion file error '+str(e)) |
---|
100 | else: |
---|
101 | raise Exception('cddflavour or cddversion file not found! ') |
---|
102 | cdd_content=list(set(cdd_content)) |
---|
103 | cdd_content.sort() |
---|
104 | return cdd_content |
---|
105 | #def detect_flavour |
---|
106 | |
---|
107 | def detect_num_cdd(): |
---|
108 | try: |
---|
109 | with open(os.devnull, 'w') as devnull: |
---|
110 | num_cdd=check_output(['dpkg-query','--showformat=\'${Version}\'','--show','lliurex-version-timestamp'],stderr=devnull).decode('utf-8') |
---|
111 | return num_cdd.strip('\'') |
---|
112 | except Exception as e1: |
---|
113 | try: |
---|
114 | with open('/usr/share/lliurex-cdd/version','r') as cdd_num_file: |
---|
115 | return cdd_num_file.readline().strip() |
---|
116 | except Exception as e2: |
---|
117 | raise Exception('Error1: '+e1+' and Error2: '+e2) |
---|
118 | #def detect_num_cdd(): |
---|
119 | |
---|
120 | def get_history_version(): |
---|
121 | try: |
---|
122 | with open('/etc/lliurex-cdd-version','r') as etc_cdd_file: |
---|
123 | return etc_cdd_file.read() |
---|
124 | except Exception as e: |
---|
125 | raise Exception('Error: '+e) |
---|
126 | #def get_history_version() |
---|
127 | |
---|
128 | def detect_type(): |
---|
129 | # |
---|
130 | # Possible output values: live,thin,semi,fat,unknown |
---|
131 | # |
---|
132 | # |
---|
133 | |
---|
134 | display=os.environ.get('DISPLAY') |
---|
135 | #On thin clients display is ip+display so it's at least 7 chars |
---|
136 | if len(display)>= 7: |
---|
137 | client_type='thin' |
---|
138 | else: |
---|
139 | if os.path.exists(ltsConfFile): |
---|
140 | #Attempt to open lts.conf as is more reliable than check environment |
---|
141 | try: |
---|
142 | for line in open (ltsConfFile): |
---|
143 | if line=="LTSP_FATCLIENT": |
---|
144 | if "true" in line: |
---|
145 | client_type="semi" |
---|
146 | else: |
---|
147 | if "true" in line: |
---|
148 | client_type="thin" |
---|
149 | else: |
---|
150 | client_type="unknown" |
---|
151 | break |
---|
152 | ltsConfFile.close() |
---|
153 | except: |
---|
154 | fatclient=os.environ.get('LTSP_FATCLIENT') |
---|
155 | if fatclient=='true': |
---|
156 | client_type="semi" |
---|
157 | else: |
---|
158 | if fatclient=='false': |
---|
159 | client_type="thin" |
---|
160 | else: |
---|
161 | client_type="unknown" |
---|
162 | else: |
---|
163 | client_type='fat' |
---|
164 | try: |
---|
165 | detect_live() |
---|
166 | client_type=client_type+',live' |
---|
167 | except: |
---|
168 | pass |
---|
169 | |
---|
170 | return client_type.rstrip() |
---|
171 | #def detect_type |
---|
172 | |
---|
173 | def detect_user(user=''): |
---|
174 | if user == '' or user == None: |
---|
175 | user_id=os.getuid() |
---|
176 | user_name=pwd.getpwuid(user_id)[0] |
---|
177 | else: |
---|
178 | try: |
---|
179 | user_uid=pwd.getpwnam(user)[3] |
---|
180 | except: |
---|
181 | raise Exception('user not found!') |
---|
182 | user_name=user |
---|
183 | #user_info [0]=>username [1]=>pwd [2]=>uid [3]=>gid [4]=>gecos [5]=>homedir [6]=>shell |
---|
184 | #grp_info [0]=>name [1]=>pwd [2]=>gid [3]=>member |
---|
185 | grupos = [ group[0] for group in grp.getgrall() if user_name in group[3] ] |
---|
186 | with open('/etc/passwd','r') as filepwd: |
---|
187 | localusers=[ line.split(':')[0] for line in filepwd.readlines() ] |
---|
188 | |
---|
189 | ret=user_name |
---|
190 | if 'admins' in grupos : |
---|
191 | ret='*'+str(user_name) |
---|
192 | if user_name in localusers: |
---|
193 | ret += '(local)' |
---|
194 | else: |
---|
195 | ret += '(ldap)' |
---|
196 | return ret |
---|
197 | #def detect_user(): |
---|
198 | |
---|
199 | def store_result(results='',namevar='',action='store'): |
---|
200 | global eval_mode |
---|
201 | global result |
---|
202 | global exit_return_code_mode |
---|
203 | |
---|
204 | if action == 'init': |
---|
205 | if eval_mode: |
---|
206 | result={} |
---|
207 | return 0 |
---|
208 | else: |
---|
209 | result=[] |
---|
210 | return 0 |
---|
211 | elif action == 'store': |
---|
212 | if results=='': |
---|
213 | raise Exception('Missing param results to store') |
---|
214 | if eval_mode and namevar=='': |
---|
215 | raise Exception('Eval mode need namevar param') |
---|
216 | |
---|
217 | if eval_mode: |
---|
218 | if namevar=='USERTYPE': |
---|
219 | res=results |
---|
220 | if res[0] == '*': |
---|
221 | result['PROMOTED_USER']='yes' |
---|
222 | res=results[1:] |
---|
223 | else: |
---|
224 | result['PROMOTED_USER']='no' |
---|
225 | result['USERNAME']=res.split('(')[0] |
---|
226 | result['LOGIN_TYPE']=res.split('(')[1].split(')')[0] |
---|
227 | elif namevar=='SESSION_TYPE': |
---|
228 | res=results.split(',') |
---|
229 | result['LIVE']='no' |
---|
230 | result['LTSP']='no' |
---|
231 | result['THIN']='no' |
---|
232 | result['SEMI']='no' |
---|
233 | result['FAT']='no' |
---|
234 | |
---|
235 | if len(res) > 1: |
---|
236 | result['LIVE']='yes' |
---|
237 | if res[0] != 'fat': |
---|
238 | result['LTSP']='yes' |
---|
239 | |
---|
240 | result[res[0].upper()]='yes' |
---|
241 | elif namevar=='FLAVOUR': |
---|
242 | res=results[-1] |
---|
243 | result['SERVER']='no' |
---|
244 | result['DESKTOP']='no' |
---|
245 | result['CLIENT']='no' |
---|
246 | result['INFANTIL']='no' |
---|
247 | result['MUSIC']='no' |
---|
248 | result['PIME']='no' |
---|
249 | |
---|
250 | #lliurex 15 specific options & catch all |
---|
251 | if res.upper() == 'NETWORK-CLIENT-PROMO': |
---|
252 | result['CLIENT']='yes' |
---|
253 | elif res.upper() == 'LLIUREX': |
---|
254 | if 'INFANTIL' in [ x.upper() for x in results ]: |
---|
255 | result['INFANTIL']='yes' |
---|
256 | else: |
---|
257 | result['DESKTOP']='yes' |
---|
258 | else: |
---|
259 | result[res.upper()]='yes' |
---|
260 | else: |
---|
261 | result[namevar]=results |
---|
262 | |
---|
263 | else: |
---|
264 | result.append(results) |
---|
265 | elif action == 'print': |
---|
266 | if exit_return_code_mode != False: |
---|
267 | if result[exit_return_code_mode.upper()]=='yes': |
---|
268 | sys.exit(0) |
---|
269 | else: |
---|
270 | sys.exit(1) |
---|
271 | if len(result) > 0: |
---|
272 | if eval_mode: |
---|
273 | for k,v in result.items(): |
---|
274 | print(k+'='+v) |
---|
275 | else: |
---|
276 | print (','.join(result)) |
---|
277 | else: |
---|
278 | raise Exception('Unknow action') |
---|
279 | |
---|
280 | |
---|
281 | #def store_result(): |
---|
282 | |
---|
283 | |
---|
284 | # |
---|
285 | # MAIN PROGRAM |
---|
286 | # |
---|
287 | |
---|
288 | parser = argparse.ArgumentParser(description='Get information about running environment') |
---|
289 | parser.add_argument('-a','--all',metavar='',action='store_const',help='Get all information',const=True) |
---|
290 | parser.add_argument('-e','--eval',metavar='',action='store_const',help='Show all information to evaluate in bash variables',const=True) |
---|
291 | parser.add_argument('-s','--session',metavar='',action='store_const',help='Get current session type',const=True) |
---|
292 | parser.add_argument('-f','--flavour',metavar='',action='store_const',help='Get the flavour of current system',const=True) |
---|
293 | parser.add_argument('-u','--usertype',metavar='username',nargs='?',const='',help='Get the usertype from current user or from passed username') |
---|
294 | code_types=['live','ltsp','fat','semi','thin','desktop','server','client','infantil','pime','music'] |
---|
295 | parser.add_argument('-x','--with-return-code',metavar='[live|ltsp|fat|semi|thin|desktop|server|client|infantil|pime|music]',nargs=1,choices=code_types,help='Execute mode testing value passed') |
---|
296 | # lliurex-version options |
---|
297 | parser.add_argument('-n','--number',metavar='',action='store_const',const=True,help='Get the cdd number version') |
---|
298 | parser.add_argument('-v','--version',metavar='',action='store_const',const=True,help='Get the cdd version') |
---|
299 | parser.add_argument('-t','--test',metavar='cdd_name',help='Test if the cdd is installed') |
---|
300 | parser.add_argument('--history',metavar='',action='store_const',const=True,help='Get the installed meta\'s history') |
---|
301 | args=parser.parse_args() |
---|
302 | |
---|
303 | #print(args) |
---|
304 | |
---|
305 | args_all=args.all |
---|
306 | #Commented due to compatibility with lliurex-version without parameters |
---|
307 | #args_all=True |
---|
308 | #if args.all != True: |
---|
309 | # for arg in vars(args): |
---|
310 | # if getattr(args,arg) != None: |
---|
311 | # args_all=False |
---|
312 | # break |
---|
313 | arg_none_for_lliurex_version=True |
---|
314 | if args.all != True: |
---|
315 | for arg in vars(args): |
---|
316 | if getattr(args,arg) != None: |
---|
317 | arg_none_for_lliurex_version=False |
---|
318 | break |
---|
319 | else: |
---|
320 | arg_none_for_lliurex_version=False |
---|
321 | |
---|
322 | eval_mode=args.eval |
---|
323 | |
---|
324 | exit_return_code_mode=False |
---|
325 | if args.with_return_code != None: |
---|
326 | eval_mode=True |
---|
327 | args_all=True |
---|
328 | exit_return_code_mode=args.with_return_code[0] |
---|
329 | |
---|
330 | |
---|
331 | store_result(action='init') |
---|
332 | |
---|
333 | try: |
---|
334 | if args_all or args.session != None: |
---|
335 | store_result(detect_type(),'SESSION_TYPE') |
---|
336 | if args_all or args.flavour != None: |
---|
337 | store_result(detect_flavour(),'FLAVOUR') |
---|
338 | if args_all or args.usertype != None: |
---|
339 | store_result(detect_user(args.usertype),'USERTYPE') |
---|
340 | |
---|
341 | #lliurex version options |
---|
342 | if args.number != None: |
---|
343 | print(detect_num_cdd()) |
---|
344 | if args.version != None: |
---|
345 | print(', '.join(detect_flavour())) |
---|
346 | if arg_none_for_lliurex_version: |
---|
347 | ret=detect_flavour() |
---|
348 | ret.append(detect_num_cdd()) |
---|
349 | print(', '.join(ret)) |
---|
350 | if args.test != None: |
---|
351 | try: |
---|
352 | ret=detect_flavour() |
---|
353 | except: |
---|
354 | sys.exit(1) |
---|
355 | if args.test in ret: |
---|
356 | sys.exit(0) |
---|
357 | else: |
---|
358 | sys.exit(1) |
---|
359 | if args.history != None: |
---|
360 | print(get_history_version().rstrip()) |
---|
361 | #end lliurex-version options |
---|
362 | |
---|
363 | store_result(action='print') |
---|
364 | except Exception as e: |
---|
365 | print('Error '+str(e)) |
---|
366 | sys.exit(1) |
---|
367 | |
---|
368 | sys.exit(0) |
---|