Created
July 28, 2010 06:15
-
-
Save cdwilson/493563 to your computer and use it in GitHub Desktop.
This script creates a git group and user account under Mac OS X for gitosis
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
#!/bin/bash | |
# This script creates a git group and user account under Mac OS X | |
# Assumes git was installed via macports (i.e. /opt/local) | |
USERNAME="git" | |
GROUPNAME="$USERNAME" | |
REALNAME="git version control" | |
PASSWORD="*" | |
if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi | |
# make sure the user name is unique | |
unset -v new_user | |
new_user="$(dscl . -search /Users name "$USERNAME")" | |
if [[ -z "$new_user" ]]; then | |
new_user="$USERNAME" | |
else | |
echo "User name already exists: $USERNAME" | |
exit 1 | |
fi | |
# make sure the user's primary group name is unique | |
unset -v new_group | |
new_group="$(dscl . -search /Groups name "$GROUPNAME")" | |
if [[ -z "$new_group" ]]; then | |
new_group="$GROUPNAME" | |
else | |
echo "Group name already exists: $GROUPNAME" | |
exit 1 | |
fi | |
# make sure there is no (file or) home directory of the same name already | |
if [[ -e "/Users/$USERNAME" ]]; then | |
echo "\nUser $USERNAME already exists at /Users/$USERNAME" | |
exit 1 | |
fi | |
# get unique id numbers (uid, gid) that are greater than 500 | |
unset -v i new_uid new_gid idvar | |
declare -i new_uid=0 new_gid=0 i=500 idvar=0 | |
while [[ $idvar -eq 0 ]]; do | |
i=$[i+1] | |
if [[ -z "$(dscl . -search /Users uid $i)" ]] && [[ -z "$(dscl . -search /Groups gid $i)" ]]; then | |
new_uid=$i | |
new_gid=$i | |
idvar=1 | |
#break | |
fi | |
done | |
if [[ $new_uid -eq 0 ]] || [[ $new_gid -eq 0 ]]; then echo 'Getting unique id numbers (uid, gid) failed!'; exit 1; fi | |
# Create the group | |
echo "creating group $new_gid: $GROUPNAME" | |
dscl . -create /Groups/$GROUPNAME | |
dscl . -append /Groups/$GROUPNAME gid "$new_gid" | |
# Create the user account | |
echo "creating user $new_uid: $USERNAME ($REALNAME)" | |
dscl . -create /Users/$USERNAME | |
dscl . -append /Users/$USERNAME UserShell /bin/bash | |
dscl . -append /Users/$USERNAME RealName "$REALNAME" | |
dscl . -append /Users/$USERNAME UniqueID "$new_uid" | |
dscl . -append /Users/$USERNAME PrimaryGroupID "$new_gid" | |
dscl . -append /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME | |
dscl . -passwd /Users/$USERNAME "$PASSWORD" | |
# Create the home directory | |
createhomedir -c | |
# Create .bashrc | |
cat > /Users/$USERNAME/.bashrc << EOF | |
if [ -x /usr/libexec/path_helper ]; then | |
eval `/usr/libexec/path_helper -s` | |
fi | |
export PATH="/opt/local/bin:/opt/local/sbin:$PATH" | |
export PATH=$(git --exec-path):$PATH | |
EOF | |
# Grant the user ssh access | |
echo "granting $USERNAME ssh access" | |
dscl . -append /Groups/com.apple.access_ssh GroupMembership $USERNAME | |
# Add _www to git group so web server can access git user's content | |
dscl . -append /Groups/git GroupMembership _www | |
echo "User: $USERNAME and Group: $GROUPNAME created." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment