You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.2 KiB

#!/bin/bash
# Source config file
. ./setup.conf
# Clone latest gitmirrors repo into local project folder
# and move it into correct locstion for project.
for group in "${groupArray[@]}"; do
git clone $gitmirrorsRepo ~/$projectLocation/mirrormanagement/$group
chmod 755 ~/$projectLocation/mirrormanagement/$group/*.sh
sed "10s,.*,user_home=\"/home/${USER}/gitmirrors-bs\"," ~/$projectLocation/mirrormanagement/$group/config.sh.SAMPLE > ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "37s/.*/gitlab_namespace=\"${group}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "39s,.*,gitlab_url=\"${gitlabURL}\"," ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "42s/.*/gitlab_user=\"${gitlabUser}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "50s/.*/http_remote=true/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "58s/.*/issues_enabled=\"${issues_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "59s/.*/wall_enabled=\"${wall_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "60s/.*/wiki_enabled=\"${wiki_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "61s/.*/snippets_enabled=\"${snippets_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "62s/.*/merge_requests_enabled=\"${merge_requests_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "63s/.*/public=\"${public}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
done
# Generate a ssh key
echo -e "\n\n\n" | ssh-keygen -t rsa
# Generate ssh server config
cat > ~/.ssh/config <<EOF
Host $gitlabURL
User git
EOF
# Generate private token file used by gitlab-mirrors
echo "$gitlabToken" > ./private_token
# Generate cron job creation script.
cat > setcron <<EOF
#!/bin/bash
# Source config file
. ./setup.conf
for group in "${groupArray[@]}"; do
if [[ $(crontab -l | egrep -v "^(#|$)" | grep -q ''"$group"'/git-mirrors.sh'; echo $?) == 1 ]]
then
set -f
printf "$(crontab -l ; echo '@hourly ~/'"$projectLocation"'/projectmanagement/'"$group"'/git-mirrors.sh')\n" | crontab -
set +f
fi
done
echo "All mirror group's cron jobs have been set up, if they haven't been already."
EOF
chmod u+x setcron
# Generate gitlab-mirror dependancy updater script.
cat > update <<EOF
#!/bin/bash
# Source config file
. ./setup.conf
for group in "${groupArray[@]}"; do
git pull $gitmirrorsRepo ~/$projectLocation/mirrormanagement/$group
chmod 755 ~/$projectLocation/mirrormanagement/$group/*.sh
sed "10s,.*,user_home=\"/home/${USER}/gitmirrors-bs\"," ~/$projectLocation/mirrormanagement/$group/config.sh.SAMPLE > ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "37s/.*/gitlab_namespace=\"${group}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "39s,.*,gitlab_url=\"${gitlabURL}\"," ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "42s/.*/gitlab_user=\"${gitlabUser}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "50s/.*/http_remote=true/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "58s/.*/issues_enabled=\"${issues_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "59s/.*/wall_enabled=\"${wall_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "60s/.*/wiki_enabled=\"${wiki_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "61s/.*/snippets_enabled=\"${snippets_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "62s/.*/merge_requests_enabled=\"${merge_requests_enabled}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
sed -i "63s/.*/public=\"${public}\"/" ~/$projectLocation/mirrormanagement/$group/config.sh
done
echo "All mirror group's gitlab-mirror dependancies have been updated."
EOF
chmod u+x update
# Generate mirror script.
cat > mirror <<'EOF'
#!/bin/bash
# Source config file
. ./setup.conf
# Setting the arguments for the bash script
while getopts g:n:r: option
do
case "${option}"
in
g) GROUP=${OPTARG};;
n) NAME=${OPTARG};;
r) REPO=${OPTARG};;
esac
done
# Test to make sure all arguments are specified by the user
if [ -z "${GROUP}" ]
then
echo "No Gitlab group was specified"
exit
fi
if [ -z "${NAME}" ]
then
echo "No Gitlab repo name was specified"
exit
fi
if [ -z "${REPO}" ]
then
echo "No Github source repo was specified"
exit
fi
# Testing to make sure the group specified is set in the variables
wronggroup=0
for group in "${groupArray[@]}"; do
if [[ ${GROUP} != "$group" ]]
then
((wronggroup=wronggroup+1))
fi
done
# Main switch script, depending on which group is specified it runs the add mirror in that location
if (( "$wronggroup" > ((groupCount-1)) ))
then
echo "The Gitlab group you specified does not exist, please make sure you select a group in the groups array of this script."
exit
else
for group in "${groupArray[@]}"; do
if [[ ${GROUP} = "$group" ]]
then
echo "Mirroring repo ${REPO} into Gitlab group $group"
~/$projectLocation/mirrormanagement/$group/add_mirror.sh --git --project-name ${NAME} --mirror ${REPO}
fi
done
fi
EOF
chmod u+x mirror
# CREATE A SCRIPT THAT WILL CREATE GROUPS IF NEEDED
echo ""
echo "Please put this SSH key into your gitlab mirror user."
echo ""
echo "SSH Key:"
cat ~/.ssh/id_rsa.pub