Class: Forger::Create::Params

Inherits:
Base
  • Object
show all
Defined in:
lib/forger/create/params.rb

Constant Summary

Constants inherited from Base

Base::BUILD_ROOT, Base::SCRIPTS_INFO_PATH

Instance Method Summary collapse

Methods inherited from Base

#derandomize, #initialize, #randomize

Constructor Details

This class inherits a constructor from Forger::Base

Instance Method Details

#cleanupObject



27
28
29
# File 'lib/forger/create/params.rb', line 27

def cleanup
  FileUtils.rm_f("#{Forger.root}/tmp/user-data.txt")
end

#decorate_params(params) ⇒ Object



12
13
14
15
16
# File 'lib/forger/create/params.rb', line 12

def decorate_params(params)
  upsert_name_tag!(params)
  replace_runtime_options!(params)
  params
end

#defaultsObject

Hard coded sensible defaults. Can be overridden easily with profiles



99
100
101
102
103
104
# File 'lib/forger/create/params.rb', line 99

def defaults
  {
    max_count: 1,
    min_count: 1,
  }
end

#generateObject

deep_symbolize_keys is ran at the very end only. up until that point we’re dealing with String keys.



5
6
7
8
9
10
# File 'lib/forger/create/params.rb', line 5

def generate
  cleanup
  params = Forger::Profile.new(@options).load
  decorate_params(params)
  normalize_launch_template(params).deep_symbolize_keys
end

#normalize_launch_template(params) ⇒ Object

Allow adding launch template as a simple string.

Standard structure: {

launch_template: { launch_template_name: "TestLaunchTemplate" },

}

Simple string:

launch_template: "TestLaunchTemplate",

When launch_template is a simple String it will get transformed to the standard structure.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/forger/create/params.rb', line 84

def normalize_launch_template(params)
  if params["launch_template"].is_a?(String)
    launch_template_identifier = params["launch_template"]
    launch_template = if launch_template_identifier =~ /^lt-/
        { "launch_template_id" => launch_template_identifier }
      else
        { "launch_template_name" => launch_template_identifier }
      end
    params["launch_template"] = launch_template
  end
  params
end

#replace_runtime_options!(params) ⇒ Object

Expose a list of runtime params that are convenient. Try to limit the number of options from the cli to keep tool simple. Most options can be easily control through profile files. The runtime options that are very convenient to have at the CLI are modified here.



22
23
24
25
# File 'lib/forger/create/params.rb', line 22

def replace_runtime_options!(params)
  params["image_id"] = @options[:source_ami_id] if @options[:source_ami_id]
  params
end

#upsert_name_tag!(params) ⇒ Object

Adds instance ec2 tag if not already provided



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
# File 'lib/forger/create/params.rb', line 32

def upsert_name_tag!(params)
  specs = params["tag_specifications"] || []

  # insert an empty spec placeholder if one not found
  spec = specs.find do |s|
    s["resource_type"] == "instance"
  end
  unless spec
    spec = {
        "resource_type" => "instance",
        "tags" => []
      }
    specs << spec
  end
  # guaranteed there's a tag_specifications with resource_type instance at this point

  tags = spec["tags"] || []

  unless tags.map { |t| t["key"] }.include?("Name")
    tags << { "key" => "Name", "value" => @name }
  end

  specs = specs.map do |s|
    # replace the name tag value
    if s["resource_type"] == "instance"
      {
        "resource_type" => "instance",
        "tags" => tags
      }
    else
      s
    end
  end

  params["tag_specifications"] = specs
  params
end