class Spiceweasel::Roles
manages parsing of Roles
Attributes
create[R]
delete[R]
role_list[R]
Public Class Methods
new(roles = {}, environments = [], cookbooks = {})
click to toggle source
# 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
Public Instance Methods
determine_role_file_type(role, rolefiles)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/spiceweasel/roles.rb, line 149 def member?(role) role_list.include?(role) end
role_run_list(c_role, cookbooks, role, roles)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
validate the content of the role file
# 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