Class: Spiceweasel::Roles

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

Overview

manages parsing of Roles

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelper

#bundler?, #create_command, #delete_command

Constructor Details

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

rubocop:disable CyclomaticComplexity



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spiceweasel/roles.rb', line 31

def initialize(roles = {}, environments = [], cookbooks = {}) # rubocop:disable CyclomaticComplexity
  @create = []
  @delete = []
  @role_list = []

  return if roles.nil? || roles.empty?

  Spiceweasel::Log.debug("roles: #{roles}")
  rolefiles = identify_role_files(cookbooks, environments, roles)
  create_command("knife role#{Spiceweasel::Config[:knife_options]} from file #{rolefiles.uniq.sort.join(' ')}")
end

Instance Attribute Details

#createObject (readonly)

Returns the value of attribute create.



29
30
31
# File 'lib/spiceweasel/roles.rb', line 29

def create
  @create
end

#deleteObject (readonly)

Returns the value of attribute delete.



29
30
31
# File 'lib/spiceweasel/roles.rb', line 29

def delete
  @delete
end

#role_listObject (readonly)

Returns the value of attribute role_list.



29
30
31
# File 'lib/spiceweasel/roles.rb', line 29

def role_list
  @role_list
end

Instance Method Details

#determine_role_file_type(role, rolefiles) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/spiceweasel/roles.rb', line 72

def determine_role_file_type(role, rolefiles)
  if File.exist?("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

#evaluate_c_role(file) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/spiceweasel/roles.rb', line 101

def evaluate_c_role(file)
  c_role = nil
  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
    begin
      c_role.from_file(file)
    rescue SyntaxError => e
      STDERR.puts "ERROR: Role '#{file}' has syntax errors."
      STDERR.puts e.message
      exit(-1)
    end
  else
    STDERR.puts "ERROR: Role unreacable else block of 'case file' entered"
    exit(-1)
  end
  c_role
end

#identify_role_files(cookbooks, environments, roles) ⇒ Object



43
44
45
46
47
48
# File 'lib/spiceweasel/roles.rb', line 43

def identify_role_files(cookbooks, environments, roles)
  flatroles = roles.map(&:keys).flatten
  rolefiles = []
  unwind_roles(cookbooks, environments, flatroles, rolefiles)
  rolefiles
end

#member?(role) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/spiceweasel/roles.rb', line 149

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

#role_run_list(c_role, cookbooks, role, roles) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/spiceweasel/roles.rb', line 126

def role_run_list(c_role, cookbooks, role, roles)
  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
end

#unwind_roles(cookbooks, environments, flatroles, rolefiles) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spiceweasel/roles.rb', line 50

def unwind_roles(cookbooks, environments, flatroles, 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.map { |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
    determine_role_file_type(role, rolefiles)
  end
end

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

validate the content of the role file



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spiceweasel/roles.rb', line 83

def validate(role, _environments, cookbooks, roles) # rubocop:disable CyclomaticComplexity
  # validate the role passed in match the name of either the .rb or .json
  file = %W(roles/#{role}.rb roles/#{role}.json).find { |f| File.exist?(f) }
  role = role.split('/').last if role =~ /\// # pull out directories
  if file
    c_role = evaluate_c_role(file)
    Spiceweasel::Log.debug("role: '#{role}' name: '#{c_role.name}'")
    unless 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
    role_run_list(c_role, cookbooks, role, roles)
  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