Class: Forger::Profile

Inherits:
Base
  • Object
show all
Includes:
Template
Defined in:
lib/forger/profile.rb

Constant Summary

Constants inherited from Base

Base::BUILD_ROOT, Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

Methods included from Template

#context

Methods inherited from Base

#derandomize, #initialize, #randomize

Constructor Details

This class inherits a constructor from Forger::Base

Instance Method Details

#check!Object



14
15
16
17
18
19
20
21
# File 'lib/forger/profile.rb', line 14

def check!
  file = profile_file(profile_name)
  return if File.exist?(file)

  puts "Unable to find a #{file.color(:green)} profile file."
  puts "Please double check that it exists or that you specified the right profile.".color(:red)
  exit 1
end

#loadObject



5
6
7
8
9
10
11
12
# File 'lib/forger/profile.rb', line 5

def load
  return @profile_params if @profile_params

  check!

  file = profile_file(profile_name)
  @profile_params = load_profile(file)
end

#load_profile(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/forger/profile.rb', line 23

def load_profile(file)
  return {} unless File.exist?(file)

  base_file, base_data = profile_file(:base), {}
  if File.exist?(base_file)
    puts "Detected profiles/base.yml"
    base_data = yaml_load(base_file)
  end

  puts "Using profile: #{file}".color(:green)
  data = yaml_load(file)
  data = base_data.merge(data)
  data.has_key?("run_instances") ? data["run_instances"] : data
end

#profile_file(name) ⇒ Object



73
74
75
# File 'lib/forger/profile.rb', line 73

def profile_file(name)
  "#{Forger.root}/profiles/#{name}.yml"
end

#profile_nameObject

Determines a valid profile_name. Falls back to default



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/forger/profile.rb', line 56

def profile_name
  # allow user to specify the path also
  if @options[:profile] && File.exist?(@options[:profile])
    filename_profile = File.basename(@options[:profile], '.yml')
  end

  name = derandomize(@name)
  if File.exist?(profile_file(name))
    name_profile = name
  end

  filename_profile ||
  @options[:profile] ||
  name_profile || # conventional profile is the name of the ec2 instance
  "default"
end

#yaml_load(file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/forger/profile.rb', line 38

def yaml_load(file)
  text = RenderMePretty.result(file, context: context)
  begin
    data = YAML.load(text) # data
    data ? data : {} # in case the file is empty
  rescue Psych::SyntaxError => e
    tmp_file = file.sub("profiles", Forger.build_root)
    FileUtils.mkdir_p(File.dirname(tmp_file))
    IO.write(tmp_file, text)
    puts "There was an error evaluating in your yaml file #{file}".color(:red)
    puts "The evaludated yaml file has been saved at #{tmp_file} for debugging."
    puts "ERROR: #{e.message}"
    exit 1
  end
end