Module: Enscalator::Plugins::ElasticsearchBitnami

Defined in:
lib/enscalator/plugins/elasticsearch_bitnami.rb

Overview

Elasticsearch related configuration

Constant Summary collapse

STORAGE =

Supported storage types in AWS

[:ebs, :'instance-store']
ARCH =

Supported Elasticsearch image architectures

[:amd64, :i386]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_mapping(storage: :ebs, arch: :amd64) ⇒ Hash

Get ami region/virtualization type mapping

Parameters:

  • storage (Symbol) (defaults to: :ebs)

    image root storage

  • arch (Symbol) (defaults to: :amd64)

    image architecture type

Returns:

  • (Hash)

    mapping



18
19
20
21
22
# File 'lib/enscalator/plugins/elasticsearch_bitnami.rb', line 18

def get_mapping(storage: :ebs, arch: :amd64)
  fail ArgumentError, "storage can only be one of #{STORAGE}" unless STORAGE.include? storage
  fail ArgumentError, "arch can only be one of #{ARCH}" unless ARCH.include? arch
  fetch_mapping(storage, arch)
end

.get_release_version(storage: :ebs, arch: :amd64) ⇒ Hash

Get ami release version string

Parameters:

  • storage (Symbol) (defaults to: :ebs)

    image root storage

  • arch (Symbol) (defaults to: :amd64)

    image architecture type

Returns:

  • (Hash)

    mapping



29
30
31
32
33
34
35
36
# File 'lib/enscalator/plugins/elasticsearch_bitnami.rb', line 29

def get_release_version(storage: :ebs, arch: :amd64)
  fail ArgumentError, "storage can only be one of #{STORAGE}" unless STORAGE.include? storage
  fail ArgumentError, "arch can only be one of #{ARCH}" unless ARCH.include? arch
  fetch_versions
    .select { |r| r.root_storage == storage && r.arch == arch }
    .map { |v| v.version.to_s }.uniq.first
    .gsub(/[-][\w\d]/, '')
end

Instance Method Details

#aws_account_idString

Get aws account id

Returns:

  • (String)

    account id



110
111
112
113
# File 'lib/enscalator/plugins/elasticsearch_bitnami.rb', line 110

def 
  # noinspection RubyArgCount
  Aws::IAM::Client.new.get_user.user.arn.split(':')[4]
end

#elasticsearch_init(storage_name, allocated_storage: 5, instance_type: 't2.medium', properties: {}, zone_name: nil) ⇒ Object

Create new elasticsearch instance

Parameters:

  • storage_name (String)

    storage name

  • allocated_storage (Integer) (defaults to: 5)

    size of instance primary storage

  • instance_type (String) (defaults to: 't2.medium')

    instance type

  • properties (Hash) (defaults to: {})

    additional properties

  • zone_name (String) (defaults to: nil)

    route53 zone name



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/enscalator/plugins/elasticsearch_bitnami.rb', line 122

def elasticsearch_init(storage_name,
                       allocated_storage: 5,
                       instance_type: 't2.medium',
                       properties: {},
                       zone_name: nil)

  @es_key_name = gen_ssh_key_name("Elasticsearch#{storage_name}", region, stack_name)
  pre_run { create_ssh_key(@es_key_name, region, force_create: false) }

  mapping 'AWSElasticsearchAMI', ElasticsearchBitnami.get_mapping

  parameter_allocated_storage "Elasticsearch#{storage_name}",
                              default: allocated_storage,
                              min: 5,
                              max: 1024

  parameter_ec2_instance_type "Elasticsearch#{storage_name}", type: instance_type

  properties[:KeyName] = @es_key_name
  properties[:InstanceType] = ref("Elasticsearch#{storage_name}InstanceType")

  version_tag = {
    Key: 'Version',
    Value: ElasticsearchBitnami.get_release_version
  }

  cluster_name_tag = {
    Key: 'ClusterName',
    Value: storage_name.downcase
  }

  plugin_tags = [version_tag, cluster_name_tag]

  # Set instance tags
  if properties.key?(:Tags) && !properties[:Tags].empty?
    properties[:Tags].concat(plugin_tags)
  else
    properties[:Tags] = plugin_tags
  end

  # Configure instance using user-data
  if !properties.key?(:UserData) || !properties[:UserData].empty?
    properties[:UserData] = Base64.encode64(read_user_data('elasticsearch'))
  end

  # Assign IAM role to instance
  properties[:IamInstanceProfile] = iam_instance_profile_with_full_access(storage_name, *%w(ec2 s3))

  storage_resource_name = "Elasticsearch#{storage_name}"
  instance_vpc storage_resource_name,
               find_in_map('AWSElasticsearchAMI', ref('AWS::Region'), :hvm),
               ref_application_subnets.first,
               [ref_private_security_group, ref_resource_security_group],
               depends_on: [],
               properties: properties

  # create s3 bucket for cluster snapshots
   = 
  bucket_name = "elasticsearch-bitnami-#{region}-#{}"
  resource "Elasticsearch#{storage_name}S3Bucket",
           Type: 'AWS::S3::Bucket',
           DeletionPolicy: 'Retain',
           Properties: {
             BucketName: bucket_name
           }

  # create a DNS record in route53 for instance private ip
  record_name = %W(#{storage_name.downcase.dasherize} #{region} #{zone_name}).join('.')
  create_single_dns_record("#{storage_name}PrivateZone",
                           stack_name,
                           zone_name,
                           record_name,
                           resource_records: [get_att(storage_resource_name, 'PrivateIp')])
end