puppet-retrospec 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +17 -0
- data/CHANGELOG.md +11 -0
- data/DEVELOPMENT.md +3 -0
- data/Gemfile +10 -9
- data/Gemfile.lock +2 -0
- data/README.md +211 -273
- data/Rakefile +8 -8
- data/VERSION +1 -1
- data/lib/retrospec-puppet.rb +3 -3
- data/lib/retrospec/plugins/v1/plugin/conditional.rb +5 -6
- data/lib/retrospec/plugins/v1/plugin/exceptions.rb +17 -0
- data/lib/retrospec/plugins/v1/plugin/generators.rb +7 -0
- data/lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb +18 -10
- data/lib/retrospec/plugins/v1/plugin/generators/function_generator.rb +187 -0
- data/lib/retrospec/plugins/v1/plugin/generators/module_generator.rb +25 -26
- data/lib/retrospec/plugins/v1/plugin/generators/{facter.rb → parsers/facter.rb} +28 -35
- data/lib/retrospec/plugins/v1/plugin/generators/parsers/function.rb +91 -0
- data/lib/retrospec/plugins/v1/plugin/generators/parsers/type.rb +79 -0
- data/lib/retrospec/plugins/v1/plugin/generators/provider_generator.rb +107 -0
- data/lib/retrospec/plugins/v1/plugin/generators/schema_generator.rb +221 -0
- data/lib/retrospec/plugins/v1/plugin/generators/type_generator.rb +118 -0
- data/lib/retrospec/plugins/v1/plugin/helpers.rb +3 -7
- data/lib/retrospec/plugins/v1/plugin/puppet.rb +141 -60
- data/lib/retrospec/plugins/v1/plugin/puppet_module.rb +29 -26
- data/lib/retrospec/plugins/v1/plugin/resource.rb +6 -7
- data/lib/retrospec/plugins/v1/plugin/spec_object.rb +5 -8
- data/lib/retrospec/plugins/v1/plugin/template_helpers.rb +9 -10
- data/lib/retrospec/plugins/v1/plugin/templates/clone-hook +15 -8
- data/lib/retrospec/plugins/v1/plugin/type_code.rb +4 -4
- data/lib/retrospec/plugins/v1/plugin/variable_store.rb +26 -30
- data/lib/retrospec/plugins/v1/plugin/version.rb +1 -1
- data/puppet-retrospec.gemspec +43 -4
- data/spec/fixtures/facts/oracle_controls.rb +38 -0
- data/spec/fixtures/fixture_modules/required_parameters/manifests/init.pp +8 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/facter/fix_installed.rb +11 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/puppet/functions/awesome_parser.rb +13 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/puppet/functions/reduce.rb +31 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/puppet/parser/functions/bad_sha1.rb +6 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/puppet/parser/functions/defined.rb +94 -0
- data/spec/fixtures/fixture_modules/sample_module/lib/puppet/parser/functions/sha1.rb +6 -0
- data/spec/fixtures/fixture_modules/sample_module/spec/unit/facter/fix_installed_spec.rb +21 -0
- data/spec/fixtures/modules/tomcat/files/.gitkeep +0 -0
- data/spec/fixtures/modules/tomcat/templates/.gitkeep +0 -0
- data/spec/fixtures/modules/tomcat/tests/.gitkeep +0 -0
- data/spec/fixtures/providers/bmc/ipmitool.rb +188 -0
- data/spec/fixtures/providers/bmcuser/ipmitool.rb +140 -0
- data/spec/fixtures/types/bmc.rb +102 -0
- data/spec/fixtures/types/bmcuser.rb +46 -0
- data/spec/fixtures/types/db_opatch.rb +93 -0
- data/spec/integration/retrospec_spec.rb +1 -3
- data/spec/spec_helper.rb +33 -6
- data/spec/unit/conditional_spec.rb +12 -15
- data/spec/unit/generators/fact_generater_spec.rb +49 -17
- data/spec/unit/generators/function_generator_spec.rb +301 -0
- data/spec/unit/generators/function_spec.rb +67 -0
- data/spec/unit/generators/parsers/fact_spec.rb +62 -0
- data/spec/unit/generators/parsers/provider_spec.rb +44 -0
- data/spec/unit/generators/parsers/type_spec.rb +93 -0
- data/spec/unit/generators/provider_generator_spec.rb +120 -0
- data/spec/unit/generators/schema_generator_spec.rb +122 -0
- data/spec/unit/generators/type_generator_spec.rb +173 -0
- data/spec/unit/module_spec.rb +7 -10
- data/spec/unit/plugin_spec.rb +213 -15
- data/spec/unit/puppet-retrospec_spec.rb +81 -100
- data/spec/unit/resource_spec.rb +16 -17
- data/spec/unit/spec_object_spec.rb +46 -0
- data/spec/unit/type_code_spec.rb +9 -11
- data/spec/unit/variable_store_spec.rb +41 -43
- metadata +54 -4
- data/spec/unit/generators/fact_spec.rb +0 -58
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Bundler.setup(:default, :development)
|
7
7
|
rescue Bundler::BundlerError => e
|
8
8
|
$stderr.puts e.message
|
9
|
-
$stderr.puts
|
9
|
+
$stderr.puts 'Run `bundle install` to install missing gems'
|
10
10
|
exit e.status_code
|
11
11
|
end
|
12
12
|
require 'rake'
|
@@ -14,13 +14,13 @@ require 'rake'
|
|
14
14
|
require 'jeweler'
|
15
15
|
Jeweler::Tasks.new do |gem|
|
16
16
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name =
|
18
|
-
gem.homepage =
|
19
|
-
gem.license =
|
20
|
-
gem.summary = %
|
21
|
-
gem.description = %
|
22
|
-
gem.email =
|
23
|
-
gem.authors = [
|
17
|
+
gem.name = 'puppet-retrospec'
|
18
|
+
gem.homepage = 'http://github.com/nwops/puppet-retrospec'
|
19
|
+
gem.license = 'MIT'
|
20
|
+
gem.summary = %(Generates puppet rspec test code based on the classes and defines inside the manifests directory. Aims to reduce some of the boilerplate coding with default test patterns.)
|
21
|
+
gem.description = %(Retrofits and generates valid puppet rspec test code to existing modules)
|
22
|
+
gem.email = 'corey@logicminds.biz'
|
23
|
+
gem.authors = ['Corey Osman']
|
24
24
|
# dependencies defined in Gemfile
|
25
25
|
end
|
26
26
|
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.12.0
|
data/lib/retrospec-puppet.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
vendor = File.join(File.dirname(File.dirname(__FILE__)),'vendor')
|
2
|
-
|
1
|
+
vendor = File.join(File.dirname(File.dirname(__FILE__)), 'vendor')
|
2
|
+
$LOAD_PATH.unshift(File.expand_path(File.join([vendor, 'gems', 'puppet-3.7.3', 'lib'])))
|
3
3
|
require 'retrospec/plugins/v1/plugin/puppet'
|
4
|
-
require 'awesome_print'
|
4
|
+
require 'awesome_print' # required for printing templates
|
@@ -8,8 +8,8 @@ class Conditional
|
|
8
8
|
# those that can be changed
|
9
9
|
# those that can be influenced (facts, other variables that contain variables)
|
10
10
|
# takes a subtype of Puppet::AST::Branch that contains conditional logic
|
11
|
-
def initialize(branch,
|
12
|
-
|
11
|
+
def initialize(branch, _parameters)
|
12
|
+
@statements = branch.statements
|
13
13
|
end
|
14
14
|
|
15
15
|
# get the attributes for the given resources found in the type code passed in
|
@@ -24,7 +24,7 @@ class Conditional
|
|
24
24
|
|
25
25
|
# a array of types the are known to contain conditional code and statements
|
26
26
|
def self.types
|
27
|
-
#test, statement, value
|
27
|
+
# test, statement, value
|
28
28
|
# if I don't have a statement that I am part of a bigger code block
|
29
29
|
# [Puppet::Parser::AST::IfStatement, Puppet::Parser::AST::CaseStatement, Puppet::Parser::AST::Else,
|
30
30
|
# Puppet::Parser::AST::CaseOpt, Puppet::Parser::AST::Selector]
|
@@ -35,7 +35,7 @@ class Conditional
|
|
35
35
|
def self.find_conditionals(type)
|
36
36
|
conds = []
|
37
37
|
if type.code.respond_to?(:find_all)
|
38
|
-
conds = type.code.find_all {|c| types.include?(c.class)
|
38
|
+
conds = type.code.find_all { |c| types.include?(c.class) }
|
39
39
|
end
|
40
40
|
conds
|
41
41
|
end
|
@@ -49,5 +49,4 @@ class Conditional
|
|
49
49
|
end
|
50
50
|
conditionals
|
51
51
|
end
|
52
|
-
|
53
|
-
end
|
52
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Retrospec
|
2
|
+
module Puppet
|
3
|
+
class ParserError < Exception
|
4
|
+
end
|
5
|
+
class NoManifestDirError < Exception
|
6
|
+
end
|
7
|
+
class InvalidModulePathError < Exception
|
8
|
+
end
|
9
|
+
module Generators
|
10
|
+
class CoreTypeException < Exception
|
11
|
+
def message
|
12
|
+
"You cannot use a core puppet type, bad stuff will happen"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require_relative 'generators/fact_generator'
|
2
|
+
require_relative 'generators/module_generator'
|
3
|
+
require_relative 'generators/function_generator'
|
4
|
+
require_relative 'generators/type_generator'
|
5
|
+
require_relative 'generators/provider_generator'
|
6
|
+
require_relative 'generators/schema_generator'
|
7
|
+
require_relative 'exceptions'
|
@@ -1,15 +1,14 @@
|
|
1
|
-
require_relative 'facter'
|
1
|
+
require_relative 'parsers/facter'
|
2
2
|
|
3
3
|
module Retrospec
|
4
4
|
module Puppet
|
5
5
|
module Generators
|
6
6
|
class FactGenerator < Retrospec::Plugins::V1::Plugin
|
7
|
-
|
8
7
|
attr_reader :template_dir, :context, :module_path, :fact_name, :config_data
|
9
8
|
|
10
9
|
# retrospec will initilalize this class so its up to you
|
11
10
|
# to set any additional variables you need to get the job done.
|
12
|
-
def initialize(module_path, spec_object={})
|
11
|
+
def initialize(module_path, spec_object = {})
|
13
12
|
# below is the Spec Object which serves as a context for template rendering
|
14
13
|
# you will need to initialize this object, so the erb templates can get the binding
|
15
14
|
# the SpecObject can be customized to your liking as its different for every plugin gem.
|
@@ -22,13 +21,13 @@ module Retrospec
|
|
22
21
|
# the global options are passed in for your usage
|
23
22
|
# http://trollop.rubyforge.org
|
24
23
|
# all options here are available in the config passed into config object
|
25
|
-
def self.run_cli(global_opts)
|
26
|
-
sub_command_opts = Trollop
|
24
|
+
def self.run_cli(global_opts, args=ARGV)
|
25
|
+
sub_command_opts = Trollop.options(args) do
|
27
26
|
banner <<-EOS
|
28
27
|
Generates a new fact with the given name
|
29
28
|
|
30
29
|
EOS
|
31
|
-
opt :name,
|
30
|
+
opt :name, 'The name of the fact you wish to create', :type => :string, :require => :true, :short => '-n'
|
32
31
|
end
|
33
32
|
unless sub_command_opts[:name]
|
34
33
|
Trollop.educate
|
@@ -60,17 +59,26 @@ Generates a new fact with the given name
|
|
60
59
|
generate_fact_spec_files
|
61
60
|
end
|
62
61
|
|
62
|
+
# returns an array of fact files found in the facter directory
|
63
|
+
def fact_files
|
64
|
+
@fact_files ||= Dir.glob(File.join(facter_dir, '*.rb')).sort
|
65
|
+
end
|
66
|
+
|
63
67
|
# generates spec files for each fact defined in the fact file
|
68
|
+
# returns a array of generated spec files
|
64
69
|
def generate_fact_spec_files
|
65
|
-
|
70
|
+
spec_files = []
|
71
|
+
template_file = File.join(template_dir, 'fact_spec.rb.retrospec.erb')
|
66
72
|
fact_files.each do | fact_file|
|
67
73
|
fact_file_data = Retrospec::Puppet::Generators::Facter.load_fact(fact_file)
|
68
74
|
fact_file_data.facts.each do |name, fact_data|
|
69
75
|
# because many facts can be in a single file we want to create a unique file for each fact
|
70
76
|
fact_spec_path = File.join(facter_spec_dir, "#{name}_spec.rb")
|
71
|
-
safe_create_template_file(fact_spec_path,
|
77
|
+
safe_create_template_file(fact_spec_path,template_file , fact_data)
|
78
|
+
spec_files << fact_spec_path
|
72
79
|
end
|
73
80
|
end
|
81
|
+
spec_files
|
74
82
|
end
|
75
83
|
|
76
84
|
# the template directory located inside the your retrospec plugin gem
|
@@ -79,7 +87,7 @@ Generates a new fact with the given name
|
|
79
87
|
def template_dir
|
80
88
|
unless @template_dir
|
81
89
|
external_templates = File.expand_path(File.join(config_data[:template_dir], 'facts', 'fact.rb.retrospec.erb'))
|
82
|
-
if File.
|
90
|
+
if File.exist?(external_templates)
|
83
91
|
@template_dir = File.join(config_data[:template_dir], 'facts')
|
84
92
|
else
|
85
93
|
@template_dir = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), 'templates', 'facts'))
|
@@ -90,4 +98,4 @@ Generates a new fact with the given name
|
|
90
98
|
end
|
91
99
|
end
|
92
100
|
end
|
93
|
-
end
|
101
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
require_relative 'parsers/function'
|
2
|
+
|
3
|
+
module Retrospec
|
4
|
+
module Puppet
|
5
|
+
module Generators
|
6
|
+
class FunctionGenerator < Retrospec::Plugins::V1::Plugin
|
7
|
+
attr_reader :template_dir, :context
|
8
|
+
|
9
|
+
# retrospec will initilalize this class so its up to you
|
10
|
+
# to set any additional variables you need to get the job done.
|
11
|
+
def initialize(module_path, spec_object = {})
|
12
|
+
super
|
13
|
+
# below is the Spec Object which serves as a context for template rendering
|
14
|
+
# you will need to initialize this object, so the erb templates can get the binding
|
15
|
+
# the SpecObject can be customized to your liking as its different for every plugin gem.
|
16
|
+
@context = OpenStruct.new(:name => spec_object[:name], :return_type => spec_object[:return_type],
|
17
|
+
:function_type => spec_object[:type], :test_type => spec_object[:test_type])
|
18
|
+
end
|
19
|
+
|
20
|
+
# Puppet currently has two versions of functions (v3, v4). At the time of writing v4 is still new
|
21
|
+
# and not used very much. Because of this we will want to allow the user to pick which version of puppet function dsl
|
22
|
+
# they wish to use. Based on the version, a slightly different template will be rendered. This method will return
|
23
|
+
# the template dir based on the version passed in.
|
24
|
+
# By default this method looks inside the user supplied template dir for existence of a template and if true returns that base path, if
|
25
|
+
# false returns the template directory inside the gem location which is mainly used for development only.
|
26
|
+
def template_dir
|
27
|
+
external_templates = File.expand_path(File.join(config_data[:template_dir], 'functions',function_type, 'function_template.rb.retrospec.erb'))
|
28
|
+
if File.exist?(external_templates)
|
29
|
+
File.join(config_data[:template_dir], 'functions', function_type)
|
30
|
+
else
|
31
|
+
File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), 'templates', 'functions', function_type))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# returns v3 or v4 to specify the function type
|
36
|
+
def function_type
|
37
|
+
context.function_type
|
38
|
+
end
|
39
|
+
|
40
|
+
# used to display subcommand options to the cli
|
41
|
+
# the global options are passed in for your usage
|
42
|
+
# http://trollop.rubyforge.org
|
43
|
+
# all options here are available in the config passed into config object
|
44
|
+
# returns the parameters
|
45
|
+
def self.run_cli(global_opts, args=ARGV)
|
46
|
+
func_types = ['v3', 'v4']
|
47
|
+
func_type = global_opts['plugins::puppet::default_function_version'] || 'v3'
|
48
|
+
test_type = global_opts['plugins::puppet::default_function_test_type'] || 'rspec'
|
49
|
+
sub_command_opts = Trollop.options(args) do
|
50
|
+
banner <<-EOS
|
51
|
+
Generates a new function with the given name.
|
52
|
+
|
53
|
+
EOS
|
54
|
+
opt :name, 'The name of the function you wish to create', :type => :string, :required => true, :short => '-n'
|
55
|
+
opt :type, 'The version type of the function (v3, v4)', :type => :string, :required => false, :short => '-t',
|
56
|
+
:default => func_type
|
57
|
+
opt :test_type, 'The type of test file to create (rspec, ruby)', :default => test_type, :type => :string, :short => '-u'
|
58
|
+
opt :return_type, 'The return type of the function (rvalue, statement)', :type => :string, :required => false,
|
59
|
+
:short => '-r', :default => 'rvalue'
|
60
|
+
end
|
61
|
+
|
62
|
+
unless func_types.include? sub_command_opts[:type].downcase
|
63
|
+
puts "Invalid type, must be one of #{func_types.join(',')}"
|
64
|
+
Trollop.educate
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
unless sub_command_opts[:name]
|
68
|
+
Trollop.educate
|
69
|
+
exit 1
|
70
|
+
end
|
71
|
+
plugin_data = global_opts.merge(sub_command_opts)
|
72
|
+
plugin_data
|
73
|
+
end
|
74
|
+
|
75
|
+
# returns the function directory to create the the function in
|
76
|
+
def function_dir
|
77
|
+
if function_type == 'v3'
|
78
|
+
v3_function_dir
|
79
|
+
else
|
80
|
+
v4_function_dir
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# returns the path to the v3 function directory
|
85
|
+
def v3_function_dir
|
86
|
+
@v3_function_dir || File.join(module_path, 'lib', 'puppet', 'parser', 'functions')
|
87
|
+
end
|
88
|
+
|
89
|
+
# returns the path to the v4 function directory
|
90
|
+
def v4_function_dir
|
91
|
+
@v4_function_dir || File.join(module_path, 'lib', 'puppet', 'functions')
|
92
|
+
end
|
93
|
+
|
94
|
+
# returns the path to the function using the function directory
|
95
|
+
def function_path
|
96
|
+
File.join(function_dir, "#{function_name}.rb")
|
97
|
+
end
|
98
|
+
|
99
|
+
# returns the name of the function
|
100
|
+
def function_name
|
101
|
+
context.name
|
102
|
+
end
|
103
|
+
|
104
|
+
# generates the function file based on the template and context
|
105
|
+
def generate_function_file
|
106
|
+
safe_create_template_file(function_path, File.join(template_dir, 'function_template.rb.retrospec.erb'), context)
|
107
|
+
function_path
|
108
|
+
end
|
109
|
+
|
110
|
+
# returns the path to the spec directory
|
111
|
+
# because this can vary between test types and function types
|
112
|
+
# there are many supporting functions to determine which directory to use
|
113
|
+
def spec_file_dir
|
114
|
+
if function_type == 'v3'
|
115
|
+
v3_spec_dir
|
116
|
+
else
|
117
|
+
v4_spec_dir
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# returns the path to the v3 spec directory if using using rspec test type
|
122
|
+
# the spec/functions is returned
|
123
|
+
def v3_spec_dir
|
124
|
+
if context[:test_type] == 'ruby'
|
125
|
+
File.join(module_path, 'spec', 'unit', 'puppet', 'parser', 'functions')
|
126
|
+
else
|
127
|
+
File.join(module_path, 'spec', 'functions')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# returns the path to the v4 spec directory if using using rspec test type
|
132
|
+
# the spec/functions is returned
|
133
|
+
def v4_spec_dir
|
134
|
+
if context[:test_type] == 'ruby'
|
135
|
+
File.join(module_path, 'spec', 'unit', 'puppet', 'functions')
|
136
|
+
else
|
137
|
+
File.join(module_path, 'spec', 'functions')
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# returns an array of functions found in either the v3 or v4 directories
|
142
|
+
def discovered_functions
|
143
|
+
Dir.glob([File.join(v3_function_dir, '*.rb'), File.join(v4_function_dir, '*.rb')]).sort
|
144
|
+
end
|
145
|
+
|
146
|
+
def is_v3_function?(function_file)
|
147
|
+
File.basename(Pathname.new(function_file).parent.parent) == 'parser'
|
148
|
+
end
|
149
|
+
|
150
|
+
# returns the template file name based on the test type
|
151
|
+
def template_file
|
152
|
+
if context[:test_type] == 'rspec'
|
153
|
+
'function_rspec_template.retrospec.erb'
|
154
|
+
else
|
155
|
+
'function_ruby_template.retrospec.erb'
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# creates the spec files for the discovered_functions
|
160
|
+
def generate_spec_files
|
161
|
+
spec_files = []
|
162
|
+
discovered_functions.each do |file|
|
163
|
+
begin
|
164
|
+
if is_v3_function?(file)
|
165
|
+
context.function_type = 'v3'
|
166
|
+
file_data = Retrospec::Puppet::Parser::Functions.load_function(file)
|
167
|
+
else
|
168
|
+
context.function_type = 'v4'
|
169
|
+
file_data = Retrospec::Puppet::Functions.load_function(file)
|
170
|
+
end
|
171
|
+
rescue NoMethodError => e
|
172
|
+
puts "Error evaluating function for #{file}, skipping".warning
|
173
|
+
next
|
174
|
+
rescue SyntaxError => e
|
175
|
+
puts "Function syntax is bad for #{file}, skipping".warning
|
176
|
+
next # lets skip functions that have bad syntax
|
177
|
+
end
|
178
|
+
spec_path = File.join(spec_file_dir, "#{file_data.name}_spec.rb")
|
179
|
+
spec_files << spec_path
|
180
|
+
safe_create_template_file(spec_path, File.join(template_dir, template_file), file_data)
|
181
|
+
end
|
182
|
+
spec_files
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
@@ -5,12 +5,11 @@ module Retrospec
|
|
5
5
|
module Puppet
|
6
6
|
module Generators
|
7
7
|
class ModuleGenerator < Retrospec::Plugins::V1::Plugin
|
8
|
-
|
9
|
-
attr_reader :template_dir, :context, :module_path, :fact_name, :config_data
|
8
|
+
attr_reader :template_dir, :context, :fact_name, :config_data
|
10
9
|
|
11
10
|
# retrospec will initilalize this class so its up to you
|
12
11
|
# to set any additional variables you need to get the job done.
|
13
|
-
def initialize(module_path, spec_object={})
|
12
|
+
def initialize(module_path, spec_object = {})
|
14
13
|
super
|
15
14
|
# below is the Spec Object which serves as a context for template rendering
|
16
15
|
# you will need to initialize this object, so the erb templates can get the binding
|
@@ -24,27 +23,28 @@ module Retrospec
|
|
24
23
|
# http://trollop.rubyforge.org
|
25
24
|
# all options here are available in the config passed into config object
|
26
25
|
# returns a new instance of this class
|
27
|
-
def self.run_cli(global_opts)
|
26
|
+
def self.run_cli(global_opts, args=ARGV)
|
28
27
|
namespace = global_opts['plugins::puppet::namespace'] || 'namespace'
|
29
28
|
author = global_opts['plugins::puppet::author'] || 'author_name'
|
30
|
-
|
29
|
+
license = global_opts['plugins::puppet::default_license'] || 'Apache-2.0'
|
30
|
+
sub_command_opts = Trollop.options(args) do
|
31
31
|
banner <<-EOS
|
32
32
|
Generates a new module with the given name and namespace
|
33
33
|
|
34
34
|
EOS
|
35
|
-
opt :name,
|
36
|
-
|
37
|
-
opt :namespace,
|
38
|
-
|
39
|
-
opt :author,
|
40
|
-
|
35
|
+
opt :name, 'The name of the module you wish to create', :type => :string, :required => true, :short => '-n',
|
36
|
+
:default => File.basename(global_opts[:module_path])
|
37
|
+
opt :namespace, 'The namespace to use only when creating a new module', :default => namespace, :required => false,
|
38
|
+
:type => :string
|
39
|
+
opt :author, 'The full name of the module author', :default => author, :required => false, :short => '-a',
|
40
|
+
:type => :string
|
41
|
+
opt :license, "The license type for the module", :default => license, :type => :string, :short => '-l'
|
41
42
|
end
|
42
43
|
unless sub_command_opts[:name]
|
43
44
|
Trollop.educate
|
44
45
|
exit 1
|
45
46
|
end
|
46
47
|
plugin_data = global_opts.merge(sub_command_opts)
|
47
|
-
Retrospec::Puppet::Generators::ModuleGenerator.new(plugin_data[:module_path], plugin_data)
|
48
48
|
end
|
49
49
|
|
50
50
|
def run(manifest_dir)
|
@@ -54,9 +54,7 @@ Generates a new module with the given name and namespace
|
|
54
54
|
unless ENV['RETROSPEC_PUPPET_AUTO_GENERATE'] == 'true'
|
55
55
|
print "The module located at: #{module_path} does not exist, do you wish to create it? (y/n): "
|
56
56
|
answer = gets.chomp
|
57
|
-
unless answer =~ /y/i
|
58
|
-
exit 1
|
59
|
-
end
|
57
|
+
exit 1 unless answer =~ /y/i
|
60
58
|
end
|
61
59
|
|
62
60
|
create_manifest_file(init_class, content)
|
@@ -77,28 +75,30 @@ Generates a new module with the given name and namespace
|
|
77
75
|
end
|
78
76
|
|
79
77
|
# generates the metadata file in the module directory
|
80
|
-
def generate_metadata_file(mod_name,config_data)
|
78
|
+
def generate_metadata_file(mod_name, config_data)
|
81
79
|
require 'puppet/module_tool/metadata'
|
82
80
|
# make sure the metadata file exists
|
83
81
|
module_path = config_data[:module_path]
|
82
|
+
license = config_data[:license] || config_data['plugins::puppet::default_license'] || 'Apache-2.0'
|
84
83
|
author = config_data[:author] || config_data['plugins::puppet::author'] || 'your_name'
|
85
84
|
namespace = config_data[:namespace] || config_data['plugins::puppet::namespace'] || 'namespace'
|
86
85
|
metadata_file = File.join(module_path, 'metadata.json')
|
87
|
-
unless File.
|
86
|
+
unless File.exist?(metadata_file)
|
88
87
|
# by default the module tool metadata checks for a namespece
|
89
|
-
if !
|
88
|
+
if !mod_name.include?('-')
|
90
89
|
name = "#{namespace}-#{mod_name}"
|
91
90
|
else
|
92
91
|
name = mod_name
|
93
92
|
end
|
94
93
|
begin
|
95
94
|
metadata = ::Puppet::ModuleTool::Metadata.new.update(
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
95
|
+
'name' => name.downcase,
|
96
|
+
'version' => '0.1.0',
|
97
|
+
'author' => author,
|
98
|
+
'license' => license,
|
99
|
+
'dependencies' => [
|
100
|
+
{ 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 4.11.0' }
|
101
|
+
]
|
102
102
|
)
|
103
103
|
rescue ArgumentError => e
|
104
104
|
puts e.message
|
@@ -107,8 +107,7 @@ Generates a new module with the given name and namespace
|
|
107
107
|
safe_create_file(metadata_file, metadata.to_json)
|
108
108
|
end
|
109
109
|
end
|
110
|
-
|
111
110
|
end
|
112
111
|
end
|
113
112
|
end
|
114
|
-
end
|
113
|
+
end
|