Class: Vagabond::InternalConfiguration

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/vagabond/internal_configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

included

Constructor Details

#initialize(vagabondfile, ui, options, args = {}) ⇒ InternalConfiguration

Returns a new instance of InternalConfiguration.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vagabond/internal_configuration.rb', line 16

def initialize(vagabondfile, ui, options, args={})
  @vagabondfile = vagabondfile
  @checksums = Mash.new
  @ui = ui
  @options = options
  create_store
  load_existing
  @config = Mash.new(
    :mappings => Mash.new,
    :template_mappings => Mash.new,
    :test_mappings => Mash.new
  ).merge(config)
  @force_bases = args[:force_bases] || []
  ensure_state
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/vagabond/internal_configuration.rb', line 11

def config
  @config
end

#force_basesObject

Returns the value of attribute force_bases.



14
15
16
# File 'lib/vagabond/internal_configuration.rb', line 14

def force_bases
  @force_bases
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/vagabond/internal_configuration.rb', line 13

def options
  @options
end

#uiObject (readonly)

Returns the value of attribute ui.



12
13
14
# File 'lib/vagabond/internal_configuration.rb', line 12

def ui
  @ui
end

Instance Method Details

#[](k) ⇒ Object



39
40
41
# File 'lib/vagabond/internal_configuration.rb', line 39

def [](k)
  @config[k]
end

#[]=(k, v) ⇒ Object



43
44
45
# File 'lib/vagabond/internal_configuration.rb', line 43

def []=(k,v)
  @config[k] = v
end

#cache_pathObject



143
144
145
146
147
148
# File 'lib/vagabond/internal_configuration.rb', line 143

def cache_path
  unless(@cache_path)
    FileUtils.mkdir_p(@cache_path = File.join(store_path, 'chef_cache'))
  end
  @cache_path
end

#cookbook_pathObject



150
151
152
153
154
155
156
# File 'lib/vagabond/internal_configuration.rb', line 150

def cookbook_path
  File.expand_path(
    File.join(
      File.dirname(__FILE__), 'cookbooks'
    )
  )
end

#create_storeObject



47
48
49
# File 'lib/vagabond/internal_configuration.rb', line 47

def create_store
  FileUtils.mkdir_p(store_path)
end

#dna_pathObject



71
72
73
# File 'lib/vagabond/internal_configuration.rb', line 71

def dna_path
  File.join(store_path, 'dna.json')
end

#ensure_stateObject



32
33
34
35
36
37
# File 'lib/vagabond/internal_configuration.rb', line 32

def ensure_state
  store_checksums
  write_dna_json
  write_solo_rb
  run_solo if solo_needed?
end

#get_checksum(path) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/vagabond/internal_configuration.rb', line 121

def get_checksum(path)
  if(File.exists?(path))
    s = Digest::SHA256.new
    s << File.read(path)
    s.hexdigest
  else
    ''
  end
end

#load_existingObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagabond/internal_configuration.rb', line 51

def load_existing
  if(File.exists?(path = File.join(store_path, 'vagabond.json')))
    @config = Mash.new(
      JSON.load(
        File.read(path)
      )
    )
  else
    @config = Mash.new
  end
end

#run_soloObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/vagabond/internal_configuration.rb', line 158

def run_solo
  ui.info ui.color('Ensuring expected system state (creating required base containers)', :yellow)
  ui.info ui.color('   - This can take a while...', :yellow)
  com = "#{options[:sudo]}chef-solo -j #{File.join(store_path, 'dna.json')} -c #{File.join(store_path, 'solo.rb')}"
  debug(com)
  cmd = Mixlib::ShellOut.new(com, :timeout => 12000, :live_stream => options[:debug])
  cmd.run_command
  cmd.error!
  ui.info ui.color('  -> COMPLETE!', :yellow)
end

#saveObject



169
170
171
172
173
# File 'lib/vagabond/internal_configuration.rb', line 169

def save
  File.open(File.join(store_path, 'vagabond.json'), 'w') do |file|
    file.write(JSON.dump(@config))
  end
end

#solo_needed?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/vagabond/internal_configuration.rb', line 131

def solo_needed?
  if(options[:force_solo])
    true
  elsif(options[:disable_solo])
    false
  else
    [dna_path, solo_path].detect do |path|
      @checksums[path] != get_checksum(path)
    end
  end
end

#solo_pathObject



75
76
77
# File 'lib/vagabond/internal_configuration.rb', line 75

def solo_path
  File.join(store_path, 'solo.rb')
end

#store_checksumsObject



115
116
117
118
119
# File 'lib/vagabond/internal_configuration.rb', line 115

def store_checksums
  [dna_path, solo_path].each do |path|
    @checksums[path] = get_checksum(path)
  end
end

#store_pathObject



63
64
65
66
67
68
69
# File 'lib/vagabond/internal_configuration.rb', line 63

def store_path
  FileUtils.mkdir_p(
    File.join(
      File.dirname(@vagabondfile.path), '.vagabond'
    )
  )
end

#write_dna_jsonObject



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
# File 'lib/vagabond/internal_configuration.rb', line 79

def write_dna_json
  conf = Mash.new(:bases => Mash.new, :customs => Mash.new)
  (Array(@vagabondfile[:nodes]).map(&:last).map{|i| i[:template]}.compact + Array(force_bases)).uniq.each do |t|
    conf[:bases][t] = Mash.new(:enabled => true) if BASE_TEMPLATES.include?(t.to_s)
  end
  Array(@vagabondfile[:templates]).each do |t_name, opts|
    if(BASE_TEMPLATES.include?(opts[:base].to_s))
      conf[:bases][opts[:base]] = Mash.new(:enabled => true)
      if(opts.has_key?(:memory) && !opts[:memory].is_a?(Hash))
        opts[:memory][:ram] = opts[:memory].to_s
      end
      conf[:customs][generated_name(t_name)] = opts
      config[:template_mappings][t_name] = generated_name(t_name)
    else
      ui.fatal "Invalid base template encountered: #{t}"
      ui.info ui.color("  -> Valid base templates: #{BASE_TEMPLATES.sort.join(', ')}", :red)
      exit EXIT_CODES[:invalid_base_template]
    end
  end
  File.open(dna_path, 'w') do |file|
    file.write(
      JSON.dump(
        :vagabond => conf,
        :run_list => %w(recipe[vagabond])
      )
    )
  end
  save
end

#write_solo_rbObject



109
110
111
112
113
# File 'lib/vagabond/internal_configuration.rb', line 109

def write_solo_rb
  File.open(solo_path, 'w') do |file|
    file.write("\nfile_cache_path \"#{cache_path}\"\ncookbook_path \"#{cookbook_path}\"\n")
  end
end