2 # -*- coding: utf-8 -*-
4 # This file is part of Karesansui.
6 # Copyright (C) 2009 HDE, Inc.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
26 from installer.const import *
29 env = os._Environ(os.environ)
30 logfile = env.get("LOGFILE")
37 @return: UUID用の16個のバイト要素を持つ配列
46 for _x in range(0, 16):
47 uuid.append(random.randint(0x00,0xff))
50 def string_from_uuid(uuid):
55 @param uuid: generate_uuid関数等で生成されたUUIDデータ
59 Convert UUID data to string
61 @param uuid: UUID data that was generated by certain function like as generate_uuid()
62 @return: The string that stands for uuid
66 return "-".join(["%02x"*4 % tuuid[0:4],
67 "%02x"*2 % tuuid[4:6],
68 "%02x"*2 % tuuid[6:8],
69 "%02x"*2 % tuuid[8:10],
70 "%02x"*6 % tuuid[10:16]
73 def detect_encoding(string,encoding_list=None):
74 func = lambda data,encoding: data.decode(encoding) and encoding
77 encoding_list = [ 'euc-jp', 'utf-8', 'shift-jis', 'iso2022-jp' ]
79 for encoding in encoding_list:
81 return func(string, encoding)
87 def execute_command(command_args):
92 subproc_args = { 'stdin': subprocess.PIPE,
93 'stdout': subprocess.PIPE,
94 'stderr': subprocess.STDOUT,
101 logfile = "/dev/null"
102 logf = open(logfile, "a")
105 pp = subprocess.Popen(command_args, **subproc_args)
106 logf.write("Exec: '%s'\n" % (command_args))
108 logf.write("Exec: '%s' failed.\n" % (command_args))
112 (stdouterr, stdin) = (pp.stdout, pp.stdin)
114 line = stdouterr.readline()
119 res.append(unicode(line, detect_encoding(line)).encode("utf-8"))
129 except (TypeError, ValueError):
132 def r_chown(path,owner):
135 if not os.path.exists(path):
139 user, group = owner.split(':')
141 user, group = [owner,None ]
143 if is_int(user) is not True:
145 pw = pwd.getpwnam(user)
150 pw = pwd.getpwuid(int(user))
156 statinfo = os.stat(path)
157 gid = statinfo.st_gid
159 if is_int(group) is not True:
161 gr = grp.getgrnam(group)
166 gr = grp.getgrgid(int(group))
171 if os.path.isfile(path) or os.path.islink(path):
173 os.chown(path,uid,gid)
177 elif os.path.isdir(path):
179 os.chown(path,uid,gid)
183 for name in os.listdir(path):
184 sub_path = os.path.join(path, name)
185 r_chown(sub_path,owner)
189 def r_chgrp(path,group):
192 if not os.path.exists(path):
195 statinfo = os.stat(path)
196 uid = statinfo.st_uid
198 if is_int(group) is not True:
200 gr = grp.getgrnam(group)
205 gr = grp.getgrgid(int(group))
210 if os.path.isfile(path) or os.path.islink(path):
212 os.chown(path,uid,gid)
216 elif os.path.isdir(path):
218 os.chown(path,uid,gid)
222 for name in os.listdir(path):
223 sub_path = os.path.join(path, name)
224 r_chgrp(sub_path,group)
228 def r_chmod(path,perm):
229 perm_regex = re.compile(r"""^(?P<user>[ugo]{0,3})(?P<action>[\+\-])(?P<value>[rwxst]{1,3})$""")
231 user_table = {"u":"USR","g":"GRP","o":"OTH"}
232 perm_table = {"r":"R","w":"W","x":"X"}
234 if not os.path.exists(path):
239 if type(perm) == str:
244 new_perm = stat.S_IMODE(s.st_mode)
246 m = perm_regex.match(perm)
248 user = m.group('user')
249 action = m.group('action')
250 value = m.group('value')
255 for k,v in user_table.iteritems():
257 for k2,v2 in perm_table.iteritems():
259 exec("bit = stat.S_I%s%s" % (v2,v,))
260 mask_perm = mask_perm | bit
265 mask_perm = mask_perm | bit
270 mask_perm = mask_perm | bit
273 mask_perm = mask_perm | bit
275 #print "new_perm1:" + dec2oct(new_perm)
276 #print "mask_perm:" + dec2oct(mask_perm)
278 new_perm = new_perm & (~ mask_perm)
280 new_perm = new_perm | mask_perm
281 #print "new_perm2:" + dec2oct(new_perm)
286 if os.path.isfile(path) or os.path.islink(path):
288 os.chmod(path,new_perm)
292 elif os.path.isdir(path):
294 os.chmod(path,new_perm)
298 for name in os.listdir(path):
299 sub_path = os.path.join(path, name)
300 r_chmod(sub_path,original_perm)
304 def sh_config_read(filename):
307 fp = open(filename,"r")
308 fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
309 for line in fp.readlines():
311 if len(line) <= 0 or line[0] == "#":
313 key, value = line.split('=',1)
315 fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
322 def sh_config_write(filename,opts):
326 if type(opts) == dict:
330 res[k] = getattr(opts,k)
333 fp = open(filename,"w")
334 fcntl.lockf(fp.fileno(), fcntl.LOCK_EX)
335 for k,v in res.iteritems():
336 if type(v) == str and k[0:2] != "__" and k[0:4] != "pass":
337 fp.write("%s=%s\n" % (k, v,))
338 fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
345 def run_chkconfig(name,flag=None):
349 (ret,res) = execute_command([COMMAND_CHKCONFIG,name])
351 (ret,res) = execute_command([COMMAND_CHKCONFIG,name,flag])
357 def run_service(name,flag=None):
361 (ret,res) = execute_command([COMMAND_SERVICE,name,"status"])
363 (ret,res) = execute_command([COMMAND_SERVICE,name,flag])
372 fqdn = socket.gethostname()
375 url_prefix = sh_config_read(DEFAULT_KARESANSUI_CONF)["application.url.prefix"]
378 ssl_port_regex = re.compile(r"^\$SERVER\[\"socket\"\] == \".*:(?P<port>[0-9]+)\"")
380 fp = open(LIGHTY_SSL_CONF,"r")
381 fcntl.lockf(fp.fileno(), fcntl.LOCK_SH)
382 for line in fp.readlines():
384 m = ssl_port_regex.match(line)
386 ssl_port = m.group('port')
387 if ssl_port == "443":
388 urls.append("https://%s%s/" % (fqdn,url_prefix))
390 urls.append("https://%s:%s%s/" % (fqdn,m.group('port'),url_prefix))
391 fcntl.lockf(fp.fileno(), fcntl.LOCK_UN)
398 port = sh_config_read(LIGHTY_PORT_CONF)["server.port"]
403 urls.append("http://%s%s/" % (fqdn,url_prefix))
405 urls.append("http://%s:%s%s/" % (fqdn,port,url_prefix))
409 def deco_msg(string,font="green"):
411 return "
\e[1m%s
\e[0m" % string
412 elif font == "underline":
413 return "
\e[4m%s
\e[0m" % string
415 return "
\e[31m%s
\e[0m" % string
416 elif font == "green":
417 return "
\e[32m%s
\e[0m" % string
418 elif font == "orange":
419 return "
\e[33m%s
\e[0m" % string
421 return "
\e[34m%s
\e[0m" % string
423 return "
\e[37m%s
\e[0m" % string
424 elif font == "red|bold":
425 return "
\e[1m
\e[31m%s
\e[0m" % string
426 elif font == "green|bold":
427 return "
\e[1m
\e[32m%s
\e[0m" % string
428 elif font == "blue|bold":
429 return "
\e[1m
\e[34m%s
\e[0m" % string
433 def is_installed(name,rpmlib=None):
435 from installer.rpmlib import RpmLib
438 rpmlib.doQuery("^%s$" % name)[name]
445 def get_distrubution(rpmlib=None):
446 ret = ('unknown',None)
449 from installer.rpmlib import RpmLib
458 "distribution-release",
459 "miraclelinux-release",
465 rpminfo = rpmlib.doQuery("")
466 for _name in distros:
468 _info = rpminfo[_name]
469 ret = (_name, "%s-%s" % (_info["version"],_info["release"],))