class Retrospec::Plugins::V1::Puppet

Attributes

context[R]
files[R]
manifest_dir[RW]
manifest_files[R]
template_dir[R]

Public Class Methods

file_type() click to toggle source

the main file type that is used to help discover what the module is @return [String] - the puppet file extension

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 382
def self.file_type
  '.pp'
end
logger() click to toggle source

@return [Logger] - instance of a logger

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 37
def self.logger
  @logger ||= begin
    require 'logger'
    log = Logger.new(STDOUT)
    if ENV['RETROSPEC_LOGGER_LEVEL'] == 'debug'
      log.level = Logger::DEBUG
    else
      log.level = Logger::INFO
    end
    log
  end
end
new(module_path = nil, config = {}) click to toggle source

@param module_path [String] - the path to the module @param config [Hash] - the configuration, which is passed to the parent

Calls superclass method
# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 27
def initialize(module_path = nil, config = {})
  super
  ::Puppet[:environment] = 'retrospec'
  ::Puppet[:environmentpath] = Utilities::PuppetModule.base_environment_path
  @manifest_dir = File.join(module_path, 'manifests')
  # user supplied a template path or user wants to use local templates
  @template_dir = setup_user_template_dir(config_data[:template_dir], config_data[:scm_url], config_data[:ref])
end
run_cli(global_opts, global_config, plugin_config, args=ARGV) click to toggle source

used to display subcommand options to the global_config cli the global options are passed in for your usage optimist.rubyforge.org all options here are available in the config passed into the initialize code this is the only entry point into the plugin @param global_opts [Hash] - the global options for retrospec supplied on the cli @param global_config [Hash] - the global config file options @param plugin_config [Hash] - the global config for the puppet plugin @param args [Array] - the args passed in on the cli through ARGV, useful for testing

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 100
        def self.run_cli(global_opts, global_config, plugin_config, args=ARGV)
          template_dir = ENV['RETROSPEC_TEMPLATES_DIR'] || plugin_config['plugins::puppet::template_dir'] || File.expand_path('~/.retrospec/repos/retrospec-puppet-templates')
          scm_url = ENV['RETROSPEC_PUPPET_SCM_URL'] || plugin_config['plugins::puppet::templates::url']
          #scm_branch = ENV['RETROSPEC_PUPPET_SCM_BRANCH'] || plugin_config['plugins::puppet::templates::ref'] || 'master'
          # TODO next major release - remove the RETROSPEC_PUPPET_SCM_BRANCH as env setting
          scm_ref = ENV['RETROSPEC_PUPPET_SCM_REF'] || ENV['RETROSPEC_PUPPET_SCM_BRANCH'] || plugin_config['plugins::puppet::templates::ref'] || 'master'
          beaker_tests  = plugin_config['plugins::puppet::enable_beaker_tests'] || false
          # a list of subcommands for this plugin
          sub_commands  = %w(new_module new_task new_fact new_type new_provider new_function new_report module_data)
          if sub_commands.count > 0
            sub_command_help = "Subcommands:\n  #{sub_commands.join("\n  ")}\n"
          else
            sub_command_help = ''
          end
          plugin_opts = Optimist.options(args) do
            version "Retrospec puppet plugin: #{Retrospec::Puppet::VERSION} (c) Corey Osman at NWOPS, LLC"
            banner <<-EOS
Generates puppet rspec test code and puppet module components.

#{sub_command_help}

            EOS
            opt :template_dir, 'Path to templates directory (only for overriding Retrospec templates)', :type => :string,
            :required => false, :default => template_dir
            opt :scm_url, 'SCM url for retrospec templates', :type => :string, :required => false,
            :default => scm_url
            opt :ref, 'Branch you want to use for the retrospec template repo', :type => :string, :required => false,
            :default => scm_ref
            opt :enable_beaker_tests, 'Enable the creation of beaker tests', :require => false, :type => :boolean, :default => beaker_tests
            stop_on sub_commands
          end
          # the passed in options will always override the config file
          plugin_data = plugin_opts.merge(global_config).merge(global_opts).merge(plugin_opts).merge(plugin_config)
          # define the default action to use the plugin here, the default is run
          sub_command = (args.shift || :run).to_sym
          # create an instance of this plugin
          plugin = new(plugin_data[:module_path], plugin_data)
          # check if the plugin supports the sub command
          begin
            if plugin.respond_to?(sub_command)
              case sub_command
              when :new_module
                plugin.send(sub_command, plugin_data, args)
                plugin.post_init # finish initialization
              when :run
                plugin.post_init   # finish initialization
              when :new_type
                plugin.new_type(plugin_data, args)
              when :new_function
                plugin.new_function(plugin_data, args)
              when :new_task
                plugin.new_task(plugin_data, args)
              when :new_fact
                plugin.new_fact(plugin_data, args)
              when :new_provider
                plugin.new_provider(plugin_data, args)
              else
                plugin.post_init   # finish initialization
                plugin.send(sub_command, plugin_data[:module_path], plugin_data, args)
              end
              plugin.send(:run)
            else
              puts "The subcommand #{sub_command} is not supported or valid".fatal
              exit 1
            end
          rescue Retrospec::Puppet::InvalidModulePathError => e
            exit 1
          rescue Retrospec::Puppet::NoManifestDirError => e
            exit 1
          rescue Retrospec::Puppet::ParserError => e
            exit 1
          rescue Retrospec::Puppet::Generators::CoreTypeException => e
            puts e.message.fatal
          rescue Errno::ENOENT => e
            puts e.message
          rescue Exception => e
            puts e.message
            exit 1
          end
          plugin_data
        end

Public Instance Methods

create_files() click to toggle source

this is the method that performs all the magic and creates all the files @return [Boolean] - returns true if the method completed

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 352
def create_files
  filter = /nodesets|acceptance|spec_helper_acceptance/ unless context.enable_beaker_tests?
  safe_create_module_files(template_dir, module_path, context, filter)
  fact(module_path, config_data)
  type_spec_files(module_path, config_data)
  provider_spec_files(module_path, config_data)
  function_spec_files(module_path, config_data)
  # FIXME temporary disabling to re-release in future update
  #new_schema(module_path, config_data)
  Retrospec::Puppet::Generators::ModuleGenerator.generate_metadata_file(context.module_name, config_data)
  Retrospec::Puppet::Generators::ResourceBaseGenerator.generate_spec_files(module_path, config_data)
  Retrospec::Puppet::Generators::AcceptanceGenerator.generate_spec_files(module_path, config_data) if context.enable_beaker_tests?
  Utilities::PuppetModule.clean_tmp_modules_dir
  true
end
description() click to toggle source

@return [String] - the description of this project

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 369
def description
  'Generates puppet rspec test code based on the classes and defines inside the manifests directory'
end
fact(module_path, config) click to toggle source

generates the fact spec files @param module_path [String] - the path to the module @param config [Hash] - the configuration from the cli and retrospec @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 299
def fact(module_path, config)
  f = Retrospec::Puppet::Generators::FactGenerator.new(module_path, config)
  f.generate_fact_spec_files
end
function_spec_files(module_path, config) click to toggle source

generates function spec files @param module_path [String] - the path to the module @param config [Hash] - the configuration from the cli and retrospec @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 240
def function_spec_files(module_path, config)
  f = Retrospec::Puppet::Generators::FunctionGenerator.new(module_path, config)
  f.generate_spec_files
end
module_data(module_path, config, args=[]) click to toggle source

generates module data files @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @param module_path [String] - the path to the module @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 199
def module_data(module_path, config, args=[])
  plugin_data = Retrospec::Puppet::Generators::ModuleDataGenerator.run_cli(config, args)
  plugin_data[:puppet_context] = context
  p = Retrospec::Puppet::Generators::ModuleDataGenerator.new(module_path, plugin_data)
  p.run
end
new_fact(plugin_data, args) click to toggle source

generates a new fact file @param args [Hash] - the main retrospec args from the config file or cli @param plugin_data [Hash] - the cli args passed in for the generator @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 289
def new_fact(plugin_data, args)
  f = Retrospec::Puppet::Generators::FactGenerator.run_cli(plugin_data, args)
  post_init # finish initialization
  f.generate_fact_file
end
new_function(config, args) click to toggle source

generates a new puppet function file @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 229
def new_function(config, args)
  plugin_data = Retrospec::Puppet::Generators::FunctionGenerator.run_cli(config, args)
  f = Retrospec::Puppet::Generators::FunctionGenerator.new(plugin_data[:module_path], plugin_data)
  post_init
  f.generate_function_file
end
new_module(plugin_data, args) click to toggle source

if the module does not exist lets create it this will create the module directory, manifests directory and basic init.pp file if the manifest directory already exist but an init.pp file does not we do not creating anything since it is not mandatory I thought about using the the module face to perform this generation but it seems like its not supported at this time, and you can't specify the path to generate the module in generates a new puppet function file @param args [Hash] - the main retrospec args from the config file or cli @param plugin_data [Hash] - the cli args passed in for the generator

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 72
def new_module(plugin_data, args)
  plugin_data = Retrospec::Puppet::Generators::ModuleGenerator.run_cli(plugin_data, args)
  # the user passed in /tmp/test1 and the name is irrelevent
  if ! File.exists?(plugin_data[:module_path])
    plugin_data[:module_path] = File.join(plugin_data[:module_path])
    # if the module path basename is the same as the module name
    # this is a parent directory or the module already exists
  elsif File.basename(plugin_data[:module_path]) != plugin_data[:name]
    plugin_data[:module_path] = File.join(plugin_data[:module_path], plugin_data[:name])
  end
  # we need to set this because the this class is created before we created the new module directory
  # so we now have to set the manifests and module directory
  self.module_path = plugin_data[:module_path]
  config_data[:module_path] = plugin_data[:module_path]
  self.manifest_dir = File.join(plugin_data[:module_path], 'manifests')
  f = Retrospec::Puppet::Generators::ModuleGenerator.new(plugin_data[:module_path], plugin_data)
  f.run(manifest_dir)
end
new_provider(config, args) click to toggle source

generates a new provider @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 249
def new_provider(config, args)
  plugin_data = Retrospec::Puppet::Generators::ProviderGenerator.run_cli(config, args)
  p = Retrospec::Puppet::Generators::ProviderGenerator.new(plugin_data[:module_path], plugin_data)
  post_init
  p.generate_provider_files
end
new_report(module_path, config, args=[]) click to toggle source

generates a new puppet report file @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @param module_path [String] - the path to the module @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 211
def new_report(module_path, config, args=[])
  plugin_data = Retrospec::Puppet::Generators::ReportGenerator.run_cli(config, args)
  p = Retrospec::Puppet::Generators::ReportGenerator.new(module_path, plugin_data)
  p.run
end
new_task(config, args) click to toggle source

generates a new puppet task @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 186
def new_task(config, args)
  plugin_data = Retrospec::Puppet::Generators::TaskGenerator.run_cli(config, args)
  plugin_data[:puppet_context] = context
  t = Retrospec::Puppet::Generators::TaskGenerator.new(plugin_data[:module_path], plugin_data)
  t.run
  post_init
end
new_type(config, args) click to toggle source

generates a new type @param args [Hash] - the main retrospec args from the config file or cli @param config [Hash] - the cli args passed in for the generator @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 269
def new_type(config, args)
  plugin_data = Retrospec::Puppet::Generators::TypeGenerator.run_cli(config, args)
  t = Retrospec::Puppet::Generators::TypeGenerator.new(plugin_data[:module_path], plugin_data)
  post_init
  t.generate_type_files
end
post_init() click to toggle source

@return [Hash] - the context generated after the module has been setup

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 51
def post_init
  # before we validate the module directory we should ensure the module exists by creating it
  # validation also occurs when setting the module path
  # these are required because the puppet module creates a singleton with some cached values
  Utilities::PuppetModule.instance.module_dir_name = File.basename(module_path)
  Utilities::PuppetModule.instance.module_name = File.basename(module_path)
  Utilities::PuppetModule.instance.module_path = module_path
  Utilities::PuppetModule.create_tmp_module_path # this is required to finish initialization
  # setting the context is required to make other methods below work.  #TODO lazy create the context
  @context = ::Retrospec::Puppet::SpecObject.new(module_path, Utilities::PuppetModule.instance, config_data)
end
provider_spec_files(module_path, config) click to toggle source

generates provider spec files @param module_path [String] - the path to the module @param config [Hash] - the configuration from the cli and retrospec @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 260
def provider_spec_files(module_path, config)
  t = Retrospec::Puppet::Generators::ProviderGenerator.new(module_path, config)
  t.generate_provider_spec_files
end
run() click to toggle source

this is the main method the starts all the magic This also represents the order in which tasks are run

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 306
def run
  run_pre_hook
  create_files
  run_post_hook
end
run_hook(hook_file) click to toggle source

@param hook_file [String] - the path to the hook file runs the hook file, on windows this is a bit tricky because ruby may not be in the path @note the hook file can only be a ruby script this is because windows cannot execute just any script and must be directed by prepending with the program name unlike unix shebang magic

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 331
def run_hook(hook_file)
  return unless File.exist?(hook_file)
  output = `ruby #{hook_file} #{module_path}`
  if $CHILD_STATUS.success?
    puts "Successfully ran hook: #{hook_file}".info
    puts output.info
  else
    puts "Error running hook: #{hook_file}".fatal
    puts output.fatal
  end
end
run_post_hook() click to toggle source

runs a user defined hook called post-hook if the template directory contains a post-hook file we run that

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 345
def run_post_hook
  hook_file = File.join(template_dir, 'post-hook')
  run_hook(hook_file)
end
run_pre_hook() click to toggle source

runs a user defined hook called pre-hook

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 319
def run_pre_hook
  hook_file = File.join(template_dir, 'pre-hook')
  run_hook(hook_file)
end
type_spec_files(module_path, config) click to toggle source

generates spec files for a puppet type @param module_path [String] - the path to the module @param config [Hash] - the configuration from the cli and retrospec @return [Array] - returns the files paths that were generated

# File lib/retrospec/plugins/v1/plugin/puppet.rb, line 280
def type_spec_files(module_path, config)
  t = Retrospec::Puppet::Generators::TypeGenerator.new(module_path, config)
  t.generate_type_spec_files
end