Class: Specification

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

Constant Summary collapse

LARGEST_INDEX =
999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, spec) ⇒ Specification

Returns a new instance of Specification.



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

def initialize(config, spec)
  @config = config
  @spec = spec
end

Instance Attribute Details

#existing_instancesObject

Returns the value of attribute existing_instances.



4
5
6
# File 'lib/maws/specification.rb', line 4

def existing_instances
  @existing_instances
end

Instance Method Details

#indexes_range_for_role(role) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
114
115
116
117
118
119
120
# File 'lib/maws/specification.rb', line 71

def indexes_range_for_role(role)
  # 'app'       =>  [nil, nil, nil]     => [nil, nil]
  # 'app-'      =>  [nil, nil, nil]     => [nil, nil]
  # 'app-*'     =>  ["*", nil, nil]     => [nil, '*']
  # 'app-3'     =>  ["3", nil, nil]     => [3,3]
  # 'app-3-'    =>  ["3", '-', nil]     => [3, nil]
  # 'app-3-*'   =>  ["3", '-', "*"]     => [3, '*']
  # 'app-1-3'   =>  ["1", '-', "3"]     => [1, 3]

  # '*' => [nil, '*']
  # ''  => [nil, nil]


  md = @spec.match(%r{\b#{role}(?=\b)-?(?![^\*\d\s])(\d+|\*)?(-)?(\d+|\*)?})

  # no match, either badly formed specification
  # or '*' or '' (star and blank) specs
  if md.nil?
    anyzone = zones.join('')
    index = if @spec.match(%r{^[#{anyzone}\s]*\*[#{anyzone}\s]*$}) # single '*' with zones
      [nil, '*']
    elsif @spec.match(%r{^[#{anyzone}\s]*$}) # blank
      [nil, nil]
    else
      nil
    end
    return index
  end

  lower, separator, upper = md[1], md[2], md[3]

  return nil unless lower.nil? || lower == '*' || lower =~ /^\d+$/
  return nil unless upper.nil? || upper == '*' || upper =~ /^\d+$/



  # convert to integers
  lower = lower.to_i unless lower == '*' or lower.nil?
  upper = upper.to_i unless upper == '*' or upper.nil?

  # single star is upper bound, not lower bound
  lower, upper = upper, lower if lower == '*'

  # lower digit without separator and without upper digit means single index
  if lower and separator.nil? and upper.nil?
    upper = lower
  end

  [lower, upper]
end

#resolve_index_range(role, range) ⇒ Object

(‘app’, [nil, *]) => [1,2,3,4,5,6,7]



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/maws/specification.rb', line 48

def resolve_index_range(role, range)
  lower = range[0]
  upper = range[1]

  lower = 1 if lower.nil?

  # replace upper
  if upper == '*'
    # max existing index
    max_existing = @existing_instances.matching(:role => role).map {|i| i.index}.max.to_i
    max_profile = @config.profile[role].count

    upper = [max_profile, max_existing].max
  end

  if upper.nil?
    # max index in profile
    upper = @config.profile[role].count
  end

  lower.upto(upper).to_a
end

#role_indexesObject

=> [1,2,3,4], ‘web’ => [3], ‘db’ => [4,5,6,7,8,9,10,11,12]



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/maws/specification.rb', line 35

def role_indexes
  indexes = {}
  roles.each {|role_name|
    indexes[role_name] = indexes_range_for_role(role_name)
  }

  indexes.delete_if {|role, index_range| index_range.nil?}
  indexes.each {|role, index_range| indexes[role] = resolve_index_range(role, index_range)}

  indexes
end

#rolesObject



122
123
124
125
126
# File 'lib/maws/specification.rb', line 122

def roles
  roles = @config.available_roles.find_all { |role_name| @spec.include?(role_name) }
  roles = @config.available_roles if roles.blank? # use all if none are specified
  roles
end

#servicesObject

:ec2, :rds, :elb


12
13
14
15
16
# File 'lib/maws/specification.rb', line 12

def services
  roles.map { |role_name|
    @config.roles[role_name].service
  }.compact.uniq.map{|service| service.to_sym}
end

#zonesObject

‘a’, ‘b’, ‘d’


19
20
21
22
23
24
# File 'lib/maws/specification.rb', line 19

def zones
  found = @config.available_zones.find_all {|az|
    @spec.match(%r{\b#{az}\b})
  }
  found.empty? ? @config.available_zones : found
end

#zones_for_role(role) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/maws/specification.rb', line 26

def zones_for_role(role)
  if @config.combined[role].scope != 'zone'
    [nil]
  else
    zones
  end
end