Class: ClusterChef::Cluster

Inherits:
ComputeBuilder show all
Defined in:
lib/cluster_chef/cluster.rb,
lib/cluster_chef/discovery.rb,
lib/cluster_chef/deprecated.rb

Overview

A cluster has many facets. Any setting applied here is merged with the facet at resolve time; if the facet explicitly sets any attributes they will win out.

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, #ui, ui

Constructor Details

#initialize(name, attrs = {}) ⇒ Cluster

Returns a new instance of Cluster.



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

def initialize(name, attrs={})
  super(name.to_sym, attrs)
  @facets            = Mash.new
  @chef_roles        = []
  environment          :_default if environment.blank?
  create_cluster_role
  create_cluster_security_group unless attrs[:no_security_group]
end

Instance Attribute Details

#facetsObject (readonly)

Returns the value of attribute facets.



7
8
9
# File 'lib/cluster_chef/cluster.rb', line 7

def facets
  @facets
end

#undefined_serversObject (readonly)

Returns the value of attribute undefined_servers.



7
8
9
# File 'lib/cluster_chef/cluster.rb', line 7

def undefined_servers
  @undefined_servers
end

Instance Method Details

#chef_clientsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cluster_chef/discovery.rb', line 13

def chef_clients
  return @chef_clients if @chef_clients
  @chef_clients = []

  # Oh for fuck's sake -- the key used to index clients changed from
  # 'clientname' in 0.10.4-and-prev to 'name' in 0.10.8. Rather than index
  # both 'clientname' and 'name', they switched it -- so we have to fall
  # back.  FIXME: While the Opscode platform is 0.10.4 I have clientname
  # first (sorry, people of the future). When it switches to 0.10.8 we'll
  # reverse them (suck it people of the past).
  # Also sometimes the server returns results that are nil on
  # recently-expired clients, so that's annoying too.
  clients, wtf, num = Chef::Search::Query.new.search(:client, "clientname:#{cluster_name}-*") ; clients.compact!
  clients, wtf, num = Chef::Search::Query.new.search(:client, "name:#{cluster_name}-*") if clients.blank?
  clients.each do |client_hsh|
    next if client_hsh.nil?
    # Return values from Chef::Search seem to be inconsistent across chef
    # versions (sometimes a hash, sometimes an object). Fix if necessary.
    client_hsh = Chef::ApiClient.json_create(client_hsh) unless client_hsh.is_a?(Chef::ApiClient)
    @chef_clients.push( client_hsh )
  end
  @chef_clients
end

#chef_nodesObject



42
43
44
45
46
47
48
49
# File 'lib/cluster_chef/discovery.rb', line 42

def chef_nodes
  return @chef_nodes if @chef_nodes
  @chef_nodes = []
  Chef::Search::Query.new.search(:node,"cluster_name:#{cluster_name}") do |n|
    @chef_nodes.push(n) unless n.blank? || (n.cluster_name != cluster_name.to_s)
  end
  @chef_nodes
end

#clusterObject



18
19
20
# File 'lib/cluster_chef/cluster.rb', line 18

def cluster
  self
end

#cluster_nameObject



22
23
24
# File 'lib/cluster_chef/cluster.rb', line 22

def cluster_name
  name
end

#cluster_role(&block) ⇒ Chef::Role

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

Examples:

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

Returns:

  • (Chef::Role)

    The auto-generated role for this facet.



37
38
39
40
# File 'lib/cluster_chef/cluster.rb', line 37

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

#discover!Object



4
5
6
7
8
9
10
11
# File 'lib/cluster_chef/discovery.rb', line 4

def discover!
  @aws_instance_hash = {}
  discover_cluster_chef!
  discover_chef_nodes!
  discover_fog_servers!  unless ClusterChef.chef_config[:cloud] == false
  discover_chef_clients!
  discover_volumes!
end

#facet(facet_name, attrs = {}) { ... } ⇒ ClusterChef::Facet

Retrieve or define the given facet

Parameters:

  • facet_name (String)

    – name of the desired facet

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

    – attributes to configure on the object

Yields:

  • a block to execute in the context of the object

Returns:



51
52
53
54
55
56
# File 'lib/cluster_chef/cluster.rb', line 51

def facet(facet_name, attrs={}, &block)
  facet_name = facet_name.to_sym
  @facets[facet_name] ||= ClusterChef::Facet.new(self, facet_name)
  @facets[facet_name].configure(attrs, &block)
  @facets[facet_name]
end

#find_client(cl_name) ⇒ Object

returns client with the given name if in catalog, nil otherwise



38
39
40
# File 'lib/cluster_chef/discovery.rb', line 38

def find_client(cl_name)
  chef_clients.find{|ccl| ccl.name == cl_name }
end

#find_facet(facet_name) ⇒ Object



62
63
64
# File 'lib/cluster_chef/cluster.rb', line 62

def find_facet(facet_name)
  @facets[facet_name] or raise("Facet '#{facet_name}' is not defined in cluster '#{cluster_name}'")
end

#find_node(nd_name) ⇒ Object

returns node with the given name if in catalog, nil otherwise



52
53
54
# File 'lib/cluster_chef/discovery.rb', line 52

def find_node(nd_name)
  chef_nodes.find{|nd| nd.name == nd_name }
end

#has_facet?(facet_name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cluster_chef/cluster.rb', line 58

def has_facet? facet_name
  @facets.include?(facet_name)
end

#resolve!Object

Resolve:



97
98
99
# File 'lib/cluster_chef/cluster.rb', line 97

def resolve!
  facets.values.each(&:resolve!)
end

#serversClusterChef::ServerSlice

All servers in this facet, sorted by facet name and index

Returns:



69
70
71
72
# File 'lib/cluster_chef/cluster.rb', line 69

def servers
  svrs = @facets.sort.map{|name, facet| facet.servers.to_a }
  ClusterChef::ServerSlice.new(self, svrs.flatten)
end

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

A slice of a cluster:

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

Parameters:

  • facet_name (String) (defaults to: nil)

    – facet to slice (or nil for all in cluster)

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

    – servers in that facet (or nil for all in facet). You must specify a facet if you use slice_indexes.

Returns:



85
86
87
88
# File 'lib/cluster_chef/cluster.rb', line 85

def slice facet_name=nil, slice_indexes=nil
  return ClusterChef::ServerSlice.new(self, self.servers) if facet_name.nil?
  find_facet(facet_name).slice(slice_indexes)
end

#to_sObject



90
91
92
# File 'lib/cluster_chef/cluster.rb', line 90

def to_s
  "#{super[0..-3]} @facets=>#{@facets.keys.inspect}}>"
end

#use(*clusters) ⇒ Object

DEPRECATED: This doesn’t really work – use reverse_merge! instead



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

def use(*clusters)
  ui.warn "The 'use' statement is deprecated #{caller.inspect}"
  clusters.each do |c|
    other_cluster =  ClusterChef.load_cluster(c)
    reverse_merge! other_cluster
  end
  self
end