class KnifeSpork::Plugins::Git
Public Instance Methods
after_bump()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 169 def after_bump cookbooks.each do |cookbook| git_add(cookbook_path_for(cookbook),"metadata.rb") end end
after_environmentcreate()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 72 def after_environmentcreate if config.auto_push save_environment(object_name) unless object_difference == '' end end
after_environmentdelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 96 def after_environmentdelete if config.auto_push delete_environment(object_name) end end
after_environmentedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 86 def after_environmentedit if config.auto_push save_environment(object_name) unless object_difference == '' end end
after_nodecreate()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 112 def after_nodecreate if config.auto_push save_node(object_name) unless object_difference == '' end end
after_nodedelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 136 def after_nodedelete if config.auto_push delete_node(object_name) end end
after_nodeedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 126 def after_nodeedit if config.auto_push save_node(object_name) unless object_difference == '' end end
after_promote_local()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 175 def after_promote_local environments.each do |environment| git_add(environment_path,"#{environment}.json") end if config.auto_push git_commit(environment_path, "promote #{cookbooks.collect{ |c| "#{c.name}@#{c.version}" }.join(",")} to #{environments.join(",")}") git_push(branch) end end
after_rolecreate()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 24 def after_rolecreate if config.auto_push if !File.directory?(role_path) ui.error "Role path #{role_path} does not exist" exit 1 end save_role(object_name) unless object_difference == '' end end
after_roledelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 52 def after_roledelete if config.auto_push delete_role(object_name) end end
after_roleedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 42 def after_roleedit if config.auto_push save_role(object_name) unless object_difference == '' end end
before_bump()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 142 def before_bump git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") cookbooks.each do |cookbook| git_pull(cookbook_path_for(cookbook)) git_pull_submodules(cookbook_path_for(cookbook)) end end
before_environmentcreate()
click to toggle source
Environmental Git
wrappers
# File lib/knife-spork/plugins/git.rb, line 59 def before_environmentcreate if config.auto_push if !File.directory?(environment_path) ui.error "Environment path #{environment_path} does not exist" exit 1 end git_pull(environment_path) if File.exist?(File.join(environment_path, object_name + '.json')) ui.error 'Environment already exists in local git, aborting creation' exit 1 end end end
before_environmentdelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 91 def before_environmentdelete if config.auto_push git_pull(environment_path) end end
before_environmentedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 77 def before_environmentedit if config.auto_push git_pull(environment_path) if !File.exist?(File.join(environment_path, object_name + '.json')) ui.error 'Environment does not exist in git, please create it first with spork' exit 1 end end end
before_nodecreate()
click to toggle source
Node Git
wrappers
# File lib/knife-spork/plugins/git.rb, line 103 def before_nodecreate if config.auto_push git_pull(node_path) if File.exist?(File.join(node_path, object_name + '.json')) ui.error 'Node already exists in local git, aborting creation' exit 1 end end end
before_nodedelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 131 def before_nodedelete if config.auto_push git_pull(node_path) end end
before_nodeedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 117 def before_nodeedit if config.auto_push git_pull(node_path) if !File.exist?(File.join(node_path, object_name + '.json')) ui.error 'Node does not exist in git, please bootstrap one first' exit 1 end end end
before_promote()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 160 def before_promote cookbooks.each do |cookbook| git_pull(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","") git_pull_submodules(environment_path) unless cookbook_path_for(cookbook).include?(environment_path.gsub"/environments","") git_pull(cookbook_path_for(cookbook)) git_pull_submodules(cookbook_path_for(cookbook)) end end
before_rolecreate()
click to toggle source
Role Git
wrappers
# File lib/knife-spork/plugins/git.rb, line 11 def before_rolecreate if config.auto_push if !File.directory?(role_path) ui.error "Role path #{role_path} does not exist" exit 1 end git_pull(role_path) if File.exist?(File.join(role_path, object_name + '.json')) ui.error 'Role already exists in local git, aborting creation' exit 1 end end end
before_roledelete()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 47 def before_roledelete if config.auto_push git_pull(role_path) end end
before_roleedit()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 33 def before_roleedit if config.auto_push git_pull(role_path) if !File.exist?(File.join(role_path, object_name + '.json')) ui.error 'Role does not exist in git, please create it first with spork' exit 1 end end end
before_upload()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 151 def before_upload git_pull(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") git_pull_submodules(environment_path) unless cookbook_path.include?(environment_path.gsub"/environments","") cookbooks.each do |cookbook| git_pull(cookbook_path_for(cookbook)) git_pull_submodules(cookbook_path_for(cookbook)) end end
delete_environment(environment)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 223 def delete_environment(environment) git_rm(environment_path, "#{environment}.json") git_commit(environment_path, "[ENV] Deleted #{environment}") git_push(branch) if config.auto_push end
delete_node(node)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 193 def delete_node(node) git_rm(node_path, "#{node}.json") git_commit(node_path, "[NODE] Deleted #{node}") git_push(branch) if config.auto_push end
delete_role(role)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 207 def delete_role(role) git_rm(role_path, "#{role}.json") if config.auto_push git_commit(role_path, "[ROLE] Deleted #{role}") git_push(branch) end end
perform()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 8 def perform; end
save_environment(environment)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 215 def save_environment(environment) json = JSON.pretty_generate(Chef::Environment.load(environment)) environment_file = File.expand_path( File.join(environment_path, "#{environment}.json") ) File.open(environment_file, 'w'){ |f| f.puts(json) } git_add(environment_path, "#{environment}.json") git_commit(environment_path, "[ENV] Updated #{environment}") git_push(branch) if config.auto_push end
save_node(node)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 185 def save_node(node) json = JSON.pretty_generate(Chef::Node.load(node)) node_file = File.expand_path( File.join(node_path, "#{node}.json") ) File.open(node_file, 'w'){ |f| f.puts(json) } git_add(node_path, "#{node}.json") git_commit(node_path, "[NODE] Updated #{node}") git_push(branch) if config.auto_push end
save_role(role)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 199 def save_role(role) json = JSON.pretty_generate(Chef::Role.load(role)) role_file = File.expand_path( File.join(role_path, "#{role}.json") ) File.open(role_file, 'w'){ |f| f.puts(json) } git_add(role_path, "#{role}.json") git_commit(role_path, "[ROLE] Updated #{role}") git_push(branch) end
Private Instance Methods
branch()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 376 def branch config.branch || `git rev-parse --abbrev-ref HEAD`.chomp end
cookbook_path_for(cookbook)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 384 def cookbook_path_for cookbook if defined?(Berkshelf) and cookbook.is_a? Berkshelf::CachedCookbook cookbook.path.to_s else cookbook.root_dir end end
get_parent_dir(path)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 353 def get_parent_dir(path) top_level = path return_code = 0 while return_code == 0 output = IO.popen("cd #{top_level}/.. && git rev-parse --show-toplevel 2>&1") Process.wait return_code = $? cmd_output = output.read.chomp #cygwin, I hate you for making me do this if cmd_output.include?("fatal: Not a git repository") return_code = 1 end if return_code == 0 top_level = cmd_output end end top_level end
git()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 230 def git safe_require 'git' log = Logger.new(STDOUT) log.level = Logger::WARN @git ||= begin cwd = FileUtils.pwd() ::Git.open(get_parent_dir(cwd) , :log => log) rescue Exception => e ui.error "You are not currently in a git repository #{cwd}. Please ensure you are in a git repo, a repo subdirectory, or remove the git plugin from your KnifeSpork configuration!" exit(0) end end
git_add(filepath,filename)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 277 def git_add(filepath,filename) if is_repo?(filepath) ui.msg "Git add'ing #{filepath}/#{filename}" output = IO.popen("cd #{filepath} && git add #{filename}") Process.wait exit_code = $? if !exit_code.exitstatus == 0 ui.error "#{output.read()}\n" exit 1 end end end
git_commit(filepath, msg)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 290 def git_commit(filepath, msg) begin if is_repo?(filepath) ui.msg "Git: Committing changes..." git.commit msg end rescue ::Git::GitExecuteError; end end
git_pull(path)
click to toggle source
In this case, a git pull will:
- Stash local changes - Pull from the remote - Pop the stash
# File lib/knife-spork/plugins/git.rb, line 247 def git_pull(path) if is_repo?(path) ui.msg "Git: Pulling latest changes from #{path}" output = IO.popen("cd #{path} && git pull 2>&1") Process.wait exit_code = $? if !exit_code.exitstatus == 0 ui.error "#{output.read()}\n" exit 1 end end end
git_pull_submodules(path)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 260 def git_pull_submodules(path) if is_repo?(path) ui.msg "Pulling latest changes from git submodules (if any)" top_level = `cd #{path} && git rev-parse --show-toplevel 2>&1`.chomp if is_submodule?(top_level) top_level = get_parent_dir(top_level) end output = IO.popen("cd #{top_level} && git submodule foreach git pull 2>&1") Process.wait exit_code = $? if !exit_code.exitstatus == 0 ui.error "#{output.read()}\n" exit 1 end end end
git_push(branch)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 300 def git_push(branch) begin ui.msg "Git: Pushing to #{branch}" git.push "origin", branch rescue ::Git::GitExecuteError => e ui.error "Could not push to master: #{e.message}" end end
git_rm(filepath, filename)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 309 def git_rm(filepath, filename) if is_repo?(filepath) ui.msg "Git rm'ing #{filepath}/#{filename}" output = IO.popen("cd #{filepath} && git rm #{filename}") Process.wait exit_code = $? if !exit_code.exitstatus == 0 ui.error "#{output.read()}\n" exit 1 end end end
git_tag(tag)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 322 def git_tag(tag) begin git.add_tag(tag) rescue ::Git::GitExecuteError => e ui.error "Could not tag #{tag_name}. Does it already exist?" ui.error 'You may need to delete the tag before running promote again.' end end
is_repo?(path)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 331 def is_repo?(path) output = IO.popen("cd #{path} && git rev-parse --git-dir 2>&1") Process.wait if $? != 0 ui.warn "#{path} is not a git repo, skipping..." return false else return true end end
is_submodule?(path)
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 342 def is_submodule?(path) top_level = `cd #{path} && git rev-parse --show-toplevel 2>&1`.chomp output = IO.popen("cd #{top_level}/.. && git rev-parse --show-toplevel 2>&1") Process.wait if $? != 0 return false else return true end end
remote()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 372 def remote config.remote || 'origin' end
tag_name()
click to toggle source
# File lib/knife-spork/plugins/git.rb, line 380 def tag_name cookbooks.collect{|c| "#{c.name}@#{c.version}"}.join('-') end