mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-11-04 00:02:37 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			124 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import Utils, Build
 | 
						|
from TaskGen import feature, before
 | 
						|
from Configure import ConfigurationError
 | 
						|
import Options
 | 
						|
import Task
 | 
						|
import os
 | 
						|
 | 
						|
def detect(conf):
 | 
						|
	if Options.platform == 'win32': raise Utils.WafError('the usermgmt tool only works on Linux')
 | 
						|
	if Options.platform == 'darwin': raise Utils.WafError('the usermgmt tool only works on Linux')
 | 
						|
	path_list = ["/usr/local/sbin","/usr/sbin","/sbin"] + os.environ.get('PATH','').split(os.pathsep)
 | 
						|
	conf.find_program("useradd",var='USERADD',mandatory=True,path_list=path_list)
 | 
						|
	conf.find_program("userdel",var='USERDEL',mandatory=True,path_list=path_list)
 | 
						|
 | 
						|
def set_options(opt):
 | 
						|
	if Options.platform == 'win32': raise Utils.WafError('the usermgmt tool only works on Linux')
 | 
						|
	if Options.platform == 'darwin': raise Utils.WafError('the usermgmt tool only works on Linux')
 | 
						|
	og = opt.get_option_group('--force')
 | 
						|
	og.add_option('--nochown',
 | 
						|
		action = 'store_true',
 | 
						|
		help = 'do not create or remove user accounts or change file ownership on installed files',
 | 
						|
		default = False,
 | 
						|
		dest = 'NOUSERMGMT')
 | 
						|
 | 
						|
def _subst_add_destdir(x,bld):
 | 
						|
	a = "${DESTDIR}" + x
 | 
						|
	a = a.replace("${DESTDIR}",Options.options.destdir)
 | 
						|
	a = Utils.subst_vars(a,bld.env)
 | 
						|
	if a.startswith("//"): a = a[1:]
 | 
						|
	return a
 | 
						|
Build.BuildContext.subst_add_destdir = staticmethod(_subst_add_destdir)
 | 
						|
 | 
						|
def _setownership(ctx,path,owner,group,mode=None):
 | 
						|
	if Options.platform == 'win32': return
 | 
						|
	if Options.platform == 'darwin': return
 | 
						|
	if not hasattr(os,"getuid"): return
 | 
						|
	if os.getuid() != 0: return
 | 
						|
	if Options.options.NOUSERMGMT: return
 | 
						|
 | 
						|
	import pwd
 | 
						|
	import grp
 | 
						|
	import stat
 | 
						|
	from os import chown as _chown, chmod as _chmod
 | 
						|
 | 
						|
	def f(bld,path,owner,group,mode):
 | 
						|
		
 | 
						|
		try: uid = pwd.getpwnam(owner).pw_uid
 | 
						|
		except KeyError,e:
 | 
						|
			raise Utils.WafError("Before using setownership() you have to create the user with bld.createuser(username...)")
 | 
						|
		try: gid = grp.getgrnam(group).gr_gid
 | 
						|
		except KeyError,e:
 | 
						|
			raise Utils.WafError("Before using setownership() you have to create the user with bld.createuser(username...)")
 | 
						|
		
 | 
						|
		path = bld.subst_add_destdir(path,bld)
 | 
						|
		current_uid,current_gid = os.stat(path).st_uid,os.stat(path).st_gid
 | 
						|
		if current_uid != uid:
 | 
						|
			Utils.pprint("GREEN","* setting owner of %s to UID %s"%(path,uid))
 | 
						|
			_chown(path,uid,current_gid)
 | 
						|
			current_uid = uid
 | 
						|
		if current_gid != gid:
 | 
						|
			Utils.pprint("GREEN","* setting group of %s to GID %s"%(path,gid))
 | 
						|
			_chown(path,current_uid,gid)
 | 
						|
			current_gid = gid
 | 
						|
		if mode is not None:
 | 
						|
			current_mode = stat.S_IMODE(os.stat(path).st_mode)
 | 
						|
			if current_mode != mode:
 | 
						|
				Utils.pprint("GREEN","* adjusting permissions on %s to mode %o"%(path,mode))
 | 
						|
				_chmod(path,mode)
 | 
						|
				current_mode = mode
 | 
						|
	
 | 
						|
	if ctx.is_install > 0:
 | 
						|
		ctx.add_post_fun(lambda ctx: f(ctx,path,owner,group,mode))
 | 
						|
Build.BuildContext.setownership = _setownership
 | 
						|
 | 
						|
def _createuser(ctx,user,homedir,shell):
 | 
						|
	if Options.platform == 'win32': return
 | 
						|
	if Options.platform == 'darwin': return
 | 
						|
	if not hasattr(os,"getuid"): return
 | 
						|
	if os.getuid() != 0: return
 | 
						|
	if Options.options.NOUSERMGMT: return
 | 
						|
	
 | 
						|
	def f(ctx,user,homedir,shell):
 | 
						|
		import pwd
 | 
						|
		try:
 | 
						|
			pwd.getpwnam(user).pw_uid
 | 
						|
			user_exists = True
 | 
						|
		except KeyError,e:
 | 
						|
			user_exists = False
 | 
						|
		if user_exists: return
 | 
						|
		
 | 
						|
		Utils.pprint("GREEN","* creating user %s"%user)
 | 
						|
		cmd = [
 | 
						|
		  ctx.env.USERADD,
 | 
						|
		  '-M',
 | 
						|
		  '-r',
 | 
						|
		  '-s',shell,
 | 
						|
		  '-d',homedir,
 | 
						|
		  user,
 | 
						|
		]
 | 
						|
		ret = Utils.exec_command(cmd)
 | 
						|
		if ret: raise Utils.WafError("Failed to run command %s"%cmd)
 | 
						|
	
 | 
						|
	def g(ctx,user,homedir,shell):
 | 
						|
		import pwd
 | 
						|
		try:
 | 
						|
			pwd.getpwnam(user).pw_uid
 | 
						|
			user_exists = True
 | 
						|
		except KeyError,e:
 | 
						|
			user_exists = False
 | 
						|
		if not user_exists: return
 | 
						|
		
 | 
						|
		Utils.pprint("GREEN","* removing user %s"%user)
 | 
						|
		cmd = [
 | 
						|
		  ctx.env.USERDEL,
 | 
						|
		  user,
 | 
						|
		]
 | 
						|
		ret = Utils.exec_command(cmd)
 | 
						|
		if ret: raise Utils.WafError("Failed to run command %s"%cmd)
 | 
						|
	
 | 
						|
	if ctx.is_install > 0:
 | 
						|
		ctx.add_pre_fun(lambda ctx: f(ctx,user,homedir,shell))
 | 
						|
	elif ctx.is_install < 0:
 | 
						|
		ctx.add_pre_fun(lambda ctx: g(ctx,user,homedir,shell))
 | 
						|
Build.BuildContext.createuser = _createuser |