Added group creation scripts

master
Facinorous 4 years ago
parent 2f3b17f72c
commit 83baa30495

@ -1,171 +0,0 @@
#!/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.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}"})
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)

249
setup

@ -3,6 +3,8 @@
# 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
@ -19,20 +21,241 @@ sed -i "60s/.*/wiki_enabled=\"${wiki_enabled}\"/" ~/$projectLocation/mirrormanag
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
@ -53,6 +276,8 @@ echo "All mirror group's cron jobs have been set up, if they haven't been alread
EOF
chmod u+x setcron
# Generate gitlab-mirror dependancy updater script.
cat > update <<'EOF'
#!/bin/bash
@ -80,6 +305,8 @@ echo "All mirror group's gitlab-mirror dependancies have been updated."
EOF
chmod u+x update
# Generate mirror script.
cat > mirror <<'EOF'
#!/bin/bash
@ -145,11 +372,27 @@ fi
EOF
chmod u+x mirror
# CREATE A SCRIPT THAT WILL CREATE GROUPS IF NEEDED
# Generate creategroup script.
cat > creategroup <<EOF
for group in "${groupArray[@]}"; do
echo "Creating all groups"
~/$projectLocation/mirrormanagement/$group/create_group.sh
done
EOF
done
EOF
chmod u+x creategroup
echo ""
echo "Please put this SSH key into your gitlab mirror user."
echo ""
echo "SSH Key:"
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub
Loading…
Cancel
Save