Module: CapEC2::Utils

Included in:
EC2Handler, StatusTable
Defined in:
lib/cap-ec2/utils.rb

Defined Under Namespace

Modules: Server

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.contact_point(instance) ⇒ Object



41
42
43
44
45
46
# File 'lib/cap-ec2/utils.rb', line 41

def self.contact_point(instance)
  ec2_interface = contact_point_mapping[fetch(:ec2_contact_point)]
  return instance.send(ec2_interface) if ec2_interface

  instance.public_dns_name || instance.public_ip_address || instance.private_ip_address
end

.contact_point_mappingObject



33
34
35
36
37
38
39
# File 'lib/cap-ec2/utils.rb', line 33

def self.contact_point_mapping
  {
    :public_dns => :public_dns_name,
    :public_ip => :public_ip_address,
    :private_ip => :private_ip_address
  }
end

Instance Method Details

#get_regions(regions_array = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/cap-ec2/utils.rb', line 75

def get_regions(regions_array=nil)
  unless regions_array.nil? || regions_array.empty?
    return regions_array
  else
    fail "You must specify at least one EC2 region."
  end
end

#load_configObject



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
# File 'lib/cap-ec2/utils.rb', line 48

def load_config
  if fetch(:ec2_profile)
    credentials = Aws::SharedCredentials.new(profile_name: fetch(:ec2_profile)).credentials
    if credentials
      set :ec2_access_key_id, credentials.access_key_id
      set :ec2_secret_access_key, credentials.secret_access_key
    end
  end

  config_location = File.expand_path(fetch(:ec2_config), Dir.pwd) if fetch(:ec2_config)
  if config_location && File.exists?(config_location)
    config = YAML.load(ERB.new(File.read(fetch(:ec2_config))))
    if config
      set :ec2_project_tag, config['project_tag'] if config['project_tag']
      set :ec2_roles_tag, config['roles_tag'] if config['roles_tag']
      set :ec2_stages_tag, config['stages_tag'] if config['stages_tag']
      set :ec2_tag_delimiter, config['tag_delimiter'] if config['tag_delimiter']

      set :ec2_access_key_id, config['access_key_id'] if config['access_key_id']
      set :ec2_secret_access_key, config['secret_access_key'] if config['secret_access_key']
      set :ec2_region, config['regions'] if config['regions']

      set :ec2_filter_by_status_ok?, config['filter_by_status_ok?'] if config['filter_by_status_ok?']
    end
  end
end

#project_tagObject



13
14
15
# File 'lib/cap-ec2/utils.rb', line 13

def project_tag
  fetch(:ec2_project_tag)
end

#roles_tagObject



17
18
19
# File 'lib/cap-ec2/utils.rb', line 17

def roles_tag
  fetch(:ec2_roles_tag)
end

#stages_tagObject



21
22
23
# File 'lib/cap-ec2/utils.rb', line 21

def stages_tag
  fetch(:ec2_stages_tag)
end

#tag_delimiterObject



25
26
27
# File 'lib/cap-ec2/utils.rb', line 25

def tag_delimiter
  fetch(:ec2_tag_delimiter)
end

#tag_value(instance, key) ⇒ Object



29
30
31
# File 'lib/cap-ec2/utils.rb', line 29

def tag_value(instance, key)
  instance.tags.find(-> { {} }) { |t| t[:key] == key.to_s }[:value]
end