Class: Cheftacular::Getter

Inherits:
Object
  • Object
show all
Defined in:
lib/cheftacular/getter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, config) ⇒ Getter

Returns a new instance of Getter.



4
5
6
# File 'lib/cheftacular/getter.rb', line 4

def initialize options, config
  @options, @config  = options, config
end

Instance Method Details

#get_address_hash(node_name, load_out_of_env_addresses = false, ret = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cheftacular/getter.rb', line 138

def get_address_hash node_name, load_out_of_env_addresses=false, ret={}
  break_on_found = false

  @config['chef_environments'].each do |env|
    next if !load_out_of_env_addresses && !@config.has_key?(env) #in case the env hashes are not loaded
    @config['initializer'].initialize_data_bags_for_environment(env, false, ['addresses']) if @options['env'] != env

    @config[env]['addresses_bag_hash']['addresses'].each do |serv_hash|
      if serv_hash['name'] == node_name
        ret[serv_hash['name']] = { "dn" => serv_hash['dn'], "priv" => serv_hash['address'], "pub" => serv_hash['public'] }
        break_on_found = true

        break
      end
    end

    break if break_on_found
  end

  ret
end

#get_addresses_hash(env = 'staging', ret_hash = {}, mode = "") ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/cheftacular/getter.rb', line 114

def get_addresses_hash env='staging', ret_hash={}, mode=""
  @config[env]['addresses_bag_hash']['addresses'].each do |serv_hash|
    ret_hash[serv_hash['public']] = serv_hash
    
  end if @config[env].has_key?('addresses_bag_hash')

  ret_hash
end

#get_current_databaseObject



94
95
96
# File 'lib/cheftacular/getter.rb', line 94

def get_current_database
  get_current_repo_config['database']
end

#get_current_real_node_name(other_node_name = '', ret = '') ⇒ Object



170
171
172
173
174
175
# File 'lib/cheftacular/getter.rb', line 170

def get_current_real_node_name other_node_name='', ret=''
  ret << @options['env'] + @config['cheftacular']['node_name_separator']
  ret << ( other_node_name.blank? ? @options['node_name'] : other_node_name )

  ret
end

#get_current_repo_configObject



98
99
100
101
102
103
104
# File 'lib/cheftacular/getter.rb', line 98

def get_current_repo_config
  if @config['cheftacular']['repositories'].has_key?(@options['role'])
    @config['cheftacular']['repositories'][@options['role']]
  else
    @options['role']
  end
end

#get_current_role_map(run_list, ret = {}) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/cheftacular/getter.rb', line 106

def get_current_role_map run_list, ret={}
  @config['cheftacular']['role_maps'].each_pair do |main_role, role_hash|
    ret = role_hash if run_list.include?("role[#{ role_hash['role_name'] }]")
  end

  ret
end

#get_current_stackObject



90
91
92
# File 'lib/cheftacular/getter.rb', line 90

def get_current_stack 
  get_current_repo_config['stack']
end

#get_db_primary_node_and_nodesObject



123
124
125
126
127
128
# File 'lib/cheftacular/getter.rb', line 123

def get_db_primary_node_and_nodes
  nodes          = get_true_node_objects true
  target_db_role = get_current_repo_config['db_primary_host_role']
  
  [@config['parser'].exclude_nodes( nodes, [{ unless: "role[#{ target_db_role }]" }, { if: { not_env: @options['env'] } }], true), nodes]
end

#get_repo_names_for_repositories(ret = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/cheftacular/getter.rb', line 160

def get_repo_names_for_repositories ret={}
  @config['cheftacular']['repositories'].each_pair do |name, repo_hash|
    ret[repo_hash['repo_name']]         = repo_hash
    
    ret[repo_hash['repo_name']]['role'] = name
  end

  ret
end

#get_repository_from_role_name(name, *args) ⇒ Object



8
9
10
# File 'lib/cheftacular/getter.rb', line 8

def get_repository_from_role_name name, *args
  @config['dummy_sshkit'].get_repository_from_role_name( name, @config['cheftacular']['repositories'], args )
end

#get_split_branch_hash(ret = {}) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/cheftacular/getter.rb', line 130

def get_split_branch_hash ret={}
  @config['cheftacular']['repositories'].each_pair do |name, repo_hash|
    ret[repo_hash['name']] = repo_hash if repo_hash.has_key?('has_split_branches') && repo_hash['has_split_branches']
  end

  ret
end

#get_true_node_objects(get_all_nodes = false, get_new_nodes = false) ⇒ Object

TODO

if ridley changes its parsing strategy



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/cheftacular/getter.rb', line 13

def get_true_node_objects get_all_nodes=false, get_new_nodes=false
  nodes, all_nodes, names, iter_arr, file_cache_nodes, h = [],[],[],[],[],{}

  @config['chef_nodes'] = @config['ridley'].node.all

  @config['helper'].completion_rate? 0, __method__

  file_cache_nodes = @config['filesystem'].check_nodes_file_cache if @config['filesystem'].compare_file_node_cache_against_chef_nodes('equal')

  @config['chef_nodes'].each do |n|
    true_obj = if !file_cache_nodes.empty? && @config['parser'].array_of_nodes_contains_node_name?(file_cache_nodes, n.name)
                 file_cache_nodes[@config['parser'].index_of_node_name_in_array_of_nodes(file_cache_nodes, n.name)]
               else
                 @config['filesystem'].cleanup_file_caches('current-nodes')

                 @config['ridley'].node.find(n.name)
               end

    iter_arr << n.name

    progress_value = (( iter_arr.length.to_f/@config['chef_nodes'].length.to_f )*100 ).floor

    @config['helper'].completion_rate? progress_value, __method__

    all_nodes << true_obj

    next if !get_all_nodes && true_obj.chef_environment != @options['env'] && true_obj.chef_environment != '_default'

    if get_all_nodes
      h[n.name] = true_obj
      names << n.name

      next
    end

    if @options['role'] == 'all'
      next if true_obj.chef_environment == '_default'

      h[n.name] = true_obj
      names << n.name

      next
    end

    if @options['node_name'] && true_obj.name == @options['node_name']
      h[n.name] = true_obj
      names << n.name

      next
    end

    if @options['address'] && true_obj.public_ipaddress == @options['address']
      h[n.name] = true_obj
      names << n.name

      next
    end

    unless ( @options['address'] || @options['node_name'] )
      if true_obj.run_list.include?("role[#{ @options['role'] }]")
        h[n.name] = true_obj
        names << n.name

        next #not needed here but good to keep in mind
      end
    end
  end

  names.sort.each { |name| nodes << h[name] }

  @config['filesystem'].write_nodes_file_cache(all_nodes) unless @config['filesystem'].compare_file_node_cache_against_chef_nodes('equal')

  puts("") unless @options['quiet']
  
  nodes
end