Skip to content

Instantly share code, notes, and snippets.

@pthiers
Created April 25, 2017 19:27
Show Gist options
  • Save pthiers/197379bac54e15065ef088a348de685f to your computer and use it in GitHub Desktop.
Save pthiers/197379bac54e15065ef088a348de685f to your computer and use it in GitHub Desktop.
Fabric migration git bare folder to gitlab
from fabric.api import env, run, cd, local, sudo
from fabric.contrib import files
from fabric.context_managers import lcd
import gitlab
gitlab_key = 'key'
gitlab_host = 'http://192.168.x.x'
env.hosts = ["192.168.x.x"]
env.user = "usuario"
env.key_filename = '/home/usuario/.ssh/id_rsa.pub'
def migrate():
gl = gitlab.Gitlab(gitlab_host,gitlab_key)
projects = gl.projects.owned()
for project in projects:
project.delete()
path = '/opt/git/'
with cd(path):
repos = list_dir()
repos = clean(repos)
with lcd('git'):
path = local('pwd')
print path
local('rm -rf *')
reposName = []
for repo in repos:
local("git clone --mirror administrador@192.168.18.10:%s" % repo)
arrRepo = repo.split('/')
reposName.append(arrRepo[len(arrRepo)-1].replace('.git',''))
for repo in reposName:
project = gl.projects.create({'name': repo})
gitRepo = repo +'.git'
with lcd(gitRepo):
local('git remote add gitlab ssh://git@192.168.x.x/usuario/'+gitRepo)
local('git push gitlab --mirror')
def list_dir(dir_=None):
"""returns a list of files in a directory (dir_) as absolute paths"""
dir_ = dir_ or env.cwd
string_ = run("for i in %s*; do echo $i; done" % dir_)
print string_
files = string_.replace("\r", "").split("\n")
# print files
return files
def clean(arr):
"""
:type arr: str array
:param arr:
:return: []
"""
response = []
for element in arr:
if element.endswith('.git'):
response.append(element)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment