#!/bin/bash GIT_PROJECT='' SSH_PUBLIC_KEY_STR='' GIT_USER='' if [[ ${SSH_PUBLIC_KEY_STR} == '' ]];then read -p "please input your ssh public key(not import key to authorized_keys if empty):" SSH_PUBLIC_KEY_STR fi if [[ ${GIT_USER} == '' ]];then read -p "please input your ssh public git user(default git):" GIT_USER if [[ ${GIT_USER} == '' ]];then GIT_USER='git' echo "git user is:"${GIT_USER}"" fi fi if [[ ${GIT_PROJECT} == '' ]];then read -p "please input your git repository(e.g test.git, git init --bare ./test.git),the bare repository will be init:" GIT_PROJECT if [[ ${GIT_PROJECT} == '' ]];then echo "repository can not be null" exit 1 fi fi yum install git -y >& /dev/null #create user if not exists egrep "^${GIT_USER}:" /etc/passwd >& /dev/null if [ $? -ne 0 ];then useradd ${GIT_USER} -s /usr/bin/git-shell fi mkdir -p /home/${GIT_USER} cd /home/${GIT_USER} && \ mkdir -p .ssh if [[ ${SSH_PUBLIC_KEY_STR} != '' ]];then #import ssh public key if key not in authorized_keys if [[ -f /home/${GIT_USER}/.ssh/authorized_keys ]];then cat /home/${GIT_USER}/.ssh/authorized_keys | grep "${SSH_PUBLIC_KEY_STR}" >& /dev/null if [ $? -eq 0 ];then echo "the key already in authorized_keys" SSH_PUBLIC_KEY_IN_FILE=true else echo "authorized_keys file exists,but the key not in authorized_keys" SSH_PUBLIC_KEY_IN_FILE=false fi else echo "authorized_keys not exists,will be create it and import key" SSH_PUBLIC_KEY_IN_FILE=false fi else echo "key empty and will not be import" fi #import key if [[ ${SSH_PUBLIC_KEY_IN_FILE} != true && ${SSH_PUBLIC_KEY_STR} != '' ]];then echo ${SSH_PUBLIC_KEY_STR} >> /home/${GIT_USER}/.ssh/authorized_keys chown ${GIT_USER}:${GIT_USER} /home/${GIT_USER}/.ssh/authorized_keys chmod 440 /home/${GIT_USER}/.ssh/authorized_keys echo "import key success" fi #create git repository if project not exists # if [[ -f /home/${GIT_USER}/${GIT_PROJECT} ]];then ls | grep ${GIT_PROJECT} >& /dev/null if [ $? -eq 0 ];then echo "git repository already exists" else git init --bare ./${GIT_PROJECT} && \ chmod -R 775 ./${GIT_PROJECT} && chown -R ${GIT_USER}:${GIT_USER} ./${GIT_PROJECT} echo "create git repository success" fi yourServerIp=`curl http://ip.sb` # echo "git repository addr is: ssh://${GIT_USER}@${yourServerIp}:22/home/${GIT_USER}/${GIT_PROJECT}" echo "git repository addr is: ssh://${GIT_USER}@127.0.0.1:sshPort/home/${GIT_USER}/${GIT_PROJECT}"