Class: Cucloud::Ec2Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/cucloud/ec2_utils.rb

Overview

EC2Utils class - anything ec2 related goes here!

Constant Summary collapse

UBUNTU_PATCH_COMMAND =

This is the command sent to ubuntu for patching

'apt-get update; apt-get -y upgrade; reboot'.freeze
AMAZON_PATCH_COMMAND =

This is the command sent to amazon linux machines for patching

'yum update -y; reboot & disown '.freeze
SECONDS_IN_A_DAY =

Used in time calculations

86_400
WAITER_MAX_ATTEMPS =

Max attemps for a waiter to try

240
WAITER_DELAY =

Delay between calls used by waiter to check status

15

Instance Method Summary collapse

Constructor Details

#initialize(ec2_client = Aws::EC2::Client.new, ssm_utils = Cucloud::SSMUtils.new) ⇒ Ec2Utils

Returns a new instance of Ec2Utils.



15
16
17
18
# File 'lib/cucloud/ec2_utils.rb', line 15

def initialize(ec2_client = Aws::EC2::Client.new, ssm_utils = Cucloud::SSMUtils.new)
  @ec2 = ec2_client
  @ssm_utils = ssm_utils
end

Instance Method Details

#associate_eip(instance, allocation_id) ⇒ Object

Assoications an Elastic IP adress with a specific instance number.

Returns:

  • association_id as a string in the form of eipassoc-569cd631. This is the link between between the elastic network interface and the elastic IP address.



66
67
# File 'lib/cucloud/ec2_utils.rb', line 66

def associate_eip(instance, allocation_id)
end

#backup_volumes_unless_recent_backup(days = 5) ⇒ Array<Hash>

Preforms a backup on volumes that do not have a recent snapshot_info

Parameters:

  • days (Integer) (defaults to: 5)

    defaults to 5

Returns:

  • (Array<Hash>)

    An array of hashes containing snapshot_id, instance_name and volume



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/cucloud/ec2_utils.rb', line 181

def backup_volumes_unless_recent_backup(days = 5)
  volumes_backed_up_recently = volumes_with_snapshot_within_last_days(days)
  snapshots_created = []

  volumes = @ec2.describe_volumes(filters: [{ name: 'attachment.status', values: ['attached'] }])
  volumes.volumes.each do |volume|
    next if volumes_backed_up_recently[volume.volume_id.to_s]
    instance_name = get_instance_name(volume.attachments[0].instance_id)

    tags = instance_name ? [{ key: 'Instance Name', value: instance_name }] : []
    snapshot_info = create_ebs_snapshot(volume.volume_id,
                                        'auto-ebs-snap-' + Time.now.strftime('%Y-%m-%d-%H:%M:%S'),
                                        tags)

    snapshots_created.push(snapshot_id: snapshot_info.snapshot_id,
                           instance_name: instance_name,
                           volume: volume.volume_id)
  end

  snapshots_created
end

#create_ebs_snapshot(volume_id, snapshot_desc, tags = []) ⇒ Object

Create a snapshot of an EBS volume and apply supplied tags will wait 20 minutes for the process to completed http://docs.aws.amazon.com/sdkforruby/api/Aws/EC2/Client.html#create_tags-instance_method

Parameters:

  • volume_id (String)

    volume id in the formate of vol-121231231231

  • snapshot_desc (String)

    Description of the snapshot

  • tags (Array) (defaults to: [])

    Array of key value pairs to be applied as tags to the snapshot

Returns:

  • snapshot information see



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cucloud/ec2_utils.rb', line 162

def create_ebs_snapshot(volume_id, snapshot_desc, tags = [])
  snapshot_info = @ec2.create_snapshot(
    volume_id: volume_id,
    description: snapshot_desc
  )

  @ec2.wait_until(:snapshot_completed, snapshot_ids: [snapshot_info.snapshot_id]) do |w|
    w.max_attempts = WAITER_MAX_ATTEMPS
    w.delay = WAITER_DELAY
  end

  @ec2.create_tags(resources: [snapshot_info.snapshot_id], tags: tags) unless tags.empty?

  snapshot_info
end

#create_instance(options) ⇒ Object

Create ec2 instance based on parameters provided. The function will pull in default information from ?????.

Parameters:

  • options (hash)

    will be hash that will override the default



72
73
# File 'lib/cucloud/ec2_utils.rb', line 72

def create_instance(options)
end

#delete_instance(instance) ⇒ Object

Terminate ec2 instance for a specific instance number.



59
60
# File 'lib/cucloud/ec2_utils.rb', line 59

def delete_instance(instance)
end

#deregister_image(image) ⇒ Object

Remove private AMI



76
77
# File 'lib/cucloud/ec2_utils.rb', line 76

def deregister_image(image)
end

#find_ami(name) ⇒ Object

Find ami based on a search of Name



80
81
# File 'lib/cucloud/ec2_utils.rb', line 80

def find_ami(name)
end

#get_instance(instance_id) ⇒ Aws::EC2::Instance

Parameters:

  • instance_id (String)

    instance id in the format of i-121231231231

Returns:

  • (Aws::EC2::Instance)

    Object represneting the intance see



24
25
26
# File 'lib/cucloud/ec2_utils.rb', line 24

def get_instance(instance_id)
  Aws::EC2::Instance.new(id: instance_id, client: @ec2)
end

#get_instance_information(instance) ⇒ array

Get instance information for a specific instance

Parameters:

  • instance (String)

    instance id in the format of i-121231231231

Returns:



32
33
34
# File 'lib/cucloud/ec2_utils.rb', line 32

def get_instance_information(instance)
  @ec2.describe_instances(instance_ids: [instance])
end

#get_instance_name(instance_id) ⇒ String

Get the nice name of the ec2 intsance from the 'Name" tag'

Parameters:

  • instance_id (String)

    instance id in the format of i-121231231231

Returns:

  • (String)

    Name of instacnce if found or nil if not found



149
150
151
152
153
# File 'lib/cucloud/ec2_utils.rb', line 149

def get_instance_name(instance_id)
  instance = get_instance(instance_id)
  tag_name = instance.tags.find { |tag| tag.key.eql?('Name') }
  tag_name ? tag_name.value : nil
end

#get_instances_by_tag(tag_name, tag_value) ⇒ array

Based on tag name and value, return instances

Parameters:

  • tag_name (string)

    name of tag

  • tag_value (string)

    the value of the tag

Returns:



88
89
90
91
92
93
94
95
# File 'lib/cucloud/ec2_utils.rb', line 88

def get_instances_by_tag(tag_name, tag_value)
  @ec2.describe_instances(filters: [
                            {
                              name: "tag:#{tag_name}",
                              values: tag_value
                            }
                          ])
end

#instances_to_patch_by_tag(tag_name = 'auto_patch', tag_value = ['1']) ⇒ Object

patch instances based on a tag name and value

Parameters:

  • tag_name (string) (defaults to: 'auto_patch')

    name of tag

  • tag_value (string) (defaults to: ['1'])

    the value of the tag



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
# File 'lib/cucloud/ec2_utils.rb', line 118

def instances_to_patch_by_tag(tag_name = 'auto_patch', tag_value = ['1'])
  resp = get_instances_by_tag(tag_name, tag_value)

  ubuntu_patch_instances = []
  amazon_patch_instances = []
  all_instances = []

  resp.reservations.each do |res|
    res.instances.each do |instance|
      instance.tags.each do |tag|
        next unless tag.key.eql?('os')
        if tag.value.eql?('ubuntu')
          ubuntu_patch_instances.push(instance.instance_id)
          all_instances.push(instance.instance_id)
        elsif tag.value.eql?('ecs') || tag.value.eql?('amazon')
          amazon_patch_instances.push(instance.instance_id)
          all_instances.push(instance.instance_id)
        end
      end
    end
  end

  @ssm_utils.send_patch_command(ubuntu_patch_instances, UBUNTU_PATCH_COMMAND) if ubuntu_patch_instances.any?
  @ssm_utils.send_patch_command(amazon_patch_instances, AMAZON_PATCH_COMMAND) if amazon_patch_instances.any?

  all_instances
end

#reboot_instance(instance) ⇒ Object

reboot instance



55
56
# File 'lib/cucloud/ec2_utils.rb', line 55

def reboot_instance(instance)
end

#rename_instance(instance, name) ⇒ Object

Set the name of the instance that will be displayed in the ec2 console



51
52
# File 'lib/cucloud/ec2_utils.rb', line 51

def rename_instance(instance, name)
end

#start_instance(instance) ⇒ Object

Start ec2 instance for a specific instance number. The function will wait until the instance has entered the running state.

Parameters:

  • instance (String)

    instance id in the format of i-121231231231



46
47
48
# File 'lib/cucloud/ec2_utils.rb', line 46

def start_instance(instance)
  @ec2.start_instances(instance_ids: [instance])
end

#start_instances_by_tag(tag_name, tag_value) ⇒ Object

start instances based on a tag name and value

Parameters:

  • tag_name (string)

    name of tag

  • tag_value (string)

    the value of the tag



109
110
111
112
113
# File 'lib/cucloud/ec2_utils.rb', line 109

def start_instances_by_tag(tag_name, tag_value)
  get_instances_by_tag(tag_name, tag_value).reservations[0].instances.each do |i|
    @ec2.start_instances(instance_ids: [i.instance_id])
  end
end

#stop_instance(instance) ⇒ Object

Stop ec2 instance for a specific instance number. The function will wait until the instance has entered the stopped state.

Parameters:

  • instance (String)

    instance id in the format of i-121231231231



39
40
41
# File 'lib/cucloud/ec2_utils.rb', line 39

def stop_instance(instance)
  @ec2.stop_instances(instance_ids: [instance])
end

#stop_instances_by_tag(tag_name, tag_value) ⇒ Object

stop instances based on a tag name and value

Parameters:

  • tag_name (string)

    name of tag

  • tag_value (string)

    the value of the tag



100
101
102
103
104
# File 'lib/cucloud/ec2_utils.rb', line 100

def stop_instances_by_tag(tag_name, tag_value)
  get_instances_by_tag(tag_name, tag_value).reservations[0].instances.each do |i|
    @ec2.stop_instances(instance_ids: [i.instance_id])
  end
end

#volumes_with_snapshot_within_last_days(days = 5) ⇒ Array

Find volumes that have a recent snapshot

Parameters:

  • days (Integer) (defaults to: 5)

    defaults to 5

Returns:

  • (Array)

    list of volume ids that have recent snapshots



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cucloud/ec2_utils.rb', line 206

def volumes_with_snapshot_within_last_days(days = 5)
  volumes_backed_up_recently = {}

  snapshots = @ec2.describe_snapshots(owner_ids: ['self'], filters: [{ name: 'status', values: ['completed'] }])
  snapshots.snapshots.each do |snapshot|
    if snapshot.start_time > Time.now - (SECONDS_IN_A_DAY * days)
      volumes_backed_up_recently[snapshot.volume_id.to_s] = true
    end
  end
  volumes_backed_up_recently
end