Module: Puppet::MetaType::Manager

Includes:
Util::ClassGen
Included in:
Type
Defined in:
lib/puppet/metatype/manager.rb

Constant Summary

Constants included from Util

Util::ALNUM, Util::ALPHA, Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::ESCAPED, Util::HEX, Util::HttpProxy, Util::PUPPET_STACK_INSERTION_FRAME, Util::RESERVED, Util::RFC_3986_URI_REGEX, Util::UNRESERVED, Util::UNSAFE

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Instance Method Summary collapse

Methods included from Util::ClassGen

#genclass, #genmodule, #rmclass

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Instance Method Details

#allclearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Only use this method for testing purposes.

An implementation specific method that removes all type instances during testing.



19
20
21
22
23
# File 'lib/puppet/metatype/manager.rb', line 19

def allclear
  @types.each { |_name, type|
    type.clear
  }
end

#clear_missesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Used after each catalog compile when always_retry_plugins is false

Clears any types that were used but absent when types were last loaded.



29
30
31
32
33
# File 'lib/puppet/metatype/manager.rb', line 29

def clear_misses
  unless @types.nil?
    @types.compact
  end
end

#eachtype {|t| ... } ⇒ Object

Iterates over all already loaded Type subclasses.

Yields:

  • (t)

    a block receiving each type

Yield Parameters:

Yield Returns:

  • (Object)

    the last returned object is also returned from this method

Returns:

  • (Object)

    the last returned value from the block.



40
41
42
43
44
45
46
47
# File 'lib/puppet/metatype/manager.rb', line 40

def eachtype
  @types.each do |_name, type|
    # Only consider types that have names
    # if ! type.parameters.empty? or ! type.validproperties.empty?
    yield type
    # end
  end
end

#loadallvoid

Note:

Should only be used for purposes such as generating documentation as this is potentially a very expensive operation.

This method returns an undefined value.

Loads all types.



54
55
56
# File 'lib/puppet/metatype/manager.rb', line 54

def loadall
  typeloader.loadall(Puppet.lookup(:current_environment))
end

#newtype(name, options = {}) {|| ... } ⇒ Class<inherits Puppet::Type>

Defines a new type or redefines an existing type with the given name. A convenience method on the form ‘new<name>` where name is the name of the type is also created. (If this generated method happens to clash with an existing method, a warning is issued and the original method is kept).

Parameters:

  • name (String)

    the name of the type to create or redefine.

  • options (Hash) (defaults to: {})

    options passed on to Util::ClassGen#genclass as the option ‘:attributes`.

Options Hash (options):

  • Puppet::Type. (Puppet::Type)

    This option is not passed on as an attribute to genclass.

Yields:

  • ()

    a block evaluated in the context of the created class, thus allowing further detailing of that class.

Returns:

See Also:

  • Util::ClassGen.genclass


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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet/metatype/manager.rb', line 74

def newtype(name, options = {}, &block)
  @manager_lock.synchronize do
    # Handle backward compatibility
    unless options.is_a?(Hash)
      # TRANSLATORS 'Puppet::Type.newtype' should not be translated
      Puppet.warning(_("Puppet::Type.newtype(%{name}) now expects a hash as the second argument, not %{argument}") %
                     { name: name, argument: options.inspect })
    end

    # First make sure we don't have a method sitting around
    name = name.intern
    newmethod = "new#{name}"

    # Used for method manipulation.
    selfobj = singleton_class

    if @types.include?(name)
      if respond_to?(newmethod)
        # Remove the old newmethod
        selfobj.send(:remove_method, newmethod)
      end
    end

    # Then create the class.

    klass = genclass(
      name,
      :parent => Puppet::Type,
      :overwrite => true,
      :hash => @types,
      :attributes => options,
      &block
    )

    # Now define a "new<type>" method for convenience.
    if respond_to? newmethod
      # Refuse to overwrite existing methods like 'newparam' or 'newtype'.
      # TRANSLATORS 'new%{method}' will become a method name, do not translate this string
      Puppet.warning(_("'new%{method}' method already exists; skipping") % { method: name.to_s })
    else
      selfobj.send(:define_method, newmethod) do |*args|
        klass.new(*args)
      end
    end

    # If they've got all the necessary methods defined and they haven't
    # already added the property, then do so now.
    klass.ensurable if klass.ensurable? and !klass.validproperty?(:ensure)

    # Now set up autoload any providers that might exist for this type.

    klass.providerloader = Puppet::Util::Autoload.new(klass, "puppet/provider/#{klass.name}")

    # We have to load everything so that we can figure out the default provider.
    klass.providerloader.loadall(Puppet.lookup(:current_environment))
    klass.providify unless klass.providers.empty?

    loc = block_given? ? block.source_location : nil
    uri = loc.nil? ? nil : URI("#{Puppet::Util.path_to_uri(loc[0])}?line=#{loc[1]}")
    Puppet::Pops::Loaders.register_runtime3_type(name, uri)

    klass
  end
end

#rmtype(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Only use this for testing.

Removes an existing type.



142
143
144
145
146
147
148
# File 'lib/puppet/metatype/manager.rb', line 142

def rmtype(name)
  # Then create the class.

  rmclass(name, :hash => @types)

  singleton_class.send(:remove_method, "new#{name}") if respond_to?("new#{name}")
end

#type(name) ⇒ Puppet::Type?

Returns a Type instance by name. This will load the type if not already defined.

Parameters:

  • name (String, Symbol)

    of the wanted Type

Returns:

  • (Puppet::Type, nil)

    the type or nil if the type was not defined and could not be loaded



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/puppet/metatype/manager.rb', line 155

def type(name)
  @manager_lock.synchronize do
    # Avoid loading if name obviously is not a type name
    if name.to_s.include?(':')
      return nil
    end

    # We are overwhelmingly symbols here, which usually match, so it is worth
    # having this special-case to return quickly.  Like, 25K symbols vs. 300
    # strings in this method. --daniel 2012-07-17
    return @types[name] if @types.include? name

    # Try mangling the name, if it is a string.
    if name.is_a? String
      name = name.downcase.intern
      return @types[name] if @types.include? name
    end
    # Try loading the type.
    if typeloader.load(name, Puppet.lookup(:current_environment))
      # TRANSLATORS 'puppet/type/%{name}' should not be translated
      Puppet.warning(_("Loaded puppet/type/%{name} but no class was created") % { name: name }) unless @types.include? name
    elsif !Puppet[:always_retry_plugins]
      # PUP-5482 - Only look for a type once if plugin retry is disabled
      @types[name] = nil
    end

    # ...and I guess that is that, eh.
    return @types[name]
  end
end

#typeloaderPuppet::Util::Autoload

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a loader for Puppet types. Defaults to an instance of Util::Autoload if no other auto loader has been set.

Returns:



190
191
192
193
194
195
196
# File 'lib/puppet/metatype/manager.rb', line 190

def typeloader
  unless defined?(@typeloader)
    @typeloader = Puppet::Util::Autoload.new(self, "puppet/type")
  end

  @typeloader
end