Skip to content

Instantly share code, notes, and snippets.

@stephenlauck
Created December 19, 2013 01:00
Show Gist options
  • Save stephenlauck/8032574 to your computer and use it in GitHub Desktop.
Save stephenlauck/8032574 to your computer and use it in GitHub Desktop.
Migrates cookbooks out of chef repo to own git repo org
#!/usr/bin/env ruby
require 'rubygems'
require 'chef'
require 'chef/knife/cookbook_metadata_from_file'
require 'chef/knife/cookbook_metadata'
require 'mixlib/shellout'
require 'json'
require 'pp'
require 'highline/import'
require 'octokit'
require 'mixlib/cli'
class MyCLI
include Mixlib::CLI
banner "cookbook_migrate (options)"
option :file,
:short => "-f FILE",
:long => "--file FILE",
:description => "File with list of cookbooks"
option :cookbook,
:short => "-c COOKBOOK",
:long => "--cookbook COOKBOOK",
:description => "Cookbook to migrate"
option :organization,
:short => "-o ORG",
:long => "--org ORG",
:description => "Github Organization to create new repo in"
option :username,
:short => "-u USERNAME",
:long => "--username USERNAME",
:description => "Github user name"
option :password,
:short => "-p PASSWORD",
:long => "--password PASSWORD",
:description => "Github user password"
option :remove,
:short => "-r",
:long => "--remove",
:description => "Remove from gitolite",
:boolean => true
option :help,
:short => "-h",
:long => "--help",
:description => "Show this message",
:on => :tail,
:boolean => true,
:show_options => true,
:exit => 0
end
cli = MyCLI.new
cli.parse_options
begin
cookbook = cli.config[:cookbook].nil? ? ask("Cookbook name: ") : cli.config[:cookbook]
org = cli.config[:organization].nil? ? ask("Github Org: "){ |q| q.default = "gap-cookbooks"} : cli.config[:organization]
username = cli.config[:username].nil? ? ask("Github username: "){ |q| q.default = ENV['USER']} : cli.config[:username]
pass = cli.config[:password].nil? ? ask("Github password: ") { |q| q.echo = false } : cli.config[:password]
cookbook_path = File.join(File.dirname(__FILE__), '../cookbooks/', cookbook)
unless File.exists?("#{cookbook_path}/metadata.json")
metadata_from_file = Chef::Knife::CookbookMetadataFromFile.new
metadata_from_file.name_args = ["#{cookbook_path}/metadata.rb"]
metadata_from_file.run
metadata = JSON.parse( IO.read("#{cookbook_path}/metadata.json") )
File.delete("#{cookbook_path}/metadata.json")
end
puts "Creating #{cookbook} cookbook in the Github #{org} for user #{username}"
Octokit.configure do |c|
c.api_endpoint = "http://github.gapinc.dev/api/v3/"
c.web_endpoint = 'http://github.gapinc.dev/'
end
client = Octokit::Client.new(:login => username, :password => pass)
client.create_repo(cookbook, { :organization => "#{org}"} )
log = Logger.new(STDOUT)
if client.repository?("#{org}/#{cookbook}")
# git checkout -b maximo_cookbook_migration
cmd = Mixlib::ShellOut.new("git checkout -b #{cookbook}_migration", :logger => log)
cmd.run_command
# remote add origin git@github.gapinc.dev:gap-cookbooks/gapNagios.git
cmd = Mixlib::ShellOut.new("git remote add github git@github.gapinc.dev:#{org}/#{cookbook}.git", :logger => log)
cmd.run_command
# git filter-branch --prune-empty --subdirectory-filter cookbooks/maximoApp maximo_cookbook_migration
cmd = Mixlib::ShellOut.new("git filter-branch -f --prune-empty --subdirectory-filter cookbooks/#{cookbook} #{cookbook}_migration", :logger => log)
cmd.run_command
# git push -u github maximo_cookbook_migration:master
cmd = Mixlib::ShellOut.new("git push -u github #{cookbook}_migration:master", :logger => log)
cmd.run_command
# git checkout master
cmd = Mixlib::ShellOut.new("git checkout master", :logger => log)
cmd.run_command
# git branch -D maximo_cookbook_migration
cmd = Mixlib::ShellOut.new("git branch -D #{cookbook}_migration", :logger => log)
cmd.run_command
# git remote remove github
cmd = Mixlib::ShellOut.new("git remote rm github", :logger => log)
cmd.run_command
end
# ask to remove cookbook from gitolite
if cli.config[:remove] ? cli.config[:remove] : agree("Remove from gitolite? (cookbook still exists in gitolite history) [y/n]")
# git checkout master
cmd = Mixlib::ShellOut.new("git rm -r #{cookbook_path}", :logger => log)
cmd.run_command
# git checkout master
cmd = Mixlib::ShellOut.new("git commit #{cookbook_path} -m \"Migrate #{cookbook} to http://github.gapinc.dev/#{org}/#{cookbook}\"", :logger => log)
cmd.run_command
# git checkout master
cmd = Mixlib::ShellOut.new("git push origin master", :logger => log)
cmd.run_command
else
say("Leaving #{cookbook} in gitolite. <%= color('beware of changing code in two places', :red, BOLD) %>")
end
rescue Exception => e
puts e.message
say("<%= color('There was an error! Please contact mugdha_pandit@gap.com or stephen_lauck@gap.com with the error message.', :red, BOLD) %>")
end
# fork http://github.gapinc.dev/enterprise-management/cookbooklist into your github account
# add cookbook to Berksfile and commit
# push back up to fork on your account
# create pull request on your fork and submit back to master; wait for approval
# check build on jenkins job at http://chefci.phx.gapinc.dev/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment