Module: Puppet::DataSync

Includes:
Util::Checksums, Util::Diff
Defined in:
lib/puppet/type/file/data_sync.rb

Constant Summary

Constants included from Util::Execution

Util::Execution::NoOptionsSpecified

Constants included from Util::Checksums

Util::Checksums::KNOWN_CHECKSUMS

Instance Method Summary collapse

Methods included from Util::Diff

diff, #lcs_diff, #string_file_diff

Methods included from Util::Execution

execpipe, execute, ruby_path

Methods included from Util::Checksums

checksum?, checksum_file, checksum_stream, ctime, ctime?, ctime_file, ctime_stream, known_checksum_types, md5, md5?, md5_file, md5_hex_length, md5_stream, md5lite, md5lite?, md5lite_file, md5lite_hex_length, md5lite_stream, mtime, mtime?, mtime_file, mtime_stream, none, none?, none_file, none_stream, sha1, sha1?, sha1_file, sha1_hex_length, sha1_stream, sha1lite, sha1lite?, sha1lite_file, sha1lite_hex_length, sha1lite_stream, sha224, sha224?, sha224_file, sha224_hex_length, sha224_stream, sha256, sha256?, sha256_file, sha256_hex_length, sha256_stream, sha256lite, sha256lite?, sha256lite_file, sha256lite_hex_length, sha256lite_stream, sha384, sha384?, sha384_file, sha384_hex_length, sha384_stream, sha512, sha512?, sha512_file, sha512_hex_length, sha512_stream, sumdata, sumtype, valid_checksum?

Instance Method Details

#checksum_insync?(param, is, has_contents, &block) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/puppet/type/file/data_sync.rb', line 26

def checksum_insync?(param, is, has_contents, &block)
  resource = param.resource
  if resource.should_be_file?
    return false if is == :absent
  else
    if resource[:ensure] == :present && has_contents && (s = resource.stat)
      # TRANSLATORS 'Ensure' is an attribute and ':present' is a value and should not be translated
      resource.warning _("Ensure set to :present but file type is %{file_type} so no content will be synced") % { file_type: s.ftype }
    end
    return true
  end

  return true unless resource.replace?

  is_insync = yield(is)

  if show_diff?(!is_insync)
    if param.sensitive
      send resource[:loglevel], "[diff redacted]"
    else
      write_temporarily(param) do |path|
        diff_output = diff(resource[:path], path)
        if diff_output.encoding == Encoding::BINARY || !diff_output.valid_encoding?
          diff_output = "Binary files #{resource[:path]} and #{path} differ"
        end
        send resource[:loglevel], "\n" + diff_output
      end
    end
  end
  is_insync
end

#contents_sync(param) ⇒ Object



95
96
97
98
99
# File 'lib/puppet/type/file/data_sync.rb', line 95

def contents_sync(param)
  return_event = param.resource.stat ? :file_changed : :file_created
  resource.write(param)
  return_event
end

#date_matches?(checksum_type, current, desired) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/type/file/data_sync.rb', line 62

def date_matches?(checksum_type, current, desired)
  time_types = [:mtime, :ctime]
  return false unless time_types.include?(checksum_type)
  return false unless current && desired

  begin
    if checksum?(current) || checksum?(desired)
      raise if !time_types.include?(sumtype(current).to_sym) || !time_types.include?(sumtype(desired).to_sym)

      current = sumdata(current)
      desired = sumdata(desired)
    end
    DateTime.parse(current) >= DateTime.parse(desired)
  rescue => detail
    self.fail Puppet::Error, "Resource with checksum_type #{checksum_type} didn't contain a date in #{current} or #{desired}", detail.backtrace
  end
end

#retrieve_checksum(resource) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet/type/file/data_sync.rb', line 80

def retrieve_checksum(resource)
  stat = resource.stat
  return :absent unless stat

  ftype = stat.ftype
  # Don't even try to manage the content on directories or links
  return nil if %w[directory link fifo socket].include?(ftype)

  begin
    resource.parameter(:checksum).sum_file(resource[:path])
  rescue => detail
    raise Puppet::Error, "Could not read #{ftype} #{resource.title}: #{detail}", detail.backtrace
  end
end

#show_diff?(has_changes) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/puppet/type/file/data_sync.rb', line 58

def show_diff?(has_changes)
  has_changes && Puppet[:show_diff] && resource.show_diff?
end

#write_temporarily(param) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet/type/file/data_sync.rb', line 13

def write_temporarily(param)
  tempfile = Tempfile.new("puppet-file")
  tempfile.open

  param.write(tempfile)

  tempfile.close

  yield tempfile.path
ensure
  tempfile.delete if tempfile
end