Last active
December 13, 2015 20:28
-
-
Save intel352/4970365 to your computer and use it in GitHub Desktop.
A Capistrano capfile with support for running commands locally. Additionally has a variety of helper commands for various tasks.
This file contains 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 'open3' | |
$CR = "\033[0m" # color reset | |
$red = "\033[1m\033[31m" | |
$green = "\033[1m\033[32m" | |
$yellow = "\033[1m\033[33m" | |
$blue = "\033[1m\033[34m" | |
ssh_options[:forward_agent] = true | |
ssh_options[:paranoid] = false | |
namespace :env do | |
desc 'Default Env settings' | |
task :default do | |
set :web_group, 'www-data' | |
end | |
desc 'Web Env' | |
task :web do | |
set :web_group, 'web' | |
end | |
end | |
namespace :gems do | |
desc 'Uninstall all gems' | |
task :reset do | |
runcmd "gem list --no-versions|tail -n+1|xargs gem uninstall -aIx" | |
end | |
end | |
namespace :file do | |
desc 'Finds any file containing search value, replaces inline' | |
task :replace do | |
raise ':path required' unless exists?(:path) | |
raise ':f required (string to find)' unless exists?(:f) | |
raise ':r required (replacement string)' unless exists?(:r) | |
default_run_options[:shell] = false | |
set :ext, '' unless exists?(:ext) | |
host = capturecmd('uname').chomp | |
sedi = '' | |
sedi = ' ' if host=='Darwin' | |
runcmd "cd #{path}; ack -al --print0 #{f} | xargs -0 -n 1 sed -i#{sedi}'#{ext}' -e 's/#{f}/#{r}/g'" | |
end | |
end | |
namespace :dir do | |
desc 'Create shared dir' | |
task :create do | |
raise ':path required' unless exists?(:path) | |
# needed for password sudo on debian | |
default_run_options[:pty] = true | |
puts "You may be asked for your #{$red}user password#{$CR}" | |
runcmd "sudo mkdir -p #{path}" | |
dir.fix_perms | |
end | |
desc 'Fix permissions for shared dir' | |
task :fix_perms do | |
raise ':path required' unless exists?(:path) | |
raise ':web_group required' unless exists?(:web_group) | |
# needed for password sudo on debian | |
default_run_options[:pty] = true | |
puts "You may be asked for your #{$red}user password#{$CR}" | |
runcmd "sudo chown -R :#{web_group} #{path}" | |
runcmd "sudo chmod -R ug+rwX,o=rX #{path}" | |
runcmd "sudo find #{path} -type d -exec chmod g+s \{} \\;" | |
end | |
end | |
namespace :git do | |
desc 'Sets up git, uses local env name and email unless you specify params' | |
task :init do | |
set :name, `git config user.name`.chomp unless exists?(:name) | |
set :email, `git config user.email`.chomp unless exists?(:email) | |
runcmd "git config --global user.name \"#{name}\"; git config --global user.email \"#{email}\"" | |
runcmd "git config --global branch.autosetupmerge true" | |
git.shared_system | |
end | |
desc 'Check out proper branch (-deploy)' | |
task :co do | |
runcmd "cd #{deploy_to}; git checkout -t -B #{branch} origin/#{branch}" | |
end | |
desc 'Make git repo shared' | |
task :shared do | |
raise ':path required' unless exists?(:path) | |
dir.fix_perms | |
runcmd "cd #{path}; git config core.sharedRepository group" | |
end | |
desc 'Make git shared across server' | |
task :shared_system do | |
# needed for password sudo on debian | |
default_run_options[:pty] = true | |
puts "You may be asked for your #{$red}user password#{$CR}" | |
runcmd 'sudo git config --system core.sharedRepository group' | |
end | |
end | |
desc 'Call first to make commands run locally' | |
task :local do | |
set :runlocal, true | |
end | |
# @todo - need to detect file vs dir? | |
def file_exist?(path) | |
begin | |
run "ls #{path}" | |
return true | |
rescue Exception => e | |
return false | |
end | |
end | |
def capturecmd(cmd) | |
output = "" | |
if exists?(:runlocal) && :runlocal != false | |
logger.debug "executing #{cmd}" | |
stdin, stdout, stderr = Open3.popen3(cmd) | |
output = stdout.read | |
err = stderr.read | |
warn "[err :: local] #{err}" unless err.empty? | |
else | |
output = capture cmd | |
end | |
output | |
end | |
# Runs command based on :runlocal | |
def runcmd(cmd) | |
output = nil | |
if exists?(:runlocal) && :runlocal != false | |
logger.debug "executing #{cmd}" | |
output = system cmd | |
else | |
output = run cmd | |
end | |
output | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment