Sha256: 21047d690242e007d5450924e77cc9bac83e71ab2723e3d15c529a74d6c49b95

Contents?: true

Size: 1.63 KB

Versions: 39

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8
# author: Adam Leff

require 'inspec/profile'

module Inspec
  class ProfileVendor
    attr_reader :profile_path

    def initialize(path)
      @profile_path = Pathname.new(path)
    end

    def vendor!
      vendor_dependencies
    end

    # The URL fetcher uses a Tempfile to retrieve the vendored
    # profile, which creates a file that is only readable by
    # the current user. In most circumstances, this is likely OK.
    # However, in environments like a Habitat package, these files
    # need to be readable by all users or the Habitat Supervisor
    # may not be able to start InSpec correctly.
    #
    # This method makes sure all vendored files are mode 644 for this
    # use case. This method is not called by default - the caller
    # vendoring the profile must make the decision as to whether this
    # is necessary.
    def make_readable
      Dir.glob("#{cache_path}/**/*") do |e|
        FileUtils.chmod(0644, e) if File.file?(e)
      end
    end

    def cache_path
      profile_path.join('vendor')
    end

    def lockfile
      profile_path.join('inspec.lock')
    end

    private

    def profile
      @profile ||= Inspec::Profile.for_target(profile_path.to_s, profile_opts)
    end

    def profile_opts
      {
        cache: Inspec::Cache.new(cache_path.to_s),
        backend: Inspec::Backend.create(target: 'mock://'),
      }
    end

    def vendor_dependencies
      delete_vendored_data
      File.write(lockfile, profile.generate_lockfile.to_yaml)
    end

    def delete_vendored_data
      FileUtils.rm_rf(cache_path) if cache_path.exist?
      File.delete(lockfile) if lockfile.exist?
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
inspec-1.31.1 lib/inspec/profile_vendor.rb
inspec-1.31.0 lib/inspec/profile_vendor.rb
inspec-1.30.0 lib/inspec/profile_vendor.rb
inspec-1.29.0 lib/inspec/profile_vendor.rb
inspec-1.28.1 lib/inspec/profile_vendor.rb
inspec-1.28.0 lib/inspec/profile_vendor.rb
inspec-1.27.0 lib/inspec/profile_vendor.rb
inspec-1.26.0 lib/inspec/profile_vendor.rb
inspec-1.25.1 lib/inspec/profile_vendor.rb
inspec-1.25.0 lib/inspec/profile_vendor.rb
inspec-1.24.0 lib/inspec/profile_vendor.rb
inspec-1.23.0 lib/inspec/profile_vendor.rb
inspec-1.22.0 lib/inspec/profile_vendor.rb
inspec-1.21.0 lib/inspec/profile_vendor.rb
inspec-1.20.0 lib/inspec/profile_vendor.rb
inspec-1.19.2 lib/inspec/profile_vendor.rb
inspec-1.19.1 lib/inspec/profile_vendor.rb
inspec-1.19.0 lib/inspec/profile_vendor.rb
inspec-1.18.0 lib/inspec/profile_vendor.rb