Class: Fizzgig

Inherits:
Object
  • Object
show all
Defined in:
lib/fizzgig.rb,
lib/fizzgig/function_stubs.rb

Defined Under Namespace

Modules: FunctionStubs

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Fizzgig

OO interface ======

basically a glorified curried function to store the configuration



89
90
91
92
# File 'lib/fizzgig.rb', line 89

def initialize(settings={})
  @modulepath  = settings[:modulepath]
  @manifestdir = settings[:manifestdir]
end

Class Method Details

.ast_for(code, compiler) ⇒ Object



69
70
71
72
# File 'lib/fizzgig.rb', line 69

def self.ast_for(code,compiler)
  parser = Puppet::Parser::Parser.new compiler.environment.name
  parser.parse(code)
end

.compile(code, compiler) ⇒ Object



59
60
61
# File 'lib/fizzgig.rb', line 59

def self.compile(code,compiler)
  resources_for(ast_for(code,compiler),compiler)
end

.include(klass, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/fizzgig.rb', line 27

def self.include(klass,options = {})
  LSpace.with(:function_stubs => options[:stubs]) do
    setup_fizzgig({modulepath: options[:modulepath],manifestdir: options[:manifestdir]})
    compiler = make_compiler(options[:facts])
    compile("include #{klass}",compiler)
    compiler.catalog
  end
end

.instantiate(type, title, params, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fizzgig.rb', line 6

def self.instantiate(type,title,params,options = {})
  LSpace.with(:function_stubs => options[:stubs]) do
    setup_fizzgig({modulepath: options[:modulepath],manifestdir: options[:manifestdir]})
    compiler = make_compiler(options[:facts])
    scope = compiler.newscope(nil)
    scope.source = Puppet::Resource::Type.new(:node,'localhost')
    # we need to force loading the type; normally this would be
    # handled by Puppet::Parser::AST::Resource calling
    # scope.resolve_type_and_titles
    scope.find_defined_resource_type(type)

    resource = Puppet::Parser::Resource.new(
                   type,
                   title,
                   :scope => scope,
                   :parameters => munge_params(params))
    resource.evaluate
    compiler.catalog
  end
end

.make_compiler(facts, hostname = 'localhost') ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/fizzgig.rb', line 50

def self.make_compiler(facts,hostname='localhost')
  node = Puppet::Node.new(hostname)
  node.merge(facts) if facts
  compiler = Puppet::Parser::Compiler.new(node)
  compiler.send :set_node_parameters
  compiler.send :evaluate_main
  compiler
end

.munge_params(params) ⇒ Object



46
47
48
# File 'lib/fizzgig.rb', line 46

def self.munge_params(params)
  params.collect {|k,v| Puppet::Parser::Resource::Param.new(:name => k, :value => v)}
end

.node(hostname, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/fizzgig.rb', line 36

def self.node(hostname,options = {})
  LSpace.with(:function_stubs => options[:stubs]) do
    setup_fizzgig({modulepath: options[:modulepath],manifestdir: options[:manifestdir]})
    Puppet[:code] = '' # we want puppet to import the Puppet[:manifest] file
    compiler = make_compiler(options[:facts], hostname)
    compiler.send :evaluate_ast_node
    compiler.catalog
  end
end

.resources_for(ast, compiler) ⇒ Object



63
64
65
66
67
# File 'lib/fizzgig.rb', line 63

def self.resources_for(ast,compiler)
  scope = compiler.newscope(nil)
  scope.source = Puppet::Resource::Type.new(:node,'localhost')
  ast.code[0].evaluate(scope)
end

.setup_fizzgig(settings = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fizzgig.rb', line 74

def self.setup_fizzgig(settings={})
  Puppet[:code] = ' ' # hack to suppress puppet from looking at Puppet[:manifest]
  Puppet[:modulepath] = settings[:modulepath] || '/etc/puppet/modules'
  Puppet[:manifestdir] = settings[:manifestdir] || nil
  # stop template() fn from complaining about missing vardir config
  Puppet[:vardir] ||= ""
  # FIXME: decide if these are needed
  #c.add_setting :manifest, :default => nil
  #c.add_setting :template_dir, :default => nil
  #c.add_setting :config, :default => nil
end

Instance Method Details

#include(classname, options = {}) ⇒ Object



98
99
100
# File 'lib/fizzgig.rb', line 98

def include(classname,options={})
  Fizzgig.include(classname,options.merge({modulepath: @modulepath, manifestdir: @manifestdir}))
end

#instantiate(type, title, params, options = {}) ⇒ Object



94
95
96
# File 'lib/fizzgig.rb', line 94

def instantiate(type,title,params,options={})
  Fizzgig.instantiate(type,title,params,options.merge({modulepath: @modulepath, manifestdir: @manifestdir}))
end

#node(hostname, options = {}) ⇒ Object



101
102
103
# File 'lib/fizzgig.rb', line 101

def node(hostname,options={})
  Fizzgig.node(hostname,options.merge({modulepath: @modulepath, manifestdir: @manifestdir}))
end