Class: Sprinkle::Policy

Inherits:
Object show all
Defined in:
lib/sprinkle/policy.rb

Overview

Policies

Policies define a set of packages which are required for a certain role (app, database, etc.). All policies defined will be run and all packages required by the policy will be installed. So whereas defining a Sprinkle::Package merely defines it, defining a Sprinkle::Policy actually causes those packages to install.

Example

policy :blog, :roles => :app do
  requires :webserver
  requires :database
  requires :rails
end

This says that for the blog on the app role, it requires certain packages. The :roles option is exactly the same as a capistrano or vlad role. A role merely defines what server the commands are run on. This way, a single Sprinkle script can provision an entire group of servers.

To define a role, put in your actor specific configuration file (recipe or script file):

role :app, "208.28.38.44"

The capistrano and vlad syntax is the same for that. If you're using a custom actor, you may have to do it differently.

Requiring a package more than once with different options

This works exactly as you might expect:

policy :bootstrap, :roles => :app do
  require :user_settings, :for => "john"
  require :user_settings, :for => "suzy"
  require :user_settings, :for => "dorothy"
end

Multiple requires for a package with no options will be collapsed; that package will be installed once.

policy :apache, :roles => :app do
  require :devtools
  ...
end
policy :git, :roles => :app do
  require :devtools
  ...
end

In this example devtools will only be installed once, prior to apache and git.

Multiple Policies

You may specify as many policies as you'd like. If the packages you're requiring are properly defined with verification blocks, then no software will be installed twice, so you may require a webserver on multiple packages within the same role without having to wait for that package to install repeatedly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, metadata = {}, &block) ⇒ Policy

creates a new policy, although policies are typically not created directly but rather via the Core#policy helper.



95
96
97
98
99
100
101
102
103
# File 'lib/sprinkle/policy.rb', line 95

def initialize(name,  = {}, &block)
  raise 'No name provided' unless name
  raise 'No roles provided' unless [:roles]

  @name = name
  @roles = [:roles]
  @packages = []
  self.instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



88
89
90
# File 'lib/sprinkle/policy.rb', line 88

def name
  @name
end

#rolesObject (readonly)

roles for which a policy should be installed [required]



90
91
92
# File 'lib/sprinkle/policy.rb', line 90

def roles
  @roles
end

Instance Method Details

#package_install_treeObject



127
128
129
# File 'lib/sprinkle/policy.rb', line 127

def package_install_tree
  @install_tree ||= normalize(tree)
end

#packagesObject

:nodoc:



110
111
112
# File 'lib/sprinkle/policy.rb', line 110

def packages #:nodoc:
  @packages.map {|x| x.first }
end

#process(deployment) ⇒ Object

:nodoc:



117
118
119
120
121
122
123
124
125
# File 'lib/sprinkle/policy.rb', line 117

def process(deployment) #:nodoc:
  raise NoMatchingServersError.new(@name, @roles) unless deployment.style.servers_for_role?(@roles)

  logger.info "[#{name}]"

  package_install_tree.each do |package|
    package.process(deployment, @roles)
  end
end

#requires(package, *args) ⇒ Object

tell a policy which packages are required



106
107
108
# File 'lib/sprinkle/policy.rb', line 106

def requires(package, *args)
  @packages << [package, args]
end

#to_sObject

:nodoc:



114
115
# File 'lib/sprinkle/policy.rb', line 114

def to_s #:nodoc:
name; end