summaryrefslogtreecommitdiff
path: root/lib/chef/resource/homebrew_update.rb
blob: cc191cbbdbc826df766fdae577d001358439001c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# 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"
require "chef-utils/dist" unless defined?(ChefUtils::Dist)

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 homebrew repository data at a specified interval**:
        ```ruby
        homebrew_update 'all platforms' do
          frequency 86400
          action :periodic
        end
        ```
        **Update the Homebrew repository at the start of a #{ChefUtils::Dist::Infra::PRODUCT} 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 %w{brew update}
            default_env true
            user Homebrew.owner
            notifies :touch, "file[#{BREW_STAMP}]", :immediately
          end
        end
      end

      action :periodic do
        return unless macos?

        unless brew_up_to_date?
          converge_by "update new lists of packages" do
            do_update
          end
        end
      end

      action :update do
        return unless macos?

        converge_by "force update new lists of packages" do
          do_update
        end
      end
    end
  end
end