Permalink
Browse files

Implement the as() helper method

  • Loading branch information...
1 parent 5df73fd commit ba25a165a63c7d1fd428a7f7465facb94edfb24f @leehambley leehambley committed Jan 15, 2013
View
@@ -22,8 +22,8 @@ namespace :test do
task :test do
Rake::Task['test:units'].execute
- Rake::Task['test:functional'].execute
Rake::Task['test:integration'].execute
+ Rake::Task['test:functional'].execute unless ENV['TRAVIS']
end
task default: :test
View
@@ -0,0 +1,11 @@
+class Hash
+ def symbolize_keys
+ inject({}) do |options, (key, value)|
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
+ def symbolize_keys!
+ self.replace(self.symbolize_keys)
+ end
+end
View
@@ -1,4 +1,5 @@
require_relative '../core_ext/array'
+require_relative '../core_ext/hash'
require_relative 'command'
require_relative 'host'
@@ -5,8 +5,12 @@ module Backend
class Abstract
- def initialize(*args)
- # Nothing here
+ attr_reader :host
+
+ def initialize(host, &block)
+ raise "Must pass a Host object" unless host.is_a? Host
+ @host = host
+ @block = block
end
def make(commands=[])
@@ -28,8 +32,8 @@ def capture(command, args=[])
def within(directory, &block)
(@pwd ||= []).push directory.to_s
execute <<-EOTEST
- if test ! -d #{File.join(@pwd)}; then
- echo "Directory does not exist '#{File.join(@pwd)}'" 2>&1
+ if test ! -d #{File.join(@pwd)}
+ then echo "Directory does not exist '#{File.join(@pwd)}'" 2>&1
false
fi
EOTEST
@@ -47,10 +51,23 @@ def with(environment, &block)
remove_instance_variable(:@_env)
end
+ def as(user, &block)
+ @user = user
+ execute <<-EOTEST
+ if ! sudo su -u #{user} whoami > /dev/null
+ then echo "You cannot switch to user '#{user}' using sudo, please check the sudoers file" 2>&1
+ false
+ fi
+ EOTEST
+ yield
+ ensure
+ remove_instance_variable(:@user)
+ end
+
private
def command(*args)
- Deploy::Command.new(*args, in: @pwd.nil? ? nil : File.join(@pwd), env: @env)
+ Deploy::Command.new(*args, in: @pwd.nil? ? nil : File.join(@pwd), env: @env, host: @host, user: @user)
end
def connection
@@ -5,13 +5,6 @@ class Printer < Abstract
include Deploy::CommandHelper
- attr_reader :host
-
- def initialize(host, &block)
- @host = host
- @block = block
- end
-
def run
instance_exec(host, &@block)
end
View
@@ -30,46 +30,69 @@ class Command
def initialize(*args)
@options = args.extract_options!
- @command = args.shift.to_sym
+ @command = args.shift.to_s.strip.to_sym
@args = args
+ @options.symbolize_keys!
+ sanitize_command!
+ end
+
+ def to_hash
+ {
+ command: command,
+ args: args,
+ options: options
+ }
+ end
+
+ def host
+ options[:host]
end
def to_s
- if command.to_s.match("\n")
- String.new.tap do |cs|
- command.to_s.lines.each do |line|
- cs << line.strip
- cs << '; ' unless line == command.to_s.lines.to_a.last
- end
+ return command if command.match /\s/
+ String.new.tap do |cs|
+ if options[:in]
+ cs << sprintf("cd %s && ", options[:in])
end
- else
- String.new.tap do |cs|
- if options[:in]
- cs << sprintf("cd %s && ", options[:in])
+ if options[:env]
+ cs << '( '
+ options[:env].each do |k,v|
+ cs << k.to_s.upcase
+ cs << "="
+ cs << v.to_s.shellescape
end
- if options[:env]
- cs << '( '
- options[:env].each do |k,v|
- cs << k.to_s.upcase
- cs << "="
- cs << v.to_s.shellescape
+ cs << ' '
+ end
+ if options[:user]
+ cs << "( sudo su -u #{options[:user]} "
+ end
+ cs << Deploy.config.command_map[command.to_sym]
+ if args.any?
+ cs << ' '
+ cs << args.join(' ')
+ end
+ if options[:user]
+ cs << ' )'
+ end
+ if options[:env]
+ cs << ' )'
+ end
+ end
+ end
+
+ private
+
+ def sanitize_command!
+ command.to_s.strip!
+ if command.to_s.match("\n")
+ @command = String.new.tap do |cs|
+ command.to_s.lines.each do |line|
+ cs << line.strip
+ cs << '; ' unless line == command.to_s.lines.to_a.last
end
- cs << ' '
- end
- cs << Deploy.config.command_map[command.to_sym]
- if args.any?
- cs << ' '
- cs << args.join(' ')
- end
- if options[:env]
- cs << ' )'
- end
- if options[:in]
- cs << '; cd -'
end
end
end
- end
end
View
@@ -115,6 +115,6 @@ class IntegrationTest < MiniTest::Unit::TestCase
# Force colours in Autotest
#
Turn.config.ansi = true
-Turn.config.format = :dot
+Turn.config.format = :pretty
MiniTest::Unit.autorun
@@ -25,7 +25,9 @@ def block_to_run
execute :ls, '-l', '/some/directory'
with rails_env: :production do
within :tmp do
- execute :touch, 'restart.txt'
+ as :root do
+ execute :touch, 'restart.txt'
+ end
end
end
end
@@ -44,11 +46,12 @@ def test_simple_printing
sio.rewind
result = sio.read
assert_equal <<-EOEXPECTED.unindent, result
- > Executing if test ! -d /opt/sites/example.com; then; echo \"Directory does not exist '/opt/sites/example.com'\" 2>&1; false; fi
- > Executing cd /opt/sites/example.com && /usr/bin/env date; cd -
- > Executing cd /opt/sites/example.com && /usr/bin/env ls -l /some/directory; cd -
- > Executing if test ! -d /opt/sites/example.com/tmp; then; echo \"Directory does not exist '/opt/sites/example.com/tmp'\" 2>&1; false; fi
- > Executing cd /opt/sites/example.com/tmp && ( RAILS_ENV=production /usr/bin/env touch restart.txt ); cd -
+ > Executing if test ! -d /opt/sites/example.com; then echo "Directory does not exist '/opt/sites/example.com'" 2>&1; false; fi
+ > Executing cd /opt/sites/example.com && /usr/bin/env date
+ > Executing cd /opt/sites/example.com && /usr/bin/env ls -l /some/directory
+ > Executing if test ! -d /opt/sites/example.com/tmp; then echo "Directory does not exist '/opt/sites/example.com/tmp'" 2>&1; false; fi
+ > Executing if ! sudo su -u root whoami > /dev/null; then echo "You cannot switch to user 'root' using sudo, please check the sudoers file" 2>&1; false; fi
+ > Executing cd /opt/sites/example.com/tmp && ( RAILS_ENV=production ( sudo su -u root /usr/bin/env touch restart.txt ) )
EOEXPECTED
end
View
@@ -24,12 +24,23 @@ def test_including_the_env
def test_working_in_a_given_directory
c = Command.new(:ls, '-l', in: "/opt/sites")
- assert_equal "cd /opt/sites && /usr/bin/env ls -l; cd -", String(c)
+ assert_equal "cd /opt/sites && /usr/bin/env ls -l", String(c)
end
def test_working_in_a_given_directory_with_env
c = Command.new(:ls, '-l', in: "/opt/sites", env: {a: :b})
- assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l ); cd -", String(c)
+ assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l )", String(c)
+ end
+
+ def test_having_a_host_passed
+ refute Command.new(:date).host
+ assert Command.new(:date, host: :foo)
+ assert_equal :foo, Command.new(host: :foo).host
+ end
+
+ def test_working_as_a_given_user
+ c = Command.new(:whoami, user: :anotheruser)
+ assert_equal "( sudo su -u anotheruser /usr/bin/env whoami )", String(c)
end
end

0 comments on commit ba25a16

Please sign in to comment.