diff options
author | Tim Smith <tsmith@chef.io> | 2020-05-27 09:51:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-27 09:51:39 -0700 |
commit | a7439473ac2575b35afb5c88e90576af03bd93e0 (patch) | |
tree | 79a3a643f4827cd4d374edd332857ac461114612 | |
parent | d897f3265650c096669ba6c6da2f7530dd21f134 (diff) | |
parent | da5cfcfd4e07b9a766ef8fa8d1a26dad379b96ef (diff) | |
download | chef-a7439473ac2575b35afb5c88e90576af03bd93e0.tar.gz |
Merge pull request #9896 from damacus/resource/homebrew_update
Adds the homebrew_update resource
-rw-r--r-- | lib/chef/resource/homebrew_update.rb | 106 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 | ||||
-rw-r--r-- | spec/unit/resource/homebrew_update_spec.rb | 31 |
3 files changed, 138 insertions, 0 deletions
diff --git a/lib/chef/resource/homebrew_update.rb b/lib/chef/resource/homebrew_update.rb new file mode 100644 index 0000000000..824d327b67 --- /dev/null +++ b/lib/chef/resource/homebrew_update.rb @@ -0,0 +1,106 @@ +# +# Author:: Joshua Timberman (<jtimberman@chef.io>) +# Author:: Dan Webb (<dan@webb-agile-solutions.ltd>) +# +# Copyright:: Copyright (c) Chef Software Inc. +# Copyright:: Copyright (c) Webb Agile Solutions Ltd. +# +# 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_relative "../resource" + +class Chef + class Resource + class HomebrewUpdate < Chef::Resource + unified_mode true + + provides(:homebrew_update) { true } + + description "Use the **homebrew_update** resource to manage Homebrew repository updates on MacOS." + introduced "16.2" + examples <<~DOC + **Update the Apt repository at a specified interval**: + ```ruby + homebrew_update 'all platforms' do + frequency 86400 + action :periodic + end + ``` + **Update the Homebrew repository at the start of a Chef Infra Client run**: + ```ruby + homebrew_update 'update' + ``` + DOC + + # allow bare homebrew_update with no name + property :name, String, default: "" + + property :frequency, Integer, + description: "Determines how frequently (in seconds) Homebrew updates are made. Use this property when the `:periodic` action is specified.", + default: 86_400 + + default_action :periodic + allowed_actions :update, :periodic + + action_class do + BREW_STAMP_DIR = "/var/lib/homebrew/periodic".freeze + BREW_STAMP = "#{BREW_STAMP_DIR}/update-success-stamp".freeze + + # Determines whether we need to run `homebrew update` + # + # @return [Boolean] + def brew_up_to_date? + ::File.exist?("#{BREW_STAMP}") && + ::File.mtime("#{BREW_STAMP}") > Time.now - new_resource.frequency + end + + def do_update + directory BREW_STAMP_DIR do + recursive true + end + + file "#{BREW_STAMP}" do + content "BREW::Update::Post-Invoke-Success\n" + action :create_if_missing + end + + execute "brew update" do + command [ "brew", "update" ] + default_env true + user Homebrew.owner + notifies :touch, "file[#{BREW_STAMP}]", :immediately + end + end + end + + action :periodic do + return unless mac_os_x? + + unless brew_up_to_date? + converge_by "update new lists of packages" do + do_update + end + end + end + + action :update do + return unless mac_os_x? + + converge_by "force update new lists of packages" do + do_update + end + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 6a87960972..c786b2d78e 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -60,6 +60,7 @@ require_relative "resource/hostname" require_relative "resource/homebrew_cask" require_relative "resource/homebrew_package" require_relative "resource/homebrew_tap" +require_relative "resource/homebrew_update" require_relative "resource/ifconfig" require_relative "resource/kernel_module" require_relative "resource/ksh" diff --git a/spec/unit/resource/homebrew_update_spec.rb b/spec/unit/resource/homebrew_update_spec.rb new file mode 100644 index 0000000000..f9c2e47f5f --- /dev/null +++ b/spec/unit/resource/homebrew_update_spec.rb @@ -0,0 +1,31 @@ +require "spec_helper" + +describe Chef::Resource::HomebrewUpdate do + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:resource) { Chef::Resource::HomebrewUpdate.new("update", run_context) } + + let(:stamp_dir) { Dir.mktmpdir("brew_update_periodic") } + let(:stamp_file) { Dir.mktmpdir("apt_update_periodic") } + let(:brew_update_cmd) { %w{homebrew update} } + + + it "sets the default action as :periodic" do + expect(resource.action).to eql([:periodic]) + end + + it "supports :periodic, :update actions" do + expect { resource.action :periodic }.not_to raise_error + expect { resource.action :update }.not_to raise_error + end + + it "default frequency is set to be 1 da1y" do + expect(resource.frequency).to eql(86_400) + end + + it "frequency accepts integers" do + resource.frequency(400) + expect(resource.frequency).to eql(400) + end +end |