diff options
author | Dan Webb <dan.webb@damacus.io> | 2020-05-21 11:15:59 +0100 |
---|---|---|
committer | Dan Webb <dan.webb@damacus.io> | 2020-05-21 11:15:59 +0100 |
commit | da5cfcfd4e07b9a766ef8fa8d1a26dad379b96ef (patch) | |
tree | bd10af12d417069d1cea70d75de0b5adc6d6aa77 /lib | |
parent | 3536688af38f6ae1e447fa8d03bdaee8c4ec4a3a (diff) | |
download | chef-da5cfcfd4e07b9a766ef8fa8d1a26dad379b96ef.tar.gz |
Adds the homebrew_update resource
Signed-off-by: Dan Webb <dan.webb@damacus.io>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/resource/homebrew_update.rb | 106 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 |
2 files changed, 107 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" |