]> git.openstreetmap.org Git - chef.git/blob - cookbooks/apt/resources/source.rb
Enable mod_headers everywhere as we use it for HSTS support
[chef.git] / cookbooks / apt / resources / source.rb
1 #
2 # Cookbook Name:: apt
3 # Resource:: apt_source
4 #
5 # Copyright 2015, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 default_action :create
21
22 property :source_name, String, :name_property => true
23 property :source_template, String, :default => "default.list.erb"
24 property :url, String, :required => true
25 property :key, String
26 property :key_url, String
27 property :update, [TrueClass, FalseClass], :default => false
28
29 def initialize(name, run_context = nil)
30   super(name, run_context)
31
32   @action = node[:apt][:sources].include?(name) ? :create : :delete
33 end
34
35 action :create do
36   if new_resource.key
37     execute "apt-key-#{new_resource.key}-clean" do
38       command "/usr/bin/apt-key adv --batch --delete-key --yes #key}"
39       only_if "/usr/bin/apt-key adv --list-keys #{new_resource.key} | fgrep expired"
40     end
41
42     if new_resource.key_url
43       execute "apt-key-#{new_resource.key}-install" do
44         command "/usr/bin/apt-key adv --fetch-keys #{new_resource.key_url}"
45         not_if "/usr/bin/apt-key adv --list-keys #{new_resource.key}"
46         notifies :run, "execute[apt-update-#{new_resource.source_name}]"
47       end
48     else
49       execute "apt-key-#{new_resource.key}-install" do
50         command "/usr/bin/apt-key adv --keyserver hkp://keys.gnupg.net --recv-keys #{new_resource.key}"
51         not_if "/usr/bin/apt-key adv --list-keys #{new_resource.key}"
52         notifies :run, "execute[apt-update-#{new_resource.source_name}]"
53       end
54     end
55   end
56
57   template source_path do
58     source new_resource.source_template
59     owner "root"
60     group "root"
61     mode 0o644
62     variables :url => new_resource.url
63     notifies :run, "execute[apt-update-#{new_resource.source_name}]"
64   end
65
66   execute "apt-update-#{new_resource.source_name}" do
67     action new_resource.update ? :run : :nothing
68     command "/usr/bin/apt-get update --no-list-cleanup -o Dir::Etc::sourcelist='#{source_path}' -o Dir::Etc::sourceparts='-'"
69   end
70 end
71
72 action :delete do
73   file source_path do
74     action :delete
75   end
76 end
77
78 action_class do
79   def source_path
80     "/etc/apt/sources.list.d/#{new_resource.source_name}.list"
81   end
82 end