Class: Formatron::CloudFormation::Template::VPC::Subnet::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/cloud_formation/template/vpc/subnet/instance.rb,
lib/formatron/cloud_formation/template/vpc/subnet/instance/setup.rb,
lib/formatron/cloud_formation/template/vpc/subnet/instance/policy.rb,
lib/formatron/cloud_formation/template/vpc/subnet/instance/block_devices.rb,
lib/formatron/cloud_formation/template/vpc/subnet/instance/security_group.rb

Overview

generates CloudFormation instance resources rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: BlockDevices, Policy, SecurityGroup, Setup

Constant Summary collapse

INSTANCE_PREFIX =
'instance'
ROLE_PREFIX =
'role'
INSTANCE_PROFILE_PREFIX =
'instanceProfile'
WAIT_CONDITION_HANDLE_PREFIX =
'waitConditionHandle'
WAIT_CONDITION_PREFIX =
'waitCondition'
PRIVATE_RECORD_SET_PREFIX =
'privateRecordSet'
PUBLIC_RECORD_SET_PREFIX =
'publicRecordSet'
PUBLIC_ALIAS_RECORD_SET_PREFIX =
'publicAliasRecordSet'
PRIVATE_ALIAS_RECORD_SET_PREFIX =
'privateAliasRecordSet'
VOLUME_PREFIX =
'volume'
VOLUME_ATTACHMENT_PREFIX =
'volumeAttachment'

Instance Method Summary collapse

Constructor Details

#initialize(instance:, key_pair:, administrator_name:, administrator_password:, availability_zone:, subnet_guid:, hosted_zone_name:, vpc_guid:, vpc_cidr:, kms_key:, private_hosted_zone_id:, public_hosted_zone_id:, bucket:, name:, target:) ⇒ Instance

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/ParameterLists



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/formatron/cloud_formation/template/vpc/subnet/instance.rb', line 33

def initialize(
  instance:,
  key_pair:,
  administrator_name:,
  administrator_password:,
  availability_zone:,
  subnet_guid:,
  hosted_zone_name:,
  vpc_guid:,
  vpc_cidr:,
  kms_key:,
  private_hosted_zone_id:,
  public_hosted_zone_id:,
  bucket:,
  name:,
  target:
)
  @instance = instance
  @guid = @instance.guid
  @setup = @instance.setup
  @block_devices = @instance.block_device
  @instance_id = "#{INSTANCE_PREFIX}#{@guid}"
  @role_id = "#{ROLE_PREFIX}#{@guid}"
  @instance_profile_id = "#{INSTANCE_PROFILE_PREFIX}#{@guid}"
  @wait_condition_handle_id =
    "#{WAIT_CONDITION_HANDLE_PREFIX}#{@guid}"
  @wait_condition_id =
    "#{WAIT_CONDITION_PREFIX}#{@guid}"
  @policy = @instance.policy
  @security_group = @instance.security_group
  @security_group_id =
    "#{SecurityGroup::SECURITY_GROUP_PREFIX}#{@guid}"
  @availability_zone = availability_zone
  @instance_type = @instance.instance_type || 't2.micro'
  @os = @instance.os || 'ubuntu'
  @ami = @instance.ami
  @key_pair = key_pair
  @administrator_name = administrator_name
  @administrator_password = administrator_password
  @subnet_guid = subnet_guid
  @subnet_id = "#{Subnet::SUBNET_PREFIX}#{@subnet_guid}"
  @sub_domain = @instance.sub_domain
  @hosted_zone_name = hosted_zone_name
  @source_dest_check = @instance.source_dest_check
  @source_dest_check =
    @source_dest_check.nil? ? true : @source_dest_check
  @vpc_guid = vpc_guid
  @vpc_cidr = vpc_cidr
  @kms_key = kms_key
  @private_hosted_zone_id = private_hosted_zone_id
  @public_hosted_zone_id = public_hosted_zone_id
  @private_record_set_id =
    "#{PRIVATE_RECORD_SET_PREFIX}#{@guid}"
  @public_record_set_id =
    "#{PUBLIC_RECORD_SET_PREFIX}#{@guid}"
  @public_aliases = @instance.public_alias
  @private_aliases = @instance.private_alias
  @volumes = @instance.volume
  @bucket = bucket
  @name = name
  @target = target
end

Instance Method Details

#merge(resources:, outputs:) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
# File 'lib/formatron/cloud_formation/template/vpc/subnet/instance.rb', line 101

def merge(resources:, outputs:)
  @outputs = outputs
  resources[@role_id] = Resources::IAM.role
  resources[@instance_profile_id] = Resources::IAM.instance_profile(
    role: @role_id
  )
  policy = Policy.new(
    policy: @policy,
    instance_guid: @guid,
    kms_key: @kms_key,
    bucket: @bucket,
    name: @name,
    target: @target
  )
  policy.merge resources: resources
  security_group = SecurityGroup.new(
    os: @os,
    security_group: @security_group,
    instance_guid: @guid,
    vpc_guid: @vpc_guid,
    vpc_cidr: @vpc_cidr
  )
  security_group.merge resources: resources
  resources[@wait_condition_handle_id] =
    Resources::CloudFormation.wait_condition_handle
  instance = Resources::EC2.instance(
    instance_profile: @instance_profile_id,
    availability_zone: @availability_zone,
    instance_type: @instance_type,
    key_name: @key_pair,
    administrator_name: @administrator_name,
    administrator_password: @administrator_password,
    subnet: @subnet_id,
    name: "#{@sub_domain}.#{@hosted_zone_name}",
    wait_condition_handle: @wait_condition_handle_id,
    security_group: @security_group_id,
    logical_id: @instance_id,
    source_dest_check: @source_dest_check,
    os: @os,
    ami: @ami
  )
  setup = Setup.new(
    setup: @setup,
    sub_domain: @sub_domain,
    hosted_zone_name: @hosted_zone_name,
    os: @os,
    wait_condition_handle: @wait_condition_handle_id
  )
  setup.merge instance: instance
  block_devices = BlockDevices.new(
    block_devices: @block_devices
  )
  block_devices.merge properties: instance[:Properties]
  resources[@instance_id] = instance
  outputs[@instance_id] = Template.output(
    Template.ref(@instance_id)
  )
  resources[@wait_condition_id] =
    Resources::CloudFormation.wait_condition(
      wait_condition_handle: @wait_condition_handle_id,
      instance: @instance_id
    )
  resources[@private_record_set_id] =
    Resources::Route53.record_set(
      hosted_zone_id: Template.ref(@private_hosted_zone_id),
      sub_domain: @sub_domain,
      hosted_zone_name: @hosted_zone_name,
      instance: @instance_id,
      attribute: 'PrivateIp'
    )
  resources[@public_record_set_id] =
    Resources::Route53.record_set(
      hosted_zone_id: @public_hosted_zone_id,
      sub_domain: @sub_domain,
      hosted_zone_name: @hosted_zone_name,
      instance: @instance_id,
      attribute: 'PublicIp'
    ) unless @public_hosted_zone_id.nil?
  _add_public_aliases resources unless @public_hosted_zone_id.nil?
  _add_private_aliases resources
  _add_volumes resources
end