Module: PuppetRepl::Support

Includes:
Compilier, Environment, Facts, Functions, InputResponders, Node, Play, Scope
Included in:
Cli
Defined in:
lib/puppet-repl/support.rb,
lib/puppet-repl/support/node.rb,
lib/puppet-repl/support/play.rb,
lib/puppet-repl/support/facts.rb,
lib/puppet-repl/support/scope.rb,
lib/puppet-repl/support/compiler.rb,
lib/puppet-repl/support/functions.rb,
lib/puppet-repl/support/environment.rb,
lib/puppet-repl/support/input_responders.rb

Defined Under Namespace

Modules: Compilier, Environment, Facts, Functions, InputResponders, Node, Play, Scope

Instance Method Summary collapse

Methods included from Play

#convert_to_text, #play_back, #play_back_string, #play_back_url

Methods included from InputResponders

#classes, #classification, #environment, #facts, #functions, #handle_set, #help, #krt, #play, #reset, #resources, #set_log_level, #static_responder_list, #vars

Methods included from Node

#convert_remote_node, #create_node, #get_remote_node, #node, #remote_node_name, #remote_node_name=, #set_node, #set_node_from_name, #set_remote_node_name

Methods included from Functions

#function_files, #function_map, #lib_dirs, #load_lib_dirs, #mod_finder, #resolve_paths

Methods included from Scope

#create_scope, #scope, #scope_vars, #set_scope

Methods included from Facts

#default_facts, #facterdb_filter, #server_facts, #set_facts

Methods included from Environment

#create_environment, #default_puppet_env_name, #environment_loaders, #puppet_env_name, #puppet_environment, #set_environment

Methods included from Compilier

#compiler, #create_compiler, #set_compiler

Instance Method Details

#default_manifests_dirObject



116
117
118
# File 'lib/puppet-repl/support.rb', line 116

def default_manifests_dir
  File.join(Puppet[:environmentpath],default_puppet_env_name,'manifests')
end

#default_modules_pathsObject

returns an array of module directories, generally this is the only place to look for puppet code by default. This is read from the puppet configuration



46
47
48
49
50
51
52
53
# File 'lib/puppet-repl/support.rb', line 46

def default_modules_paths
  dirs = []
  do_initialize if Puppet[:codedir].nil?
  # add the puppet-repl directory so we can load any defined functions
  dirs << File.join(Puppet[:environmentpath],default_puppet_env_name,'modules') unless Puppet[:environmentpath].empty?
  dirs << Puppet.settings[:basemodulepath].split(':')
  dirs.flatten
end

#default_site_manifestObject



120
121
122
# File 'lib/puppet-repl/support.rb', line 120

def default_site_manifest
  File.join(default_manifests_dir, 'site.pp')
end

#do_initializeObject

this is required in order to load things only when we need them



96
97
98
99
100
101
102
103
# File 'lib/puppet-repl/support.rb', line 96

def do_initialize
  begin
    Puppet.initialize_settings
    Puppet[:trusted_node_data] = true
  rescue
    # do nothing otherwise calling init twice raises an error
  end
end

#initialize_from_scope(value) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/puppet-repl/support.rb', line 67

def initialize_from_scope(value)
  set_scope(value)
  unless value.nil?
    set_environment(value.environment)
    set_node(value.compiler.node)
    set_compiler(value.compiler)
  end
end

#known_resource_typesObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppet-repl/support.rb', line 76

def known_resource_types
  res = {
    :hostclasses => scope.known_resource_types.hostclasses.keys,
    :definitions => scope.known_resource_types.definitions.keys,
    :nodes => scope.known_resource_types.nodes.keys,
  }
  if sites = scope.known_resource_types.instance_variable_get(:@sites)
    res.merge!(:sites => scope.known_resource_types.instance_variable_get(:@sites).first)
  end
  if scope.known_resource_types.respond_to?(:applications)
    res.merge!(:applications => scope.known_resource_types.applications.keys)
  end
  # some versions of puppet do not support capabilities
  if scope.known_resource_types.respond_to?(:capability_mappings)
    res.merge!(:capability_mappings => scope.known_resource_types.capability_mappings.keys)
  end
  res
end

#modules_pathsObject

returns all the modules paths defined in the environment



63
64
65
# File 'lib/puppet-repl/support.rb', line 63

def modules_paths
  puppet_environment.full_modulepath
end

#parse_error(error) ⇒ Object

parses the error type into a more useful error message defined in errors.rb returns new error object or the original if error cannot be parsed



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet-repl/support.rb', line 21

def parse_error(error)
  case error
  when SocketError
    PuppetRepl::Exception::ConnectError.new(:message => "Unknown host: #{Puppet[:server]}")
  when Net::HTTPError
    PuppetRepl::Exception::AuthError.new(:message => error.message)
  when Errno::ECONNREFUSED
    PuppetRepl::Exception::ConnectError.new(:message => error.message)
  when Puppet::Error
    if error.message =~ /could\ not\ find\ class/i
      PuppetRepl::Exception::NoClassError.new(:default_modules_paths => default_modules_paths,
       :message => error.message)
    elsif error.message =~ /default\ node/i
      PuppetRepl::Exception::NodeDefinitionError.new(:default_site_manifest => default_site_manifest,
       :message => error.message)
    else
      error
    end
  else
    error
  end
end

#parserObject

returns a future parser for evaluating code



112
113
114
# File 'lib/puppet-repl/support.rb', line 112

def parser
  @parser || ::Puppet::Pops::Parser::EvaluatingParser.new
end

#puppet_lib_dirObject



105
106
107
108
109
# File 'lib/puppet-repl/support.rb', line 105

def puppet_lib_dir
  # returns something like "/Library/Ruby/Gems/2.0.0/gems/puppet-4.2.2/lib/puppet.rb"
  # this is only useful when returning a namespace with the functions
  @puppet_lib_dir ||= File.dirname(Puppet.method(:[]).source_location.first)
end

#puppet_repl_lib_dirObject

this is the lib directory of this gem in order to load any puppet functions from this gem we need to add the lib path of this gem



58
59
60
# File 'lib/puppet-repl/support.rb', line 58

def puppet_repl_lib_dir
  File.expand_path(File.join(File.dirname(File.dirname(File.dirname(__FILE__))), 'lib'))
end