Shadow Puppet overview and examples

auxesis edited this page Aug 19, 2010 · 5 revisions

Moonshine is based on Shadow Puppet, which in turn is based on Puppet. All of the Puppet types and options are valid, although the usage is slightly different and more Ruby-like in Moonshine/Shadow Puppet.

This page contains examples for various tasks you might want to do in your manifest. All of these should go into a recipe method that is called in your manifest, like so:

def cron_tasks
  # define cron jobs, example below.
recipe :cron_tasks

You can also dump all of your configuration into the application_packages method which is present in your application_manifest by default.


Installing a package (via apt-get) or a gem

package 'some_native_package', :ensure => :installed
gem 'some-gem'

Defining a Rake task to be run from cron

cron 'Run some task at midnight',
  :command => "/usr/bin/rake -f #{configuration[:deploy_to]}/current/Rakefile custom:task RAILS_ENV=#{ENV['RAILS_ENV']}", 
  :user => configuration[:user],
  :minute => 0, 
  :hour => 0

Any of the time options can be specified as, e.g. '*/5' to run the task every five minutes. See the Puppet documentation for all available options.

Create a mail alias to forward all mail for the root and rails users.

%w( root rails ).each do |user|
  mailalias user, :recipient => ''

Configure a file with specific contents

farm_config = <<-CONFIG
  HORSIES = 10
file '/etc/farm', :ensure => :directory
file '/etc/farm/farm.conf', 
  :content => farm_config,
  :mode => '644',
  :owner => configuration[:user],
  :require => '/etc/farm'

Rotate log files. Logs for Rails, MySQL, and Apache are rotated by default

logrotate '/var/log/some_service.log', 
  :options => ['weekly','rotate 7','missingok', 'compress'), 
  :postrotate => '/etc/init.d/some_service restart'

Build and install from source

exec 'build foo',
  :command => ['wget',
               'tar xzf foo.tgz',
               'cd foo',
               'make install'].join(' && ')
  :cwd => '/tmp',
  :creates => '/usr/bin/foo'

Only configure certain components on the ‘testing’ stage.

Note: This assumes you are using capistrano-ext multistage functionality when deploying.

# only configure a server a certain way on production:
if deploy_stage == 'production'
  configure( :mysql => {:bind_address => ''})

def application_packages
  # only run parts of a recipe on testing:
  on_stage 'testing' do
    file '/etc/motd', :ensure => :file, :content => "Welcome to the TEST server!"