Class: Sprinkle::Installers::Transfer

Inherits:
Installer show all
Defined in:
lib/sprinkle/installers/transfer.rb

Overview

File transfer installer

This installer copies files from the local disk to remote servers using SCP. Symbolic links will be followed and the files copied, but the symbolic links themselves will not be preserved. That's just how SCP works.

Example Usage

Installing a nginx.conf onto remote servers

package :nginx_conf do
  transfer 'files/nginx.conf', '/etc/nginx.conf'
end

If you user has access to 'sudo' and theres a file that requires priveledges, you can pass :sudo => true

package :nginx_conf do
  transfer 'files/nginx.conf', '/etc/nginx.conf', :sudo => true
end

By default, transfers are recursive and you can move whole directories via this method. If you wish to disable recursive transfers, you can pass :recursive => false, although it will not be obeyed when using the Vlad actor.

As an alternative to :recursive, you can use the :tarball option. When this is supplied, the source file(s) are first packed in a tar.gz archive, then transferred to a temp dir and finally unpacked at the destination. This is usually much faster when transferring many small files (Such as a typical rails application) You can optionally supply :exclude, which is an array of glob-patterns to not include in the tarball

package :webapp do
  transfer 'app/', '/var/www/' do
    tarball :exclude => %w(.git log/*)
  end
end

Should you need to run commands before or after the file transfer (making directories or changing permissions), you can use the pre/post :install directives and they will be run.

Rendering templates

Rendering templates with transfer has been depreciated. Please see the file installer if you want to use templates.

Constant Summary collapse

DEPRECATED =

Include deprecated code Mainly, this makes it easier to see where to cut, when next major version comes along

true

Instance Attribute Summary collapse

Attributes inherited from Installer

#delivery, #options, #package, #post, #pre

Instance Method Summary collapse

Methods inherited from Installer

#announce, api, #commands_from_block, #defer, #escape_shell_arg, inherited, #install_sequence, #method_missing, #per_host?, #process, subclasses, verify_api

Methods included from Sudo

#sudo?, #sudo_cmd, #sudo_stack

Methods included from Attributes

#defaults, #set_defaults

Constructor Details

#initialize(parent, source, destination, options = {}, &block) ⇒ Transfer

:nodoc:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sprinkle/installers/transfer.rb', line 65

def initialize(parent, source, destination, options = {}, &block) #:nodoc:
  options.reverse_merge! :recursive => true
  
  @source = source # Original source
  @sourcepath = source # What the actor will transfer (may be the same as @source)
  @final_destination = destination # Final destination
  @destination = destination # Where the actor will place the file (May be same as @final_destination)
  
  owner(options[:owner]) if options[:owner]
  mode(options[:mode]) if options[:mode]
  tarball(options[:tarball]) if options[:tarball]
  
  super parent, options, &block
  
  if DEPRECATED
    @binding = options[:binding]
    options[:render] = true if source_is_template?
    options[:recursive] = false if options[:render]
    setup_rendering if options[:render]
  end
  setup_tarball if tarball?
  setup_sudo if sudo?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sprinkle::Installers::Installer

Instance Attribute Details

#destinationObject

:nodoc:



52
53
54
# File 'lib/sprinkle/installers/transfer.rb', line 52

def destination
  @destination
end

#sourceObject

:nodoc:



52
53
54
# File 'lib/sprinkle/installers/transfer.rb', line 52

def source
  @source
end

#sourcepathObject

:nodoc:



52
53
54
# File 'lib/sprinkle/installers/transfer.rb', line 52

def sourcepath
  @sourcepath
end

Instance Method Details

#install_commandsObject



108
109
110
111
# File 'lib/sprinkle/installers/transfer.rb', line 108

def install_commands
  Commands::Transfer.new(sourcepath, destination,
    :recursive => options[:recursive])
end

#mode(mode) ⇒ Object



99
100
101
102
# File 'lib/sprinkle/installers/transfer.rb', line 99

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

#owner(owner) ⇒ Object



94
95
96
97
# File 'lib/sprinkle/installers/transfer.rb', line 94

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

#render_template(template, context, prefix) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/sprinkle/installers/transfer.rb', line 114

def render_template(template, context, prefix)
  output = @package.template(template, context)
  final_tempfile = Tempfile.new(prefix.to_s)
  final_tempfile.print(output)
  final_tempfile.close
  final_tempfile
end

#render_template_file(path, context, prefix) ⇒ Object



122
123
124
125
126
# File 'lib/sprinkle/installers/transfer.rb', line 122

def render_template_file(path, context, prefix)
  template = source_is_template? ? path : File.read(path)
  tempfile = render_template(template, context, @package.name)
  tempfile
end

#setup_renderingObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sprinkle/installers/transfer.rb', line 132

def setup_rendering
  ActiveSupport::Deprecation.warn("transfer :render is depreciated, please use the `file` installer now.")
  ActiveSupport::Deprecation.warn("transfer :render will be removed from Sprinkle v0.8")
  if @options[:render]
    raise "Incompatible combination of options :render and :tarball" if tarball?
    if @options[:locals]
      context = {}
      @options[:locals].each_pair do |k,v|
        if v.respond_to?(:call)
          context[k] = v.call
        else
          context[k] = v
        end
      end
    else
      context = @binding
    end

    @tempfile = render_template_file(@source, context, @package.name).path
    @sourcepath = @tempfile
    @options[:recursive] = false
  end
end

#setup_sudoObject



173
174
175
176
177
178
# File 'lib/sprinkle/installers/transfer.rb', line 173

def setup_sudo
  @destination = "/tmp/sprinkle_#{File.basename(@destination)}"
  # make sure we push the move ahead of any other post install tasks
  # a user may have requested
  post(:install).unshift "#{sudo_cmd}mv #{@destination} #{@final_destination}"
end

#setup_tarballObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sprinkle/installers/transfer.rb', line 157

def setup_tarball
  # tar files locally and scp to a temp location
  # then untar after transfer
  tar_options = @exclude.map {|glob| "--exclude \"#{glob}\" " }.join('')
  @tempfile = make_tmpname
  local_command = "cd '#{@source}' ; #{local_tar_bin} -zcf '#{@tempfile}' #{tar_options}."
  logger.debug "    --> Compressing #{@source} locally"
  raise "Unable to tar #{@source}" unless system(local_command)
  @sourcepath = @tempfile
  @destination = "/tmp/#{File.basename(@tempfile)}"
  post(:install).unshift [
    "#{sudo_cmd}tar -zxf '#{@destination}' -C '#{@final_destination}'",
    "#{sudo_cmd}rm '#{@destination}'"
  ]
end

#source_is_template?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/sprinkle/installers/transfer.rb', line 128

def source_is_template?
  @source.split("\n").size > 1
end

#tarball(options = {}) ⇒ Object



89
90
91
92
# File 'lib/sprinkle/installers/transfer.rb', line 89

def tarball(options = {})
  @tarball = true
  @exclude = options===true ? [] : options[:exclude]
end

#tarball?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/sprinkle/installers/transfer.rb', line 104

def tarball?
  @tarball
end