Created
August 26, 2010 19:07
-
-
Save eevans/551999 to your computer and use it in GitHub Desktop.
This file contains 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
from fabric.api import put, sudo | |
from os.path import join, basename | |
HOME = '/home' | |
SHELL = '/bin/bash' | |
SUDO_GROUP = 'sudo' | |
def adduser(username, sshpubkey=None, admin=False): | |
sudo('adduser --gecos \',,,\' --home %s --shell %s --disabled-login %s' % \ | |
(join(HOME, username), SHELL, username)) | |
# If a public key was given, copy it over. | |
if sshpubkey: copy_sshpubkey(username, sshpubkey) | |
# Give admins sudo access | |
if admin: sudo_access(username) | |
def copy_sshpubkey(username, sshpubkey): | |
path = '/home/%s/.ssh' % username | |
sudo('mkdir -p %s' % path) | |
src = join('/tmp', basename(sshpubkey)) | |
put(sshpubkey, join('/tmp', basename(sshpubkey))) | |
# Permissions / ownership | |
dst = join(path, 'authorized_keys') | |
sudo('mv %s %s' % (src, dst)) | |
sudo('chmod 700 %s' % path) | |
sudo('chmod 600 %s' % dst) | |
sudo('chown -R %s: %s' % (username, path)) | |
def sudo_access(username): | |
sudo('adduser %s %s' % (username, SUDO_GROUP)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment