Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #111 from shuhaowu/create-user-with-custom-shell
Browse files Browse the repository at this point in the history
Create user with a custom shell
  • Loading branch information
robvdl committed Mar 10, 2018
2 parents 0f347ef + 169128a commit 86e6c7d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions lxdock/conf/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_schema():
Required('name'): All(str, Length(max=32)),
'home': str,
'password': str,
'shell': str,
}],
}

Expand Down
8 changes: 4 additions & 4 deletions lxdock/container.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import logging
import os
import shlex
Expand Down Expand Up @@ -402,10 +403,9 @@ def _setup_users(self):

logger.info('Ensuring users are created...')
for user_config in users:
name = user_config.get('name')
home = user_config.get('home')
password = user_config.get('password')
self._guest.create_user(name, home=home, password=password)
config = copy.copy(user_config)
name = config.pop('name')
self._guest.create_user(name, **config)

def _unsetup_hostnames(self):
""" Removes the configuration associated with the hostnames of the container. """
Expand Down
4 changes: 3 additions & 1 deletion lxdock/guests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ def add_ssh_pubkey_to_root_authorized_keys(self, pubkey):
self.run(['mkdir', '-p', '/root/.ssh'])
self.lxd_container.files.put('/root/.ssh/authorized_keys', pubkey)

def create_user(self, username, home=None, password=None):
def create_user(self, username, home=None, password=None, shell=None):
""" Adds the passed user to the container system. """
options = ['--create-home', ]
if home is not None:
options += ['--home-dir', home, ]
if password is not None:
options += ['-p', password, ]
if shell is not None:
options += ['-s', shell, ]
self.run(['useradd', ] + options + [username, ])

########################################################
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/guests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class DummyGuest(Guest):
assert guest.lxd_container.execute.call_args[0] == \
(['useradd', '--create-home', '-p', password, 'usertest'], )

def test_can_create_a_user_with_a_custom_shell(self):
class DummyGuest(Guest):
name = 'dummy'
guest = DummyGuest(FakeContainer())
shell = "/bin/zsh"
guest.create_user('usertest', shell=shell)
assert guest.lxd_container.execute.call_count == 1
assert guest.lxd_container.execute.call_args[0] == \
(['useradd', '--create-home', '-s', shell, 'usertest'], )

def test_can_copy_file(self):
class DummyGuest(Guest):
name = 'dummy'
Expand Down

0 comments on commit 86e6c7d

Please sign in to comment.