3 module Puppet::Parser::Functions
4 newfunction(:fqdn_uuid, :type => :rvalue, :doc => <<-END) do |args|
6 Creates a UUID based on a given string, assumed to be the FQDN
8 For example, to generate a UUID based on the FQDN of a system:
12 $uuid = fqdn_uuid($::fqdn)
14 The generated UUID will be the same for the given hostname
16 The resulting UUID is returned on the form:
18 1d839dea-5e10-5243-88eb-e66815bd7d5c
20 (u.e. without any curly braces.)
22 The generated UUID is a version 5 UUID with the V5 DNS namespace:
24 6ba7b810-9dad-11d1-80b4-00c04fd430c8
26 This only supports a the V5 SHA-1 hash, using the DNS namespace.
28 Please consult http://www.ietf.org/rfc/rfc4122.txt for the details on
29 UUID generation and example implementation.
31 No verification is present at the moment as whether the domain name given
32 is in fact a correct fully-qualified domain name. Therefore any arbitrary
33 string and/or alpha-numeric value can subside for a domain name.
39 raise(ArgumentError, "fqdn_uuid: No arguments given")
40 elsif args.length == 1
43 raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})")
46 # Code lovingly taken from
47 # https://github.com/puppetlabs/marionette-collective/blob/master/lib/mcollective/ssl.rb
49 # This is the UUID version 5 type DNS name space which is as follows:
51 # 6ba7b810-9dad-11d1-80b4-00c04fd430c8
53 uuid_name_space_dns = [0x6b,
69 ].map {|b| b.chr}.join
71 sha1 = Digest::SHA1.new
72 sha1.update(uuid_name_space_dns)
76 bytes = sha1.digest[0, 16].bytes.to_a
78 # version 5 adjustments
86 bytes = [4, 2, 2, 2, 6].collect do |i|
87 bytes.slice!(0, i).pack('C*').unpack('H*')