Class: Hubcap::Hub

Inherits:
Group
  • Object
show all
Defined in:
lib/hubcap/hub.rb

Constant Summary

Constants inherited from Group

Group::IP_PATTERN

Instance Attribute Summary collapse

Attributes inherited from Group

#children, #name, #parent

Instance Method Summary collapse

Methods inherited from Group

#absorb, #application, #cap_attribute, #cap_attributes, #cap_role, #cap_roles, #collectable?, #extra, #extras, #group, #history, #host, #hosts, #hub, #lookup, #param, #params, #processable?, #puppet_role, #puppet_roles, #resolv, #role, #server

Constructor Details

#initialize(filter_string) ⇒ Hub

Returns a new instance of Hub.



8
9
10
11
12
13
14
15
16
17
# File 'lib/hubcap/hub.rb', line 8

def initialize(filter_string)
  @filters = filter_string.split(',').collect { |fltr| fltr.split('.') }
  @filters = [[]]  if @filters.empty?
  @cap_sets = {}
  @cap_set_clashes = []
  @applications = []
  @servers = []
  @groups = []
  super(nil, '')
end

Instance Attribute Details

#applicationsObject (readonly)

Returns the value of attribute applications.



5
6
7
# File 'lib/hubcap/hub.rb', line 5

def applications
  @applications
end

#cap_setsObject (readonly)

Returns the value of attribute cap_sets.



5
6
7
# File 'lib/hubcap/hub.rb', line 5

def cap_sets
  @cap_sets
end

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/hubcap/hub.rb', line 5

def filters
  @filters
end

#groupsObject (readonly)

Returns the value of attribute groups.



5
6
7
# File 'lib/hubcap/hub.rb', line 5

def groups
  @groups
end

#serversObject (readonly)

Returns the value of attribute servers.



5
6
7
# File 'lib/hubcap/hub.rb', line 5

def servers
  @servers
end

Instance Method Details

#cap_set(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/hubcap/hub.rb', line 20

def cap_set(hash)
  hash.each_pair { |k, v|
    if @cap_sets[k] && @cap_sets[k] != v
      @cap_set_clashes << { k => v }
    else
      @cap_sets[k] = v
    end
  }
end

#capistrano_is_agnostic?(cap) ⇒ Boolean

In agnostic mode, Capistrano recipes for specific applications are not loaded, and cap_set collisions are ignored.

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hubcap/hub.rb', line 72

def capistrano_is_agnostic?(cap)
  return cap.fetch(:hubcap_agnostic)  if cap.exists?(:hubcap_agnostic)
  ag = true
  options = cap.logger.instance_variable_get(:@options)
  if options && options[:actions] && options[:actions].any?
    tasks = options[:actions].clone
    while tasks.any?
      ag = false  unless cap.find_task(tasks.shift)
    end
  end
  cap.set(:hubcap_agnostic, ag)
  ag
end

#configure_capistrano(cap) ⇒ Object

Does a few things:

  • Sets the :hubcap variable in the Capistrano instance.

  • Loads the default Hubcap recipes into the Capistrano instance.

  • Defines each server as a Capistrano server().

If we are in “agnostic mode” - executing a default task - that’s all we do. If we are in “application mode” - executing at least one non-standard task - then we do a few more things:

  • Load all the recipes for the application into the Capistrano instance.

  • Set all the @cap_set variables in the Capistrano instance.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hubcap/hub.rb', line 44

def configure_capistrano(cap)
  raise(Hubcap::CapistranoAlreadyConfigured)  if cap.exists?(:hubcap)
  cap.set(:hubcap, self)

  cap.instance_eval {
    require('hubcap/recipes/servers')
    require('hubcap/recipes/ssh')
    require('hubcap/recipes/puppet')
  }

  # Declare the servers.
  servers.each { |svr|
    # Raises an error if we cannot resolve the resulting address with DNS.
    resolv(svr.address)  unless svr.address.match(Hubcap::Group::IP_PATTERN)
    opts = {
      :name => svr.name,
      :full_name => svr.history.join('.')
    }.merge(svr.cap_attributes)
    cap.server(svr.address, *(svr.cap_roles + [opts]))
  }

  configure_application_mode(cap)  unless capistrano_is_agnostic?(cap)
end