Created
November 11, 2009 21:23
-
-
Save bitprophet/232311 to your computer and use it in GitHub Desktop.
1st-draft quality Fabric user adding task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_user(name=None, password=None, admin='no', apache='no', skip='yes'): | |
""" | |
Add a new user named ``name`` to the remote system. | |
If ``name`` or ``password`` are unspecified, you will be prompted for them. | |
To have a user added to the admin/wheel group, specify ``admin=yes``. | |
To add the user to the appropriate Apache group, specify ``apache=yes``. | |
To force recreation / re-passwd'ing of user even if it already exists, | |
specify ``skip=no``. | |
""" | |
apache_user = 'www-data' if is_debian() else 'apache' | |
# TODO: make this sort of crap work better | |
usermod = '/usr/sbin/usermod ' | |
# Get name | |
if not name: | |
name = prompt("New user's username: ") | |
# Check for user, short circuit if necessary | |
if user_exists(name) and skip.lower() in ['y', 'yes']: | |
print("User %s already exists, skipping" % name) | |
return | |
# Add user | |
with settings(warn_only=True): | |
sudo('/usr/sbin/useradd -m %s -s /bin/bash' % name) | |
# Set password if necessary | |
while not password: | |
password = getpass.getpass("New user's password: ") | |
password_confirmation = getpass.getpass("Again: ") | |
if password != password_confirmation: | |
print("Sorry, passwords do not match.") | |
password = None | |
# Since we're getpass-ing, it'd be silly to print out the password here. | |
# So hide the running line. No, this is still very insecure on the remote end :( | |
with hide('running'): | |
sudo('echo "%s\n%s" | /usr/bin/passwd %s' % (password, password, name)) | |
# Handle Apache if necessary | |
if apache.lower() in ['yes', 'y']: | |
# Make apache group their login group (for ease of collaboration) | |
sudo(usermod + '-g %s %s' % (apache_user, name)) | |
# Add them back to their own personal group | |
sudo(usermod + '-a -G %s %s' % (name, name)) | |
# If admin, add to the admin group | |
if admin.lower() in ['yes', 'y']: | |
if is_really_debian(): | |
admin_name = 'sudo' | |
elif is_debian(): | |
admin_name = 'admin' | |
else: | |
admin_name = 'wheel' | |
sudo(usermod + '-a -G %s %s' % (admin_name, name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment