lib/ors/helpers.rb in ors-0.2.10 vs lib/ors/helpers.rb in ors-0.3.0
- old
+ new
@@ -1,92 +1,133 @@
-module ORS
+class ORS
module Helpers
- include Config
+ # Helpers for Commands when parsing in ARGV
+ module ParseHelpers
+ def parse_remote_and_or_branch
+ option = ORS.config[:args].shift
- def setup_repo server
- info "[#{server}] installing codebase..."
+ unless option.nil?
+ if option.match(/^[a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+$/)
+ remote, *branch = option.split("/")
- execute_command server, %(cd #{base_path}),
- %(rm -rf #{deploy_directory}),
- %(git clone #{repo}:#{name} #{deploy_directory}),
- %(mkdir -p #{deploy_directory}/tmp/pids),
- %(mkdir -p #{deploy_directory}/log)
+ ORS.config[:remote] = remote
+ ORS.config[:branch] = branch.join('/')
+ else
+ ORS.config[:remote] = option
+ end
+ end
+ end
end
+ include ParseHelpers
- def setup_ruby server
- info "[#{server}] installing ruby and gems..."
- execute_command server, prepare_initial_environment,
- %(gem install rubygems-update),
- %(gem update --system),
- %(gem install bundler),
- %(bundle install --without development test osx_development > bundler.log)
- end
+ # Helpers for preparing to run a command on a server
+ module PrepareHelpers
+ def prepare_environment
+ [%({ cd #{ORS.config[:deploy_directory]} > /dev/null; })]
+ end
- def update_code server
- info "[#{server}] updating codebase..."
+ def prepare_environment_with_rvm
+ [%(source ~/.rvm/scripts/rvm)] + prepare_environment
+ end
- execute_command server, prepare_environment,
- %(git fetch),
- %(git checkout -q -f origin/#{environment}),
- %(git reset --hard),
- %(git submodule update --init)
+ def prepare_initial_environment
+ # We do a source and a git checkout here because the master
+ # branch may not always contain the proper rvmrc/Gemfile
+ # we need when setting up the rest of the deploy
+ prepare_environment_with_rvm + [
+ %(git checkout -q -f #{ORS.config[:remote]}/#{ORS.config[:branch]}),
+ %(git reset --hard),
+ %(source .rvmrc)
+ ]
+ end
end
+ include PrepareHelpers
- def bundle_install server
- info "[#{server}] installing bundle..."
+ # Helpers for commands for re-use
+ module CommandHelpers
+ def setup_repo server
+ info "[#{server}] installing codebase..."
- execute_command server, prepare_environment,
- %(bundle install --without development test osx_development > bundler.log)
- end
+ execute_command server, %(cd #{ORS.config[:base_path]}),
+ %(rm -rf #{ORS.config[:deploy_directory]}),
+ %(git clone #{ORS.config[:remote_url]} #{ORS.config[:deploy_directory]}),
+ %(mkdir -p #{ORS.config[:deploy_directory]}/tmp/pids),
+ %(mkdir -p #{ORS.config[:deploy_directory]}/log)
+ end
- def start_server server
- info "[#{server}] starting unicorn..."
+ def setup_ruby server
+ info "[#{server}] installing ruby and gems..."
- execute_command server, prepare_environment,
- %(if [ -f config.ru ]; then RAILS_ENV=#{environment} bundle exec unicorn -c config/unicorn.rb -D -E #{environment}; else RAILS_ENV=#{environment} bundle exec unicorn_rails -c config/unicorn.rb -D -E #{environment}; fi)
- end
+ execute_command server, prepare_initial_environment,
+ %(gem install rubygems-update),
+ %(gem update --system),
+ %(gem install bundler),
+ %(bundle install --without development test osx_development > bundler.log)
+ end
- def stop_server server
- info "[#{server}] stopping unicorn..."
+ def update_code server
+ info "[#{server}] updating codebase..."
- execute_command server, prepare_environment,
- %(kill \\`cat tmp/pids/unicorn.pid\\`)
- end
+ execute_command server, prepare_environment,
+ %(git fetch #{ORS.config[:remote]}),
+ %(git checkout -q -f #{ORS.config[:remote]}/#{ORS.config[:branch]}),
+ %(git reset --hard),
+ %(git submodule update --init)
+ end
- def restart_server server
- info "[#{server}] restarting unicorn..."
+ def bundle_install server
+ info "[#{server}] installing bundle..."
- execute_command server, prepare_environment,
- %(kill -USR2 \\`cat tmp/pids/unicorn.pid\\`)
- end
+ execute_command server, prepare_environment_with_rvm,
+ %(bundle install --without development test osx_development > bundler.log)
+ end
- def run_migrations server
- info "[#{server}] running migrations..."
+ def start_server server
+ info "[#{server}] starting unicorn..."
- execute_command server, prepare_environment,
- %(RAILS_ENV=#{environment} bundle exec rake db:migrate db:seed)
- end
+ execute_command server, prepare_environment_with_rvm,
+ %(if [ -f config.ru ]; then RAILS_ENV=#{ORS.config[:environment]} bundle exec unicorn -c config/unicorn.rb -D; else RAILS_ENV=#{ORS.config[:environment]} bundle exec unicorn_rails -c config/unicorn.rb -D; fi)
+ end
- def execute_in_parallel servers
- servers.map do |server|
- Thread.new(server) do |server|
- yield server
- end
- end.map {|thread| thread.join }
+ def stop_server server
+ info "[#{server}] stopping unicorn..."
+
+ execute_command server, prepare_environment,
+ %(kill \\`cat tmp/pids/unicorn.pid\\`)
+ end
+
+ def restart_server server
+ info "[#{server}] restarting unicorn..."
+
+ execute_command server, prepare_environment,
+ %(kill -USR2 \\`cat tmp/pids/unicorn.pid\\`)
+ end
+
+ def run_migrations server
+ info "[#{server}] running migrations..."
+
+ execute_command server, prepare_environment_with_rvm,
+ %(RAILS_ENV=#{ORS.config[:environment]} bundle exec rake db:migrate db:seed)
+ end
end
+ include CommandHelpers
+ #
+ # How we actually execute/build commands
+ #
+
# options = {:exec => ?, :capture => ?, :quiet_ssh => ?}
- def execute_command server, *command_array
+ def execute_command(server, *command_array)
options = {:exec => false, :capture => false, :quiet_ssh => false}
options.merge!(command_array.pop) if command_array.last.is_a?(Hash)
options[:local] = true if server.to_s == "localhost"
command = build_command(server, command_array, options)
- if pretending
+ if ORS.config[:pretending]
info("[#{server}] #{command}")
else
if options[:exec]
exec command
else
@@ -118,41 +159,31 @@
quiet_ssh = options[:quiet_ssh] ? '-q ' : ''
if options[:local]
commands
else
- if use_gateway
- %(ssh #{quiet_ssh}#{psuedo_tty}#{gateway} 'ssh #{quiet_ssh}#{psuedo_tty}#{deploy_user}@#{server} "#{commands}"')
+ if ORS.config[:use_gateway]
+ %(ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:gateway]} 'ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:user]}@#{server} "#{commands}"')
else
- %(ssh #{quiet_ssh}#{psuedo_tty}#{deploy_user}@#{server} "#{commands}")
+ %(ssh #{quiet_ssh}#{psuedo_tty}#{ORS.config[:user]}@#{server} "#{commands}")
end
end
end
- def prepare_initial_environment
- # We do 2 cd's and a git checkout here because the master
- # branch may not always contain the proper rvmrc/Gemfile
- # we need when setting up the rest of the deploy
- prepare_environment + [
- %(git checkout -q -f origin/#{environment}),
- %(git reset --hard),
- %(cd ../),
- %(cd #{deploy_directory})
- ]
+ def execute_in_parallel servers
+ servers.map do |server|
+ Thread.new(server) do |server|
+ yield server
+ end
+ end.map {|thread| thread.join }
end
- def prepare_environment
- [%(source ~/.rvm/scripts/rvm),
- %({ cd #{deploy_directory} > /dev/null; })] # Silence RVM's "Using... gemset..."
- end
-
def info message
STDOUT.puts message
end
def fatal message
info message
exit 1
end
-
end
end