berkshelf 7.0.8 → 7.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -12
  3. data/Rakefile +3 -3
  4. data/berkshelf.gemspec +3 -4
  5. data/bin/berks +1 -1
  6. data/lib/berkshelf.rb +8 -9
  7. data/lib/berkshelf/api_client/chef_server_connection.rb +3 -2
  8. data/lib/berkshelf/api_client/connection.rb +1 -1
  9. data/lib/berkshelf/api_client/remote_cookbook.rb +1 -1
  10. data/lib/berkshelf/berksfile.rb +41 -39
  11. data/lib/berkshelf/cached_cookbook.rb +5 -3
  12. data/lib/berkshelf/chef_config_compat.rb +1 -1
  13. data/lib/berkshelf/chef_repo_universe.rb +4 -2
  14. data/lib/berkshelf/cli.rb +6 -6
  15. data/lib/berkshelf/commands/shelf.rb +1 -1
  16. data/lib/berkshelf/community_rest.rb +6 -6
  17. data/lib/berkshelf/config.rb +3 -3
  18. data/lib/berkshelf/cookbook_store.rb +2 -4
  19. data/lib/berkshelf/core_ext.rb +1 -1
  20. data/lib/berkshelf/core_ext/file_utils.rb +3 -3
  21. data/lib/berkshelf/dependency.rb +1 -1
  22. data/lib/berkshelf/downloader.rb +9 -6
  23. data/lib/berkshelf/errors.rb +5 -2
  24. data/lib/berkshelf/file_syncer.rb +10 -12
  25. data/lib/berkshelf/formatters/human.rb +1 -1
  26. data/lib/berkshelf/formatters/json.rb +1 -1
  27. data/lib/berkshelf/installer.rb +1 -1
  28. data/lib/berkshelf/location.rb +3 -3
  29. data/lib/berkshelf/locations/git.rb +6 -12
  30. data/lib/berkshelf/lockfile.rb +11 -11
  31. data/lib/berkshelf/logger.rb +4 -2
  32. data/lib/berkshelf/mixin/git.rb +3 -3
  33. data/lib/berkshelf/packager.rb +5 -7
  34. data/lib/berkshelf/resolver.rb +1 -1
  35. data/lib/berkshelf/ridley_compat.rb +1 -1
  36. data/lib/berkshelf/shell.rb +2 -1
  37. data/lib/berkshelf/shell_out.rb +4 -3
  38. data/lib/berkshelf/source.rb +8 -7
  39. data/lib/berkshelf/source_uri.rb +1 -1
  40. data/lib/berkshelf/ssl_policies.rb +5 -9
  41. data/lib/berkshelf/thor.rb +1 -1
  42. data/lib/berkshelf/thor_ext.rb +1 -1
  43. data/lib/berkshelf/uploader.rb +8 -6
  44. data/lib/berkshelf/validator.rb +2 -8
  45. data/lib/berkshelf/version.rb +1 -1
  46. data/lib/berkshelf/visualizer.rb +3 -3
  47. data/spec/config/knife.rb +1 -1
  48. data/spec/spec_helper.rb +1 -1
  49. data/spec/support/chef_server.rb +2 -2
  50. data/spec/support/git.rb +18 -18
  51. data/spec/support/path_helpers.rb +4 -4
  52. data/spec/unit/berkshelf/berksfile_spec.rb +7 -7
  53. data/spec/unit/berkshelf/cli_spec.rb +1 -2
  54. data/spec/unit/berkshelf/community_rest_spec.rb +1 -1
  55. data/spec/unit/berkshelf/core_ext/file_utils_spec.rb +2 -2
  56. data/spec/unit/berkshelf/dependency_spec.rb +5 -5
  57. data/spec/unit/berkshelf/downloader_spec.rb +4 -8
  58. data/spec/unit/berkshelf/locations/base_spec.rb +1 -2
  59. data/spec/unit/berkshelf/locations/git_spec.rb +2 -5
  60. data/spec/unit/berkshelf/locations/path_spec.rb +1 -2
  61. data/spec/unit/berkshelf/lockfile_spec.rb +9 -18
  62. data/spec/unit/berkshelf/ridley_compat_spec.rb +2 -2
  63. data/spec/unit/berkshelf/source_spec.rb +30 -19
  64. data/spec/unit/berkshelf/ssl_policies_spec.rb +3 -6
  65. data/spec/unit/berkshelf/uploader_spec.rb +6 -10
  66. data/spec/unit/berkshelf/validator_spec.rb +0 -13
  67. metadata +10 -10
@@ -1,5 +1,5 @@
1
- require "mixlib/config"
2
- require "openssl"
1
+ require "mixlib/config" unless defined?(Mixlib::Config)
2
+ require "openssl" unless defined?(OpenSSL)
3
3
 
4
4
  # we need this method, but have to inject it into mixlib-config directly
5
5
  # to have it available from config contexts
@@ -122,7 +122,7 @@ module Berkshelf
122
122
  default :email, nil
123
123
  default :license, nil
124
124
  end
125
- default :allowed_licenses, Array.new
125
+ default :allowed_licenses, []
126
126
  default :raise_license_exception, false
127
127
  config_context :vagrant do
128
128
  config_context :vm do
@@ -1,4 +1,4 @@
1
- require "fileutils"
1
+ require "fileutils" unless defined?(FileUtils)
2
2
  require "chef/exceptions"
3
3
 
4
4
  module Berkshelf
@@ -49,9 +49,7 @@ module Berkshelf
49
49
 
50
50
  # Destroy the contents of the initialized storage path.
51
51
  def clean!
52
- Dir.chdir(storage_path) do
53
- FileUtils.rm_rf(Dir.glob("*"))
54
- end
52
+ FileUtils.rm_rf(Dir.glob("#{storage_path}/*"))
55
53
  end
56
54
 
57
55
  # Import a cookbook found on the local filesystem into this instance of the cookbook store.
@@ -1,3 +1,3 @@
1
1
  Dir["#{File.dirname(__FILE__)}/core_ext/*.rb"].sort.each do |path|
2
- require_relative "core_ext/#{File.basename(path, '.rb')}"
2
+ require_relative "core_ext/#{File.basename(path, ".rb")}"
3
3
  end
@@ -1,4 +1,4 @@
1
- require "fileutils"
1
+ require "fileutils" unless defined?(FileUtils)
2
2
 
3
3
  module FileUtils
4
4
  class << self
@@ -14,10 +14,10 @@ module FileUtils
14
14
  # symlink on Linux
15
15
  # @see {FileUtils::mv}
16
16
  def mv(src, dest, options = {})
17
- old_mv(src, dest, options)
17
+ old_mv(src, dest, **options)
18
18
  rescue Errno::EACCES, Errno::ENOENT
19
19
  options.delete(:force) if options.key?(:force)
20
- FileUtils.cp_r(src, dest, options)
20
+ FileUtils.cp_r(src, dest, **options)
21
21
  FileUtils.rm_rf(src)
22
22
  end
23
23
  end
@@ -171,7 +171,7 @@ module Berkshelf
171
171
  "#{name} (#{version_constraint})",
172
172
  "locked_version: #{locked_version.inspect}",
173
173
  "groups: #{groups}",
174
- "location: #{location || 'default'}>",
174
+ "location: #{location || "default"}>",
175
175
  ].join(", ")
176
176
  end
177
177
 
@@ -1,6 +1,7 @@
1
- require "net/http"
2
- require "mixlib/archive"
3
- require "berkshelf/ssl_policies"
1
+ require "net/http" unless defined?(Net::HTTP)
2
+ require "mixlib/archive" unless defined?(Mixlib::Archive)
3
+ require_relative "ssl_policies"
4
+ require "faraday" unless defined?(Faraday)
4
5
 
5
6
  module Berkshelf
6
7
  class Downloader
@@ -83,7 +84,7 @@ module Berkshelf
83
84
  client_key: source.options[:client_key] || Berkshelf::Config.instance.chef.client_key,
84
85
  ssl: source.options[:ssl],
85
86
  }
86
- RidleyCompat.new_client(credentials) do |conn|
87
+ RidleyCompat.new_client(**credentials) do |conn|
87
88
  cookbook = Chef::CookbookVersion.load(name, version)
88
89
  manifest = cookbook.cookbook_manifest
89
90
  manifest.by_parent_directory.each do |segment, files|
@@ -120,7 +121,7 @@ module Berkshelf
120
121
  )
121
122
 
122
123
  begin
123
- url = URI(github_client.archive_link(cookbook_uri.path.gsub(/^\//, ""), ref: "v#{version}"))
124
+ url = URI(github_client.archive_link(cookbook_uri.path.gsub(%r{^/}, ""), ref: "v#{version}"))
124
125
  rescue Octokit::Unauthorized
125
126
  return nil
126
127
  end
@@ -131,6 +132,7 @@ module Berkshelf
131
132
  http.verify_mode = (options["ssl_verify"].nil? || options["ssl_verify"]) ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
132
133
  resp = http.get(url.request_uri)
133
134
  return nil unless resp.is_a?(Net::HTTPSuccess)
135
+
134
136
  open(archive_path, "wb") { |file| file.write(resp.body) }
135
137
 
136
138
  Mixlib::Archive.new(archive_path).extract(unpack_dir)
@@ -143,7 +145,7 @@ module Berkshelf
143
145
 
144
146
  File.join(unpack_dir, cookbook_directory)
145
147
  when :uri
146
- require "open-uri"
148
+ require "open-uri" unless defined?(OpenURI)
147
149
 
148
150
  tmp_dir = Dir.mktmpdir
149
151
  archive_path = Pathname.new(tmp_dir) + "#{name}-#{version}.tar.gz"
@@ -183,6 +185,7 @@ module Berkshelf
183
185
 
184
186
  resp = connection.get(cookbook_uri.request_uri + "&private_token=" + options["private_token"])
185
187
  return nil unless resp.status == 200
188
+
186
189
  open(archive_path, "wb") { |file| file.write(resp.body) }
187
190
 
188
191
  Mixlib::Archive.new(archive_path).extract(unpack_dir)
@@ -83,7 +83,7 @@ module Berkshelf
83
83
 
84
84
  def to_s
85
85
  @original_exception.to_s +
86
- "Unable to find a solution for demands: #{demands.join(', ')}"
86
+ "Unable to find a solution for demands: #{demands.join(", ")}"
87
87
  end
88
88
 
89
89
  alias_method :message, :to_s
@@ -91,6 +91,7 @@ module Berkshelf
91
91
 
92
92
  class CookbookSyntaxError < BerkshelfError; set_status_code(107); end
93
93
  class ConstraintNotSatisfied < BerkshelfError; set_status_code(111); end
94
+
94
95
  class BerksfileReadError < BerkshelfError
95
96
  set_status_code(113)
96
97
 
@@ -254,6 +255,7 @@ module Berkshelf
254
255
  end
255
256
 
256
257
  class UploadFailure < BerkshelfError; end
258
+
257
259
  class FrozenCookbook < UploadFailure
258
260
  set_status_code(126)
259
261
 
@@ -372,7 +374,7 @@ module Berkshelf
372
374
 
373
375
  def to_s
374
376
  "Unknown license: '#{license}'\n" +
375
- "Available licenses: #{CookbookGenerator::LICENSES.join(', ')}"
377
+ "Available licenses: #{CookbookGenerator::LICENSES.join(", ")}"
376
378
  end
377
379
 
378
380
  alias_method :message, :to_s
@@ -436,6 +438,7 @@ module Berkshelf
436
438
  end
437
439
 
438
440
  class DuplicateDemand < BerkshelfError; set_status_code(138); end
441
+
439
442
  class LockfileNotFound < BerkshelfError
440
443
  set_status_code(140)
441
444
 
@@ -1,4 +1,4 @@
1
- require "fileutils"
1
+ require "fileutils" unless defined?(FileUtils)
2
2
 
3
3
  module Berkshelf
4
4
  module FileSyncer
@@ -24,8 +24,8 @@ module Berkshelf
24
24
  # versions of windows points at 'C:\users'. Some users have their
25
25
  # temp directory still referring to 'C:\Documents and Settings'.
26
26
  #
27
- def glob(pattern)
28
- Dir.glob(pattern, File::FNM_DOTMATCH).sort.reject do |file|
27
+ def glob(pattern, **kwargs)
28
+ Dir.glob(pattern, File::FNM_DOTMATCH, **kwargs).sort.reject do |file|
29
29
  basename = File.basename(file)
30
30
  IGNORED_FILES.include?(basename)
31
31
  end
@@ -65,11 +65,10 @@ module Berkshelf
65
65
  [exclude, "#{exclude}/*"]
66
66
  end.flatten
67
67
 
68
- source_files = Dir.chdir(source) do
69
- glob("**/*").reject do |source_file|
68
+ source_files =
69
+ glob("**/*", base: source).reject do |source_file|
70
70
  excludes.any? { |exclude| File.fnmatch?(exclude, source_file, File::FNM_DOTMATCH) }
71
71
  end
72
- end
73
72
 
74
73
  # Ensure the destination directory exists
75
74
  FileUtils.mkdir_p(destination) unless File.directory?(destination)
@@ -88,10 +87,11 @@ module Berkshelf
88
87
  target = File.readlink(source_file)
89
88
 
90
89
  destination = File.expand_path(destination)
91
- Dir.chdir(destination) do
92
- FileUtils.ln_sf(target, "#{destination}/#{relative_path}")
93
- end
90
+ FileUtils.ln_sf(target, "#{destination}/#{relative_path}")
94
91
  when :file
92
+ # TODO: Workaround issue related to [1] which impacts running ChefSpec on Github Actions
93
+ # [1] https://github.com/docker/for-linux/issues/1015
94
+ FileUtils.touch(source_file)
95
95
  FileUtils.cp(source_file, "#{destination}/#{relative_path}")
96
96
  else
97
97
  type = File.ftype(source_file)
@@ -103,9 +103,7 @@ module Berkshelf
103
103
 
104
104
  if options[:delete]
105
105
  # Remove any files in the destination that are not in the source files
106
- destination_files = Dir.chdir(destination) do
107
- glob("**/*")
108
- end
106
+ destination_files = glob("**/*", base: destination)
109
107
 
110
108
  # Remove any extra files that are present in the destination, but are
111
109
  # not in the source list
@@ -68,7 +68,7 @@ module Berkshelf
68
68
 
69
69
  hash.each do |name, info|
70
70
  info["remote"].each do |remote_source, remote_version|
71
- out = " * #{name} (#{info['local']} => #{remote_version})"
71
+ out = " * #{name} (#{info["local"]} => #{remote_version})"
72
72
 
73
73
  unless remote_source.default?
74
74
  out << " [#{remote_source.uri}]"
@@ -12,7 +12,7 @@ module Berkshelf
12
12
  messages: [],
13
13
  warnings: [],
14
14
  }
15
- @cookbooks = Hash.new
15
+ @cookbooks = {}
16
16
 
17
17
  Berkshelf.ui.mute { super }
18
18
  end
@@ -1,4 +1,4 @@
1
- require "berkshelf/api-client"
1
+ require_relative "api-client"
2
2
  require "concurrent/executors"
3
3
  require "concurrent/future"
4
4
 
@@ -28,9 +28,9 @@ module Berkshelf
28
28
 
29
29
  private
30
30
 
31
- # Load the correct location from the given options.
32
- #
33
- # @return [Class, nil]
31
+ # Load the correct location from the given options.
32
+ #
33
+ # @return [Class, nil]
34
34
  def klass_from_options(options)
35
35
  options.each do |key, _|
36
36
  id = key.to_s.capitalize
@@ -37,28 +37,22 @@ module Berkshelf
37
37
  scratch_path = Pathname.new(Dir.mktmpdir)
38
38
 
39
39
  if cached?
40
- Dir.chdir(cache_path) do
41
- git %{fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"}
42
- end
40
+ git(%{fetch --force --tags #{uri} "refs/heads/*:refs/heads/*"}, cwd: cache_path.to_s)
43
41
  else
44
42
  git %{clone #{uri} "#{cache_path}" --bare --no-hardlinks}
45
43
  end
46
44
 
47
- Dir.chdir(cache_path) do
48
- @revision ||= git %{rev-parse #{@rev_parse}}
49
- end
45
+ @revision ||= git(%{rev-parse #{@rev_parse}}, cwd: cache_path.to_s)
50
46
 
51
47
  # Clone into a scratch directory for validations
52
48
  git %{clone --no-checkout "#{cache_path}" "#{scratch_path}"}
53
49
 
54
50
  # Make sure the scratch directory is up-to-date and account for rel paths
55
- Dir.chdir(scratch_path) do
56
- git %{fetch --force --tags "#{cache_path}"}
57
- git %{reset --hard #{@revision}}
51
+ git(%{fetch --force --tags "#{cache_path}"}, cwd: scratch_path.to_s)
52
+ git(%{reset --hard #{@revision}}, cwd: scratch_path.to_s)
58
53
 
59
- if rel
60
- git %{filter-branch --subdirectory-filter "#{rel}" --force}
61
- end
54
+ if rel
55
+ git(%{filter-branch --subdirectory-filter "#{rel}" --force}, cwd: scratch_path.to_s)
62
56
  end
63
57
 
64
58
  # Validate the scratched path is a valid cookbook
@@ -228,7 +228,7 @@ module Berkshelf
228
228
 
229
229
  # @return [Array<CachedCookbook>]
230
230
  def cached
231
- graph.locks.values.collect { |dependency| dependency.cached_cookbook }
231
+ graph.locks.values.collect(&:cached_cookbook)
232
232
  end
233
233
 
234
234
  # The list of dependencies constrained in this lockfile.
@@ -511,8 +511,8 @@ module Berkshelf
511
511
  "#<Berkshelf::Lockfile #{Pathname.new(filepath).basename}, dependencies: #{dependencies.inspect}>"
512
512
  end
513
513
 
514
- # The class responsible for parsing the lockfile and turning it into a
515
- # useful data structure.
514
+ # The class responsible for parsing the lockfile and turning it into a
515
+ # useful data structure.
516
516
  class LockfileParser
517
517
  NAME_VERSION = '(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
518
518
  DEPENDENCY_PATTERN = /^ {2}#{NAME_VERSION}$/.freeze
@@ -570,7 +570,7 @@ module Berkshelf
570
570
  end
571
571
  end
572
572
 
573
- graph << " #{name} (#{info['locked_version']})\n"
573
+ graph << " #{name} (#{info["locked_version"]})\n"
574
574
  end
575
575
 
576
576
  contents = "#{dependencies}\n#{graph}"
@@ -599,9 +599,9 @@ module Berkshelf
599
599
 
600
600
  private
601
601
 
602
- # Parse a dependency line.
603
- #
604
- # @param [String] line
602
+ # Parse a dependency line.
603
+ #
604
+ # @param [String] line
605
605
  def parse_dependency(line)
606
606
  if line =~ DEPENDENCY_PATTERN
607
607
  name, version = $1, $2
@@ -615,9 +615,9 @@ module Berkshelf
615
615
  end
616
616
  end
617
617
 
618
- # Parse a graph line.
619
- #
620
- # @param [String] line
618
+ # Parse a graph line.
619
+ #
620
+ # @param [String] line
621
621
  def parse_graph(line)
622
622
  if line =~ DEPENDENCY_PATTERN
623
623
  name, version = $1, $2
@@ -631,7 +631,7 @@ module Berkshelf
631
631
  end
632
632
  end
633
633
 
634
- # The class representing an internal graph.
634
+ # The class representing an internal graph.
635
635
  class Graph
636
636
  include Enumerable
637
637
 
@@ -3,7 +3,7 @@ module Berkshelf
3
3
  def initialize(device = STDOUT)
4
4
  super
5
5
  self.level = Logger::WARN
6
- @filter_params = Array.new
6
+ @filter_params = []
7
7
  end
8
8
 
9
9
  # Reimplements Logger#add adding message filtering. The info,
@@ -28,6 +28,7 @@ module Berkshelf
28
28
  if @logdev.nil? || severity < (@level)
29
29
  return true
30
30
  end
31
+
31
32
  progname ||= @progname
32
33
  if message.nil?
33
34
  if block_given?
@@ -38,7 +39,8 @@ module Berkshelf
38
39
  end
39
40
  end
40
41
  @logdev.write(
41
- format_message(format_severity(severity), Time.now, progname, filter(message)))
42
+ format_message(format_severity(severity), Time.now, progname, filter(message))
43
+ )
42
44
  true
43
45
  end
44
46
 
@@ -1,4 +1,4 @@
1
- require "berkshelf/shell_out"
1
+ require_relative "../shell_out"
2
2
 
3
3
  module Berkshelf
4
4
  module Mixin
@@ -13,12 +13,12 @@ module Berkshelf
13
13
  #
14
14
  # @raise [String]
15
15
  # the +$stdout+ from the command
16
- def git(command, error = true)
16
+ def git(command, error = true, **kwargs)
17
17
  unless Berkshelf.which("git") || Berkshelf.which("git.exe") || Berkshelf.which("git.bat")
18
18
  raise GitNotInstalled.new
19
19
  end
20
20
 
21
- response = shell_out(%{git #{command}})
21
+ response = shell_out(%{git #{command}}, **kwargs)
22
22
 
23
23
  if response.error?
24
24
  raise GitCommandError.new(command, cache_path, response.stderr)
@@ -1,5 +1,5 @@
1
1
  require "archive/tar/minitar"
2
- require "zlib"
2
+ require "zlib" unless defined?(Zlib)
3
3
 
4
4
  module Berkshelf
5
5
  # A class for archiving and compressing directory containing one or more cookbooks.
@@ -40,10 +40,8 @@ module Berkshelf
40
40
  # @return [String]
41
41
  # path to the generated archive
42
42
  def run(source)
43
- Dir.chdir(source.to_s) do |dir|
44
- tgz = Zlib::GzipWriter.new(File.open(out_file, "wb"))
45
- Archive::Tar::Minitar.pack(Dir.glob("*"), tgz)
46
- end
43
+ tgz = Zlib::GzipWriter.new(File.open(out_file, "wb"))
44
+ Archive::Tar::Minitar.pack(Dir.glob("#{source}/*"), tgz)
47
45
 
48
46
  out_file
49
47
  rescue SystemCallError => ex
@@ -64,10 +62,10 @@ module Berkshelf
64
62
 
65
63
  private
66
64
 
67
- # @return [String]
65
+ # @return [String]
68
66
  attr_reader :out_dir
69
67
 
70
- # @return [String]
68
+ # @return [String]
71
69
  attr_reader :filename
72
70
  end
73
71
  end
@@ -21,7 +21,7 @@ module Berkshelf
21
21
  def initialize(berksfile, demands = [])
22
22
  @berksfile = berksfile
23
23
  @graph = Graph.new
24
- @demands = Array.new
24
+ @demands = []
25
25
 
26
26
  Array(demands).each { |demand| add_demand(demand) }
27
27
  compute_solver_engine(berksfile)