Module: ManagedMacCommon

Defined in:
lib/puppet/managedmac/common.rb

Constant Summary collapse

RECORD_TYPES =
[:users, :groups, :computers, :computergroups]
DSCL =
'/usr/bin/dscl'
SEARCH_NODE =
'/Search'
FILTERED_PAYLOAD_KEYS =
['PayloadIdentifier',
'PayloadDescription',
'PayloadDisplayName',
'PayloadOrganization',
'PayloadRemovalDisallowed',
'PayloadScope',
'PayloadVersion',]
DO_NOT_DESTRING =
['LastSeenCloudProductVersion']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.content_to_uuid(content) ⇒ Object

Generate a UUID using the supplied content



20
21
22
23
24
# File 'lib/puppet/managedmac/common.rb', line 20

def self.content_to_uuid(content)
  digest  = Digest::MD5.hexdigest content.to_s
  pattern = /\A([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})\z/
  digest.gsub pattern, "\\1-\\2-\\3-\\4-\\5"
end

.destringify(data) ⇒ Object

Recurse the data argument and transform it into real Ruby objects



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
# File 'lib/puppet/managedmac/common.rb', line 27

def self.destringify(data)
  case data
  when /\A-?\d+\z/ # Fixnum
    data.to_i
  when /\A-?\d+\.\d+\z/ # Float
    data.to_f
  when /\Atrue\z/ # TrueClass
    true
  when /\Afalse\z/ # FalseClass
    false
  when NilClass
    data.to_s
  when String, Fixnum, Float, TrueClass, FalseClass, Time, Date, DateTime
    # Leave my elevator alone
    data
  when Array
    data.map { |e| destringify e }
  when Hash
    array = data.map do |k, v|
      DO_NOT_DESTRING.include?(k.to_s) ? [k, v] : [k, destringify(v)]
    end
    Hash[array]
  else
    raise Puppet::Error, "Cast Error: #destringify unknown type:
      #{data.class}, #{data}"
  end
end

.dscl_find_by(record_type, attribute, value) ⇒ Object

Search OpenDirectory

  • find records in OpenDirectory given type, attribute and value

  • returns Array of record names (not the actual records)



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
# File 'lib/puppet/managedmac/common.rb', line 58

def self.dscl_find_by(record_type, attribute, value)

  unless RECORD_TYPES.member? record_type.to_sym
    raise Puppet::Error, "not an OpenDirectory type: #{record_type}"
  end

  if attribute.empty? or value.empty?
    raise Puppet::Error, "Search params empty: \'#{attribute}\', \'#{value}\'"
  end

  # If we are looking for a record name, if might have backslahes in it
  # Active Directory records are returned this way, so split it.
  domain, value = value.split('\\') if value =~ /\w\\\w+/ and attribute.eql? 'name'

  cmd_args = [DSCL, SEARCH_NODE, '-search',
    "/#{record_type.to_s.capitalize}", attribute.to_s, "\'#{value.to_s}\'"]

  # Excute the search
  dscl_result = `#{cmd_args.join(' ')}`

  # If there is a domain component to the record name, rejoin them
  if domain and attribute.eql? 'name'
    value = [domain, value].join('\\') if domain
  end

  dscl_result.scan /#{Regexp.quote(value)}/i
end

Instance Method Details

#nil_or_empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/puppet/managedmac/common.rb', line 86

def nil_or_empty?(value)
  value.nil? || value.empty?
end