Skip to content

Commit

Permalink
support transfers properly with new api and allow owner and mode params
Browse files Browse the repository at this point in the history
  • Loading branch information
joshgoebel committed Apr 6, 2013
1 parent 9cd8698 commit dd9a4e6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
22 changes: 19 additions & 3 deletions lib/sprinkle/installers/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,30 @@ class Transfer < Installer
attr_accessor :source, :destination, :sourcepath #:nodoc:

def initialize(parent, source, destination, options={}, &block) #:nodoc:
super parent, options, &block
@source = source
@destination = destination
super parent, options, &block
@binding = options[:binding]
# perform the transfer in two steps if we're using sudo
if sudo?
final = @destination
@destination = "/tmp/sprinkle_#{File.basename(@destination)}"
post :install, "sudo mv #{@destination} #{final}"
post :install, "#{sudo_cmd}mv #{@destination} #{final}"
end
owner(options[:owner]) if options[:owner]
mode(optinos[:mode]) if options[:mode]

options[:recursive]=false if options[:render]
end

def owner(owner)
@owner = owner
post :install, "#{sudo_cmd}chown #{owner} #{destination}"
end

def mode(mode)
@mode = mode
post :install, "#{sudo_cmd}chmod #{mode} #{destination}"
end

def install_commands
Expand Down Expand Up @@ -147,7 +162,8 @@ def process(roles) #:nodoc:
end
end
else
context = binding()
# context = binding()
context = @binding
end

tempfile = render_template_file(@source, context, @package.name)
Expand Down
4 changes: 3 additions & 1 deletion lib/sprinkle/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,13 @@ def reconnect(options, &block)
end

def transfer(source, destination, options = {}, &block)
options.merge!(:binding => binding())
install Sprinkle::Installers::Transfer.new(self, source, destination, options, &block)
end

def runner(*cmds, &block)
install Sprinkle::Installers::Runner.new(self, cmds, &block)
options = cmds.extract_options!
install Sprinkle::Installers::Runner.new(self, cmds, options, &block)
end

def verify(description = '', &block)
Expand Down
47 changes: 31 additions & 16 deletions spec/sprinkle/installers/transfer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
include Sprinkle::Deployment

before do
@package = mock(Sprinkle::Package, :name => 'package')
@package = mock(Sprinkle::Package, :name => 'package', :sudo? => false)
@empty = Proc.new { }
@delivery = mock(Sprinkle::Deployment, :process => true)
@delivery = mock(Sprinkle::Deployment, :install => true)
@source = 'source'
@destination = 'destination'
@installer = create_transfer(@source, @destination)
Expand All @@ -33,26 +33,43 @@ def create_transfer(source, dest, options={}, &block)

describe 'during installation' do

context "setting mode and owner" do
before do
@installer = create_transfer @source, @destination do
mode "744"
owner "root"
end
@installer_commands = @installer.install_sequence
end

it "should include command to set owner" do
@installer_commands.should include("chmod 744 #{@destination}")
end

it "should include command to set mode" do
@installer_commands.should include("chown root #{@destination}")
end

end

context 'single pre/post commands' do
before do
@installer = create_transfer @source, @destination do
pre :install, 'op1'
post :install, 'op2'
end

@installer_commands = @installer.install_sequence
@delivery = @installer.delivery
end

it "should call the pre and post install commands around the file transfer" do
@delivery.should_receive(:process).with(@package.name, ['op1'], @roles).and_return
@delivery.should_receive(:transfer).and_return
@delivery.should_receive(:process).with(@package.name, ['op2'], @roles).and_return
@installer_commands.should == ["op1",:TRANSFER, "op2"]
end

it "should call transfer with recursive defaulted to nil" do
@delivery.should_receive(:process).and_return
@delivery.should_receive(:transfer).with(@package.name, @source, @destination, @roles, nil)
end
# it "should call transfer with recursive defaulted to nil" do
# @delivery.should_receive(:process).and_return
# @delivery.should_receive(:transfer).with(@package.name, @source, @destination, @roles, nil)
# end

end

Expand All @@ -62,14 +79,12 @@ def create_transfer(source, dest, options={}, &block)
pre :install, 'op1', 'op1-1'
post :install, 'op2', 'op2-1'
end

@installer_commands = @installer.install_sequence
@delivery = @installer.delivery
end

it "should call the pre and post install commands around the file transfer" do
@delivery.should_receive(:process).with(@package.name, ['op1', 'op1-1'], @roles).and_return
@delivery.should_receive(:transfer).and_return
@delivery.should_receive(:process).with(@package.name, ['op2', 'op2-1'], @roles).and_return
@installer_commands.should == ["op1","op1-1",:TRANSFER, "op2","op2-1"]
end

end
Expand All @@ -95,7 +110,7 @@ def create_transfer(source, dest, options={}, &block)
it "should call transfer with recursive set to false" do
@tempfile = Tempfile.new("foo")
@installer.should_receive(:render_template_file).with(@source, anything, @package.name).and_return(@tempfile)
@delivery.should_receive(:transfer).with(@package.name, @tempfile.path, @destination, @roles, false).ordered.and_return
@installer.options[:recursive].should == false
end

after do
Expand All @@ -110,7 +125,7 @@ def create_transfer(source, dest, options={}, &block)

it "should call transfer with recursive set to false" do
delivery = @installer.delivery
delivery.should_receive(:transfer).with(@package.name, @source, @destination, @roles, false).ordered.and_return
@installer.options[:recursive].should == false
end

after do
Expand Down

0 comments on commit dd9a4e6

Please sign in to comment.