Skip to content

Instantly share code, notes, and snippets.

@gstark
Last active December 18, 2015 04:19
Show Gist options
  • Save gstark/5724479 to your computer and use it in GitHub Desktop.
Save gstark/5724479 to your computer and use it in GitHub Desktop.
#
# Cookbook Name:: apt
# Provider:: repository
#
# Copyright 2010-2011, Opscode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# use_inline_resources
def whyrun_supported?
true
end
# install apt key from keyserver
def install_key_from_keyserver(key, keyserver)
execute "install-key #{key}" do
if !node['apt']['key_proxy'].empty?
command "apt-key adv --keyserver-options http-proxy=#{node['apt']['key_proxy']} --keyserver #{keyserver} --recv #{key}"
else
command "apt-key adv --keyserver #{keyserver} --recv #{key}"
end
action :run
not_if "apt-key list | grep #{key}"
end
end
# run command and extract gpg ids
def extract_gpg_ids_from_cmd(cmd)
so = Mixlib::ShellOut.new(cmd)
so.run_command
so.stdout.split(/\n/).collect do |t|
if z = t.match(/^pub\s+\d+\w\/([0-9A-F]{8})/)
z[1]
end
end.compact
end
# install apt key from URI
def install_key_from_uri(uri)
key_name = uri.split(/\//).last
cached_keyfile = "#{Chef::Config[:file_cache_path]}/#{key_name}"
if new_resource.key =~ /http/
remote_file cached_keyfile do
source new_resource.key
mode 00644
action :create
end
else
cookbook_file cached_keyfile do
source new_resource.key
cookbook new_resource.cookbook
mode 00644
action :create
end
end
execute "install-key #{key_name}" do
command "apt-key add #{cached_keyfile}"
action :run
not_if do
installed_ids = extract_gpg_ids_from_cmd("apt-key finger")
key_ids = extract_gpg_ids_from_cmd("gpg --with-fingerprint #{cached_keyfile}")
(installed_ids & key_ids).sort == key_ids.sort
end
end
end
# build repo file contents
def build_repo(uri, distribution, components, arch, add_deb_src)
components = components.join(' ') if components.respond_to?(:join)
repo_info = "#{uri} #{distribution} #{components}\n"
repo_info = "[arch=#{arch}] #{repo_info}" if arch
repo = "deb #{repo_info}"
repo << "deb-src #{repo_info}" if add_deb_src
repo
end
action :add do
# add key
if new_resource.keyserver && new_resource.key
install_key_from_keyserver(new_resource.key, new_resource.keyserver)
elsif new_resource.key
install_key_from_uri(new_resource.key)
end
file "/var/lib/apt/periodic/update-success-stamp" do
action :nothing
end
execute "apt-get update" do
ignore_failure true
action :nothing
end
# build repo file
repository = build_repo(new_resource.uri,
new_resource.distribution,
new_resource.components,
new_resource.arch,
new_resource.deb_src)
file "/etc/apt/sources.list.d/#{new_resource.name}.list" do
owner "root"
group "root"
mode 00644
content repository
action :create
notifies :delete, "file[/var/lib/apt/periodic/update-success-stamp]", :immediately
notifies :run, "execute[apt-get update]", :immediately if new_resource.cache_rebuild
end
end
action :remove do
if ::File.exists?("/etc/apt/sources.list.d/#{new_resource.name}.list")
Chef::Log.info "Removing #{new_resource.name} repository from /etc/apt/sources.list.d/"
file "/etc/apt/sources.list.d/#{new_resource.name}.list" do
action :delete
end
end
end
Thu, 06 Jun 2013 15:58:53 -0400] INFO: file[/etc/apt/sources.list.d/mediainfo.list] created file /etc/apt/sources.list.d/mediainfo.list
[Thu, 06 Jun 2013 15:58:53 -0400] INFO: file[/etc/apt/sources.list.d/mediainfo.list] owner changed to 0
[Thu, 06 Jun 2013 15:58:53 -0400] INFO: file[/etc/apt/sources.list.d/mediainfo.list] group changed to 0
[Thu, 06 Jun 2013 15:58:53 -0400] INFO: file[/etc/apt/sources.list.d/mediainfo.list] mode changed to 644
[Thu, 06 Jun 2013 15:58:53 -0400] INFO: file[/etc/apt/sources.list.d/mediainfo.list] sending delete action to file[/var/lib/apt/periodic/update-success-stamp] (immediate)
[Thu, 06 Jun 2013 15:58:53 -0400] ERROR: file[/etc/apt/sources.list.d/mediainfo.list] (/var/chef/cookbooks/apt/providers/repository.rb:114:in `class_from_file') had an error:
undefined method `run_action' for "file[/var/lib/apt/periodic/update-success-stamp]":String
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:45:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:53:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:51:in `each'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:51:in `run_action'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:in `each'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:81:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection.rb:94:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/resource_collection.rb:92:in `execute_each_resource'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/runner.rb:76:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/client.rb:312:in `converge'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/client.rb:160:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application/solo.rb:192:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application/solo.rb:183:in `loop'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application/solo.rb:183:in `run_application'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/../lib/chef/application.rb:67:in `run'
/usr/lib/ruby/gems/1.8/gems/chef-0.10.8/bin/chef-solo:25
/usr/bin/chef-solo:19:in `load'
/usr/bin/chef-solo:19
[Thu, 06 Jun 2013 15:58:53 -0400] ERROR: Running exception handlers
[Thu, 06 Jun 2013 15:58:53 -0400] ERROR: Exception handlers complete
[Thu, 06 Jun 2013 15:58:53 -0400] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[Thu, 06 Jun 2013 15:58:53 -0400] FATAL: NoMethodError: undefined method `run_action' for "file[/var/lib/apt/periodic/update-success-stamp]":String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment