class ChefRake::Task::Package

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/chef/raketasks/package.rb, line 24
def initialize
  super

  namespace :package do
    desc 'Package cookbook as .tgz file'
    task :cookbook do
      # Berkshelf::Packager does not use chefignore, so a cleanup is necessary before
      Rake::Task['clean:cookbook'].execute

      require 'berkshelf'
      current_dir = Rake.application.original_dir
      metadata = Chef::Cookbook::Metadata.new
      metadata.from_file File.join(current_dir, 'metadata.rb')
      file_name = format('cookbook-%<name>s-%<version>s.tar.gz', metadata.to_hash.transform_keys(&:to_sym))
      rel_path = File.join('pkg', file_name)
      abs_path = File.join(current_dir, rel_path)
      # Clean up and prepare
      Dir.mkdir('pkg') unless Dir.exist?('pkg')
      packager = Berkshelf::Packager.new abs_path
      packager.run(current_dir)
      printf("Cookbook(s) packaged to %s (size %d bytes)\n", rel_path, File.size(rel_path))

    rescue StandardError => e
      puts ">>> Gem load error: #{e}, omitting package" unless ENV['CI']
    end

    desc 'Package InSpec profile as .tgz file'
    task :inspec do
      # Berkshelf::Packager does not use chefignore, so a cleanup is necessary before
      Rake::Task['clean:inspec'].execute

      require 'inspec'
      require 'train'
      current_dir = Rake.application.original_dir
      pkg_path = File.join(current_dir, 'pkg')

      data = File.read(File.join(current_dir, 'inspec.yml'))
      metadata = Inspec::Metadata.from_yaml(nil, data, nil)
      metadata_as_hash = metadata.params.to_hash.transform_keys(&:to_sym)
      file_name = format('inspecprofile-%<name>s-%<version>s.tar.gz', metadata_as_hash)
      pkg_rel_path = File.join('pkg', file_name)
      abs_path = File.join(current_dir, pkg_rel_path)

      Dir.mkdir(pkg_path) unless Dir.exist?(pkg_path)
      Dir.mkdir File.join(ENV['HOME'], '.inspec/cache') unless Dir.exist? File.join(ENV['HOME'], '.inspec/cache')

      cmd = Train.create('local', command_runner: :generic).connection
      command = 'inspec'
      command << " archive #{current_dir}"
      command << ' --overwrite'
      command << " --output #{abs_path}"
      # command << ' --vendor-cache=' + ENV['HOME'] + "/.inspec/cache" # looks like this has an error in main code

      puts command
      cmd.run_command(command)

      printf("InSpec Profile(s) packaged to %s (size %d bytes)\n", abs_path, File.size(abs_path))

    rescue StandardError => e
      puts ">>> Gem load error: #{e}, omitting package" unless ENV['CI']
    end

    namespace :policyfile do
      desc 'Generate new policyfile lock'
      task :install do
        # Rake::Task["clean:policyfile"].execute
        current_dir = Rake.application.original_dir

        require 'chef-cli/cli'
        policyfile_rel_path = 'Policyfile.rb'
        policyfile_full_path = File.expand_path(policyfile_rel_path, current_dir)

        cli = ChefCLI::CLI.new(['install', policyfile_full_path])
        subcommand_name, *subcommand_params = cli.argv
        subcommand = cli.instantiate_subcommand(subcommand_name)
        subcommand.run_with_default_options(subcommand_params)

      rescue StandardError => e
        puts ">>> Gem load error: #{e}, omitting package" unless ENV['CI']
      end

      desc 'Update current policyfile.lock.json'
      task :update do
        current_dir = Rake.application.original_dir
        require 'chef-cli/cli'

        policyfile_rel_path = 'Policyfile.rb'
        policyfile_full_path = File.expand_path(policyfile_rel_path, current_dir)

        cli = ChefCLI::CLI.new(['update', policyfile_full_path])

        subcommand_name, *subcommand_params = cli.argv
        subcommand = cli.instantiate_subcommand(subcommand_name)
        subcommand.run_with_default_options(subcommand_params)

      rescue StandardError => e
        puts ">>> Gem load error: #{e}, omitting package" unless ENV['CI']
      end

      desc 'Pack current policyfile.lock.json'
      task :pack do
        current_dir = Rake.application.original_dir

        require 'chef-cli/cli'
        policyfile_rel_path = 'Policyfile.rb'
        policyfile_full_path = File.expand_path(policyfile_rel_path, current_dir)

        cli = ChefCLI::CLI.new(['update', policyfile_full_path])

        subcommand_name, *subcommand_params = cli.argv
        subcommand = cli.instantiate_subcommand(subcommand_name)
        subcommand.run_with_default_options(subcommand_params)

      rescue StandardError => e
        puts ">>> Gem load error: #{e}, omitting package" unless ENV['CI']
      end
    end # namespace policyfile
  end # namespace package
end