Class: Lightswitch::Cloud

Inherits:
Object
  • Object
show all
Defined in:
lib/lightswitch/cloud.rb

Constant Summary collapse

EC2_REGIONS =
%w( eu-central-1 sa-east-1 ap-northeast-1 eu-west-1 us-east-1 us-west-1 us-west-2 ap-southeast-2 ap-southeast-1 )

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Cloud

Returns a new instance of Cloud.



9
10
11
12
13
14
15
16
# File 'lib/lightswitch/cloud.rb', line 9

def initialize(credentials)
  access_key_id = credentials[:access_key_id]
  secret_access_key = credentials[:secret_access_key]
  raise Errors::AwsCredentialsNotFoundError, 'AWS credentials were not provided' unless access_key_id && secret_access_key

  Aws.config.update({access_key_id: access_key_id, secret_access_key: secret_access_key})
  validate_access
end

Instance Method Details

#get_instance_status(instance_id, region_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lightswitch/cloud.rb', line 42

def get_instance_status(instance_id, region_name)
  begin
    client = get_client(region_name)
    response = client.describe_instance_status({instance_ids: [instance_id], include_all_instances: true})
    if response
      response.first['instance_statuses'].first.instance_state.name
    else
      'unavailable'
    end
  end
end

#list_instances(region_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lightswitch/cloud.rb', line 18

def list_instances(region_name)
  instance_info = []
  begin
    client = get_client(region_name)
    response = client.describe_instances

    if response
      instances = response['reservations'].collect do |struct|
        struct['instances']
      end
      instance_info = instances.collect do |instance_struct|
        instance = instance_struct.first
        {
            instance_id: instance.instance_id,
            name_tag: (instance['tags'].last.value || ''),
            state: instance.state['name'],
            public_dns_name: instance.public_dns_name,
        }
      end
    end
  end
  instance_info
end

#validate_accessObject



54
55
56
57
58
59
60
# File 'lib/lightswitch/cloud.rb', line 54

def validate_access
  client = get_client(EC2_REGIONS.first)
  begin
    client.describe_regions
  end
  true
end