class Utilities::PuppetModule

Attributes

module_path[RW]

Public Class Methods

base_environment_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 27
def self.base_environment_path
  Utilities::PuppetModule.instance.base_environment_path
end
clean_tmp_modules_dir() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 48
def self.clean_tmp_modules_dir
  FileUtils.remove_entry_secure instance.tmp_modules_dir # ensure we remove the temporary directory
end
create_tmp_module_path() click to toggle source

create the temporary module create, validate the

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 44
def self.create_tmp_module_path
  Utilities::PuppetModule.instance.create_tmp_module_path(module_path)
end
instance() click to toggle source

gets an instance of the module class. The create_tmp_module_path must first be called before instance can do anything useful.

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 59
def self.instance
  @@instance ||= new
end
module_dir_name() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 35
def self.module_dir_name
  Utilities::PuppetModule.instance.module_dir_name
end
module_name() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 31
def self.module_name
  Utilities::PuppetModule.instance.module_name
end
module_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 23
def self.module_path
  Utilities::PuppetModule.instance.module_path
end
module_types() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 39
def self.module_types
  Utilities::PuppetModule.instance.types
end
parser() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 11
def self.parser
  Utilities::PuppetModule.instance.parser
end
tmp_module_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 15
def self.tmp_module_path
  Utilities::PuppetModule.instance.tmp_module_path
end
tmp_modules_dir() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 19
def self.tmp_modules_dir
  Utilities::PuppetModule.instance.tmp_modules_dir
end

Public Instance Methods

base_environment_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 157
def base_environment_path
  @base_environment_path ||= Dir.mktmpdir
end
create_tmp_module_path(module_path) click to toggle source

puts a symlink in that module directory that points back to the user supplied module path

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 103
def create_tmp_module_path(module_path)
  fail 'ModulePathNotFound' unless module_path
  path = File.join(tmp_modules_dir, module_dir_name)
  unless File.exist?(path) # only create if it doesn't already exist
    # create a link where source is the current repo and dest is /tmp/modules/module_name
    FileUtils.ln_s(module_path, path)
  end
  path
end
default_modules_paths() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 161
def default_modules_paths
  [tmp_modules_dir]
end
default_puppet_env_name() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 117
def default_puppet_env_name
  'retrospec'
end
find_resource(resource_name) click to toggle source

returns the resource type object given a resource name ie. tomcat::connector

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 192
def find_resource(resource_name)
  request = request(resource_name, 'find')
  resource_type_parser.find(request)
end
module_dir_name() click to toggle source

the directory name of the module usually this is the same as the module name but it can be namespaced sometimes

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 123
def module_dir_name
  fail 'ModulePathNotFound' unless module_path
  @module_dir_name ||= File.basename(module_path)
end
module_dir_name=(name) click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 128
def module_dir_name=(name)
  @module_dir_name = name
end
module_name() click to toggle source

returns the name of the module ie. mysql::config => mysql

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 141
def module_name
  @module_name ||= types.first.name.split('::').first
rescue
  @module_name ||= module_dir_name
end
module_name=(name) click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 136
def module_name=(name)
  @module_name = name
end
module_type_names() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 132
def module_type_names
  types.map(&:name)
end
parser() click to toggle source

returns a future/4.x parser for evaluating code

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 98
def parser
  @parser ||= ::Puppet::Pops::Parser::EvaluatingParser.new
end
puppet_environment() click to toggle source

creates a puppet environment given a module path and environment name

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 170
def puppet_environment
  @puppet_environment ||= ::Puppet::Node::Environment.create(
      default_puppet_env_name,
      default_modules_paths,
    )
end
request(key, method) click to toggle source

creates a puppet resource request to be used indirectly

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 178
def request(key, method)
  instance = ::Puppet::Indirector::Indirection.instance(:resource_type)
  indirection_name = 'test'
  @request = ::Puppet::Indirector::Request.new(indirection_name, method, key, instance)
  @request.environment = puppet_environment
  @request
end
resource_type_parser() click to toggle source

creates an instance of the resource type parser

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 187
def resource_type_parser
  @resource_type_parser ||= ::Puppet::Indirector::ResourceType::Parser.new
end
search_module(pattern = '*') click to toggle source

returns the resource types found in the module

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 198
def search_module(pattern = '*')
  request = request(pattern, 'search')
  resource_type_parser.search(request)
end
temporary_environment_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 165
def temporary_environment_path
  @temporary_environment_path ||= File.join(base_environment_path,default_puppet_env_name)
end
tmp_module_path() click to toggle source
# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 113
def tmp_module_path
  @tmp_module_path ||= File.join(tmp_modules_dir, module_dir_name)
end
tmp_modules_dir() click to toggle source

creates a tmp module directory so puppet can work correctly

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 148
def tmp_modules_dir
  if @tmp_modules_dir.nil? || !File.exist?(@tmp_modules_dir)
    tmp_path = File.expand_path(File.join(temporary_environment_path, 'modules'))
    FileUtils.mkdir_p(tmp_path)
    @tmp_modules_dir = tmp_path
  end
  @tmp_modules_dir
end
types() click to toggle source

TODO: we need to parse the types and find all the types that inherit other types and then order them so we can load the files first

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 204
def types
  @types ||= search_module || []
end
validate_module_dir(dir) click to toggle source

processes a directory and expands to its full path, assumes './' returns the validated dir

# File lib/retrospec/plugins/v1/plugin/puppet_module.rb, line 65
def validate_module_dir(dir)
  # first check to see if manifests directory even exists when path is nil
  if dir.nil?
    dir = '.'
  elsif dir.instance_of?(Array)
    puts 'Retrospec - an array of module paths is not supported at this time'.fatal
    raise Retrospec::Puppet::InvalidModulePathError
  end
  dir = File.expand_path(dir)
  manifest_dir = File.join(dir, 'manifests')
  if !File.exist?(manifest_dir)
    puts "No manifest directory in #{manifest_dir}, cannot validate this is a module".fatal
    raise Retrospec::Puppet::NoManifestDirError
  else
    files = Dir.glob("#{manifest_dir}/**/*.pp")
    warn "No puppet manifest files found at #{manifest_dir}".warning if files.length < 1
    # validate the manifest files, because if one files doesn't work it affects everything
    files.each do |file|
      begin
        parser.parse_file(file)
      rescue Puppet::ParseError
        puts "Manifest file: #{file} has parser errors, please fix and re-check using\n puppet parser validate #{file}".fatal
        raise Retrospec::Puppet::ParserError
      rescue SystemExit => e
        puts "Manifest file: #{file} has parser errors, please fix and re-check using\n puppet parser validate #{file}".fatal
        raise Retrospec::Puppet::ParserError
      end
    end
  end
  dir
end