Skip to content

Commit

Permalink
[#20] Code refine for create_user and fix #21
Browse files Browse the repository at this point in the history
Also, using new style string formatter
  • Loading branch information
iblislin committed Sep 4, 2015
1 parent b45175b commit e001d3d
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions cloudbaseinit/osutils/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import subprocess
import os

from subprocess import CalledProcessError
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.osutils import base
from cloudbaseinit.utils.helper import isiterable
from subprocess import CalledProcessError


log = logging.getLogger(__name__)


class FreeBSDUtils(base.BaseOSUtils):
Expand All @@ -21,25 +26,34 @@ def user_exists(self, username):
return False
return True

def create_user(self, username, password, invite_group=None, password_expires=False):
"""
:param invite_group: it must be a list of string.
"""
home_dir = '/home/' + username
def create_user(self, username, password, invite_group=None,
password_expires=False):
'''
:param invite_group: a sequence of strings
:param password_expires: currently unfunctional
'''
home_dir = '/home/{}'.format(username)
user_shell = '/bin/tcsh'
user_comment = 'Created by bsdcloud-init'
grouplist = ''

assert not invite_group or isinstance(invite_group, list), "param invite_group must be a list."
assert invite_group, "invite_group cannot be empty."
for i in invite_group:
grouplist += i+','
grouplist = grouplist[:-1]

pw_cmd = "echo " + password + " | pw useradd -n " + username + " -c '" + user_comment + "' -d '" + home_dir + "' -s /bin/tcsh -h 0 -G " + grouplist
user_comment = 'Created by bsd-cloudinit'
invite_group = [] if invite_group is None else invite_group

if not isiterable(invite_group, exclude=str):
raise TypeError('invite_group')
grouplist = ','.join(invite_group)

pw_cmd = ("echo {password} | pw useradd -n {username} "
"-c '{user_comment}' -d '{home_dir}' "
"-s {user_shell} -h 0 {grouplist}").format(
password=password,
username=username,
user_comment=user_comment,
home_dir=home_dir,
user_shell=user_shell,
grouplist='-G {}'.format(grouplist) if grouplist else ''
)
subprocess.check_call(pw_cmd, shell=True)
subprocess.check_call("mkdir -p %s" % (home_dir), shell=True)
self.chown(username, username, home_dir)
subprocess.check_call('mkdir -p {}'.format(home_dir), shell=True)
self.chown(user=username, group=username, path=home_dir)

def set_host_name(self, new_host_name):
subprocess.check_call(['hostname', new_host_name])
Expand Down

0 comments on commit e001d3d

Please sign in to comment.