Class: Sprinkle::Installers::Transfer

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

Overview

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 Installer

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

Attributes included from Configurable

#delivery

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#assert_delivery, #defaults, #method_missing, #option?

Constructor Details

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

:nodoc:



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

def initialize(parent, source, destination, options={}, &block) #:nodoc:
  super parent, options, &block
  @source = source
  @destination = destination
end

Dynamic Method Handling

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

Instance Attribute Details

#destinationObject

:nodoc:



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

def destination
  @destination
end

#sourceObject

:nodoc:



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

def source
  @source
end

Class Method Details

.render_template(template, context, prefix) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sprinkle/installers/transfer.rb', line 99

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)
     final_tempfile.print(output)
     final_tempfile.close
	final_tempfile
end

Instance Method Details

#install_commandsObject



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

def install_commands
	nil
end

#process(roles) ⇒ Object

:nodoc:



126
127
128
129
130
131
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
# File 'lib/sprinkle/installers/transfer.rb', line 126

def process(roles) #:nodoc:
  assert_delivery

  if logger.debug?
    logger.debug "transfer: #{@source} -> #{@destination} for roles: #{roles}\n"
  end

  unless Sprinkle::OPTIONS[:testing]
					pre = pre_commands(:install)
					unless pre.empty?
sequence = pre; sequence = sequence.join('; ') if sequence.is_a? Array
logger.info "#{@package.name} pre-transfer commands: #{sequence} for roles: #{roles}\n"
@delivery.process @package.name, sequence, roles
					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
logger.info "Rendering template #{@source} to temporary file #{sourcepath}"
recursive = false
					else
sourcepath = @source
					end
					
					logger.info "--> Transferring #{sourcepath} to #{@destination} for roles: #{roles}"
    @delivery.transfer(@package.name, sourcepath, @destination, roles, recursive)
					
					post = post_commands(:install)
					unless post.empty?
sequence = post; sequence = sequence.join('; ') if sequence.is_a? Array
logger.info "#{@package.name} post-transfer commands: #{sequence} for roles: #{roles}\n"
@delivery.process @package.name, sequence, roles
					end
  end
end

#render_template(template, context, prefix) ⇒ Object



116
117
118
# File 'lib/sprinkle/installers/transfer.rb', line 116

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

#render_template_file(path, context, prefix) ⇒ Object



120
121
122
123
124
# File 'lib/sprinkle/installers/transfer.rb', line 120

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