Class: Hocho::InventoryProviders::Ec2

Inherits:
Base
  • Object
show all
Defined in:
lib/hocho/inventory_providers/ec2.rb

Defined Under Namespace

Modules: TemplateHelper Classes: HostnameTemplate, RunlistTemplate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, filters: nil, hostname_template: "<%= instance.private_dns_name %>", runlist_template: nil, cache_path: nil, cache_duration: 3600, cache_version: nil) ⇒ Ec2

Returns a new instance of Ec2.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hocho/inventory_providers/ec2.rb', line 39

def initialize(region:, filters: nil, hostname_template: "<%= instance.private_dns_name %>", runlist_template: nil, cache_path: nil, cache_duration: 3600, cache_version: nil)
  @region = region
  @filters = filters
  @cache_path = cache_path
  @cache_duration = cache_duration&.to_i
  @cache_version = cache_version
  @hostname_template = Class.new(HostnameTemplate) do |c|
    c.erb = ERB.new(hostname_template, trim_mode: '-')
  end
  @runlist_template = eval("Class.new(RunlistTemplate){ def result; #{runlist_template}\nend; }")
end

Instance Attribute Details

#cache_durationObject (readonly)

Returns the value of attribute cache_duration.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def cache_duration
  @cache_duration
end

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def cache_path
  @cache_path
end

#cache_versionObject (readonly)

Returns the value of attribute cache_version.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def cache_version
  @cache_version
end

#filtersObject (readonly)

Returns the value of attribute filters.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def filters
  @filters
end

#hostname_templateObject (readonly)

Returns the value of attribute hostname_template.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def hostname_template
  @hostname_template
end

#regionObject (readonly)

Returns the value of attribute region.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def region
  @region
end

#runlist_templateObject (readonly)

Returns the value of attribute runlist_template.



51
52
53
# File 'lib/hocho/inventory_providers/ec2.rb', line 51

def runlist_template
  @runlist_template
end

Instance Method Details

#cache!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hocho/inventory_providers/ec2.rb', line 84

def cache!
  return yield unless cache_enabled?
  if ::File.exist?(cache_path)
    @cache = YAML.load_file(cache_path)
  end
  @cache = nil if !@cache&.key?(:ts) || (Time.now - @cache[:ts]) > cache_duration
  @cache = nil if cache_version && @cache[:user_version] != cache_version

  unless @cache
    @cache = {ts: Time.now, user_version: cache_version, content: yield}
    FileUtils.mkdir_p ::File.dirname(cache_path)
    ::File.write cache_path, YAML.dump(@cache)
  end
  @cache[:content]
end

#cache_enabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/hocho/inventory_providers/ec2.rb', line 80

def cache_enabled?
  @cache_path && @cache_duration
end

#ec2Object



53
54
55
# File 'lib/hocho/inventory_providers/ec2.rb', line 53

def ec2
  @ec2 ||= Aws::EC2::Client.new(region: region)
end

#fetch_hostsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hocho/inventory_providers/ec2.rb', line 57

def fetch_hosts
  vpcs = ec2.describe_vpcs().flat_map do |page|
    page.vpcs.map do |vpc|
      [vpc.vpc_id, vpc]
    end
  end.to_h
  subnets= ec2.describe_subnets().flat_map do |page|
    page.subnets.map do |subnet|
      [subnet.subnet_id, subnet]
    end
  end.to_h
  ec2.describe_instances(filters: filters).flat_map do |page|
    page.reservations.flat_map do |reservation|
      reservation.instances.map do |instance|
        next if instance.state.name == 'terminated' || instance.state.name == 'terminating'
        vpc = vpcs[instance.vpc_id]
        subnet = subnets[instance.subnet_id]
        fetch_instance(instance, vpc, subnet)
      end.compact
    end
  end
end

#hostsObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hocho/inventory_providers/ec2.rb', line 100

def hosts
  @hosts ||= cache! { fetch_hosts }.map do |host_data|
    Host.new(
      host_data.fetch(:name),
      providers: self.class,
      properties: host_data[:properties],
      tags: host_data[:tags],
      ssh_options: host_data[:ssh_options],
    )
  end
end