Class: Sprinkle::Installers::FileInstaller

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

Overview

File installer

This installer creates a file on the remote server.

Example Usage

Installing a nginx.conf onto remote servers

package :nginx_conf do
  file '/etc/nginx.conf', :content => File.read('files/nginx.conf'),
    :sudo => true
end

Sudo is only necessary when the user your sprinkle is running as does not have necessarily permissions to create the file on its own. Such as when the file is in /etc.

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.

Rendering templates

Use the template render helper to render an ERB template to a remote file (you can use variables in your templates by setting them as instance variables inside your package. Templates have access to package methods such as opts, args, etc.

package :nginx_conf do
  @nginx_port = 8080
  file '/etc/nginx.conf',
    :contents => render("nginx.conf")
    # where [cwd] is the current working dir you're running sprinkle from
    # [cwd]/templates/nginx.conf.erb or
    # [cwd]/templates/nginx.conf should contain the erb template
end

You can also tell the package where to look for templates, so that if you have a complex package hierarchy such as:

.../packages/p/postfix.rb
.../packages/p/postfix/templates/main.cf.erb

package :postfix do
  template_search_path File.dirname(__FILE__)
  file '/etc/postfix/main.cf', :contents => render("main.cf")
  # searches for:
  #   ../packages/p/main.cf[.erb]
  #   ../packages/p/templates/main.cf[.erb]
end

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, destination, options = {}, &block) ⇒ FileInstaller

:nodoc:


63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sprinkle/installers/file.rb', line 63

def initialize(parent, destination, options={}, &block) #:nodoc:
  @destination = destination
  @contents = options[:content] || options[:contents]
  raise "need :contents key for file" unless @contents
  super parent, options, &block

  # setup file attributes
  owner options[:owner] if options[:owner]
  mode options[:mode] if options[:mode]

  post_move_if_sudo
  setup_source
end

Dynamic Method Handling

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

Instance Attribute Details

#contentsObject (readonly)

:nodoc:


54
55
56
# File 'lib/sprinkle/installers/file.rb', line 54

def contents
  @contents
end

#destinationObject (readonly)

:nodoc:


54
55
56
# File 'lib/sprinkle/installers/file.rb', line 54

def destination
  @destination
end

#sourcepathObject (readonly)

:nodoc:


54
55
56
# File 'lib/sprinkle/installers/file.rb', line 54

def sourcepath
  @sourcepath
end

Instance Method Details

#install_commandsObject

:nodoc:


77
78
79
# File 'lib/sprinkle/installers/file.rb', line 77

def install_commands #:nodoc:
  Commands::Transfer.new(sourcepath, destination)
end

#mode(mode) ⇒ Object

calls chmod to set the files permissions


88
89
90
91
# File 'lib/sprinkle/installers/file.rb', line 88

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

#owner(owner) ⇒ Object

calls chown own to set the file ownership


82
83
84
85
# File 'lib/sprinkle/installers/file.rb', line 82

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