Class: Utilities::PuppetModule

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/retrospec/puppet_module.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#future_parserObject

Returns the value of attribute future_parser.



6
7
8
# File 'lib/retrospec/puppet_module.rb', line 6

def future_parser
  @future_parser
end

#module_pathObject



43
44
45
# File 'lib/retrospec/puppet_module.rb', line 43

def module_path
  @module_path
end

Class Method Details

.clean_tmp_modules_dirObject



39
40
41
# File 'lib/retrospec/puppet_module.rb', line 39

def self.clean_tmp_modules_dir
  FileUtils.remove_entry_secure instance.tmp_modules_dir  # ensure we remove the temporary directory
end

.create_tmp_module_pathObject

create the temporary module create, validate the



35
36
37
# File 'lib/retrospec/puppet_module.rb', line 35

def self.create_tmp_module_path
  Utilities::PuppetModule::instance.create_tmp_module_path(module_path)
end

.instanceObject

gets an instance of the module class. The create_tmp_module_path must first be called before instance can do anything useful.



54
55
56
# File 'lib/retrospec/puppet_module.rb', line 54

def self.instance
  @@instance ||= new
end

.module_dir_nameObject



26
27
28
# File 'lib/retrospec/puppet_module.rb', line 26

def self.module_dir_name
  Utilities::PuppetModule.instance.module_dir_name
end

.module_nameObject



22
23
24
# File 'lib/retrospec/puppet_module.rb', line 22

def self.module_name
  Utilities::PuppetModule.instance.module_name
end

.module_pathObject



18
19
20
# File 'lib/retrospec/puppet_module.rb', line 18

def self.module_path
  Utilities::PuppetModule.instance.module_path
end

.module_typesObject



30
31
32
# File 'lib/retrospec/puppet_module.rb', line 30

def self.module_types
  Utilities::PuppetModule.instance.types
end

.tmp_module_pathObject



10
11
12
# File 'lib/retrospec/puppet_module.rb', line 10

def self.tmp_module_path
  Utilities::PuppetModule.instance.tmp_module_path
end

.tmp_modules_dirObject



14
15
16
# File 'lib/retrospec/puppet_module.rb', line 14

def self.tmp_modules_dir
  Utilities::PuppetModule.instance.tmp_modules_dir
end

Instance Method Details

#create_tmp_module_path(module_path) ⇒ Object

puts a symlink in that module directory that points back to the user supplied module path



93
94
95
96
97
98
99
100
101
# File 'lib/retrospec/puppet_module.rb', line 93

def create_tmp_module_path(module_path)
  raise "ModulePathNotFound" unless module_path
  path = File.join(tmp_modules_dir, module_dir_name)
  unless File.exists?(path) # only create if it doesn't already exist
    # create a link where source is the current repo and dest is /tmp/modules/module_name
    FileUtils.ln_s(module_path, path)
  end
  path
end

#find_resource(resource_name) ⇒ Object

returns the resource type object given a resource name ie. tomcat::connector



159
160
161
162
# File 'lib/retrospec/puppet_module.rb', line 159

def find_resource(resource_name)
  request = request(resource_name, 'find')
  resource_type_parser.find(request)
end

#module_dir_nameObject

the directory name of the module usually this is the same as the module name but it can be namespaced sometimes



109
110
111
112
# File 'lib/retrospec/puppet_module.rb', line 109

def module_dir_name
  raise "ModulePathNotFound" unless module_path
  @module_dir_name ||= File.basename(module_path)
end

#module_nameObject

returns the name of the module ie. mysql::config => mysql



119
120
121
122
123
124
125
# File 'lib/retrospec/puppet_module.rb', line 119

def module_name
  begin
    @module_name ||= types.first.name.split('::').first
  rescue
    @module_name = module_dir_name
  end
end

#module_type_namesObject



114
115
116
# File 'lib/retrospec/puppet_module.rb', line 114

def module_type_names
  types.map {|x| x.name}
end

#puppet_environmentObject

creates a puppet environment given a module path and environment name



140
141
142
# File 'lib/retrospec/puppet_module.rb', line 140

def puppet_environment
  @puppet_environment ||= Puppet::Node::Environment.create('production', [tmp_modules_dir])
end

#request(key, method) ⇒ Object

creates a puppet resource request to be used indirectly



145
146
147
148
149
150
151
# File 'lib/retrospec/puppet_module.rb', line 145

def request(key, method)
  instance = Puppet::Indirector::Indirection.instance(:resource_type)
  indirection_name = 'test'
  @request = Puppet::Indirector::Request.new(indirection_name, method, key, instance)
  @request.environment = puppet_environment
  @request
end

#resource_type_parserObject

creates an instance of the resource type parser



154
155
156
# File 'lib/retrospec/puppet_module.rb', line 154

def resource_type_parser
  @resource_type_parser ||= Puppet::Indirector::ResourceType::Parser.new
end

#search_module(pattern = '*') ⇒ Object

returns the resource types found in the module



165
166
167
168
# File 'lib/retrospec/puppet_module.rb', line 165

def search_module(pattern='*')
  request = request(pattern, 'search')
  resource_type_parser.search(request)
end

#tmp_module_pathObject



103
104
105
# File 'lib/retrospec/puppet_module.rb', line 103

def tmp_module_path
  @tmp_module_path ||= File.join(tmp_modules_dir, module_dir_name)
end

#tmp_modules_dirObject

creates a tmp module directory so puppet can work correctly



128
129
130
131
132
133
134
135
136
# File 'lib/retrospec/puppet_module.rb', line 128

def tmp_modules_dir
  if @tmp_modules_dir.nil? or not File.exists?(@tmp_modules_dir)
    dir = Dir.mktmpdir
    tmp_path = File.expand_path(File.join(dir, 'modules'))
    FileUtils.mkdir_p(tmp_path)
    @tmp_modules_dir = tmp_path
  end
  @tmp_modules_dir
end

#typesObject

TODO we need to parse the types and find all the types that inherit other types and then order them so we can load the files first



171
172
173
# File 'lib/retrospec/puppet_module.rb', line 171

def types
  @types ||= search_module || []
end

#validate_module_dir(dir) ⇒ Object

processes a directory and expands to its full path, assumes ‘./’ returns the validated dir



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
85
86
87
88
89
90
# File 'lib/retrospec/puppet_module.rb', line 60

def validate_module_dir(dir)
  # first check to see if manifests directory even exists when path is nil
  if dir.nil?
    dir = '.'
  elsif dir.instance_of?(Array)
    puts "Retrospec - an array of module paths is not supported at this time".fatal
    exit 1
  end
  dir = File.expand_path(dir)
  manifest_dir = File.join(dir,'manifests')
  if ! File.exist?(manifest_dir)
    puts "No manifest directory in #{manifest_dir}, cannot validate this is a module".fatal
    exit 1
  else
    files = Dir.glob("#{manifest_dir}/**/*.pp")
    warn "No puppet manifest files found at #{manifest_dir}".warning if files.length < 1
    # validate the manifest files, because if one files doesn't work it affects everything
    files.each do |file|
      begin
        Puppet[:parser] = 'future' if future_parser
        Puppet::Face[:parser, :current].validate(file)
      rescue SystemExit => e
        puts "Manifest file: #{file} has parser errors, please fix and re-check using\n puppet parser validate #{file}".fatal
        exit 1
      end
    end
    # switch back to current parser, since we rely on the AST parser
    Puppet[:parser] = 'current' if future_parser
  end
  dir
end