Merge pull request #22887 from hvnsweeting/18843-fix-user-present-home

fix #18843
This commit is contained in:
Thomas S Hatch 2015-04-22 09:47:05 -06:00
commit 12d2b91d85
3 changed files with 33 additions and 16 deletions

View file

@ -3428,12 +3428,16 @@ def makedirs_(path,
if os.path.isdir(dirname):
# There's nothing for us to do
return 'Directory {0!r} already exists'.format(dirname)
msg = 'Directory {0!r} already exists'.format(dirname)
log.debug(msg)
return msg
if os.path.exists(dirname):
return 'The path {0!r} already exists and is not a directory'.format(
msg = 'The path {0!r} already exists and is not a directory'.format(
dirname
)
log.debug(msg)
return msg
directories_to_create = []
while True:
@ -3447,6 +3451,7 @@ def makedirs_(path,
directories_to_create.reverse()
for directory_to_create in directories_to_create:
# all directories have the user, group and mode set!!
log.debug('Creating directory: %s', directory_to_create)
mkdir(directory_to_create, user=user, group=group, mode=mode)

View file

@ -111,10 +111,14 @@ def add(name,
except OSError:
log.debug('Error reading /etc/login.defs', exc_info=True)
if createhome:
cmd.append('-m')
elif createhome is False:
cmd.append('-M')
if isinstance(createhome, bool):
if createhome:
cmd.append('-m')
else:
cmd.append('-M')
else:
log.error('Value passes to ``createhome`` must be a boolean')
return False
if home is not None:
cmd.extend(['-d', home])
@ -282,8 +286,8 @@ def chshell(name, shell):
def chhome(name, home, persist=False):
'''
Change the home directory of the user, pass True for persist to copy files
to the new home dir
Change the home directory of the user, pass True for persist to move files
to the new home directory if the old home directory exist.
CLI Example:

View file

@ -107,8 +107,10 @@ def _changes(name,
if home:
if lusr['home'] != home:
change['home'] = home
if createhome and not os.path.isdir(home):
change['homeDoesNotExist'] = home
if createhome:
newhome = home if home else lusr['home']
if not os.path.isdir(newhome):
change['homeDoesNotExist'] = newhome
if shell:
if lusr['shell'] != shell:
@ -211,7 +213,10 @@ def present(name,
the state, True by default
home
The location of the home directory to manage
The custom login directory of user. Uses default value of underlying
system if not set. Notice that this directory does not have to exists.
This also the location of the home directory to create if createhome is
set to True.
createhome
If True, the home directory will be created if it doesn't exist.
@ -375,11 +380,14 @@ def present(name,
if key == 'date':
__salt__['shadow.set_date'](name, date)
continue
if key == 'home' or key == 'homeDoesNotExist':
if createhome:
__salt__['user.chhome'](name, val, True)
else:
__salt__['user.chhome'](name, val, False)
# run chhome once to avoid any possible bad side-effect
if key == 'home' and 'homeDoesNotExist' not in changes:
__salt__['user.chhome'](name, val, False)
continue
if key == 'homeDoesNotExist':
__salt__['user.chhome'](name, val, True)
if not os.path.isdir(val):
__salt__['file.mkdir'](val, pre['uid'], pre['gid'], 0755)
continue
if key == 'mindays':
__salt__['shadow.set_mindays'](name, mindays)