Class: Soloist::ChefConfigGenerator

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/soloist/chef_config_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#camelize, #fileify, #walk_up_and_find_file, #with_or_without_dot

Constructor Details

#initialize(config, relative_path_to_soloistrc) ⇒ ChefConfigGenerator

Returns a new instance of ChefConfigGenerator.



7
8
9
10
11
12
13
# File 'lib/soloist/chef_config_generator.rb', line 7

def initialize(config, relative_path_to_soloistrc)
  @recipes = []
  @cookbook_paths = []
  @cookbook_gems = []
  @preserved_environment_variables = %w{PATH BUNDLE_PATH GEM_HOME GEM_PATH RAILS_ENV RACK_ENV}
  merge_config(config, relative_path_to_soloistrc)
end

Instance Attribute Details

#cookbook_gems ⇒ Object (readonly)

Returns the value of attribute cookbook_gems.



15
16
17
# File 'lib/soloist/chef_config_generator.rb', line 15

def cookbook_gems
  @cookbook_gems
end

#cookbook_paths ⇒ Object (readonly)

Returns the value of attribute cookbook_paths.



15
16
17
# File 'lib/soloist/chef_config_generator.rb', line 15

def cookbook_paths
  @cookbook_paths
end

#preserved_environment_variables ⇒ Object (readonly)

Returns the value of attribute preserved_environment_variables.



15
16
17
# File 'lib/soloist/chef_config_generator.rb', line 15

def preserved_environment_variables
  @preserved_environment_variables
end

#recipes ⇒ Object

Returns the value of attribute recipes.



16
17
18
# File 'lib/soloist/chef_config_generator.rb', line 16

def recipes
  @recipes
end

Instance Method Details

#append_path(paths, relative_path_to_soloistrc) ⇒ Object



24
25
26
27
28
# File 'lib/soloist/chef_config_generator.rb', line 24

def append_path(paths, relative_path_to_soloistrc)
  paths.map do |path|
    path.slice(0,1) == '/' ? path : "#{FileUtils.pwd}/#{relative_path_to_soloistrc}/#{path}"
  end
end

#json_file ⇒ Object



71
72
73
# File 'lib/soloist/chef_config_generator.rb', line 71

def json_file
  json_hash.to_json
end

#json_hash ⇒ Object



65
66
67
68
69
# File 'lib/soloist/chef_config_generator.rb', line 65

def json_hash
  {
    "recipes" => @recipes
  }
end

#merge_config(sub_hash, relative_path_to_soloistrc) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/soloist/chef_config_generator.rb', line 30

def merge_config(sub_hash, relative_path_to_soloistrc)
  sub_hash = support_old_format(sub_hash)
  if sub_hash["recipes"]
    @recipes = (@recipes + sub_hash["recipes"]).uniq
  end
  if sub_hash["cookbook_paths"]
    @cookbook_paths = (@cookbook_paths + append_path(sub_hash["cookbook_paths"], relative_path_to_soloistrc)).uniq
  end
  if sub_hash["cookbook_gems"]
    (@cookbook_gems += sub_hash["cookbook_gems"]).uniq!
  end
  if sub_hash["env_variable_switches"]
    merge_env_variable_switches(sub_hash["env_variable_switches"], relative_path_to_soloistrc)
  end
end

#merge_env_variable_switches(hash_to_merge, relative_path_to_soloistrc) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/soloist/chef_config_generator.rb', line 46

def merge_env_variable_switches(hash_to_merge, relative_path_to_soloistrc)
  hash_to_merge.keys.each do |variable|
    @preserved_environment_variables << variable
    ENV[variable] && ENV[variable].split(',').each do |env_variable_value|
      sub_hash = hash_to_merge[variable] && hash_to_merge[variable][env_variable_value]
      merge_config(sub_hash, relative_path_to_soloistrc) if sub_hash
    end
  end
end

#preserved_environment_variables_string ⇒ Object



75
76
77
78
79
80
# File 'lib/soloist/chef_config_generator.rb', line 75

def preserved_environment_variables_string
  variable_array = []
  preserved_environment_variables.map do |env_variable|
    "#{env_variable}=#{ENV[env_variable]}" unless ENV[env_variable].nil?
  end.compact.join(" ")
end

#solo_rb ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/soloist/chef_config_generator.rb', line 56

def solo_rb
  all_cookbook_paths = cookbook_paths
  cookbook_gems.each do |gem_cookbook|
    require gem_cookbook
    all_cookbook_paths << Kernel.const_get(camelize(gem_cookbook)).const_get('COOKBOOK_PATH')
  end
  "cookbook_path #{all_cookbook_paths.inspect}"
end

#support_old_format(hash) ⇒ Object



18
19
20
21
22
# File 'lib/soloist/chef_config_generator.rb', line 18

def support_old_format(hash)
  hash['recipes'] ||= hash.delete('Recipes')
  hash['cookbook_paths'] ||= hash.delete('Cookbook_Paths')
  hash
end