summaryrefslogtreecommitdiff
path: root/lib/chef/resource/file/verification/systemd_unit.rb
blob: bfc8881522d01327f2437ca8b7768f6b02117af5 (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
#
# Author:: Mal Graty (<mal.graty@googlemail.com>)
# Copyright:: Copyright (c) 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_relative "../../../mixin/which"

class Chef
  class Resource
    class File
      class Verification

        #
        # Systemd provides a binary for verifying the correctness of
        # unit files.  Unfortunately some units have constraints on the
        # filename meaning that normal verification against temp files
        # won't work.
        #
        # Working around that requires placing a copy of the temp file
        # in a temp directory, under its real name and running the
        # verification tool against that file.
        #

        class SystemdUnit < Chef::Resource::File::Verification
          include Chef::Mixin::Which

          provides :systemd_unit

          def initialize(parent_resource, command, opts, &block)
            super
            @command = systemd_analyze_cmd
          end

          def verify(path, opts = {})
            return true unless systemd_analyze_path

            Dir.mktmpdir("chef-systemd-unit") do |dir|
              temp = "#{dir}/#{::File.basename(@parent_resource.path)}"
              ::FileUtils.cp(path, temp)
              verify_command(temp, opts)
            end
          end

          def systemd_analyze_cmd
            @systemd_analyze_cmd ||= "#{systemd_analyze_path} verify %{path}"
          end

          def systemd_analyze_path
            @systemd_analyze_path ||= which("systemd-analyze")
          end
        end
      end
    end
  end
end