Class: ChefDK::Policyfile::GitLockFetcher
- Inherits:
-
Object
- Object
- ChefDK::Policyfile::GitLockFetcher
- Defined in:
- lib/chef-dk/policyfile/git_lock_fetcher.rb
Overview
A Policyfile lock fetcher that can read a lock file from a git repository.
Instance Attribute Summary collapse
- #branch ⇒ Object readonly
- #name ⇒ Object
- #path ⇒ Object readonly
- #ref ⇒ Object readonly
- #revision ⇒ Object readonly
- #source_options ⇒ Object
- #storage_config ⇒ Object
- #tag ⇒ Object readonly
- #uri ⇒ Object readonly
Instance Method Summary collapse
-
#apply_locked_source_options(options_from_lock) ⇒ Object
Applies source options from a lock file.
-
#errors ⇒ Array<String>
Check the options provided when craeting this class for errors.
-
#initialize(name, source_options, storage_config) ⇒ GitLockFetcher
constructor
Initialize a GitLockFetcher.
-
#lock_data ⇒ Hash
Of the policyfile lock data.
-
#source_options_for_lock ⇒ Hash
The source_options that describe how to fetch this exact lock again.
- #valid? ⇒ True, False
Constructor Details
#initialize(name, source_options, storage_config) ⇒ GitLockFetcher
Initialize a GitLockFetcher
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 51 def initialize(name, , storage_config) @name = name @storage_config = storage_config @source_options = symbolize_keys() @revision = @source_options[:revision] @path = @source_options[:path] || @source_options[:rel] @uri = @source_options[:git] @branch = @source_options[:branch] @tag = @source_options[:tag] @ref = @source_options[:ref] # The revision to parse @rev_parse = @source_options[:ref] || @source_options[:branch] || @source_options[:tag] || "master" end |
Instance Attribute Details
#branch ⇒ Object (readonly)
42 43 44 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 42 def branch @branch end |
#name ⇒ Object
35 36 37 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 35 def name @name end |
#path ⇒ Object (readonly)
41 42 43 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 41 def path @path end |
#ref ⇒ Object (readonly)
44 45 46 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 44 def ref @ref end |
#revision ⇒ Object (readonly)
40 41 42 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 40 def revision @revision end |
#source_options ⇒ Object
36 37 38 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 36 def @source_options end |
#storage_config ⇒ Object
37 38 39 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 37 def storage_config @storage_config end |
#tag ⇒ Object (readonly)
43 44 45 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 43 def tag @tag end |
#uri ⇒ Object (readonly)
39 40 41 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 39 def uri @uri end |
Instance Method Details
#apply_locked_source_options(options_from_lock) ⇒ Object
Applies source options from a lock file. This is used to make sure that the same policyfile lock is loaded that was locked
95 96 97 98 99 100 101 102 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 95 def () = .inject({}) do |acc, (key, value)| acc[key.to_sym] = value acc end .merge!() raise ChefDK::InvalidLockfile, "Invalid source_options provided from lock data: #{.inspect}" unless valid? end |
#errors ⇒ Array<String>
Check the options provided when craeting this class for errors
75 76 77 78 79 80 81 82 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 75 def errors = [] [:git].each do |key| << "include_policy for #{name} is missing key #{key}" unless [key] end end |
#lock_data ⇒ Hash
Returns of the policyfile lock data.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 105 def lock_data @lock_data ||= fetch_lock_data.tap do |data| data["cookbook_locks"].each do |cookbook_name, cookbook_lock| if cookbook_lock["source_options"].key?("path") cookbook_lock["source_options"].tap do |opt| opt["git"] = uri unless opt.key?("git") opt["revision"] = revision unless opt.key?("revision") opt["branch"] = branch unless opt.key?("branch") || branch.nil? opt["tag"] = tag unless opt.key?("tag") || branch.nil? opt["ref"] = ref unless opt.key?("ref") || ref.nil? path_keys = %w{path rel}.map { |path_key| path_key if opt.key?(path_key) }.compact path_keys.each do |name| # We can safely grab the entire cookbook when the Policyfile defines a cookbook path of itself (".") if opt[name] == "." opt.delete(name) next end # Mutate the path key to a rel key so that we identify the source_type # as a git repo and not a local directory. Git also doesn't like paths # prefixed with `./` and cannot use relative paths outside the repo. # http://rubular.com/r/JYpdYHT19p pattern = %r{(^../)|(^./)} opt["rel"] = opt[name].gsub(pattern, "") end # Delete the path key if present to ensure we use the git source_type opt.delete("path") end end # cookbook_lock["source_options"] end # data["cookbook_locks"].each end # fetch_lock_data.tap @lock_data end |
#source_options_for_lock ⇒ Hash
Returns The source_options that describe how to fetch this exact lock again.
85 86 87 88 89 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 85 def .merge({ revision: revision, }) end |
#valid? ⇒ True, False
68 69 70 |
# File 'lib/chef-dk/policyfile/git_lock_fetcher.rb', line 68 def valid? errors.empty? end |