diff options
author | Tim Smith <tsmith@chef.io> | 2016-11-08 15:20:34 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-05-12 09:29:33 -0700 |
commit | d018ca2d667c91b4552bfc271193ceb3b0737167 (patch) | |
tree | 08633b1dab40a3788c4b410f821fa952e7870e00 | |
parent | 82edefc9c48683df91b158ffc84ad54eebfbe9ce (diff) | |
download | chef-d018ca2d667c91b4552bfc271193ceb3b0737167.tar.gz |
Initial apt_preference commit
Specs are a placeholder on the provider side
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/provider/apt_preference.rb | 106 | ||||
-rw-r--r-- | lib/chef/resource/apt_preference.rb | 36 | ||||
-rw-r--r-- | spec/unit/provider/apt_preference_spec.rb | 53 | ||||
-rw-r--r-- | spec/unit/resource/apt_preference_spec.rb | 45 |
4 files changed, 240 insertions, 0 deletions
diff --git a/lib/chef/provider/apt_preference.rb b/lib/chef/provider/apt_preference.rb new file mode 100644 index 0000000000..92eeeb9aa5 --- /dev/null +++ b/lib/chef/provider/apt_preference.rb @@ -0,0 +1,106 @@ +# +# Author:: Tim Smith (<tsmith@chef.io>) +# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require "chef/resource" +require "chef/dsl/declare_resource" +require "chef/mixin/which" +require "chef/provider/noop" +require "chef/log" + +class Chef + class Provider + class AptPreference < Chef::Provider + use_inline_resources + + extend Chef::Mixin::Which + + provides :apt_preference do + which("apt-get") + end + + def whyrun_supported? + true + end + + def load_current_resource + end + + action :add do + preference = build_pref( + new_resource.glob || new_resource.package_name, + new_resource.pin, + new_resource.pin_priority + ) + + declare_resource(:directory, "/etc/apt/preferences.d") do + owner "root" + group "root" + mode "0755" + recursive true + action :create + end + + name = safe_name(new_resource.name) + + declare_resource(:file, "/etc/apt/preferences.d/#{new_resource.name}.pref") do + action :delete + if ::File.exist?("/etc/apt/preferences.d/#{new_resource.name}.pref") + Chef::Log.warn "Replacing #{new_resource.name}.pref with #{name}.pref in /etc/apt/preferences.d/" + end + only_if { name != new_resource.name } + end + + declare_resource(:file, "/etc/apt/preferences.d/#{new_resource.name}") do + action :delete + if ::File.exist?("/etc/apt/preferences.d/#{new_resource.name}") + Chef::Log.warn "Replacing #{new_resource.name} with #{new_resource.name}.pref in /etc/apt/preferences.d/" + end + end + + declare_resource(:file, "/etc/apt/preferences.d/#{name}.pref") do + owner "root" + group "root" + mode "0644" + content preference + action :create + end + end + + action :delete do + name = safe_name(new_resource.name) + if ::File.exist?("/etc/apt/preferences.d/#{name}.pref") + Chef::Log.info "Un-pinning #{name} from /etc/apt/preferences.d/" + declare_resource(:file, "/etc/apt/preferences.d/#{name}.pref") do + action :delete + end + end + end + end + + # Build preferences.d file contents + def build_pref(package_name, pin, pin_priority) + "Package: #{package_name}\nPin: #{pin}\nPin-Priority: #{pin_priority}\n" + end + + def safe_name(name) + name.tr(".", "_").gsub("*", "wildcard") + end + end +end + +Chef::Provider::Noop.provides :apt_preference diff --git a/lib/chef/resource/apt_preference.rb b/lib/chef/resource/apt_preference.rb new file mode 100644 index 0000000000..913f7382ce --- /dev/null +++ b/lib/chef/resource/apt_preference.rb @@ -0,0 +1,36 @@ +# +# Author:: Tim Smith (<tsmith@chef.io>) +# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require "chef/resource" + +class Chef + class Resource + class AptPreference < Chef::Resource + resource_name :apt_repository + provides :apt_preference + + property :package_name, String, name_property: true, regex: [/^([a-z]|[A-Z]|[0-9]|_|-|\.|\*|\+)+$/] + property :glob, String + property :pin, String + property :pin_priority, String + + default_action :add + allowed_actions :add, :remove + end + end +end diff --git a/spec/unit/provider/apt_preference_spec.rb b/spec/unit/provider/apt_preference_spec.rb new file mode 100644 index 0000000000..dc5297ee0a --- /dev/null +++ b/spec/unit/provider/apt_preference_spec.rb @@ -0,0 +1,53 @@ +# +# Author:: Thom May (<thom@chef.io>) +# Author:: Tim Smith (<tim@chef.io>) +# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require "spec_helper" + +describe Chef::Provider::AptPreference do + let(:new_resource) { Chef::Resource::AptPreference.new("libmysqlclient16") } + let(:provider) do + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + Chef::Provider::AptPreference.new(new_resource, run_context) + end + + it "responds to load_current_resource" do + expect(provider).to respond_to(:load_current_resource) + end + + it "creates apt preferences directory if it does not exist" do + provider.run_action(:update) + expect(new_resource).to be_updated_by_last_action + expect(File.exist?('/etc/apt/preferences.d')).to be true + expect(File.directory?('/etc/apt/preferences.d')).to be true + end + + it "cleans up legacy non-sanitized name files" do + # something + end + + it "creates an apt .pref file" do + # something + end + + it "creates an apt .pref file with a sanitized filename" do + # something + end +end diff --git a/spec/unit/resource/apt_preference_spec.rb b/spec/unit/resource/apt_preference_spec.rb new file mode 100644 index 0000000000..c0da12edc6 --- /dev/null +++ b/spec/unit/resource/apt_preference_spec.rb @@ -0,0 +1,45 @@ +# +# Author:: Tim Smith (<tsmith@chef.io>) +# Copyright:: Copyright (c) 2016 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# 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. +# + +require "spec_helper" + +describe Chef::Resource::AptPreference do + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:resource) { Chef::Resource::AptPreference.new("libmysqlclient16", run_context) } + + it "should create a new Chef::Resource::AptPreference" do + expect(resource).to be_a_kind_of(Chef::Resource) + expect(resource).to be_a_kind_of(Chef::Resource::AptPreference) + end + + it "should resolve to a AptPreference class class when apt-get is found" do + expect(Chef::Provider::AptPreference).to receive(:which).with("apt-get").and_return(true) + expect(resource.provider_for_action(:add)).to be_a(Chef::Provider::AptPreference) + end + + it "should resolve to a Noop class when apt-get is not found" do + expect(Chef::Provider::AptPreference).to receive(:which).with("apt-get").and_return(false) + expect(resource.provider_for_action(:add)).to be_a(Chef::Provider::Noop) + end + + it "should resolve to a NoOp provider" do + expect(resource.provider_for_action(:add)).to be_a(Chef::Provider::Noop) + end +end |