Class: ClusterChef::Facet

Inherits:
ComputeBuilder show all
Defined in:
lib/cluster_chef/facet.rb

Instance Attribute Summary collapse

Attributes inherited from ComputeBuilder

#chef_roles, #cloud, #volumes

Instance Method Summary collapse

Methods inherited from ComputeBuilder

#bogus?, #ec2, #raid_group, #recipe, #role, role_implication, #root_volume, #run_list, #run_list_groups, #volume

Methods inherited from DslObject

#configure, #die, #dump, has_keys, #reverse_merge!, #safely, #set, #step, #to_hash, #to_mash, #to_s, #ui, ui

Constructor Details

#initialize(cluster, facet_name, attrs = {}) ⇒ Facet

Returns a new instance of Facet.



6
7
8
9
10
11
12
13
14
# File 'lib/cluster_chef/facet.rb', line 6

def initialize cluster, facet_name, attrs={}
  super(facet_name.to_sym, attrs)
  @cluster    = cluster
  @servers    = Mash.new
  @chef_roles = []
  @settings[:instances] ||= 1
  create_facet_role
  create_facet_security_group unless attrs[:no_security_group]
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



3
4
5
# File 'lib/cluster_chef/facet.rb', line 3

def cluster
  @cluster
end

Instance Method Details

#assign_volume_ids(volume_name, *volume_ids) ⇒ Object



40
41
42
43
44
# File 'lib/cluster_chef/facet.rb', line 40

def assign_volume_ids(volume_name, *volume_ids)
  volume_ids.flatten.zip(servers).each do |volume_id, server|
    server.volume(volume_name){ volume_id(volume_id) } if server
  end
end

#cluster_nameObject



16
17
18
# File 'lib/cluster_chef/facet.rb', line 16

def cluster_name
  cluster.name
end

#facet_nameObject



20
21
22
# File 'lib/cluster_chef/facet.rb', line 20

def facet_name
  name
end

#facet_role(&block) ⇒ Chef::Role

The auto-generated role for this facet. Instance-evals the given block in the context of that role,

Examples:

facet_role do
  override_attributes({
    :time_machine => { :transition_speed => 88 },
  })
end

Returns:

  • (Chef::Role)

    The auto-generated role for this facet.



35
36
37
38
# File 'lib/cluster_chef/facet.rb', line 35

def facet_role(&block)
  @facet_role.instance_eval( &block ) if block_given?
  @facet_role
end

#has_server?(idx) ⇒ Boolean

if the server has been added to this facet or is in range

Returns:

  • (Boolean)


63
64
65
# File 'lib/cluster_chef/facet.rb', line 63

def has_server? idx
  (idx.to_i < instances) || @servers.include?(idx.to_i)
end

#indexesObject

indexes in the 0…instances range plus bogus ones that showed up (probably from chef or fog)



101
102
103
# File 'lib/cluster_chef/facet.rb', line 101

def indexes
  [@servers.keys, valid_indexes].flatten.compact.uniq.sort
end

#resolve!Object

Resolve:



108
109
110
# File 'lib/cluster_chef/facet.rb', line 108

def resolve!
  servers.each(&:resolve!)
end

#server(idx, attrs = {}) { ... } ⇒ ClusterChef::Facet

Retrieve or define the given server

Parameters:

  • idx (Integer)

    – the index of the desired server

  • attrs (Hash) (defaults to: {})

    – attributes to configure on the object

Yields:

  • a block to execute in the context of the object

Returns:



55
56
57
58
59
60
# File 'lib/cluster_chef/facet.rb', line 55

def server(idx, attrs={}, &block)
  idx = idx.to_i
  @servers[idx] ||= ClusterChef::Server.new(self, idx)
  @servers[idx].configure(attrs, &block)
  @servers[idx]
end

#serversClusterChef::ServerSlice

All servers in this facet

Returns:



74
75
76
# File 'lib/cluster_chef/facet.rb', line 74

def servers
  slice(indexes)
end

#slice(slice_indexes = nil) ⇒ ClusterChef::ServerSlice

A slice of servers from this facet, in index order

If slice_indexes is nil, returns all servers. Otherwise, takes slice (given by *args) from the requested facet.

Parameters:

  • slice_indexes (Array, String) (defaults to: nil)

    – servers in that facet (or nil for all in facet).

Returns:



87
88
89
90
91
92
# File 'lib/cluster_chef/facet.rb', line 87

def slice(slice_indexes=nil)
  slice_indexes = self.indexes if slice_indexes.blank?
  slice_indexes = indexes_from_intervals(slice_indexes) if slice_indexes.is_a?(String)
  svrs = Array(slice_indexes).map(&:to_i).sort!.select{|idx| has_server?(idx) }.map{|idx| server(idx) }
  ClusterChef::ServerSlice.new(self.cluster, svrs)
end

#valid_indexesObject

all valid server indexes



95
96
97
# File 'lib/cluster_chef/facet.rb', line 95

def valid_indexes
  (0 ... instances).to_a # note the '...'
end