Skip to content

Instantly share code, notes, and snippets.

@insoul
Last active December 15, 2015 13:39
Show Gist options
  • Save insoul/5269205 to your computer and use it in GitHub Desktop.
Save insoul/5269205 to your computer and use it in GitHub Desktop.
capistrano recipe for using git file as capistrano recipe
# use recipe on git.
#
# necessary variable: none
#
# optional variable:
# gitipe_repository:
# if this is empty, use source as gitipe
# gitipe_branch:
# if gitipe_repository is not empty, this varaible is not used
# default "master"
# gitipe_root:
# if gitipe_repository is empty and :deploy_via is :rsync_with_remote_cache
# use fetch('local_cache')
# else
# default ".gitipe/#{gitipe_repository.split(':').last.gsub(/\.git$/, '')}
# gitipe_load_paths (default "[]", ex. "[recipes/deploy.rb, recipes/control.rb]")
def gitipe_config
return @gitipe_config if @gitipe_config
@gitipe_config = Capistrano::Configuration.new
gitipe_repository = fetch(:gitipe_repository, nil)
gitipe_branch = fetch(:gitipe_branch, 'master')
if gitipe_repository
@gitipe_config.set(:repository, gitipe_repository)
@gitipe_config.set(:branch, gitipe_branch)
return @gitipe_config
end
if fetch(:deploy_via) == :rsync_with_remote_cache
begin
strategy.update_local_cache
rescue => e
logger.important("local cache error!! #{e.message}")
end
@gitipe_config.set(:use_source, true)
return @gitipe_config
end
raise "[GITIPE_ERROR] gitipe_repository or repository must be defined"
end
def gitipe_source
Capistrano::Deploy::SCM.new('git', gitipe_config)
end
def gitipe_real_revision
gitipe_source.local.query_revision(gitipe_config.branch) { |cmd| with_env("LC_ALL", "C") { run_locally(cmd) } }
end
def gitipe_root
if gitipe_config.use_source
fetch(:local_cache)
else
gitipe_root = fetch(:gitipe_root, ".gitipe/#{gitipe_config.repository.split(':').last.gsub(/\.git$/, '')}")
end
end
def gitipe_root_exists?
File.exist?(gitipe_root)
end
def gitipe_root_valid?
gitipe_root_exists? && File.directory?(gitipe_root)
end
def gitipe_sync_command
if gitipe_root_valid?
gitipe_source.sync(gitipe_real_revision, gitipe_root)
elsif !gitipe_root_exists?
"mkdir -p #{gitipe_root} && #{gitipe_source.checkout(gitipe_real_revision, gitipe_root)}"
else
raise InvalidCacheError, "The local cache exists but is not valid (#{gitipe_root})"
end
end
system(gitipe_sync_command) unless gitipe_config.use_source
# To set array in webistrano, refer Webistrano::Deployer.type_cast method
def gitipe_type_cast(val)
return [] if val.nil?
return val if val.is_a? Array
val.strip!
return [] if val.empty?
case val
when /\A\[(.*)\]/
$1.split(',').map{|subval| gitipe_type_cast(subval)}
else # symbol or string
if val.index(':') == 0
val.slice(1, val.size).to_sym
elsif match = val.match(/'(.*)'/) || val.match(/"(.*)"/)
match[1]
else
val
end
end
end
def gitipe_load_paths
return @gitipe_load_paths if @gitipe_load_paths
@gitipe_load_paths = gitipe_type_cast(fetch(:gitipe_load_paths, nil))
@gitipe_load_paths = [@gitipe_load_paths] unless @gitipe_load_paths.is_a? Array
@gitipe_load_paths
end
gitipe_load_paths.each do |path|
logger.important "loading gitipe #{gitipe_root}/#{path}"
load "#{gitipe_root}/#{path}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment