Class: RightScraper::Scanners::CookbookMetadata

Inherits:
Base
  • Object
show all
Defined in:
lib/right_scraper/scanners/cookbook_metadata.rb

Overview

Load cookbook metadata from a filesystem.

Defined Under Namespace

Classes: MetadataError

Constant Summary collapse

JSON_METADATA =
'metadata.json'
RUBY_METADATA =
'metadata.rb'
UNDEFINED_COOKBOOK_NAME =
'undefined'
KNIFE_METADATA_SCRIPT_NAME =
'knife_metadata.rb'
JAILED_FILE_SIZE_CONSTRAINT =

128 KB

128 * 1024
FREED_FILE_SIZE_CONSTRAINT =

64 KB

64 * 1024
TARBALL_CREATE_TIMEOUT =

..to create the tarball

30
TARBALL_ARCHIVE_NAME =
'cookbook.tar'

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RightScraper::Scanners::Base

Instance Method Details

#begin(resource) ⇒ Object



55
56
57
58
59
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 55

def begin(resource)
  @read_blk = nil
  @cookbook = resource
  true
end

#end(resource) ⇒ Object

Complete a scan for the given resource.

Parameters ===

resource(RightScraper::Resources::Base)

resource to scan



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
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 65

def end(resource)
  @logger.operation(:metadata_parsing) do
    if @read_blk
       = ::JSON.parse(@read_blk.call)
      resource. = 

      # check for undefined cookbook name.
      #
      # note that many specs in right_scraper use badly formed metadata
      # that is not even a hash so, to avoid having to fix all of them
      # (and also in case the user's metadata.json is not a hash) check
      # for the has_key? method.
      #
      # if real metadata is not a hash then that should cause failure
      # at a higher level. if the cookbook name is actually defined as
      # being 'undefined' then the user gets a warning anyway.
      if (.respond_to?(:has_key?) &&
          ['name'] == UNDEFINED_COOKBOOK_NAME)
        message =
          'Cookbook name appears to be undefined and has been' +
          ' supplied automatically.'
        @logger.note_warning(message)
      end
    else
      # should not be scanning at all unless one of the metadata files was
      # detected before starting scan.
      fail 'Unexpected missing metadata'
    end
  end
  true
ensure
  @read_blk = nil
  @cookbook = nil
end

#finishObject

All done scanning this repository, we can tear down the warden container we may or may not have created while parsing the cookbooks for this repository.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 103

def finish
  begin
    FileUtils.remove_entry_secure(tls[:tmpdir]) if tls[:tmpdir]
  rescue ::Exception => e
    @logger.note_warning(e.message)
  end

  if warden = tls[:warden]
    begin
      warden.cleanup
    rescue ::Exception => e
      @logger.note_warning(e.message)
    end
  end

ensure
  # Cleanup thread-local storage
  tls.clear
end

#notice(relative_position, &blk) ⇒ Object

Notice a file during scanning.

Block

Return the data for this file. We use a block because it may not always be necessary to read the data.

Parameters

relative_position(String)

relative pathname for the file from root of cookbook



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 131

def notice(relative_position, &blk)
  case relative_position
  when 
    # preferred over RUBY_METADATA.
    @read_blk = blk
  when 
    # defer to any JSON_METADATA, which we hope refers to the same info.
    @read_blk ||= self.method(:generate_metadata_json)
  end
  true
end

#notice_dir(relative_position) ⇒ Object

Notice a directory during scanning. Since metadata.json,rb is by definition only in the root directory we don’t need to recurse, but we do need to go into the first directory (identified by relative_position being nil).

Parameters

relative_position(String)

relative pathname for the directory from root of cookbook

Returns

Boolean

should the scanning recurse into the directory



153
154
155
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 153

def notice_dir(relative_position)
  relative_position == nil
end

#tlsObject



51
52
53
# File 'lib/right_scraper/scanners/cookbook_metadata.rb', line 51

def tls
  Thread.current[self.class.to_s.to_sym] ||= {}
end