Created
March 16, 2017 15:06
-
-
Save acidprime/93cc194a878b2a482c618f9fbd087294 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'r10k/puppetfile' | |
require 'gitlab' | |
GITLAB_TRUE = 1 | |
GITLAB_FALSE = 0 | |
# this is just to mirror what we had on the internetz internally | |
task :mirror do | |
gitlab_token = '<REDACTED>' | |
gitlab_host = 'gitlab.example.com' | |
gitlab_server = "https://#{gitlab_host}/api/v3" | |
gitlab_group = 'puppet' | |
proxy_host = 'http://proxy.example.com:8080' | |
# Start an API session | |
client = Gitlab.client( :endpoint => gitlab_server, | |
:private_token => gitlab_token) | |
# Extract the group id from the group name | |
group_id = client.groups.map do |group| | |
group.id if group.name == gitlab_group | |
end.compact.first.inspect | |
cputs "Found gitlab id #{group_id} for group #{gitlab_group}" | |
# Environmental vars for our shell commands | |
proxy_hash = { | |
"http_proxy" => proxy_host, | |
"https_proxy" => proxy_host | |
} | |
cputs "Loading Puppetfile remotes..." | |
# Have r10k parse the Puppetfile for us | |
puppetfile = R10K::Puppetfile.new(Dir.getwd,Dir.getwd,'Puppetfile') | |
puppetfile.load | |
remote_sub_hash = {} | |
# Enum the modules listed in the puppetfile | |
puppetfile.modules.each do |git_repo| | |
# Parsed values from r10k | |
remote = git_repo.working_dir.remote | |
repo_title = File.basename(remote) | |
unless remote.match /.*(#{gitlab_host}).*/ | |
if File.directory?("/tmp/#{repo_title}") | |
cputs "/tmp/#{repo_title} already exists" | |
next | |
end | |
cputs "Cloning the bare repo..." | |
system(proxy_hash,"git clone --bare #{remote} /tmp/#{repo_title}") | |
cputs "Creating the new repo ( project ) in gitlab..." | |
project = client.create_project( repo_title.chomp(".git"), | |
:description => "Forked from #{remote}", | |
:namespace_id => group_id, | |
:public => GITLAB_TRUE) | |
cputs "Created new project #{repo_title.chomp(".git")} with id #{project.id}" | |
# Build a substituion hash for the Puppetfile | |
remote_sub_hash[remote] = project.http_url_to_repo | |
cputs "Mirror the local repo to the new remote" | |
git_hash = { 'GIT_DIR' => "/tmp/#{repo_title}" } | |
gitlab_remote = "git@#{gitlab_host}:#{gitlab_group}/#{repo_title}" | |
system( git_hash, | |
"git push --mirror #{gitlab_remote}") | |
# Update remotes for good house keeping | |
system( git_hash, | |
"git remote rename origin upstream" ) | |
# Add new remote | |
system( git_hash, | |
"git remote add origin #{gitlab_remote}" ) | |
cputs "Create the source branch to merge upstream changes..." | |
#(assume master is the default currently) | |
system( git_hash, | |
"git push origin master:source" ) | |
else | |
# Catch all our URLs that are already internal | |
remote_sub_hash[remote] = remote | |
end | |
end | |
cputs "Possible changes to your Puppetfile:" | |
puts remote_sub_hash.inspect | |
puts File.read('Puppetfile').gsub(/http(|s)\:\/\/([A-Za-z0-9.]+)(:|\/).*(\.git)/,remote_sub_hash) | |
end | |
def cputs(string) | |
puts "\033[1m#{string}\033[0m" | |
end | |
def cprint(string) | |
print "\033[1m#{string}\033[0m" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment