Class: Instance

Inherits:
Object show all
Defined in:
lib/maws/instance.rb

Direct Known Subclasses

EBS, EC2, ELB, RDS

Defined Under Namespace

Classes: EBS, EC2, ELB, RDS

Constant Summary collapse

NA_STATUS =
'n/a'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maws, config, name, region, prefix, zone, role, index) ⇒ Instance

Returns a new instance of Instance.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/maws/instance.rb', line 30

def initialize(maws, config, name, region, prefix, zone, role, index)
  @maws = maws
  @config = config
  @name = name
  @region = region
  @zone = zone
  @role = role
  @index = index

  @prefix = prefix

  @description = mash
  @groups = %w(all)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



111
112
113
114
115
# File 'lib/maws/instance.rb', line 111

def method_missing(method_name, *args, &block)
  config(method_name) ||
  description[method_name] ||
  @config.command_line[method_name]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/maws/instance.rb', line 9

def description
  @description
end

#groupsObject

Returns the value of attribute groups.



8
9
10
# File 'lib/maws/instance.rb', line 8

def groups
  @groups
end

#indexObject

Returns the value of attribute index.



8
9
10
# File 'lib/maws/instance.rb', line 8

def index
  @index
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/maws/instance.rb', line 7

def name
  @name
end

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/maws/instance.rb', line 8

def prefix
  @prefix
end

#regionObject

Returns the value of attribute region.



8
9
10
# File 'lib/maws/instance.rb', line 8

def region
  @region
end

#roleObject

Returns the value of attribute role.



8
9
10
# File 'lib/maws/instance.rb', line 8

def role
  @role
end

#zoneObject

Returns the value of attribute zone.



8
9
10
# File 'lib/maws/instance.rb', line 8

def zone
  @zone
end

Class Method Details

.create(maws, config, prefix, zone, role, index, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/maws/instance.rb', line 13

def self.create(maws, config, prefix, zone, role, index, options = {})
  options = mash(options)

  service = options.service || config.combined[role].service
  region = options.region || config.region
  name = options.name || name_for(config, prefix, zone, role, index)

  klass = Instance.const_get("#{service.to_s.upcase}")

  klass.new(maws, config, name, region, prefix, zone, role, index)
end

.name_for(config, prefix, zone, role, index) ⇒ Object



25
26
27
28
# File 'lib/maws/instance.rb', line 25

def self.name_for(config, prefix, zone, role, index)
  add_prefix = prefix.empty? ? "" : prefix + "."
  "#{add_prefix}#{config.profile.name}-#{role}-#{index}#{zone}"
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/maws/instance.rb', line 87

def alive?
  aws_id && !terminated?
end

#aws_idObject



66
67
68
# File 'lib/maws/instance.rb', line 66

def aws_id
  description.aws_id
end

#config(key, required = false) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/maws/instance.rb', line 117

def config(key, required=false)
  if required && @config.combined[role][key].nil?
    raise ArgumentError.new("Missing required config: #{key}")
  end

  @config.combined[role][key]
end

#connectionObject



45
46
47
# File 'lib/maws/instance.rb', line 45

def connection
  @maws.connection
end

#displayObject



144
145
146
# File 'lib/maws/instance.rb', line 144

def display
  InstanceDisplay.new(self, display_fields)
end

#display_fieldsObject



140
141
142
# File 'lib/maws/instance.rb', line 140

def display_fields
  [:zone, :name, :status]
end

#has_approximate_status?(approximate_status) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/maws/instance.rb', line 99

def has_approximate_status?(approximate_status)
  if approximate_status == "n/a" or approximate_status == "terminated"
    !alive?
  elsif approximate_status == "ssh"
    self.respond_to?(:ssh_available?) ? self.ssh_available? : has_approximate_status?("available")
  elsif approximate_status == "running" || approximate_status == "available"
    status == "running" || status == "available"
  else
    approximate_status == status
  end
end

#inspectObject



95
96
97
# File 'lib/maws/instance.rb', line 95

def inspect
  "<#{self.class} #{to_s}>"
end

#instancesObject



49
50
51
# File 'lib/maws/instance.rb', line 49

def instances
  @maws.instances
end

#logical_zoneObject



58
59
60
# File 'lib/maws/instance.rb', line 58

def logical_zone
  @zone
end

#matches?(filters = {}) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
# File 'lib/maws/instance.rb', line 148

def matches?(filters={})
  approximate_status = filters.delete(:approximate_status)
  matched = InstanceMatcher.matches?(self, filters)

  # approximate status is not a single state
  # it might require an ssh connection
  matched &&= has_approximate_status?(approximate_status) if approximate_status
  matched
end

#physical_zoneObject



62
63
64
# File 'lib/maws/instance.rb', line 62

def physical_zone
  @zone || @description.physical_zone || @config.specified_zones.first
end

#region_physical_zoneObject



78
79
80
# File 'lib/maws/instance.rb', line 78

def region_physical_zone
  region + physical_zone
end

#region_zoneObject



74
75
76
# File 'lib/maws/instance.rb', line 74

def region_zone
  region + zone
end

#security_groupsObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/maws/instance.rb', line 125

def security_groups
  groups = config(:security_groups).to_a.dup
  groups << "#{service}_default"

  if @config.profile.security_rules and @config.profile.security_rules[role]
    groups << "#{@profile.name}-#{role_name}"
  end

  groups
end

#serviceObject

Raises:

  • (ArgumentError)


136
137
138
# File 'lib/maws/instance.rb', line 136

def service
  raise ArgumentError, "No service for generic instance"
end

#statusObject



70
71
72
# File 'lib/maws/instance.rb', line 70

def status
  description.status || 'n/a'
end

#terminated?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/maws/instance.rb', line 83

def terminated?
  status == 'terminated'
end

#to_sObject



91
92
93
# File 'lib/maws/instance.rb', line 91

def to_s
  "#{name}   #{status}    #{aws_id}"
end