Class: RunlistMap

Inherits:
Object
  • Object
show all
Defined in:
lib/mofa/runlist_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunlistMap

Returns a new instance of RunlistMap.



19
20
21
# File 'lib/mofa/runlist_map.rb', line 19

def initialize
  @mp = {}
end

Instance Attribute Details

#cookbookObject

Returns the value of attribute cookbook.



3
4
5
# File 'lib/mofa/runlist_map.rb', line 3

def cookbook
  @cookbook
end

#default_runlist_recipesObject

Returns the value of attribute default_runlist_recipes.



7
8
9
# File 'lib/mofa/runlist_map.rb', line 7

def default_runlist_recipes
  @default_runlist_recipes
end

#hostlistObject

Returns the value of attribute hostlist.



4
5
6
# File 'lib/mofa/runlist_map.rb', line 4

def hostlist
  @hostlist
end

#mpObject

Returns the value of attribute mp.



2
3
4
# File 'lib/mofa/runlist_map.rb', line 2

def mp
  @mp
end

#option_runlistObject

Returns the value of attribute option_runlist.



6
7
8
# File 'lib/mofa/runlist_map.rb', line 6

def option_runlist
  @option_runlist
end

#tokenObject

Returns the value of attribute token.



5
6
7
# File 'lib/mofa/runlist_map.rb', line 5

def token
  @token
end

Class Method Details

.create(cookbook, hostlist, token, option_runlist = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/mofa/runlist_map.rb', line 9

def self.create(cookbook, hostlist, token, option_runlist = nil)
  rl = RunlistMap.new
  rl.cookbook = cookbook
  rl.hostlist = hostlist
  rl.token = token
  rl.option_runlist = option_runlist
  rl.default_runlist_recipes = (!option_runlist.nil?) ? option_runlist : nil
  rl
end

Instance Method Details

#generateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mofa/runlist_map.rb', line 23

def generate
  @default_runlist_recipes ||= [ "#{cookbook.name}::default" ]
  @default_runlist_recipes = [ "#{@default_runlist_recipes}" ] unless @default_runlist_recipes.kind_of?(Array)

  if option_runlist.nil?
    case cookbook.type
      when 'env'
        guess_runlists_by_hostnames
      else
        set_default_runlist_for_every_host
    end
  else
    hostlist.list.each do |hostname|
      @mp.store(hostname, option_runlist)
    end
  end
end

#guess_runlists_by_hostnamesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mofa/runlist_map.rb', line 41

def guess_runlists_by_hostnames
  # recipes/jkmaster.rb --> runlist[<env_cookbook_name>::jkmaster] for all hosts with shortname jkmaster
  # recipes/jkslave.rb --> runlist[<env_cookbook_name>::jkslave] for all hosts with shortname jkslave[0-9]
  # and so on
  hostlist.list.each do |hostname|
    cookbook.recipes.each do |recipe|
      recipe_regex = "^#{recipe}[0-9]*\\\."
      if hostname.match(recipe_regex)
        @mp.store(hostname, "recipe[#{cookbook.name}::#{recipe}]")
        break
      end
    end
  end
end

#set_default_runlist_for_every_hostObject



56
57
58
59
60
61
62
63
# File 'lib/mofa/runlist_map.rb', line 56

def set_default_runlist_for_every_host
  hostlist.list.each do |hostname|
    @default_runlist_recipes.each do |rl_entry|
      next unless rl_entry.split(/::/)[0] == cookbook.name
      @mp.store(hostname,  rl_entry) if cookbook.recipes.kind_of?(Array) and cookbook.recipes.include?(rl_entry.split(/::/)[1])
    end
  end
end