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.
396 lines
12 KiB
396 lines
12 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
|
|
|
|
|
|
cat > ~/$projectLocation/mirrormanagement/$group/create_group.sh <<'EOF'
|
|
#!/bin/bash
|
|
|
|
#bash option stop on first error
|
|
set -e
|
|
|
|
#Include all user options and dependencies
|
|
git_mirrors_dir="${0%/*}"
|
|
source ${git_mirrors_dir}/includes.sh
|
|
|
|
#check if api version is set
|
|
[ -z $gitlab_api_version ] && gitlab_api_version=4
|
|
|
|
#export env vars for python script
|
|
export gitlab_user_token_secret gitlab_url gitlab_namespace gitlab_user ssl_verify gitlab_api_version
|
|
|
|
PROGNAME="${0##*/}"
|
|
PROGVERSION="${VERSION}"
|
|
|
|
#Default script options
|
|
svn=false
|
|
git=false
|
|
bzr=false
|
|
hg=false
|
|
project_name=""
|
|
mirror=""
|
|
force=false
|
|
no_create_set="${no_create_set:-false}"
|
|
no_remote_set="${no_remote_set:-false}"
|
|
http_remote="${http_remote:-false}"
|
|
|
|
python ./lib/manage_gitlab_project.py --creategroup ${gitlab_namespace}
|
|
EOF
|
|
chmod u+x ~/$projectLocation/mirrormanagement/$group/create_group.sh
|
|
|
|
cat > ~/$projectLocation/mirrormanagement/$group/lib/manage_gitlab_project.py <<'EOF'
|
|
#!/usr/bin/env python
|
|
#Created by Sam Gleske
|
|
#MIT License
|
|
#Created Tue Sep 10 23:01:08 EDT 2013
|
|
|
|
from sys import argv,exit,stderr
|
|
from optparse import OptionParser
|
|
import os
|
|
try:
|
|
import gitlab
|
|
except ImportError:
|
|
raise ImportError("python-gitlab module is not installed. You probably didn't read the install instructions closely enough. See docs/prerequisites.md.")
|
|
|
|
|
|
|
|
try:
|
|
token_secret=os.environ['gitlab_user_token_secret']
|
|
gitlab_url=os.environ['gitlab_url']
|
|
gitlab_namespace=os.environ['gitlab_namespace']
|
|
gitlab_user=os.environ['gitlab_user']
|
|
ssl_verify=os.environ['ssl_verify']
|
|
gitlab_api_version=os.environ['gitlab_api_version']
|
|
except KeyError:
|
|
print >> stderr, "Environment config missing. Do not run this script standalone."
|
|
exit(1)
|
|
parser = OptionParser()
|
|
parser.add_option("--creategroup",dest="creategroup",action="store_true",default=False)
|
|
parser.add_option("--issues",dest="issues",action="store_true",default=False)
|
|
parser.add_option("--wall",dest="wall",action="store_true",default=False)
|
|
parser.add_option("--merge",dest="merge",action="store_true",default=False)
|
|
parser.add_option("--wiki",dest="wiki",action="store_true",default=False)
|
|
parser.add_option("--snippets",dest="snippets",action="store_true",default=False)
|
|
parser.add_option("--public",dest="public",action="store_true",default=False)
|
|
parser.add_option("--create",dest="create",action="store_true",default=False)
|
|
parser.add_option("--delete",dest="delete",action="store_true",default=False)
|
|
parser.add_option("--desc",dest="desc",metavar="DESC",default=False)
|
|
parser.add_option("--http",dest="http",action="store_true",default=False)
|
|
(options,args) = parser.parse_args()
|
|
if len(args) == 0:
|
|
print >> stderr, "No project name specified. Do not run this script standalone."
|
|
exit(1)
|
|
elif len(args) > 1:
|
|
print >> stderr, "Too many arguments. Do not run this script standalone."
|
|
exit(1)
|
|
|
|
project_name=args[0]
|
|
|
|
if not eval(ssl_verify.capitalize()):
|
|
git=gitlab.Gitlab(gitlab_url,token_secret,ssl_verify=False,api_version=gitlab_api_version)
|
|
else:
|
|
git=gitlab.Gitlab(gitlab_url,token_secret,ssl_verify=True,api_version=gitlab_api_version)
|
|
|
|
def find_group(**kwargs):
|
|
groups = git.groups.list()
|
|
return _find_matches(groups, kwargs, False)
|
|
|
|
def find_project(**kwargs):
|
|
projects = git.projects.list(as_list=True)
|
|
return _find_matches(projects, kwargs, False)
|
|
|
|
def _find_matches(objects, kwargs, find_all):
|
|
"""Helper function for _add_find_fn. Find objects whose properties
|
|
match all key, value pairs in kwargs.
|
|
Source: https://github.com/doctormo/python-gitlab3/blob/master/gitlab3/__init__.py
|
|
"""
|
|
ret = []
|
|
for obj in objects:
|
|
match = True
|
|
# Match all supplied parameters
|
|
for param, val in kwargs.items():
|
|
if not getattr(obj, param) == val:
|
|
match = False
|
|
break
|
|
if match:
|
|
if find_all:
|
|
ret.append(obj)
|
|
else:
|
|
return obj
|
|
if not find_all:
|
|
return None
|
|
return ret
|
|
|
|
# transfer the project from the source namespace to the specified group namespace
|
|
def transfer_project(src_project, group):
|
|
value = group.transfer_project(src_project.id)
|
|
dest_project = find_project(name=src_project.name)
|
|
return dest_project
|
|
|
|
def createproject(pname):
|
|
if len(options.desc) == 0:
|
|
if options.public:
|
|
description="Public mirror of %s." % project_name
|
|
else:
|
|
description="Git mirror of %s." % project_name
|
|
else:
|
|
description=options.desc
|
|
project_options={
|
|
'issues_enabled': options.issues,
|
|
'wall_enabled': options.wall,
|
|
'merge_requests_enabled': options.merge,
|
|
'wiki_enabled': options.wiki,
|
|
'snippets_enabled': options.snippets,
|
|
'public': options.public,
|
|
'namespace_id': find_group(name=gitlab_namespace).id,
|
|
}
|
|
#make all project options lowercase boolean strings i.e. true instead of True
|
|
for x in project_options.keys():
|
|
project_options[x] = str(project_options[x]).lower()
|
|
print >> stderr, "Creating new project %s" % pname
|
|
project_options['name'] = pname
|
|
project_options['description'] = description
|
|
git.projects.create(project_options)
|
|
found_project = find_project(name=pname)
|
|
if needs_transfer(gitlab_user, gitlab_namespace, found_project):
|
|
found_project = transfer_project(found_project, found_group)
|
|
return found_project
|
|
|
|
# returns a Bool True if the transfer is required
|
|
def needs_transfer(user, groupname, project):
|
|
namespace = False
|
|
if groupname:
|
|
namespace = groupname
|
|
else:
|
|
namespace = user
|
|
if type(project.namespace) == gitlab.v4.objects.Group:
|
|
return project.namespace.name != namespace
|
|
else:
|
|
return project.namespace['name'] != namespace
|
|
|
|
if options.create:
|
|
found_group = find_group(name=gitlab_namespace)
|
|
found_project = None
|
|
|
|
found_project= find_project(name=project_name)
|
|
#exit()
|
|
|
|
if found_project:
|
|
if needs_transfer(gitlab_user, gitlab_namespace, found_project):
|
|
found_project = transfer_project(found_project, found_group)
|
|
if not found_project:
|
|
print >> stderr, "There was a problem transferring {group}/{project}. Did you give {user} user Admin rights in gitlab?".format(group=gitlab_namespace,project=project_name,user=gitlab_user)
|
|
exit(1)
|
|
else:
|
|
found_project=createproject(project_name)
|
|
if not found_project:
|
|
print >> stderr, "There was a problem creating {group}/{project}. Did you give {user} user Admin rights in gitlab?".format(group=gitlab_namespace,project=project_name,user=gitlab_user)
|
|
exit(1)
|
|
if options.http:
|
|
print found_project.http_url_to_repo
|
|
else:
|
|
print found_project.ssh_url_to_repo
|
|
elif options.delete:
|
|
try:
|
|
deleted_project=find_project(name=project_name).delete()
|
|
except Exception as e:
|
|
print >> stderr, e
|
|
exit(1)
|
|
|
|
elif options.creategroup:
|
|
found_group = find_group(name=gitlab_namespace)
|
|
|
|
if found_group:
|
|
print >> stderr, "There is already a group named {group}.".format(group=gitlab_namespace)
|
|
exit(1)
|
|
|
|
else:
|
|
create_group=git.groups.create({'name': gitlab_namespace, 'path': gitlab_namespace})
|
|
print >> stderr, "{group} has been created.".format(group=gitlab_namespace)
|
|
|
|
else:
|
|
print >> stderr, "No --create or --delete option added."
|
|
exit(1)
|
|
|
|
EOF
|
|
|
|
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
|
|
(cd ~/$projectLocation/mirrormanagement/$group && git pull)
|
|
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
|
|
|
|
|
|
|
|
# Generate creategroup script.
|
|
cat > creategroups <<'EOF'
|
|
|
|
# Source config file
|
|
. ./setup.conf
|
|
|
|
for group in "${groupArray[@]}"; do
|
|
echo "Creating all groups"
|
|
(cd ~/$projectLocation/mirrormanagement/$group && ./create_group.sh)
|
|
done
|
|
|
|
EOF
|
|
chmod u+x creategroups
|
|
|
|
|
|
echo ""
|
|
echo "Please put this SSH key into your gitlab mirror user."
|
|
echo ""
|
|
echo "SSH Key:"
|
|
cat ~/.ssh/id_rsa.pub |