Class: Cumulus::EC2::InstanceConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/models/InstanceConfig.rb

Overview

Public: An object representing configuration for a network interface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, json = nil) ⇒ InstanceConfig

Public: Constructor

json - a hash containing the JSON configuration for the group



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ec2/models/InstanceConfig.rb', line 37

def initialize(name, json = nil)
  @name = name
  if !json.nil?
    @ebs_optimized = json["ebs-optimized"] || false
    @placement_group = json["placement-group"]
    @profile = json["profile"]
    @image = json["image"]
    @key_name = json["key-name"]
    @monitoring = json["monitoring"] || false
    @network_interfaces = json["network-interfaces"] || 0
    @source_dest_check = json["source-dest-check"]
    @private_ip_address = json["private-ip-address"]
    @security_groups = json["security-groups"] || []
    @subnet = json["subnet"]
    @tenancy = json["tenancy"] || "default"
    @type = json["type"]
    @user_data = json["user-data"]
    @volume_groups = json["volume-groups"] || []
    @tags = json["tags"] || {}
  end
end

Instance Attribute Details

#ebs_optimizedObject (readonly)

Returns the value of attribute ebs_optimized.



17
18
19
# File 'lib/ec2/models/InstanceConfig.rb', line 17

def ebs_optimized
  @ebs_optimized
end

#imageObject (readonly)

Returns the value of attribute image.



20
21
22
# File 'lib/ec2/models/InstanceConfig.rb', line 20

def image
  @image
end

#key_nameObject (readonly)

Returns the value of attribute key_name.



21
22
23
# File 'lib/ec2/models/InstanceConfig.rb', line 21

def key_name
  @key_name
end

#monitoringObject (readonly)

Returns the value of attribute monitoring.



22
23
24
# File 'lib/ec2/models/InstanceConfig.rb', line 22

def monitoring
  @monitoring
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/ec2/models/InstanceConfig.rb', line 16

def name
  @name
end

#network_interfacesObject (readonly)

Returns the value of attribute network_interfaces.



23
24
25
# File 'lib/ec2/models/InstanceConfig.rb', line 23

def network_interfaces
  @network_interfaces
end

#placement_groupObject (readonly)

Returns the value of attribute placement_group.



18
19
20
# File 'lib/ec2/models/InstanceConfig.rb', line 18

def placement_group
  @placement_group
end

#private_ip_addressObject (readonly)

Returns the value of attribute private_ip_address.



25
26
27
# File 'lib/ec2/models/InstanceConfig.rb', line 25

def private_ip_address
  @private_ip_address
end

#profileObject (readonly)

Returns the value of attribute profile.



19
20
21
# File 'lib/ec2/models/InstanceConfig.rb', line 19

def profile
  @profile
end

#security_groupsObject (readonly)

Returns the value of attribute security_groups.



26
27
28
# File 'lib/ec2/models/InstanceConfig.rb', line 26

def security_groups
  @security_groups
end

#source_dest_checkObject (readonly)

Returns the value of attribute source_dest_check.



24
25
26
# File 'lib/ec2/models/InstanceConfig.rb', line 24

def source_dest_check
  @source_dest_check
end

#subnetObject (readonly)

Returns the value of attribute subnet.



27
28
29
# File 'lib/ec2/models/InstanceConfig.rb', line 27

def subnet
  @subnet
end

#tagsObject (readonly)

Returns the value of attribute tags.



32
33
34
# File 'lib/ec2/models/InstanceConfig.rb', line 32

def tags
  @tags
end

#tenancyObject (readonly)

Returns the value of attribute tenancy.



28
29
30
# File 'lib/ec2/models/InstanceConfig.rb', line 28

def tenancy
  @tenancy
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/ec2/models/InstanceConfig.rb', line 29

def type
  @type
end

#user_dataObject (readonly)

Returns the value of attribute user_data.



30
31
32
# File 'lib/ec2/models/InstanceConfig.rb', line 30

def user_data
  @user_data
end

#volume_groupsObject (readonly)

Returns the value of attribute volume_groups.



31
32
33
# File 'lib/ec2/models/InstanceConfig.rb', line 31

def volume_groups
  @volume_groups
end

Instance Method Details

#diff(aws) ⇒ Object

Public: Produce an array of differences between this local configuration and the configuration in AWS

aws - the AWS resource

Returns an array of the InterfaceDiffs that were found



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ec2/models/InstanceConfig.rb', line 121

def diff(aws)
  diffs = []

  if aws.ebs_optimized != @ebs_optimized
    diffs << InstanceDiff.new(InstanceChange::EBS, aws.ebs_optimized, @ebs_optimized)
  end

  if aws.profile != @profile
    diffs << InstanceDiff.new(InstanceChange::PROFILE, aws.profile, @profile)
  end

  if aws.monitoring != @monitoring
    diffs << InstanceDiff.new(InstanceChange::MONITORING, aws.monitoring, @monitoring)
  end

  if aws.network_interfaces != @network_interfaces
    diffs << InstanceDiff.new(InstanceChange::INTERFACES, aws.network_interfaces, @network_interfaces)
  end

  if aws.source_dest_check != @source_dest_check
    diffs << InstanceDiff.new(InstanceChange::SDCHECK, aws.source_dest_check, @source_dest_check)
  end

  if aws.security_groups.sort != @security_groups.sort
    changes = Common::ListChange::simple_list_diff(aws.security_groups, @security_groups)
    diffs << InstanceDiff.new(InstanceChange::SECURITY_GROUPS, aws.security_groups, @security_groups, changes)
  end

  if aws.subnet != @subnet
    diffs << InstanceDiff.new(InstanceChange::SUBNET, aws.subnet, @subnet)
  end

  if aws.type != @type
    diffs << InstanceDiff.new(InstanceChange::TYPE, aws.type, @type)
  end

  if aws.tenancy != @tenancy
    diffs << InstanceDiff.new(InstanceChange::TENANCY, aws.tenancy, @tenancy)
  end

  if aws.tags != @tags
    diffs << InstanceDiff.new(InstanceChange::TAGS, aws.tags, @tags)
  end

  # Check for diffs in volume groups and diffs in how many volumes are attached

  # Get the volumes that are attached to the instance
  aws_instance = EC2::named_instances[aws.name]
  attached_volumes = aws_instance.nonroot_devices.map do |m|
    EC2::id_ebs_volumes[m.ebs.volume_id]
  end
  # Group by volume group, reject nil groups
  group_volumes = attached_volumes.group_by(&:group).reject { |k, v| k.nil? }

  aws_ebs_groups = Hash[group_volumes.map { |group, vols| [group, EbsGroupConfig.new(group).populate!(vols)] }]
  local_ebs_groups = Hash[@volume_groups.map { |vg| [vg, EC2::group_ebs_volumes[vg]] }]

  added_groups = Hash[local_ebs_groups.reject { |k, v| aws_ebs_groups.has_key?(k) }.map do |group_name, group_config|
    [group_name, EbsGroupDiff.added(group_config)]
  end]
  removed_groups = Hash[aws_ebs_groups.reject { |k, v| aws_ebs_groups.has_key?(k) }.map do |group_name, group_config|
    [group_name, EbsGroupDiff.unmanaged(group_config)]
  end]
  changed_groups = Hash[local_ebs_groups.select { |k, v| aws_ebs_groups.has_key?(k) }.map do |group_name, group_config|
    aws_config = aws_ebs_groups[group_name]
    group_diffs = group_config.diff(aws_config)
    if !group_diffs.empty?
      [group_name, EbsGroupDiff.modified(aws_config, group_config, group_diffs)]
    end
  end.reject { |v| v.nil? }]

  ebs_changes = Common::ListChange.new(added_groups, removed_groups, changed_groups)
  if !ebs_changes.empty?
    diffs << InstanceDiff.new(InstanceChange::VOLUME_GROUPS, aws_ebs_groups, local_ebs_groups, ebs_changes)
  end

  diffs
end

#populate!(aws_instance, user_data_file, tags) ⇒ Object

Public: Populate a config object with AWS configuration

aws_instance - the instance from AWS user_data_file - the name of the user data script file tags - a Hash of tags for the instance



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ec2/models/InstanceConfig.rb', line 85

def populate!(aws_instance, user_data_file, tags)
  @ebs_optimized = aws_instance.ebs_optimized
  @placement_group = aws_instance.placement.group_name
  if @placement_group.empty? then @placement_group = nil end

  profile_arn = (aws_instance.iam_instance_profile.arn rescue nil)
  @profile = if profile_arn then profile_arn[profile_arn.rindex("/") + 1 .. profile_arn.length] end

  @image = aws_instance.image_id
  @key_name = aws_instance.key_name
  @monitoring = ["enabled", "pending"].include? aws_instance.monitoring.state
  @network_interfaces = aws_instance.network_interfaces.length
  @source_dest_check = aws_instance.source_dest_check
  @private_ip_address = aws_instance.private_ip_address
  @security_groups = aws_instance.security_groups.map(&:group_id).map { |id| SecurityGroups::id_security_groups[id].group_name }.sort
  @subnet = EC2::id_subnets[aws_instance.subnet_id].name
  @tenancy = aws_instance.placement.tenancy
  @type = aws_instance.instance_type
  @user_data = user_data_file

  # Get the volumes for each device mapping
  @volume_groups = aws_instance.nonroot_devices.map do |m|
    EC2::id_ebs_volumes[m.ebs.volume_id]
  end.map(&:group).reject(&:nil?).uniq.sort

  @tags = tags

  self
end

#to_hashObject



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

def to_hash
  {
    "ebs-optimized" => @ebs_optimized,
    "placement-group" => @placement_group,
    "profile" => @profile,
    "image" => @image,
    "key-name" => @key_name,
    "monitoring" => @monitoring,
    "network-interfaces" => @network_interfaces,
    "source-dest-check" => @source_dest_check,
    "private-ip-address" => @private_ip_address,
    "security-groups" => @security_groups,
    "subnet" => @subnet,
    "tenancy" => @tenancy,
    "type" => @type,
    "user-data" => @user_data,
    "volume-groups" => @volume_groups,
    "tags" => @tags,
  }
end