Class: Berkshelf::Berksfile
- Inherits:
-
Object
- Object
- Berkshelf::Berksfile
- Extended by:
- Forwardable
- Includes:
- Mixin::DSLEval, Mixin::Logging
- Defined in:
- lib/berkshelf/berksfile.rb
Constant Summary collapse
- DEFAULT_API_URL =
"https://api.berkshelf.com".freeze
Instance Attribute Summary collapse
-
#filepath ⇒ String
readonly
The path on disk to the file representing this instance of Berksfile.
Attributes included from Mixin::Logging
Class Method Summary collapse
- .from_file(file, options = {}) ⇒ Berksfile
-
.from_options(options = {}) ⇒ Object
Instantiate a Berksfile from the given options.
Instance Method Summary collapse
- #[](name) ⇒ Dependency (also: #get_dependency)
-
#add_dependency(name, constraint = nil, options = {}) ⇒ Array<Dependency]
Add a dependency of the given name and constraint to the array of dependencies.
- #chef_api(*args) ⇒ Object
-
#cookbook(*args) ⇒ Object
Add a cookbook dependency to the Berksfile to be retrieved and have its dependencies recursively retrieved and resolved.
-
#cookbooks ⇒ Array<CachedCookbook>
Behaves the same as #dependencies, but this method returns an array of CachedCookbook objects instead of dependency objects.
- #dependencies ⇒ Array<Dependency>
-
#extension(name) ⇒ true
Activate a Berkshelf extension at runtime.
-
#find(name) ⇒ Dependency?
Find a dependency defined in this berksfile by name.
- #group(*args) ⇒ Object
-
#groups ⇒ Hash
A hash containing group names as keys and an array of Dependencies that are a member of that group as values.
-
#has_dependency?(dependency) ⇒ Boolean
Check if the Berksfile has the given dependency, taking into account
group
and –only/–except flags. -
#initialize(path, options = {}) ⇒ Berksfile
constructor
Create a new Berksfile object.
-
#install ⇒ Array<CachedCookbook>
Install the dependencies listed in the Berksfile, respecting the locked versions in the Berksfile.lock.
-
#list ⇒ Hash<Dependency, CachedCookbook>
The cached cookbooks installed by this Berksfile.
-
#lockfile ⇒ Lockfile
Get the lockfile corresponding to this Berksfile.
-
#metadata(options = {}) ⇒ Object
Use a Cookbook metadata file to determine additional cookbook dependencies to retrieve.
-
#outdated(*names) ⇒ Hash
List of all the cookbooks which have a newer version found at a source that satisfies the constraints of your dependencies.
-
#package(path) ⇒ String
Package the given cookbook for distribution outside of berkshelf.
-
#retrieve_locked(dependency) ⇒ CachedCookbook
Retrieve information about a given cookbook that is installed by this Berksfile.
- #site(*args) ⇒ Object
-
#source(api_url) ⇒ Array<Source>
Add a Berkshelf API source to use when building the index of known cookbooks.
- #source_for(name, version) ⇒ Object
- #sources ⇒ Array<Source>
-
#update(*names) ⇒ Object
Update the given set of dependencies (or all if no names are given).
-
#upload(*args) ⇒ Array<CachedCookbook>
Upload the cookbooks installed by this Berksfile.
-
#vendor(destination) ⇒ String?
Install the Berksfile or Berksfile.lock and then copy the cached cookbooks into directories within the given destination matching their name.
-
#viz(outfile = nil) ⇒ String
Visualize the current Berksfile as a “graph” using DOT.
Methods included from Mixin::DSLEval
#dsl_eval, #dsl_eval_file, included
Constructor Details
#initialize(path, options = {}) ⇒ Berksfile
Create a new Berksfile object.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/berkshelf/berksfile.rb', line 59 def initialize(path, = {}) @filepath = File.(path) @dependencies = Hash.new @sources = Hash.new if [:except] && [:only] raise ArgumentError, 'Cannot specify both :except and :only!' elsif [:except] except = Array([:except]).collect(&:to_sym) @filter = ->(dependency) { (except & dependency.groups).empty? } elsif [:only] only = Array([:only]).collect(&:to_sym) @filter = ->(dependency) { !(only & dependency.groups).empty? } else @filter = ->(dependency) { true } end end |
Instance Attribute Details
#filepath ⇒ String (readonly)
Returns The path on disk to the file representing this instance of Berksfile.
46 47 48 |
# File 'lib/berkshelf/berksfile.rb', line 46 def filepath @filepath end |
Class Method Details
.from_file(file, options = {}) ⇒ Berksfile
19 20 21 22 23 24 25 26 27 |
# File 'lib/berkshelf/berksfile.rb', line 19 def from_file(file, = {}) raise BerksfileNotFound.new(file) unless File.exist?(file) begin new(file, ).dsl_eval_file(file) rescue => ex raise BerksfileReadError.new(ex) end end |
.from_options(options = {}) ⇒ Object
Instantiate a Berksfile from the given options. This method is used heavily by the CLI to reduce duplication.
10 11 12 13 |
# File 'lib/berkshelf/berksfile.rb', line 10 def ( = {}) [:berksfile] ||= File.join(Dir.pwd, Berkshelf::DEFAULT_FILENAME) from_file([:berksfile], .slice(:except, :only)) end |
Instance Method Details
#[](name) ⇒ Dependency Also known as: get_dependency
339 340 341 |
# File 'lib/berkshelf/berksfile.rb', line 339 def [](name) @dependencies[name] end |
#add_dependency(name, constraint = nil, options = {}) ⇒ Array<Dependency]
Add a dependency of the given name and constraint to the array of dependencies.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/berkshelf/berksfile.rb', line 248 def add_dependency(name, constraint = nil, = {}) if @dependencies[name] # Only raise an exception if the dependency is a true duplicate groups = ([:group].nil? || [:group].empty?) ? [:default] : [:group] if !(@dependencies[name].groups & groups).empty? raise DuplicateDependencyDefined.new(name) end end if [:path] = File.join([:path], 'metadata.rb') end [:constraint] = constraint @dependencies[name] = Dependency.new(self, name, ) end |
#chef_api(*args) ⇒ Object
remove in Berkshelf 4.0
224 225 226 227 228 |
# File 'lib/berkshelf/berksfile.rb', line 224 def chef_api(*args) raise DeprecatedError.new "Your Berksfile contains a chef_api location. Chef API locations have " + " been replaced by the source location. Please remove your site location and try again. For more " + " information visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations" end |
#cookbook(name, version_constraint, options = {}) ⇒ Object #cookbook(name, options = {}) ⇒ Object
Add a cookbook dependency to the Berksfile to be retrieved and have its dependencies recursively retrieved and resolved.
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/berkshelf/berksfile.rb', line 134 def cookbook(*args) = args.last.is_a?(Hash) ? args.pop : Hash.new name, constraint = args [:path] &&= File.([:path], File.dirname(filepath)) [:group] = Array([:group]) if @active_group [:group] += @active_group end add_dependency(name, constraint, ) end |
#cookbooks ⇒ Array<CachedCookbook>
Behaves the same as #dependencies, but this method returns an array of CachedCookbook objects instead of dependency objects. This method relies on the #retrieve_locked method to load the proper cached cookbook from the Berksfile + lockfile combination.
296 297 298 |
# File 'lib/berkshelf/berksfile.rb', line 296 def cookbooks dependencies.map { |dependency| retrieve_locked(dependency) } end |
#dependencies ⇒ Array<Dependency>
279 280 281 |
# File 'lib/berkshelf/berksfile.rb', line 279 def dependencies @dependencies.values.sort.select(&@filter) end |
#extension(name) ⇒ true
Activate a Berkshelf extension at runtime.
89 90 91 92 93 94 95 |
# File 'lib/berkshelf/berksfile.rb', line 89 def extension(name) require "berkshelf/#{name}" true rescue LoadError raise LoadError, "Could not load an extension by the name `#{name}'. " \ "Please make sure it is installed." end |
#find(name) ⇒ Dependency?
Find a dependency defined in this berksfile by name.
306 307 308 |
# File 'lib/berkshelf/berksfile.rb', line 306 def find(name) @dependencies[name] end |
#group(*args) ⇒ Object
148 149 150 151 152 |
# File 'lib/berkshelf/berksfile.rb', line 148 def group(*args) @active_group = args yield @active_group = nil end |
#groups ⇒ Hash
Returns a hash containing group names as keys and an array of Dependencies that are a member of that group as values
Example:
{
nautilus: [
#<Dependency: nginx (~> 1.0.0)>,
#<Dependency: mysql (~> 1.2.4)>
],
skarner: [
#<Dependency: nginx (~> 1.0.0)>
]
}.
324 325 326 327 328 329 330 331 332 333 |
# File 'lib/berkshelf/berksfile.rb', line 324 def groups {}.tap do |groups| dependencies.each do |dependency| dependency.groups.each do |group| groups[group] ||= [] groups[group] << dependency end end end end |
#has_dependency?(dependency) ⇒ Boolean
Check if the Berksfile has the given dependency, taking into account group
and –only/–except flags.
273 274 275 276 |
# File 'lib/berkshelf/berksfile.rb', line 273 def has_dependency?(dependency) name = Dependency.name(dependency) dependencies.map(&:name).include?(name) end |
#install ⇒ Array<CachedCookbook>
Install the dependencies listed in the Berksfile, respecting the locked versions in the Berksfile.lock.
-
Check that a lockfile exists. If a lockfile does not exist, all dependencies are considered to be “unlocked”. If a lockfile is specified, a definition is created via the following algorithm:
-
For each source, see if there exists a locked version that still satisfies the version constraint in the Berksfile. If there exists such a source, remove it from the list of unlocked sources. If not, then either a version constraint has changed, or a new source has been added to the Berksfile. In the event that a locked_source exists, but it no longer satisfies the constraint, this method will raise a OutdatedCookbookSource, and inform the user to run
berks update COOKBOOK
to remedy the issue. -
Remove any locked sources that no longer exist in the Berksfile (i.e. a cookbook source was removed from the Berksfile).
-
-
Resolve the collection of locked and unlocked dependencies.
-
Write out a new lockfile.
370 371 372 |
# File 'lib/berkshelf/berksfile.rb', line 370 def install Installer.new(self).run end |
#list ⇒ Hash<Dependency, CachedCookbook>
The cached cookbooks installed by this Berksfile.
426 427 428 429 430 431 432 |
# File 'lib/berkshelf/berksfile.rb', line 426 def list validate_lockfile_present! validate_lockfile_trusted! validate_dependencies_installed! lockfile.graph.locks.values end |
#lockfile ⇒ Lockfile
Get the lockfile corresponding to this Berksfile. This is necessary because the user can specify a different path to the Berksfile. So assuming the lockfile is named “Berksfile.lock” is a poor assumption.
652 653 654 |
# File 'lib/berkshelf/berksfile.rb', line 652 def lockfile @lockfile ||= Lockfile.from_berksfile(self) end |
#metadata(options = {}) ⇒ Object
Use a Cookbook metadata file to determine additional cookbook dependencies to retrieve. All dependencies found in the metadata will use the default locations set in the Berksfile (if any are set) or the default locations defined by Berkshelf.
162 163 164 165 166 167 168 |
# File 'lib/berkshelf/berksfile.rb', line 162 def ( = {}) path = [:path] || File.dirname(filepath) = File.(File.join(path, 'metadata.rb')) = Ridley::Chef::Cookbook::Metadata.from_file() add_dependency(.name, nil, path: path, metadata: true) end |
#outdated(*names) ⇒ Hash
List of all the cookbooks which have a newer version found at a source that satisfies the constraints of your dependencies.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/berkshelf/berksfile.rb', line 445 def outdated(*names) validate_lockfile_present! validate_lockfile_trusted! validate_dependencies_installed! validate_cookbook_names!(names) # Calculate the list of cookbooks to unlock if names.empty? list = dependencies else list = dependencies.select { |dependency| names.include?(dependency.name) } end lockfile.graph.locks.inject({}) do |hash, (name, dependency)| sources.each do |source| cookbooks = source.versions(name) latest = cookbooks.select do |cookbook| dependency.version_constraint.satisfies?(cookbook.version) && cookbook.version.to_s != dependency.locked_version.to_s end.sort_by { |cookbook| cookbook.version }.last unless latest.nil? hash[name] ||= {} hash[name][source.uri.to_s] = latest end end hash end end |
#package(path) ⇒ String
Package the given cookbook for distribution outside of berkshelf. If the name attribute is not given, all cookbooks in the Berksfile will be packaged.
552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/berkshelf/berksfile.rb', line 552 def package(path) packager = Packager.new(path) packager.validate! outdir = Dir.mktmpdir do |temp_dir| Berkshelf.ui.mute { vendor(File.join(temp_dir, 'cookbooks')) } packager.run(temp_dir) end Berkshelf.formatter.package(outdir) outdir end |
#retrieve_locked(dependency) ⇒ CachedCookbook
Retrieve information about a given cookbook that is installed by this Berksfile. Unlike #find, which returns a dependency, this method returns the corresponding CachedCookbook for the given name.
413 414 415 |
# File 'lib/berkshelf/berksfile.rb', line 413 def retrieve_locked(dependency) lockfile.retrieve(dependency) end |
#site(*args) ⇒ Object
remove in Berkshelf 4.0
206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/berkshelf/berksfile.rb', line 206 def site(*args) if args.first == :opscode Berkshelf.formatter.deprecation "Your Berksfile contains a site location pointing to the Opscode Community " + "Site (site :opscode). Site locations have been replaced by the source location. Change this to: " + "'source \"http://api.berkshelf.com\"' to remove this warning. For more information visit " + "https://github.com/berkshelf/berkshelf/wiki/deprecated-locations" source(DEFAULT_API_URL) return end raise DeprecatedError.new "Your Berksfile contains a site location. Site locations have been " + " replaced by the source location. Please remove your site location and try again. For more information " + " visit https://github.com/berkshelf/berkshelf/wiki/deprecated-locations" end |
#source(api_url) ⇒ Array<Source>
Add a Berkshelf API source to use when building the index of known cookbooks. The indexes will be searched in the order they are added. If a cookbook is found in the first source then a cookbook in a second source would not be used.
184 185 186 |
# File 'lib/berkshelf/berksfile.rb', line 184 def source(api_url) @sources[api_url] = Source.new(api_url) end |
#source_for(name, version) ⇒ Object
199 200 201 |
# File 'lib/berkshelf/berksfile.rb', line 199 def source_for(name, version) sources.find { |source| source.cookbook(name, version) } end |
#sources ⇒ Array<Source>
189 190 191 192 193 194 195 |
# File 'lib/berkshelf/berksfile.rb', line 189 def sources if @sources.empty? raise NoAPISourcesDefined else @sources.values end end |
#update(*names) ⇒ Object
Update the given set of dependencies (or all if no names are given).
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/berkshelf/berksfile.rb', line 378 def update(*names) validate_lockfile_present! validate_cookbook_names!(names) Berkshelf.log.info "Updating cookbooks" # Calculate the list of cookbooks to unlock if names.empty? Berkshelf.log.debug " Unlocking all the things!" lockfile.unlock_all else names.each do |name| Berkshelf.log.debug " Unlocking #{name}" lockfile.unlock(name, true) end end # NOTE: We intentionally do NOT pass options to the installer self.install end |
#upload(names = []) ⇒ Array<CachedCookbook> #upload(names = [], options = {}) ⇒ Array<CachedCookbook>
Upload the cookbooks installed by this Berksfile
533 534 535 536 537 538 539 |
# File 'lib/berkshelf/berksfile.rb', line 533 def upload(*args) validate_lockfile_present! validate_lockfile_trusted! validate_dependencies_installed! Uploader.new(self, *args).run end |
#vendor(destination) ⇒ String?
Install the Berksfile or Berksfile.lock and then copy the cached cookbooks into directories within the given destination matching their name.
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/berkshelf/berksfile.rb', line 573 def vendor(destination) destination = File.(destination) if Dir.exist?(destination) raise VendorError, "destination already exists #{destination}. Delete it and try again or use a " + "different filepath." end # Ensure the parent directory exists, in case a nested path was given FileUtils.mkdir_p(File.(File.join(destination, '..'))) scratch = Berkshelf.mktmpdir chefignore = nil cached_cookbooks = install return nil if cached_cookbooks.empty? cached_cookbooks.each do |cookbook| Berkshelf.formatter.vendor(cookbook, destination) cookbook_destination = File.join(scratch, cookbook.cookbook_name, '/') FileUtils.mkdir_p(cookbook_destination) # Dir.glob does not support backslash as a File separator src = cookbook.path.to_s.gsub('\\', '/') files = Dir.glob(File.join(src, '*')) chefignore = Ridley::Chef::Chefignore.new(cookbook.path.to_s) rescue nil chefignore.apply!(files) if chefignore unless cookbook. cookbook.(cookbook_destination) end # Don't vendor the raw metadata (metadata.rb). The raw metadata is unecessary for the # client, and this is required until compiled metadata (metadata.json) takes precedence over # raw metadata in the Chef-Client. # # We can change back to including the raw metadata in the future after this has been fixed or # just remove these comments. There is no circumstance that I can currently think of where # raw metadata should ever be read by the client. # # - Jamie # # See the following tickets for more information: # * https://tickets.opscode.com/browse/CHEF-4811 # * https://tickets.opscode.com/browse/CHEF-4810 files.reject! { |file| File.basename(file) == "metadata.rb" } FileUtils.cp_r(files, cookbook_destination) end FileUtils.cp(lockfile.filepath, File.join(scratch, Lockfile::DEFAULT_FILENAME)) FileUtils.mv(scratch, destination) destination end |
#viz(outfile = nil) ⇒ String
Visualize the current Berksfile as a “graph” using DOT.
637 638 639 640 641 642 643 |
# File 'lib/berkshelf/berksfile.rb', line 637 def viz(outfile = nil) outfile = File.join(Dir.pwd, outfile || 'graph.png') validate_lockfile_present! validate_lockfile_trusted! Visualizer.from_lockfile(lockfile).to_png(outfile) end |