Skip to content

Commit

Permalink
add an option to preserve the home directory instead of deleting it i…
Browse files Browse the repository at this point in the history
…f the uid or username collide (#164)

* add an option to preserve the home directory instead of deleting it if the uid or username collide

Signed-off-by: Tully Foote <[email protected]>

* add tests for preserve home options

Signed-off-by: Tully Foote <[email protected]>

* fix default override name string

Signed-off-by: Tully Foote <[email protected]>
  • Loading branch information
tfoote authored Dec 1, 2021
1 parent 37a60a0 commit 0f1c7d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/rocker/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def get_snippet(self, cliargs):
if 'user_override_name' in cliargs and cliargs['user_override_name']:
substitutions['name'] = cliargs['user_override_name']
substitutions['dir'] = os.path.join('/home/', cliargs['user_override_name'])
substitutions['user_preserve_home'] = True if 'user_preserve_home' in cliargs and cliargs['user_preserve_home'] else False
substitutions['home_extension_active'] = True if 'home' in cliargs and cliargs['home'] else False
return em.expand(snippet, substitutions)

Expand All @@ -231,8 +232,12 @@ def register_arguments(parser, defaults={}):
help="mount the current user's id and run as that user")
parser.add_argument('--user-override-name',
action='store',
default=defaults.get('user-override-username', None),
default=defaults.get('user-override-name', None),
help="override the current user's name")
parser.add_argument('--user-preserve-home',
action='store_true',
default=defaults.get('user-preserve-home', False),
help="Do not delete home directory if it exists when making a new user.")


class Environment(RockerExtension):
Expand Down
4 changes: 2 additions & 2 deletions src/rocker/templates/user_snippet.Dockerfile.em
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ RUN if ! command -v sudo >/dev/null; then \

@[if name != 'root']@
RUN existing_user_by_uid=`getent passwd "@(uid)" | cut -f1 -d: || true` && \
if [ -n "${existing_user_by_uid}" ]; then userdel -r "${existing_user_by_uid}"; fi && \
if [ -n "${existing_user_by_uid}" ]; then userdel @('' if user_preserve_home else '-r') "${existing_user_by_uid}"; fi && \
existing_user_by_name=`getent passwd "@(name)" | cut -f1 -d: || true` && \
if [ -n "${existing_user_by_name}" ]; then userdel -r "${existing_user_by_name}"; fi && \
if [ -n "${existing_user_by_name}" ]; then userdel @('' if user_preserve_home else '-r') "${existing_user_by_name}"; fi && \
existing_group_by_gid=`getent group "@(gid)" | cut -f1 -d: || true` && \
if [ -z "${existing_group_by_gid}" ]; then \
groupadd -g "@(gid)" "@name"; \
Expand Down
7 changes: 6 additions & 1 deletion test/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,15 @@ def test_user_extension(self):

user_override_active_cliargs = mock_cliargs
user_override_active_cliargs['user_override_name'] = 'testusername'
print(p.get_snippet(user_override_active_cliargs))
snippet_result = p.get_snippet(user_override_active_cliargs)
self.assertTrue('USER testusername' in snippet_result)
self.assertTrue('WORKDIR /home/testusername' in snippet_result)
self.assertTrue('userdel -r' in snippet_result)

user_override_active_cliargs['user_preserve_home'] = True
snippet_result = p.get_snippet(user_override_active_cliargs)
self.assertFalse('userdel -r' in snippet_result)



class PulseExtensionTest(unittest.TestCase):
Expand Down

0 comments on commit 0f1c7d1

Please sign in to comment.