Class: Retrospec

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-retrospec.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, default_template_dir = ENV['RETROSPEC_TEMPLATES_PATH']) ⇒ Retrospec

module path is the relative or absolute path to the module that should retro fitted



14
15
16
17
18
19
20
21
22
# File 'lib/puppet-retrospec.rb', line 14

def initialize(path=nil, default_template_dir=ENV['RETROSPEC_TEMPLATES_PATH'])
  # user supplied a template path or user wants to use local templates
  if not default_template_dir.nil? or ENV['RETROSPEC_ENABLE_LOCAL_TEMPLATES'] =~ /true/i
    default_template_dir = Helpers.setup_user_template_dir(default_template_dir)
  end
  @module_path = validate_module_dir(path)
  @template_dir = default_template_dir
  tmp_module_path
end

Instance Attribute Details

#default_modulesObject

Returns the value of attribute default_modules.



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

def default_modules
  @default_modules
end

#facts_usedObject

Returns the value of attribute facts_used.



11
12
13
# File 'lib/puppet-retrospec.rb', line 11

def facts_used
  @facts_used
end

#included_declarationsObject (readonly)

Returns the value of attribute included_declarations.



7
8
9
# File 'lib/puppet-retrospec.rb', line 7

def included_declarations
  @included_declarations
end

#module_pathObject (readonly)

Returns the value of attribute module_path.



8
9
10
# File 'lib/puppet-retrospec.rb', line 8

def module_path
  @module_path
end

#tmp_module_pathObject (readonly)

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



81
82
83
# File 'lib/puppet-retrospec.rb', line 81

def tmp_module_path
  @tmp_module_path
end

Class Method Details

.run(path = nil, template_dir = ) ⇒ Object

pass in either the path to the module directory or the path to a specific manifest defaults to all manifests in the current directory if ENV = ‘true’ the use the default user templates path if ENV is set then we will override the default user template path with the path provided we will use the necessary templates from that directory instead of the default gem path



113
114
115
116
# File 'lib/puppet-retrospec.rb', line 113

def self.run(path=nil, template_dir=ENV['RETROSPEC_TEMPLATES_PATH'])
  spec = Retrospec.new(path, template_dir)
  spec.create_files
end

Instance Method Details

#create_filesObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet-retrospec.rb', line 28

def create_files
  safe_create_spec_helper
  safe_create_fixtures_file
  safe_create_gemfile
  safe_create_rakefile
  safe_make_shared_context
  types.each do |type|
    safe_create_resource_spec_files(type)
  end
  FileUtils.remove_entry_secure tmp_modules_dir  # ensure we remove the temporary directory
  true
end

#find_resource(resource_name) ⇒ Object

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



228
229
230
231
# File 'lib/puppet-retrospec.rb', line 228

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

#module_nameObject



91
92
93
# File 'lib/puppet-retrospec.rb', line 91

def module_name
  @module_name ||= File.basename(module_path)
end

#modules_includedObject



124
125
126
# File 'lib/puppet-retrospec.rb', line 124

def modules_included
  @modules_included ||= default_modules + referenced_modules
end

#puppet_environmentObject

creates a puppet environment given a module path and environment name



223
224
225
# File 'lib/puppet-retrospec.rb', line 223

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

#referenced_modulesObject



128
129
130
# File 'lib/puppet-retrospec.rb', line 128

def referenced_modules
  []
end

#request(key, method) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/puppet-retrospec.rb', line 210

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



218
219
220
# File 'lib/puppet-retrospec.rb', line 218

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

#safe_create_fixtures_file(template = 'fixtures_file.erb') ⇒ Object



49
50
51
# File 'lib/puppet-retrospec.rb', line 49

def safe_create_fixtures_file(template='fixtures_file.erb')
  safe_create_template_file('.fixtures.yml', template)
end

#safe_create_gemfile(template = 'gemfile.erb') ⇒ Object



57
58
59
# File 'lib/puppet-retrospec.rb', line 57

def safe_create_gemfile(template='gemfile.erb')
  safe_create_template_file('Gemfile', template)
end

#safe_create_rakefile(template = 'rakefile.erb') ⇒ Object



41
42
43
# File 'lib/puppet-retrospec.rb', line 41

def safe_create_rakefile(template='rakefile.erb')
  safe_create_template_file('Rakefile', template)
end

#safe_create_resource_spec_files(type, template = 'resource_spec_file.erb') ⇒ Object

Creates an associated spec file for each type and even creates the subfolders for nested classes one::two::three



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/puppet-retrospec.rb', line 179

def safe_create_resource_spec_files(type,template='resource_spec_file.erb')
  classes_dir = 'spec/classes'
  defines_dir = 'spec/defines'
  hosts_dir   = 'spec/hosts'
  @parameters = type.arguments
  @type = type
  case type.type
    when :hostclass
      type_dir_name = classes_dir
    when :definition
      type_dir_name = defines_dir
    else
      raise "#{type.type} is not a supported resource type yet"
  end
  tokens = type.name.split('::')
  file_name = tokens.pop  # the last item should be the filename
  # if there are only two tokens ie. tomcat::params we dont need to create a subdirectory
  if tokens.count > 1
    # this is a deep level resource ie. tomcat::config::server::connector
    # however we don't need the tomcat directory so we can just remove it
    # this should leave us with config/server/connector_spec.rb
    tokens.delete_at(0)
    # so lets make a directory structure out of it
    dir_name = File.join(tokens)  # config/server
    dir_name = File.join(type_dir_name,dir_name) # spec/classes/tomcat/config/server
    safe_create_template_file(File.join(dir_name,"#{file_name}_spec.rb"), template)
  else
    safe_create_template_file(File.join(type_dir_name,"#{file_name}_spec.rb"), template)
  end
end

#safe_create_spec_helper(template = 'spec_helper_file.erb') ⇒ Object



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

def safe_create_spec_helper(template='spec_helper_file.erb')
  safe_create_template_file(File.join('spec','spec_helper.rb'), template)
end

#safe_create_template_file(path, template) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet-retrospec.rb', line 61

def safe_create_template_file(path, template)
  # check to ensure parent directory exists
  file_dir_path = File.expand_path(File.join(module_path,File.dirname(path)))
  if ! File.exists?(file_dir_path)
    Helpers.safe_mkdir(file_dir_path)
  end
  template_path = File.join(template_dir, template)
  File.open(template_path) do |file|
    renderer = ERB.new(file.read, 0, '-')
    content = renderer.result binding
    dest_path = File.expand_path(File.join(module_path,path))
    Helpers.safe_create_file(dest_path, content)
  end
end

#safe_make_shared_context(template = 'shared_context.erb') ⇒ Object



45
46
47
# File 'lib/puppet-retrospec.rb', line 45

def safe_make_shared_context(template='shared_context.erb')
  safe_create_template_file(File.join('spec','shared_contexts.rb'), template)
end

#search_module(pattern = '*') ⇒ Object

returns the resource types found in the module



234
235
236
237
# File 'lib/puppet-retrospec.rb', line 234

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

#template_dirObject

if user doesn’t supply template directory we assume we should use the templates in this gem



119
120
121
# File 'lib/puppet-retrospec.rb', line 119

def template_dir
  @template_dir ||= Helpers.gem_template_dir
end

#tmp_modules_dirObject

creates a tmp module directory so puppet can work correctly



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

def tmp_modules_dir
  if @modules_dir.nil?
    dir = Dir.mktmpdir
    tmp_modules_path = File.expand_path(File.join(dir, 'modules'))
    FileUtils.mkdir_p(tmp_modules_path)
    @modules_dir = tmp_modules_path
  end
  @modules_dir
end

#typesObject



76
77
78
# File 'lib/puppet-retrospec.rb', line 76

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/puppet-retrospec.rb', line 134

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)
    raise "Retrospec - an array of moudule paths is not supported at this time"
  end
  dir = File.expand_path(dir)
  manifest_dir = File.join(dir,'manifests')
  if ! File.exist?(manifest_dir)
    raise "No manifest directory in #{manifest_dir}, cannot validate this is a module"
  else
    files = Dir.glob("#{manifest_dir}/**/*.pp")
    warn 'No puppet manifest files found at #' if files.length < 1
  end
  dir
end