Module: MagicRecipes

Defined in:
lib/magic_recipes.rb,
lib/magic_recipes/db.rb,
lib/magic_recipes/git.rb,
lib/magic_recipes/rvm.rb,
lib/magic_recipes/gems.rb,
lib/magic_recipes/thin.rb,
lib/magic_recipes/nginx.rb,
lib/magic_recipes/rbenv.rb,
lib/magic_recipes/assets.rb,
lib/magic_recipes/nodejs.rb,
lib/magic_recipes/sqlite.rb,
lib/magic_recipes/unicorn.rb,
lib/magic_recipes/passenger.rb,
lib/magic_recipes/postgresql.rb,
lib/magic_recipes/private_pub.rb,
lib/generators/magic_recipes/capify_generator.rb,
lib/generators/magic_recipes/git_cap_generator.rb

Defined Under Namespace

Modules: Assets, Db, Gems, Generators, Git, Nginx, Nodejs, Passenger, Postgresql, PrivatePub, Rbenv, Rvm, Sqlite, Thin, Unicorn

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object

MagicRecipes

Some capistrano-recipes for our deployment .. still in development!

Methods:

template(from, to) # => write erb template to path

set_default(name, *args, &block) # => set default value

random_string(length=42) # => generate a random string

use_recipe(recipe_name) # => load one recipe

magic_recipes(*recipes) # => load several recipes

Usage:

add magic_recipes to your Gemfile

gem 'magic_recipes', :require => nil

install the gem

bundle install

run the generator

rails g magic_recipes:capify

edit ‘config/deploy’

enjoy some magic!

config/deploy.rb:

uncomment and edit all needed vars

add all recipes

magic_recipes :assets, :db, :nginx, :postgresql, :private_pub, :rvm, :sqlite, :thin

add your recipes

magic_recipes :assets, :nginx, :rvm, :thin


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/magic_recipes.rb', line 43

def self.load_into(configuration)
  configuration.load do
    
    @magical_recipes = []

    class << self
      attr_reader :magical_recipes
    end
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    # Magic-Helper:
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    require 'bundler/capistrano'
    
    default_run_options[:pty] = true
    ssh_options[:forward_agent] = true
    

    def set_default(name, *args, &block)
      set(name, *args, &block) unless exists?(name)
    end

    def random_string(length=42)
      # chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
      chars = 'abcdefghjkmnpqrstuvwxyz234567890'
      password = ''
      length.times { password << chars[rand(chars.size)] }
      password
    end
    
    # default put_via sftp
    set_default :put_via,   :sftp           # => :sftp | :scp
    
    def template(from, to)
      erb = File.read(File.expand_path("../magic_recipes/templates/#{from}", __FILE__))
      put ERB.new(erb).result(binding), to, via: put_via
    end
    
    set_default :use_rvm,   false           # => no_rvm
    
    namespace :deploy do
      desc "Install everything onto the server"
      task :install do
        run "#{sudo} apt-get -y update"
        run "#{sudo} apt-get -y install python-software-properties"
      end
    end
    
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    # Load-Helper:
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
    
    def use_recipe(recipe_name)
      return if @magical_recipes.include?(recipe_name.to_sym)

      begin
        require "magic_recipes/#{recipe_name.to_s}"

        recipe = ::MagicRecipes.const_get( recipe_name.to_s.capitalize.gsub(/_(\w)/) { $1.upcase } )
        recipe.load_into(self)
        @magical_recipes << recipe_name.to_sym
      rescue LoadError
        abort "There is no recipe called `#{recipe_name}`!"
      end
    end

    def magic_recipes(*recipes)
      recipes.map{ |recipe| use_recipe(recipe) }
    end

  end
end