Class: Chef::Provider::AptRepository

Inherits:
Chef::Provider show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/provider/apt_repository.rb

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary

Attributes inherited from Chef::Provider

#action, #current_resource, #new_resource, #recipe_name, #run_context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods inherited from Chef::Provider

#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #define_resource_requirements, #events, include_resource_dsl, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited

Methods included from DeprecatedLWRPClass

#const_missing, #deprecated_constants, #register_deprecated_lwrp_class

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

This class inherits a constructor from Chef::Provider

Class Method Details

.uses_apt?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/chef/provider/apt_repository.rb', line 107

def self.uses_apt?
  ENV["PATH"] ||= ""
  paths = %w{ /bin /usr/bin /sbin /usr/sbin } + ENV["PATH"].split(::File::PATH_SEPARATOR)
  paths.any? { |path| ::File.executable?(::File.join(path, "apt-get")) }
end

Instance Method Details

#build_repo(uri, distribution, components, trusted, arch, add_src = false) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/chef/provider/apt_repository.rb', line 237

def build_repo(uri, distribution, components, trusted, arch, add_src = false)
  uri = make_ppa_url(uri) if is_ppa_url?(uri)

  uri = '"' + uri + '"' unless uri.start_with?("'", '"')
  components = Array(components).join(" ")
  options = []
  options << "arch=#{arch}" if arch
  options << "trusted=yes" if trusted
  optstr = unless options.empty?
             "[" + options.join(" ") + "]"
           end
  info = [ optstr, uri, distribution, components ].compact.join(" ")
  repo =  "deb      #{info}\n"
  repo << "deb-src  #{info}\n" if add_src
  repo
end

#cookbook_nameObject



145
146
147
# File 'lib/chef/provider/apt_repository.rb', line 145

def cookbook_name
  new_resource.cookbook || new_resource.cookbook_name
end

#extract_fingerprints_from_cmd(cmd) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/chef/provider/apt_repository.rb', line 118

def extract_fingerprints_from_cmd(cmd)
  so = shell_out(cmd)
  so.run_command
  so.stdout.split(/\n/).map do |t|
    if z = t.match(/^ +Key fingerprint = ([0-9A-F ]+)/)
      z[1].split.join
    end
  end.compact
end

#has_cookbook_file?(fn) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/chef/provider/apt_repository.rb', line 149

def has_cookbook_file?(fn)
  run_context.has_cookbook_file_in_cookbook?(cookbook_name, fn)
end

#install_key_from_keyserver(key = new_resource.key, keyserver = new_resource.keyserver) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/provider/apt_repository.rb', line 189

def install_key_from_keyserver(key = new_resource.key, keyserver = new_resource.keyserver)
  cmd = "apt-key adv --recv"
  cmd << " --keyserver-options http-proxy=#{new_resource.key_proxy}" if new_resource.key_proxy
  cmd << " --keyserver "
  cmd << if keyserver.start_with?("hkp://")
           keyserver
         else
           "hkp://#{keyserver}:80"
         end

  cmd << " #{key}"

  declare_resource(:execute, "install-key #{key}") do
    command cmd
    sensitive new_resource.sensitive
    not_if do
      present = extract_fingerprints_from_cmd("apt-key finger").any? do |fp|
        fp.end_with? key.upcase
      end
      present && key_is_valid?("apt-key list", key.upcase)
    end
    notifies :run, "execute[apt-cache gencaches]", :immediately
  end

  raise "The key #{key} is invalid and cannot be used to verify an apt repository." unless key_is_valid?("apt-key list", key.upcase)
end

#install_key_from_uriObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef/provider/apt_repository.rb', line 159

def install_key_from_uri
  key_name = new_resource.key.split(%r{\/}).last
  cached_keyfile = ::File.join(Chef::Config[:file_cache_path], key_name)
  type = if new_resource.key.start_with?("http")
           :remote_file
         elsif has_cookbook_file?(new_resource.key)
           :cookbook_file
         else
           raise Chef::Exceptions::FileNotFound, "Cannot locate key file"
         end

  declare_resource(type, cached_keyfile) do
    source new_resource.key
    mode "0644"
    sensitive new_resource.sensitive
    action :create
  end

  raise "The key #{cached_keyfile} is invalid and cannot be used to verify an apt repository." unless key_is_valid?("gpg #{cached_keyfile}", "")

  declare_resource(:execute, "apt-key add #{cached_keyfile}") do
    sensitive new_resource.sensitive
    action :run
    not_if do
      no_new_keys?(cached_keyfile)
    end
    notifies :run, "execute[apt-cache gencaches]", :immediately
  end
end

#install_ppa_key(owner, repo) ⇒ Object



216
217
218
219
220
221
222
# File 'lib/chef/provider/apt_repository.rb', line 216

def install_ppa_key(owner, repo)
  url = "https://launchpad.net/api/1.0/~#{owner}/+archive/#{repo}"
  key_id = Chef::HTTP::Simple.new(url).get("signing_key_fingerprint").delete('"')
  install_key_from_keyserver(key_id, "keyserver.ubuntu.com")
rescue Net::HTTPServerException => e
  raise "Could not access Launchpad ppa API: #{e.message}"
end

#is_key_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/chef/provider/apt_repository.rb', line 113

def is_key_id?(id)
  id = id[2..-1] if id.start_with?("0x")
  id =~ /^\h+$/ && [8, 16, 40].include?(id.length)
end

#is_ppa_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/chef/provider/apt_repository.rb', line 224

def is_ppa_url?(url)
  url.start_with?("ppa:")
end

#key_is_valid?(cmd, key) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/provider/apt_repository.rb', line 128

def key_is_valid?(cmd, key)
  valid = true

  so = shell_out(cmd)
  so.run_command
  so.stdout.split(/\n/).map do |t|
    if t =~ %r{^\/#{key}.*\[expired: .*\]$}
      Chef::Log.debug "Found expired key: #{t}"
      valid = false
      break
    end
  end

  Chef::Log.debug "key #{key} #{valid ? "is valid" : "is not valid"}"
  valid
end

#load_current_resourceObject



40
41
# File 'lib/chef/provider/apt_repository.rb', line 40

def load_current_resource
end

#make_ppa_url(ppa) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/chef/provider/apt_repository.rb', line 228

def make_ppa_url(ppa)
  return unless is_ppa_url?(ppa)
  owner, repo = ppa[4..-1].split("/")
  repo ||= "ppa"

  install_ppa_key(owner, repo)
  "http://ppa.launchpad.net/#{owner}/#{repo}/ubuntu"
end

#no_new_keys?(file) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
# File 'lib/chef/provider/apt_repository.rb', line 153

def no_new_keys?(file)
  installed_keys = extract_fingerprints_from_cmd("apt-key finger")
  proposed_keys = extract_fingerprints_from_cmd("gpg --with-fingerprint #{file}")
  (installed_keys & proposed_keys).sort == proposed_keys.sort
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/chef/provider/apt_repository.rb', line 36

def whyrun_supported?
  true
end