Class: ChefCLI::Policyfile::GitLockFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile/git_lock_fetcher.rb

Overview

A Policyfile lock fetcher that can read a lock file from a git repository.

Author:

  • Ryan Hass

  • Daniel DeLeo

Since:

  • 3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, source_options, storage_config) ⇒ GitLockFetcher

Initialize a GitLockFetcher

Parameters:

  • name (String)

    The name of the policyfile

  • source_options (Hash)

    A hash with a :path key pointing at the location of the lock

Since:

  • 3.0



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 54

def initialize(name, source_options, storage_config)
  @name           = name
  @storage_config = storage_config
  @source_options = symbolize_keys(source_options)
  @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

#branchObject (readonly)

Since:

  • 3.0



45
46
47
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 45

def branch
  @branch
end

#nameObject

Since:

  • 3.0



38
39
40
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 38

def name
  @name
end

#pathObject (readonly)

Since:

  • 3.0



44
45
46
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 44

def path
  @path
end

#refObject (readonly)

Since:

  • 3.0



47
48
49
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 47

def ref
  @ref
end

#revisionObject (readonly)

Since:

  • 3.0



43
44
45
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 43

def revision
  @revision
end

#source_optionsObject

Since:

  • 3.0



39
40
41
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 39

def source_options
  @source_options
end

#storage_configObject

Since:

  • 3.0



40
41
42
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 40

def storage_config
  @storage_config
end

#tagObject (readonly)

Since:

  • 3.0



46
47
48
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 46

def tag
  @tag
end

#uriObject (readonly)

Since:

  • 3.0



42
43
44
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 42

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

Parameters:

  • options_from_lock (Hash)

    The source options loaded from a policyfile lock

Raises:

Since:

  • 3.0



98
99
100
101
102
103
104
105
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 98

def apply_locked_source_options(options_from_lock)
  options = options_from_lock.inject({}) do |acc, (key, value)|
    acc[key.to_sym] = value
    acc
  end
  source_options.merge!(options)
  raise ChefCLI::InvalidLockfile, "Invalid source_options provided from lock data: #{options_from_lock_file.inspect}" unless valid?
end

#errorsArray<String>

Check the options provided when craeting this class for errors

Returns:

  • (Array<String>)

    A list of errors found

Since:

  • 3.0



78
79
80
81
82
83
84
85
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 78

def errors
  error_messages = []
  [:git].each do |key|
    error_messages << "include_policy for #{name} is missing key #{key}" unless source_options[key]
  end

  error_messages
end

#lock_dataHash

Returns of the policyfile lock data.

Returns:

  • (Hash)

    of the policyfile lock data

Since:

  • 3.0



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
142
143
144
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 108

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_lockHash

Returns The source_options that describe how to fetch this exact lock again.

Returns:

  • (Hash)

    The source_options that describe how to fetch this exact lock again

Since:

  • 3.0



88
89
90
91
92
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 88

def source_options_for_lock
  source_options.merge({
                         revision: revision,
                       })
end

#valid?True, False

Returns:

  • (True)

    if there were no errors with the provided source_options

  • (False)

    if there were errors with the provided source_options

Since:

  • 3.0



71
72
73
# File 'lib/chef-cli/policyfile/git_lock_fetcher.rb', line 71

def valid?
  errors.empty?
end