lib/berkshelf/lockfile.rb in berkshelf-4.1.1 vs lib/berkshelf/lockfile.rb in berkshelf-4.2.0
- old
+ new
@@ -192,29 +192,35 @@
# @param [String] name
# the name of the environment to apply the locks to
#
# @option options [Hash] :ssl_verify (true)
# Disable/Enable SSL verification during uploads
+ # @option options [String] :envfile
+ # Environment file to update
#
# @raise [EnvironmentNotFound]
# 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 = {})
- Berkshelf.ridley_connection(options) do |connection|
- environment = connection.environment.find(name)
+ locks = graph.locks.inject({}) do |hash, (name, dependency)|
+ hash[name] = "= #{dependency.locked_version.to_s}"
+ hash
+ end
- raise EnvironmentNotFound.new(name) if environment.nil?
+ if options[:envfile]
+ update_environment_file(options[:envfile], locks) if options[:envfile]
+ else
+ Berkshelf.ridley_connection(options) do |connection|
+ environment = connection.environment.find(name)
- locks = graph.locks.inject({}) do |hash, (name, dependency)|
- hash[name] = "= #{dependency.locked_version.to_s}"
- hash
- end
+ raise EnvironmentNotFound.new(name) if environment.nil?
- environment.cookbook_versions = locks
- environment.save
+ environment.cookbook_versions = locks
+ environment.save unless options[:envfile]
+ end
end
end
# @return [Array<CachedCookbook>]
def cached
@@ -293,9 +299,35 @@
version = locked.locked_version || locked.version_constraint
raise CookbookNotFound.new(name, version, 'in the cookbook store')
end
locked.cached_cookbook
+ end
+
+ # Update local environment file
+ #
+ # @param [String] environment_file
+ # path to the envfile to update
+ #
+ # @param [Hash] locks
+ # 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)
+ raise EnvironmentFileNotFound.new(environment_file)
+ end
+
+ json_environment = JSON.parse(File.read(environment_file))
+
+ json_environment['cookbook_versions'] = locks
+
+ json = JSON.pretty_generate(json_environment)
+
+ File.open(environment_file, 'w'){ |f| f.puts(json) }
+
+ Berkshelf.log.info "Updated environment file #{environment_file}"
end
# Replace the list of dependencies.
#
# @param [Array<Berkshelf::Dependency>] dependencies