Class: Utilities::PuppetModule

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#module_pathObject

Returns the value of attribute module_path.



51
52
53
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 51

def module_path
  @module_path
end

Class Method Details

.base_environment_pathObject



27
28
29
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 27

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

.clean_tmp_modules_dirObject



48
49
50
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 48

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



44
45
46
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 44

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.



59
60
61
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 59

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

.module_dir_nameObject



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

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

.module_nameObject



31
32
33
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 31

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

.module_pathObject



23
24
25
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 23

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

.module_typesObject



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

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

.parserObject



11
12
13
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 11

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

.tmp_module_pathObject



15
16
17
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 15

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

.tmp_modules_dirObject



19
20
21
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 19

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

Instance Method Details

#base_environment_pathObject



157
158
159
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 157

def base_environment_path
  @base_environment_path ||= Dir.mktmpdir
end

#create_tmp_module_path(module_path) ⇒ Object

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



103
104
105
106
107
108
109
110
111
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 103

def create_tmp_module_path(module_path)
  fail 'ModulePathNotFound' unless module_path
  path = File.join(tmp_modules_dir, module_dir_name)
  unless File.exist?(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

#default_modules_pathsObject



161
162
163
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 161

def default_modules_paths
  [tmp_modules_dir]
end

#default_puppet_env_nameObject



117
118
119
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 117

def default_puppet_env_name
  'retrospec'
end

#find_resource(resource_name) ⇒ Object

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



192
193
194
195
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 192

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



123
124
125
126
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 123

def module_dir_name
  fail 'ModulePathNotFound' unless module_path
  @module_dir_name ||= File.basename(module_path)
end

#module_dir_name=(name) ⇒ Object



128
129
130
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 128

def module_dir_name=(name)
  @module_dir_name = name
end

#module_nameObject

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



141
142
143
144
145
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 141

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

#module_name=(name) ⇒ Object



136
137
138
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 136

def module_name=(name)
  @module_name = name
end

#module_type_namesObject



132
133
134
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 132

def module_type_names
  types.map(&:name)
end

#parserObject

returns a future/4.x parser for evaluating code



98
99
100
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 98

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

#puppet_environmentObject

creates a puppet environment given a module path and environment name



170
171
172
173
174
175
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 170

def puppet_environment
  @puppet_environment ||= ::Puppet::Node::Environment.create(
      default_puppet_env_name,
      default_modules_paths,
    )
end

#request(key, method) ⇒ Object

creates a puppet resource request to be used indirectly



178
179
180
181
182
183
184
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 178

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



187
188
189
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 187

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



198
199
200
201
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 198

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

#temporary_environment_pathObject



165
166
167
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 165

def temporary_environment_path
  @temporary_environment_path ||= File.join(base_environment_path,default_puppet_env_name)
end

#tmp_module_pathObject



113
114
115
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 113

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



148
149
150
151
152
153
154
155
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 148

def tmp_modules_dir
  if @tmp_modules_dir.nil? || !File.exist?(@tmp_modules_dir)
    tmp_path = File.expand_path(File.join(temporary_environment_path, '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



204
205
206
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 204

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



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
91
92
93
94
95
# File 'lib/retrospec/plugins/v1/plugin/puppet_module.rb', line 65

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
    raise Retrospec::Puppet::InvalidModulePathError
  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
    raise Retrospec::Puppet::NoManifestDirError
  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
        parser.parse_file(file)
      rescue Puppet::ParseError
        puts "Manifest file: #{file} has parser errors, please fix and re-check using\n puppet parser validate #{file}".fatal
        raise Retrospec::Puppet::ParserError
      rescue SystemExit => e
        puts "Manifest file: #{file} has parser errors, please fix and re-check using\n puppet parser validate #{file}".fatal
        raise Retrospec::Puppet::ParserError
      end
    end
  end
  dir
end