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

#module_pathObject



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

def module_path
  @module_path
end

Class Method Details

.clean_tmp_modules_dirObject



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

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



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

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.



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

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

.module_dir_nameObject



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

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

.module_nameObject



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

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

.module_pathObject



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

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

.module_typesObject



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

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

.tmp_module_pathObject



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

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

.tmp_modules_dirObject



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

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



80
81
82
83
84
85
86
87
88
# File 'lib/retrospec/puppet_module.rb', line 80

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



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

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



96
97
98
99
# File 'lib/retrospec/puppet_module.rb', line 96

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



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

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

#module_type_namesObject



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

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

#puppet_environmentObject

creates a puppet environment given a module path and environment name



127
128
129
# File 'lib/retrospec/puppet_module.rb', line 127

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



132
133
134
135
136
137
138
# File 'lib/retrospec/puppet_module.rb', line 132

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



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

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



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

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

#tmp_module_pathObject



90
91
92
# File 'lib/retrospec/puppet_module.rb', line 90

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



115
116
117
118
119
120
121
122
123
# File 'lib/retrospec/puppet_module.rb', line 115

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



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

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/retrospec/puppet_module.rb', line 59

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"
    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"
    exit 1
  else
    files = Dir.glob("#{manifest_dir}/**/*.pp")
    warn "No puppet manifest files found at #{manifest_dir}" if files.length < 1
  end
  dir
end