Class: Moonshadow::Manifest

Inherits:
ShadowPuppet::Manifest
  • Object
show all
Defined in:
lib/moonshadow/manifest.rb

Overview

This is the base Moonshadow Manifest class, which provides a simple system for loading moonshadow recpies from plugins, a template helper, and parses several configuration files:

config/moonshadow.yml

The contents of config/moonshadow.yml are expected to serialize into a hash, and are loaded into the manifest’s Configatron::Store.

config/database.yml

The contents of your database config are parsed and are available at configuration[:database].

If you’d like to create another ‘default rails stack’ using other tools that what Moonshadow::Manifest::Rails uses, subclass this and go nuts.

Direct Known Subclasses

Rails

Defined Under Namespace

Classes: Rails

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deploy_stageObject

The current deployment target. Best when used with capistrano-ext’s multistage settings.



65
66
67
# File 'lib/moonshadow/manifest.rb', line 65

def self.deploy_stage
  ENV['DEPLOY_STAGE'] || 'undefined'
end

.initial_configurationObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/moonshadow/manifest.rb', line 136

def self.initial_configuration
  # config/moonshadow.yml
  moonshadow_yml = IO.read(File.join(rails_root, 'config', 'moonshadow.yml')) rescue nil
  configure(YAML::load(ERB.new(moonshadow_yml).result)) if moonshadow_yml

  # database config
  database_yml = IO.read(File.join(rails_root, 'config', 'database.yml')) rescue nil
  configure(:database => YAML::load(ERB.new(database_yml).result)) if database_yml

  # gems
  configure(:gems => (YAML.load_file(File.join(rails_root, 'config', 'gems.yml')) rescue nil))
end

.plugin(name = nil) ⇒ Object

Load a Moonshadow Plugin

class MyManifest < Moonshadow::Manifest

  # Evals vendor/plugins/moonshadow_my_app/moonshadow/init.rb
  plugin :moonshadow_my_app

  # Evals lib/my_recipe.rb
  plugin 'lib/my_recipe.rb'

  ...
end


30
31
32
33
34
35
36
37
38
# File 'lib/moonshadow/manifest.rb', line 30

def self.plugin(name = nil)
  if name.is_a?(Symbol)
    path = File.join(rails_root, 'vendor', 'plugins', 'moonshadow_' + name.to_s, 'moonshadow', 'init.rb')
  else
    path = name
  end
  Kernel.eval(File.read(path), binding, path)
  true
end

.rails_envObject

The current Rails environment



50
51
52
# File 'lib/moonshadow/manifest.rb', line 50

def self.rails_env
  ENV["RAILS_ENV"] || 'production'
end

.rails_rootObject

The working directory of the Rails application this manifests describes.



41
42
43
# File 'lib/moonshadow/manifest.rb', line 41

def self.rails_root
 @rails_root ||= File.expand_path(ENV["RAILS_ROOT"] || Dir.getwd)
end

Instance Method Details

#database_environmentObject

The current environment’s database configuration



60
61
62
# File 'lib/moonshadow/manifest.rb', line 60

def database_environment
 configuration[:database][rails_env.to_sym]
end

#deploy_stageObject

The current deployment target. Best when used with capistrano-ext’s multistage settings.



70
71
72
# File 'lib/moonshadow/manifest.rb', line 70

def deploy_stage
  self.class.deploy_stage
end

#on_stage(*args) ⇒ Object

Only run tasks on the specified deploy_stage.

You can call it with the exact stage you want to run on:

on_stage(:my_stage) do
  puts "I'm on my_stage"
end

Or you can pass an array of stages to run on:

on_stage(:my_stage, :my_other_stage) do
  puts "I'm on one of my stages"
end

Or you can run a task unless it is on a stage:

on_stage(:unless => :my_stage) do
  puts "I'm not on my_stage"
end

Or you can run a task unless it is on one of several stages:

on_stage(:unless => [:my_stage, :my_other_stage]) do
  puts "I'm not on my stages"
end


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/moonshadow/manifest.rb', line 99

def on_stage(*args)
  options = args.extract_options!
  if_opt = options[:if]
  unless_opt = options[:unless]

  unless if_opt || unless_opt
    if_opt = args
  end

  if if_opt && if_opt.is_a?(Array) && if_opt.map {|x| x.to_s}.include?(deploy_stage)
    yield
  elsif if_opt && (if_opt.is_a?(String) || if_opt.is_a?(Symbol)) && deploy_stage == if_opt.to_s
    yield
  elsif unless_opt && unless_opt.is_a?(Array) && !unless_opt.map {|x| x.to_s}.include?(deploy_stage)
    yield
  elsif unless_opt && (unless_opt.is_a?(String) || unless_opt.is_a?(Symbol)) && deploy_stage != unless_opt.to_s
    yield
  end
end

#rails_envObject

The current Rails environment



55
56
57
# File 'lib/moonshadow/manifest.rb', line 55

def rails_env
  self.class.rails_env
end

#rails_rootObject



45
46
47
# File 'lib/moonshadow/manifest.rb', line 45

def rails_root
 self.class.rails_root
end

#template(pathname, b = binding) ⇒ Object

Render the ERB template located at pathname. If a template exists with the same basename at RAILS_ROOT/app/manifests/templates, it is used instead. This is useful to override templates provided by plugins to customize application configuration files.



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/moonshadow/manifest.rb', line 123

def template(pathname, b = binding)
  template_contents = nil
  basename = pathname.index('/') ? pathname.split('/').last : pathname
  if File.exist?(File.expand_path(File.join(rails_root, 'app', 'manifests', 'templates', basename)))
    template_contents = File.read(File.expand_path(File.join(rails_root, 'app', 'manifests', 'templates', basename)))
  elsif File.exist?(File.expand_path(pathname))
    template_contents = File.read(File.expand_path(pathname))
  else
    raise LoadError, "Can't find template #{pathname}"
  end
  ERB.new(template_contents).result(b)
end