Module: Capitate::CapExt::Variables

Included in:
Capistrano::Configuration
Defined in:
lib/capitate/cap_ext/variables.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



5
6
7
8
# File 'lib/capitate/cap_ext/variables.rb', line 5

def self.included(base) #:nodoc:
  base.send :alias_method, :fetch_without_capitate, :fetch
  base.send :alias_method, :fetch, :fetch_with_capitate
end

Instance Method Details

#fetch_or_default(variable, default, *args) ⇒ Object

Fetch or set and return default.

Options

variable

Variable to fetch

default

Default value if not set

args

?

Examples

fetch_or_default(:memcached_port, 11211) => 11211
# Any calls to fetch(memcached_port) in the future will return this value 11211 (unless overriden)


44
45
46
47
48
49
50
51
# File 'lib/capitate/cap_ext/variables.rb', line 44

def fetch_or_default(variable, default, *args)
  if exists?(variable)
    fetch(variable, *args)
  else
    set variable, default
    default
  end        
end

#fetch_or_set(variable, *default_variables) ⇒ Object

Fetch or set and fetch any default variable listed.

Options

variable

Variable to fetch

variables

List if variables to try in order

Examples

fetch_or_set(:sphinx_db_host, :db_host)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/capitate/cap_ext/variables.rb', line 62

def fetch_or_set(variable, *default_variables)
  return fetch(variable) if exists?(variable)
  
  default_variables.each do |default_variable|
    if exists?(default_variable)
      value = fetch(default_variable) 
      set variable, value
      return value
    end
  end
  nil        
end

#fetch_role(name, options = {}) ⇒ Object

Fetch first role with name and options.

Options

name

Role name to look for

options

Options to match on, e.g. :primary => true

Examples

fetch_roles(:db) => [ "10.0.6.71", "10.0.6.72" ]
fetch_roles(:db, :primary => true) => [ "10.0.6.71" ]


122
123
124
125
126
# File 'lib/capitate/cap_ext/variables.rb', line 122

def fetch_role(name, options = {})
  matched = fetch_roles(name, options)
  return matched.first if matched
  nil
end

#fetch_roles(name, options = {}) ⇒ Object

Fetch roles with name and options. I don’t actually use this.

Options

name

Role name to look for

options

Options to match on, e.g. :primary => true

Examples

fetch_roles(:db) => [ "10.0.6.71", "10.0.6.72" ]
fetch_roles(:db, :primary => true) => [ "10.0.6.71" ]


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
# File 'lib/capitate/cap_ext/variables.rb', line 86

def fetch_roles(name, options = {})
  matched_roles = Set.new
  
  roles.each do |role_info|
    role_name = role_info.first 
    
    next unless role_name == name
      
    role = role_info.last
    role.each do |server|
      
      abort = false
      options.each do |k, v|
        unless server.options[k] == v
          abort = true
          break 
        end
      end
      next if abort
      
      matched_roles << server
    end
  end
  matched_roles.to_a
end

#fetch_with_capitate(variable, *args) ⇒ Object

Fetch. Displays usage message from recipe docs if variable not found.

See capistrano fetch for usage info.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/capitate/cap_ext/variables.rb', line 15

def fetch_with_capitate(variable, *args)
  begin
    fetch_without_capitate(variable, *args)
  rescue IndexError
    message = capitate.usage(variable)

    if message.blank?
      message = "\n      Please set :\#{variable} variable in your Capfile or deploy.rb\n\n      EOS\n    end\n  \n    raise IndexError, \"\\n\#{message}\"\n  end        \nend\n"