Class: Spiceweasel::Roles

Inherits:
Object
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/spiceweasel/roles.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#create_command, #delete_command

Constructor Details

#initialize(roles = {}, environments = [], cookbooks = {}) ⇒ Roles

Returns a new instance of Roles.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spiceweasel/roles.rb', line 29

def initialize(roles = {}, environments = [], cookbooks = {})
  @create = Array.new
  @delete = Array.new
  @role_list = Array.new
  if roles
    Spiceweasel::Log.debug("roles: #{roles}")
    flatroles = roles.collect {|x| x.keys}.flatten
    rolefiles = []
    flatroles.each do |role|
      Spiceweasel::Log.debug("role: #{role}")
      if File.directory?("roles")
        #expand wildcards and push into flatroles
        if role =~ /\*/ #wildcard support
          wildroles = Dir.glob("roles/#{role}")
          #remove anything not ending in .json or .rb
          wildroles.delete_if {|x| !x.end_with?(".rb", ".json")}
          Spiceweasel::Log.debug("found roles '#{wildroles}' for wildcard: #{role}")
          flatroles.concat(wildroles.collect {|x| x[x.rindex('/')+1..x.rindex('.')-1]})
          next
        end
        validate(role, environments, cookbooks, flatroles) unless Spiceweasel::Config[:novalidation]
      elsif !Spiceweasel::Config[:novalidation]
        STDERR.puts "ERROR: 'roles' directory not found, unable to validate or load roles"
        exit(-1)
      end
      if File.exists?("roles/#{role}.json")
        rolefiles.push("#{role}.json")
      else #assume no .json means they want .rb and catchall for misssing dir
        rolefiles.push("#{role}.rb")
      end
      delete_command("knife role#{Spiceweasel::Config[:knife_options]} delete #{role} -y")
      @role_list.push(role)
    end
    create_command("knife role#{Spiceweasel::Config[:knife_options]} from file #{rolefiles.uniq.sort.join(' ')}")
  end
end

Instance Attribute Details

#createObject (readonly)

Returns the value of attribute create.



27
28
29
# File 'lib/spiceweasel/roles.rb', line 27

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



27
28
29
# File 'lib/spiceweasel/roles.rb', line 27

def delete
  @delete
end

#role_listObject (readonly)

Returns the value of attribute role_list.



27
28
29
# File 'lib/spiceweasel/roles.rb', line 27

def role_list
  @role_list
end

Instance Method Details

#member?(role) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/spiceweasel/roles.rb', line 117

def member?(role)
  role_list.include?(role)
end

#validate(role, environments, cookbooks, roles) ⇒ Object

validate the content of the role file



67
68
69
70
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
# File 'lib/spiceweasel/roles.rb', line 67

def validate(role, environments, cookbooks, roles)
  #validate the role passed in match the name of either the .rb or .json
  file = %W(roles/#{role}.rb roles/#{role}.json).detect{|f| File.exists?(f)}
  if role =~ /\// #pull out directories
    role = role.split('/').last
  end
  if file
    case file
    when /\.json$/
      c_role = Chef::JSONCompat.from_json(IO.read(file))
    when /\.rb$/
      if (Chef::VERSION.split('.')[0].to_i < 11)
        c_role = Chef::Role.new(true)
      else
        c_role = Chef::Role.new
      end
      c_role.from_file(file)
    end
    Spiceweasel::Log.debug("role: '#{role}' name: '#{c_role.name}'")
    if !role.eql?(c_role.name)
      STDERR.puts "ERROR: Role '#{role}' listed in the manifest does not match the name '#{c_role.name}' within the #{file} file."
      exit(-1)
    end
    c_role.run_list.each do |runlist_item|
      if(runlist_item.recipe?)
        Spiceweasel::Log.debug("recipe: #{runlist_item.name}")
        cookbook,recipe = runlist_item.name.split('::')
        Spiceweasel::Log.debug("role: '#{role}' cookbook: '#{cookbook}' dep: '#{runlist_item}'")
        unless(cookbooks.member?(cookbook))
          STDERR.puts "ERROR: Cookbook dependency '#{runlist_item}' from role '#{role}' is missing from the list of cookbooks in the manifest."
          exit(-1)
        end
      elsif(runlist_item.role?)
        Spiceweasel::Log.debug("role: '#{role}' role: '#{runlist_item}': dep: '#{runlist_item.name}'")
        unless(roles.member?(runlist_item.name))
          STDERR.puts "ERROR: Role dependency '#{runlist_item.name}' from role '#{role}' is missing from the list of roles in the manifest."
          exit(-1)
        end
      else
        STDERR.puts "ERROR: Unknown item in runlist: #{runlist_item.name}"
        exit(-1)
      end
    end
    #TODO validate any environment-specific runlists
  else #role is not here
    STDERR.puts "ERROR: Invalid Role '#{role}' listed in the manifest but not found in the roles directory."
    exit(-1)
  end
end