Skip to content

Commit

Permalink
Merge branch 'COOK-889'
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimberman committed Mar 8, 2012
2 parents a320381 + e468fa7 commit 56a8afb
Showing 1 changed file with 71 additions and 41 deletions.
112 changes: 71 additions & 41 deletions apt/providers/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,89 @@
# limitations under the License.
#

# install apt key from keyserver
def install_key_from_keyserver(key, keyserver)
unless system("apt-key list | grep #{key}")
execute "install-key #{key}" do
command "apt-key adv --keyserver #{keyserver} --recv #{key}"
action :nothing
end.run_action(:run)
new_resource.updated_by_last_action(true)
end
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/)
r = remote_file cached_keyfile do
source new_resource.key
mode "0644"
action :nothing
end
else
r = cookbook_file cached_keyfile do
source new_resource.key
mode "0644"
action :nothing
end
end

r.run_action(:create_if_missing)

execute "install-key #{key_name}" do
command "apt-key add #{cached_keyfile}"
action :nothing
end.run_action(:run)
new_resource.updated_by_last_action(true)
end

# build repo file contents
def build_repo(uri, distribution, components, add_deb_src)
components = components.join(' ') if components.respond_to?(:join)
repo_info = "#{uri} #{distribution} #{components}\n"
repo = "deb #{repo_info}"
repo << "deb-src #{repo_info}" if add_deb_src
repo
end

action :add do
unless ::File.exists?("/etc/apt/sources.list.d/#{new_resource.repo_name}-source.list")
Chef::Log.info "Adding #{new_resource.repo_name} repository to /etc/apt/sources.list.d/#{new_resource.repo_name}-source.list"
new_resource.updated_by_last_action(false)

# add key
if new_resource.keyserver && new_resource.key
execute "install-key #{new_resource.key}" do
command "apt-key adv --keyserver #{new_resource.keyserver} --recv #{new_resource.key}"
action :nothing
end.run_action(:run)
install_key_from_keyserver(new_resource.key, new_resource.keyserver)
elsif new_resource.key
key_name = new_resource.key.split(/\//).last
target = "#{Chef::Config[:file_cache_path]}/#{key_name}"

if (new_resource.key =~ /http/)
r = remote_file target do
source new_resource.key
mode "0644"
action :nothing
end
else
r = cookbook_file target do
source new_resource.key
mode "0644"
action :nothing
end
end
install_key_from_uri(new_resource.key)
end

r.run_action(:create_if_missing)
# build repo file
repository = build_repo(new_resource.uri,
new_resource.distribution,
new_resource.components,
new_resource.deb_src)

execute "install-key #{key_name}" do
command "apt-key add #{Chef::Config[:file_cache_path]}/#{key_name}"
action :nothing
end.run_action(:run)
end
# build our listing
repo_info = "#{new_resource.uri} #{new_resource.distribution} #{new_resource.components.join(" ")}"
repository = "deb #{repo_info}\n"
repository += "deb-src #{repo_info}\n" if new_resource.deb_src
# write out the file, replace it if it already exists
file "/etc/apt/sources.list.d/#{new_resource.repo_name}-source.list" do
repo_file = file "/etc/apt/sources.list.d/#{new_resource.repo_name}-source.list" do
owner "root"
group "root"
mode 0644
content repository + "\n"
content repository
action :nothing
end.run_action(:create)
execute "update package index" do
command "apt-get update"
end

# write out the repo file, replace it if it already exists
repo_file.run_action(:create)

apt_get_update = execute "apt-get update" do
ignore_failure true
action :nothing
end.run_action(:run)
new_resource.updated_by_last_action(true)
end
end

if repo_file.updated_by_last_action?
new_resource.updated_by_last_action(true)
apt_get_update.run_action(:run)
end
end

action :remove do
Expand Down

0 comments on commit 56a8afb

Please sign in to comment.