commit 1c36acbae79d3a48fd6fb010e9196704e947f700 Author: Facinorous Date: Wed Nov 4 17:31:40 2020 -0500 Initial Push. Project still WIP. Don't use this shit. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7297eb1 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +1. Create a user in your gitlab named gitmirror +2. Create a new user on your linux system. +3. Clone this repo into your new users home directory ~ +4. Edit setup.conf to your desires +5. Run ./setup to behin the set up process +6. Copy ssh key shown in terminal at the end of setup script into your gitlab users account +7. Run ./creategroups to make all the groups in gitlab +8. Run ./mirror to set up a mirror + +Write script to create cron jobs for auto mirrors + +Use ./update to update your gitlab-mirror dependancy diff --git a/creategroups b/creategroups new file mode 100644 index 0000000..948b5af --- /dev/null +++ b/creategroups @@ -0,0 +1,6 @@ +#!/bin/bash + +# Source config file +. ./setup.conf + +# Update the gitmirrors repo to latest version. diff --git a/removemelater/manage_gitlab_project.py b/removemelater/manage_gitlab_project.py new file mode 100644 index 0000000..2b639f7 --- /dev/null +++ b/removemelater/manage_gitlab_project.py @@ -0,0 +1,175 @@ +#!/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() +#NEW CODE +parser.add_option("--creategroup",dest="creategroup",action="store_true",default=False) +#END OF NEW CODE +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 + + #NEW CODE +if 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=gl.groups.create({'name': f"{gitlab_namespace}", 'path': f"{gitlab_namespace}"}) + #END OF NEWCODD + +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) +else: + print >> stderr, "No --create or --delete option added." + exit(1) + diff --git a/setup b/setup new file mode 100644 index 0000000..aecd2a2 --- /dev/null +++ b/setup @@ -0,0 +1,106 @@ +#!/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 ./mirrormanagement/$group +sed "37s/.*/gitlab_namespace=\"${group}\"/" ./mirrormanagement/$group/config.sh.SAMPLE > ./mirrormanagement/$group/config.sh +sed -i "39s/.*/gitlab_url=\"${gitlabURL}\"/" ./mirrormanagement/$group/config.sh +sed -i "42s/.*/gitlab_user=\"${gitlabUser}\"/" ./mirrormanagement/$group/config.sh +sed -i "50s/.*/http_remote=true/" ./mirrormanagement/$group/config.sh +sed -i "58s/.*/issues_enabled=\"${issues_enabled}\"/" ./mirrormanagement/$group/config.sh +sed -i "59s/.*/wall_enabled=\"${wall_enabled}\"/" ./mirrormanagement/$group/config.sh +sed -i "60s/.*/wiki_enabled=\"${wiki_enabled}\"/" ./mirrormanagement/$group/config.sh +sed -i "61s/.*/snippets_enabled=\"${snippets_enabled}\"/" ./mirrormanagement/$group/config.sh +sed -i "62s/.*/gitlab_user=\"${merge_requests_enabled}\"/" ./mirrormanagement/$group/config.sh +sed -i "63s/.*/public=\"${public}\"/" ./mirrormanagement/$group/config.sh +rm ./mirrormanagement/$group/config.sh.SAMPLE +done + +# Generate an ssh key +echo -e "\n\n\n" | ssh-keygen -t rsa + +# Generate ssh server config +mkdir ./.ssh +cat > ./.ssh/config < ./private_token + +# 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 TODO: make this into proper for loop that works with user variables. +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" + ./mirrormanagement/$group/gitlab-mirrors/add_mirror.sh --git --project-name ${NAME} --mirror ${REPO} +fi +done + +fi +EOF +chmod u+x mirror + +echo "" +echo "Please put this SSH key into your gitlab mirror user." +echo "" +echo "SSH Key:" +cat ./ssh/id_rsa.pub + diff --git a/setup.conf b/setup.conf new file mode 100644 index 0000000..c1260fb --- /dev/null +++ b/setup.conf @@ -0,0 +1,32 @@ +## USER VARIABLES ## + +# Your gitlab information +gitlabURL=https://git.example.com +gitlabUser=gitmirror +gitlabToken=REPLACE_ME_WITH_GITLAB_PRIVATE_TOKEN_OF_MIRROR_ACCOUNT + +# Groups repos csn be mirrored into on your gitlab instance +groupArray=(test1) + +# +# Gitlab new project default settings. If a project needs to be created by +# gitlab-mirrors then it will assign the following values as defaults. +# + +#values must be true or false +issues_enabled=false +wall_enabled=false +wiki_enabled=false +snippets_enabled=false +merge_requests_enabled=false +public=false + +#################### + + +## SYSTEM VARIABLES ## + +groupCount=${#groupArray[@]} +gitmirrorsRepo=https://github.com/samrocketman/gitlab-mirrors.git + +#################### \ No newline at end of file diff --git a/update b/update new file mode 100644 index 0000000..948b5af --- /dev/null +++ b/update @@ -0,0 +1,6 @@ +#!/bin/bash + +# Source config file +. ./setup.conf + +# Update the gitmirrors repo to latest version.