Class: ChefCLI::Policyfile::CookbookLocationSpecification

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

Constant Summary collapse

SOURCE_TYPES =
%i{git github path artifactserver chef_server chef_server_artifact artifactory}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Constructor Details

#initialize(name, version_constraint, source_options, storage_config) ⇒ CookbookLocationSpecification

Returns a new instance of CookbookLocationSpecification.



48
49
50
51
52
53
54
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 48

def initialize(name, version_constraint, source_options, storage_config)
  @name = name
  @version_constraint = Semverse::Constraint.new(version_constraint)
  @source_options = source_options
  @source_type = SOURCE_TYPES.find { |type| source_options.key?(type) }
  @storage_config = storage_config
end

Instance Attribute Details

#nameObject (readonly)

– Required by CookbookOmnifetch API contract



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

def name
  @name
end

#source_optionsObject (readonly)

– Required by CookbookOmnifetch API contract



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

def source_options
  @source_options
end

#source_typeObject (readonly)

Returns the value of attribute source_type.



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

def source_type
  @source_type
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



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

def storage_config
  @storage_config
end

#version_constraintObject (readonly)

– Required by CookbookOmnifetch API contract



36
37
38
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 36

def version_constraint
  @version_constraint
end

Instance Method Details

#==(other) ⇒ Object



56
57
58
59
60
61
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 56

def ==(other)
  other.is_a?(self.class) &&
    other.name == name &&
    other.version_constraint == version_constraint &&
    other.source_options == source_options
end

#cache_keyObject



102
103
104
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 102

def cache_key
  installer.cache_key
end

#cached_cookbookObject



132
133
134
135
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 132

def cached_cookbook
  # TODO: handle 'bad' return values here (cookbook not installed yet)
  installer.cached_cookbook
end

#cookbook_has_recipe?(recipe_name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 126

def cookbook_has_recipe?(recipe_name)
  expected_path = cookbook_path.join("recipes/#{recipe_name}.rb")
  expected_path_yml = cookbook_path.join("recipes/#{recipe_name}.yml")
  expected_path.exist? || expected_path_yml.exist?
end

#cookbook_pathObject



145
146
147
148
149
150
151
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 145

def cookbook_path
  if installer.respond_to?(:expanded_path)
    installer.expanded_path
  else
    installer.install_path.expand_path
  end
end

#dependenciesObject



122
123
124
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 122

def dependencies
  cached_cookbook.dependencies
end

#ensure_cachedObject



79
80
81
82
83
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 79

def ensure_cached
  unless installer.installed?
    installer.install
  end
end

#errorsObject



89
90
91
92
93
94
95
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 89

def errors
  error_messages = []
  if source_options_invalid?
    error_messages << "Cookbook `#{name}' has invalid source options `#{source_options.inspect}'"
  end
  error_messages
end

#installed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 75

def installed?
  installer.installed?
end

#installerObject



97
98
99
100
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 97

def installer
  # TODO: handle 'bad' return values here (invalid source_options, etc.)
  @installer ||= CookbookOmnifetch.init(self, source_options)
end

#mirrors_canonical_upstream?Boolean

Returns:

  • (Boolean)


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

def mirrors_canonical_upstream?
  %i{git github artifactserver chef_server chef_server_artifact artifactory}.include?(source_type)
end

#relative_pathObject



106
107
108
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 106

def relative_path
  installer.relative_path.to_s
end

#source_options_for_lockObject



137
138
139
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 137

def source_options_for_lock
  installer.lock_data
end

#source_options_invalid?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 141

def source_options_invalid?
  !source_options.empty? && installer.nil?
end

#to_sObject



63
64
65
66
67
68
69
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 63

def to_s
  # Note, this may appear in exceptions
  s = "Cookbook '#{name}'"
  s << " #{version_constraint}"
  s << " #{source_options}" unless source_options.empty?
  s
end

#uriObject



110
111
112
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 110

def uri
  installer.uri
end

#valid?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 85

def valid?
  errors.empty?
end

#versionObject



118
119
120
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 118

def version
  cached_cookbook.version
end

#version_fixed?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/chef-cli/policyfile/cookbook_location_specification.rb', line 114

def version_fixed?
  %i{git github path chef_server_artifact}.include?(@source_type)
end