lib/berkshelf/lockfile.rb in berkshelf-6.3.4 vs lib/berkshelf/lockfile.rb in berkshelf-7.0.0
- old
+ new
@@ -1,6 +1,7 @@
require_relative "dependency"
+require "chef/environment"
module Berkshelf
class Lockfile
class << self
# Initialize a Lockfile from the given filepath
@@ -55,11 +56,11 @@
@filepath = options[:filepath].to_s
@berksfile = options[:berksfile]
@dependencies = {}
@graph = Graph.new(self)
- parse if File.exists?(@filepath)
+ parse if File.exist?(@filepath)
end
# Parse the lockfile.
#
# @return true
@@ -73,11 +74,11 @@
# Determine if this lockfile actually exists on disk.
#
# @return [Boolean]
# true if this lockfile exists on the disk, false otherwise
def present?
- File.exists?(filepath) && !File.read(filepath).strip.empty?
+ File.exist?(filepath) && !File.read(filepath).strip.empty?
end
# Determine if we can "trust" this lockfile. A lockfile is trustworthy if:
#
# 1. All dependencies defined in the Berksfile are present in this
@@ -111,11 +112,11 @@
if graphed.nil?
Berkshelf.log.debug " Not in graph - cannot be trusted!"
return false
end
- if cookbook = locked.cached_cookbook
+ if ( cookbook = locked.cached_cookbook )
Berkshelf.log.debug " Detected there is a cached cookbook"
unless (cookbook.dependencies.keys - graphed.dependencies.keys).empty?
Berkshelf.log.debug " Cached cookbook has different dependencies - cannot be trusted!"
return false
@@ -201,24 +202,27 @@
# if the target environment was not found on the remote Chef Server
# @raise [ChefConnectionError]
# if you are locking cookbooks with an invalid or not-specified client
# configuration
def apply(name, options = {})
- locks = graph.locks.inject({}) do |hash, (name, dependency)|
- hash[name] = "= #{dependency.locked_version}"
+ locks = graph.locks.inject({}) do |hash, (dep_name, dependency)|
+ hash[dep_name] = "= #{dependency.locked_version}"
hash
end
if options[:envfile]
update_environment_file(options[:envfile], locks) if options[:envfile]
else
Berkshelf.ridley_connection(options) do |connection|
- environment = connection.environment.find(name)
+ environment =
+ begin
+ Chef::Environment.from_hash(connection.get("environments/#{name}"))
+ rescue Berkshelf::APIClient::ServiceNotFound
+ raise EnvironmentNotFound.new(name)
+ end
- raise EnvironmentNotFound.new(name) if environment.nil?
-
- environment.cookbook_versions = locks
+ environment.cookbook_versions locks
environment.save unless options[:envfile]
end
end
end
@@ -312,11 +316,11 @@
# A hash of cookbooks and versions to update the environment with
#
# @raise [EnvironmentFileNotFound]
# If environment file doesn't exist
def update_environment_file(environment_file, locks)
- unless File.exists?(environment_file)
+ unless File.exist?(environment_file)
raise EnvironmentFileNotFound.new(environment_file)
end
json_environment = JSON.parse(File.read(environment_file))
@@ -429,11 +433,11 @@
# Locking dependency version to the graphed version if
# constraints are satisfied by it.
dependency.locked_version = graphed.version
- if cookbook = dependency.cached_cookbook
+ if ( cookbook = dependency.cached_cookbook )
Berkshelf.log.debug " Cached cookbook exists"
Berkshelf.log.debug " Updating cookbook dependencies if required"
graphed.set_dependencies(cookbook.dependencies)
end
end
@@ -797,11 +801,11 @@
out = "#{Lockfile::GRAPH}\n"
@graph.sort.each do |name, item|
out << " #{name} (#{item.version})\n"
unless item.dependencies.empty?
- item.dependencies.sort.each do |name, constraint|
- out << " #{name} (#{constraint})\n"
+ item.dependencies.sort.each do |dep_name, constraint|
+ out << " #{dep_name} (#{constraint})\n"
end
end
end
out