Class: ChefRake::Task::Package

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/chef/raketasks/package.rb

Instance Method Summary collapse

Constructor Details

#initializePackage

Returns a new instance of Package.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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
       = Chef::Cookbook::Metadata.new
      .from_file File.join(current_dir, 'metadata.rb')
      file_name = format('cookbook-%<name>s-%<version>s.tar.gz', .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'))
       = Inspec::Metadata.from_yaml(nil, data, nil)
       = .params.to_hash.transform_keys(&:to_sym)
      file_name = format('inspecprofile-%<name>s-%<version>s.tar.gz', )
      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