Class: Bakist::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bakist/config.rb

Direct Known Subclasses

RemoteConfig

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(royal_crown) ⇒ Config

Returns a new instance of Config.



14
15
16
# File 'lib/bakist/config.rb', line 14

def initialize(royal_crown)
  @royal_crown = royal_crown
end

Instance Attribute Details

#node_json_pathObject



61
62
63
64
65
66
67
# File 'lib/bakist/config.rb', line 61

def node_json_path
  @node_json_path ||= Tempfile.new(["node", ".json"]).tap do |file|
    puts JSON.pretty_generate(as_node_json) if debug?
    file.write(JSON.dump(as_node_json))
    file.close
  end.path
end

#royal_crownObject (readonly)

Returns the value of attribute royal_crown.



7
8
9
# File 'lib/bakist/config.rb', line 7

def royal_crown
  @royal_crown
end

#solo_rb_pathObject



53
54
55
56
57
58
59
# File 'lib/bakist/config.rb', line 53

def solo_rb_path
  @solo_rb_path ||= Tempfile.new(["solo", ".rb"]).tap do |file|
    puts as_solo_rb if debug?
    file.write(as_solo_rb)
    file.close
  end.path
end

Class Method Details

.from_file(royal_crown_path) ⇒ Object



9
10
11
12
# File 'lib/bakist/config.rb', line 9

def self.from_file(royal_crown_path)
  rc = Bakist::RoyalCrown.from_file(royal_crown_path)
  new(rc)
end

Instance Method Details

#as_node_jsonObject



34
35
36
# File 'lib/bakist/config.rb', line 34

def as_node_json
  compiled.node_attributes.to_hash.merge({ "recipes" => compiled.recipes })
end

#as_solo_rbObject



26
27
28
29
30
31
32
# File 'lib/bakist/config.rb', line 26

def as_solo_rb
  <<-SOLO_RB
    file_cache_path "#{chef_cache_path}"
    cookbook_path #{cookbook_paths.inspect}
    json_attribs "#{node_json_path}"
  SOLO_RB
end

#chef_cache_pathObject



38
39
40
41
42
43
# File 'lib/bakist/config.rb', line 38

def chef_cache_path
  "/var/chef/cache".tap do |cache_path|
    system(conditional_sudo("mkdir -p #{cache_path}")) \
      unless File.directory?(cache_path)
  end
end

#chef_soloObject



22
23
24
# File 'lib/bakist/config.rb', line 22

def chef_solo
  "chef-solo -c '#{solo_rb_path}' -l '#{log_level}'"
end

#compiledObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bakist/config.rb', line 76

def compiled
  @compiled ||= royal_crown.dup.tap do |working_royal_crown|
    while working_royal_crown["env_variable_switches"]
      working_royal_crown.delete("env_variable_switches").each do |variable, switch|
        switch.each do |value, inner|
          working_royal_crown.merge!(inner) if ENV[variable] == value
        end
      end
    end
  end
end

#cookbook_pathsObject



45
46
47
48
49
50
51
# File 'lib/bakist/config.rb', line 45

def cookbook_paths
  ([royal_crown_cookbooks_directory] + compiled.cookbook_paths).map do |path|
    File.expand_path(path, royal_crown_path)
  end.uniq.select do |path|
    File.directory?(path)
  end
end

#debug?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/bakist/config.rb', line 92

def debug?
  log_level == "debug"
end

#log_levelObject



88
89
90
# File 'lib/bakist/config.rb', line 88

def log_level
  ENV["LOG_LEVEL"] || "fatal"
end

#merge!(other) ⇒ Object



69
70
71
72
73
74
# File 'lib/bakist/config.rb', line 69

def merge!(other)
  royal_crown.recipes += other.royal_crown.recipes
  royal_crown.cookbook_paths += other.royal_crown.cookbook_paths
  royal_crown.node_attributes.merge!(other.royal_crown.node_attributes)
  royal_crown.env_variable_switches.merge!(other.royal_crown.env_variable_switches)
end

#run_chefObject



18
19
20
# File 'lib/bakist/config.rb', line 18

def run_chef      
  exec(conditional_sudo("bash -c '#{chef_solo}'"))
end