Class: Pot::Installers::Transfer

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

Overview

Beware, another strange “installer” coming your way.

File transfer installer

This installer pushes files from the local disk to remote servers.

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.

If you pass the option :render => true, this tells transfer that the source file is an ERB template to be rendered locally before being transferred (you can declare variables in the package scope). When render is true, recursive is turned off. Note you can also explicitly pass locals in to render with the :locals option.

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

Finally, 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.

Instance Attribute Summary collapse

Attributes inherited from Pot::Installer

#actor, #options, #package, #post, #pre

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pot::Installer

#archives, #builds, #commands, #prefix

Constructor Details

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

:nodoc:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pot/installers/transfer.rb', line 89

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

Instance Attribute Details

#destinationObject

:nodoc:



87
88
89
# File 'lib/pot/installers/transfer.rb', line 87

def destination
  @destination
end

#sourceObject

:nodoc:



87
88
89
# File 'lib/pot/installers/transfer.rb', line 87

def source
  @source
end

Class Method Details

.render_template(template, context, prefix) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pot/installers/transfer.rb', line 105

def self.render_template(template, context, prefix)
  require 'tempfile'
  require 'erubis'

  begin
    eruby = Erubis::Eruby.new(template)
    output = eruby.result(context)
  rescue Object => e
    raise TemplateError.new(e, template, context)
  end

  final_tempfile = Tempfile.new(prefix.to_s)
  final_tempfile.print(output)
  final_tempfile.close
  final_tempfile
end

Instance Method Details

#install_commandsObject



101
102
103
# File 'lib/pot/installers/transfer.rb', line 101

def install_commands
  nil
end

#process(actor) ⇒ Object

:nodoc:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pot/installers/transfer.rb', line 132

def process(actor) #:nodoc:
  if Pot.logger.debug?
    Pot.logger.debug "transfer: #{@source} -> #{@destination}\n"
  end

  unless Pot.config.testing?
    pre = pre_commands(:install)
    unless pre.empty?
      sequence = pre
      sequence = sequence.join('; ') if sequence.is_a? Array
      Pot.logger.info "#{@package.name} pre-transfer commands: #{sequence}\n"
      actor.execute [pre].flatten
    end

    recursive = @options[:recursive]

    if options[:render]
      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)
      sourcepath = tempfile.path
      Pot.logger.info "Rendering template #{@source} to temporary file #{sourcepath}"
      recursive = false
    else
      sourcepath = @source
    end

    Pot.logger.info "--> Transferring #{sourcepath} to #{@destination}"
    actor.transfer(sourcepath, @destination, recursive)

    post = post_commands(:install)
    unless post.empty?
      sequence = post;
      sequence = sequence.join('; ') if sequence.is_a? Array
      Pot.logger.info "#{@package.name} post-transfer commands: #{sequence}\n"
      actor.execute [post].flatten
    end
  end
end

#render_template(template, context, prefix) ⇒ Object



122
123
124
# File 'lib/pot/installers/transfer.rb', line 122

def render_template(template, context, prefix)
  self.class.render_template(template, context, prefix)
end

#render_template_file(path, context, prefix) ⇒ Object



126
127
128
129
130
# File 'lib/pot/installers/transfer.rb', line 126

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