puppet_generator 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/.gitignore +17 -0
  2. data/.rdebugrc +7 -0
  3. data/.rspec +2 -0
  4. data/.simplecov +8 -0
  5. data/.travis.yml +6 -0
  6. data/.yardopts +5 -0
  7. data/CONTRIBUTING.md +8 -0
  8. data/Gemfile +34 -0
  9. data/LICENSE.md +22 -0
  10. data/README.DEVELOPER.md +11 -0
  11. data/README.md +477 -0
  12. data/Rakefile +3 -0
  13. data/TODO.md +2 -0
  14. data/bin/ppgen +16 -0
  15. data/cucumber.yml +2 -0
  16. data/features/generate_file_definition.feature +357 -0
  17. data/features/generate_module_directories.feature +36 -0
  18. data/features/generate_package_definition.feature +206 -0
  19. data/features/generate_role_definitions.feature +64 -0
  20. data/features/generate_user_definition.feature +193 -0
  21. data/features/list.feature +22 -0
  22. data/features/log_actions.feature +93 -0
  23. data/features/step_definitions.rb +3 -0
  24. data/features/support/env.rb +7 -0
  25. data/features/tasks.feature +25 -0
  26. data/gemfiles/Gemfile.default +34 -0
  27. data/gemfiles/Gemfile.travis +17 -0
  28. data/lib/puppet_generator/actions/copy_files_to_module_directory.rb +23 -0
  29. data/lib/puppet_generator/actions/null.rb +9 -0
  30. data/lib/puppet_generator/api.rb +108 -0
  31. data/lib/puppet_generator/definition.rb +10 -0
  32. data/lib/puppet_generator/entry_converters/file.rb +9 -0
  33. data/lib/puppet_generator/entry_converters/package.rb +9 -0
  34. data/lib/puppet_generator/entry_converters/role.rb +9 -0
  35. data/lib/puppet_generator/entry_converters/user.rb +9 -0
  36. data/lib/puppet_generator/exceptions.rb +104 -0
  37. data/lib/puppet_generator/export_filters/build_role_includes_for_directory.rb +35 -0
  38. data/lib/puppet_generator/export_filters/filesystem_attributes.rb +57 -0
  39. data/lib/puppet_generator/export_filters/null.rb +9 -0
  40. data/lib/puppet_generator/exporters/directory.rb +23 -0
  41. data/lib/puppet_generator/exporters/file.rb +17 -0
  42. data/lib/puppet_generator/exporters/stdout.rb +16 -0
  43. data/lib/puppet_generator/import_filters/null.rb +9 -0
  44. data/lib/puppet_generator/import_filters/passwd.rb +22 -0
  45. data/lib/puppet_generator/import_filters/yaml.rb +20 -0
  46. data/lib/puppet_generator/importers/directory.rb +21 -0
  47. data/lib/puppet_generator/importers/file.rb +17 -0
  48. data/lib/puppet_generator/importers/stdin.rb +16 -0
  49. data/lib/puppet_generator/logger.rb +62 -0
  50. data/lib/puppet_generator/main.rb +36 -0
  51. data/lib/puppet_generator/middleware/apply_export_filters.rb +33 -0
  52. data/lib/puppet_generator/middleware/check_for_empty_source.rb +16 -0
  53. data/lib/puppet_generator/middleware/configure_logging.rb +30 -0
  54. data/lib/puppet_generator/middleware/create_module_directories.rb +42 -0
  55. data/lib/puppet_generator/middleware/create_output.rb +46 -0
  56. data/lib/puppet_generator/middleware/create_puppet_object_from_entry.rb +24 -0
  57. data/lib/puppet_generator/middleware/enable_debugging_libraries.rb +15 -0
  58. data/lib/puppet_generator/middleware/execute_actions.rb +34 -0
  59. data/lib/puppet_generator/middleware/filter_imported_data.rb +22 -0
  60. data/lib/puppet_generator/middleware/handle_errors.rb +62 -0
  61. data/lib/puppet_generator/middleware/output_debug_information_for_models.rb +21 -0
  62. data/lib/puppet_generator/middleware/read_input.rb +27 -0
  63. data/lib/puppet_generator/models/action.rb +36 -0
  64. data/lib/puppet_generator/models/base.rb +115 -0
  65. data/lib/puppet_generator/models/class_based_model.rb +56 -0
  66. data/lib/puppet_generator/models/entry_converter.rb +39 -0
  67. data/lib/puppet_generator/models/error_message.rb +60 -0
  68. data/lib/puppet_generator/models/export_filter.rb +44 -0
  69. data/lib/puppet_generator/models/exporter.rb +51 -0
  70. data/lib/puppet_generator/models/filesystem_based_model.rb +63 -0
  71. data/lib/puppet_generator/models/import_filter.rb +44 -0
  72. data/lib/puppet_generator/models/importer.rb +52 -0
  73. data/lib/puppet_generator/models/logger.rb +11 -0
  74. data/lib/puppet_generator/models/template.rb +120 -0
  75. data/lib/puppet_generator/puppet_helper.rb +19 -0
  76. data/lib/puppet_generator/puppet_objects/file.rb +29 -0
  77. data/lib/puppet_generator/puppet_objects/package.rb +24 -0
  78. data/lib/puppet_generator/puppet_objects/role.rb +27 -0
  79. data/lib/puppet_generator/puppet_objects/user.rb +27 -0
  80. data/lib/puppet_generator/setup/bare.rb +25 -0
  81. data/lib/puppet_generator/setup/default_actions.rb +9 -0
  82. data/lib/puppet_generator/setup/default_entry_converter.rb +8 -0
  83. data/lib/puppet_generator/setup/default_error_messages.rb +10 -0
  84. data/lib/puppet_generator/setup/default_export_filter.rb +9 -0
  85. data/lib/puppet_generator/setup/default_exporter.rb +11 -0
  86. data/lib/puppet_generator/setup/default_import_filter.rb +10 -0
  87. data/lib/puppet_generator/setup/default_importer.rb +10 -0
  88. data/lib/puppet_generator/setup/file.rb +29 -0
  89. data/lib/puppet_generator/setup/module.rb +22 -0
  90. data/lib/puppet_generator/setup/package.rb +27 -0
  91. data/lib/puppet_generator/setup/role.rb +28 -0
  92. data/lib/puppet_generator/setup/user.rb +27 -0
  93. data/lib/puppet_generator/task.rb +39 -0
  94. data/lib/puppet_generator/ui/commandline_parser_helper.rb +31 -0
  95. data/lib/puppet_generator/ui/create.rb +59 -0
  96. data/lib/puppet_generator/ui/runner.rb +23 -0
  97. data/lib/puppet_generator/ui/tasks.rb +16 -0
  98. data/lib/puppet_generator/version.rb +4 -0
  99. data/lib/puppet_generator.rb +79 -0
  100. data/puppet_generator.gemspec +35 -0
  101. data/rakefiles/default.rake +3 -0
  102. data/rakefiles/travis.rake +3 -0
  103. data/script/console +8 -0
  104. data/script/terminal +16 -0
  105. data/spec/examples/actions/missing_method.rb +7 -0
  106. data/spec/examples/apply_export_filters/file.txt +0 -0
  107. data/spec/examples/entry_converters/missing_method.rb +7 -0
  108. data/spec/examples/execute_actions/file.txt +0 -0
  109. data/spec/examples/export_filter/invalid_filter_1.rb +8 -0
  110. data/spec/examples/export_filter/invalid_filter_2.rb +7 -0
  111. data/spec/examples/export_filter/missing_method.rb +7 -0
  112. data/spec/examples/exporter/missing_method.rb +7 -0
  113. data/spec/examples/exporter/test.d/.gitkeep +0 -0
  114. data/spec/examples/exporter/test_file.txt +1 -0
  115. data/spec/examples/import_filter/forbidden_keyword.rb +0 -0
  116. data/spec/examples/import_filter/invalid_filter_1.rb +8 -0
  117. data/spec/examples/import_filter/invalid_filter_2.rb +7 -0
  118. data/spec/examples/import_filter/missing_method.rb +7 -0
  119. data/spec/examples/importer/missing_method.rb +7 -0
  120. data/spec/examples/importer/test.d/.gitkeep +0 -0
  121. data/spec/examples/importer/test_file.txt +1 -0
  122. data/spec/examples/templates/invalid_template.pp.erb +1 -0
  123. data/spec/examples/templates/missing_method.rb +7 -0
  124. data/spec/export_filters/build_role_includes_for_directory_spec.rb +39 -0
  125. data/spec/middleware/apply_export_filters_spec.rb +45 -0
  126. data/spec/middleware/execute_actions_spec.rb +44 -0
  127. data/spec/middleware/handle_errors_spec.rb +12 -0
  128. data/spec/models/action_spec.rb +37 -0
  129. data/spec/models/entry_converter_spec.rb +59 -0
  130. data/spec/models/error_message_spec.rb +78 -0
  131. data/spec/models/export_filter_spec.rb +36 -0
  132. data/spec/models/exporter_spec.rb +71 -0
  133. data/spec/models/import_filter_spec.rb +33 -0
  134. data/spec/models/importer_spec.rb +66 -0
  135. data/spec/models/template_spec.rb +47 -0
  136. data/spec/puppet_generator_spec.rb +7 -0
  137. data/spec/setup/role_spec.rb +22 -0
  138. data/spec/spec_helper.rb +10 -0
  139. data/spec/support/filesystem_helper.rb +12 -0
  140. data/spec/support/libraries.rb +10 -0
  141. data/spec/support/rspec_configuration.rb +5 -0
  142. data/templates/puppet/many_per_file/file.pp.erb +16 -0
  143. data/templates/puppet/many_per_file/package.pp.erb +10 -0
  144. data/templates/puppet/many_per_file/role.pp.erb +7 -0
  145. data/templates/puppet/many_per_file/user.pp.erb +23 -0
  146. data/templates/puppet/one_per_file/file.pp.erb +14 -0
  147. data/templates/puppet/one_per_file/package.pp.erb +8 -0
  148. data/templates/puppet/one_per_file/role.pp.erb +5 -0
  149. data/templates/puppet/one_per_file/user.pp.erb +21 -0
  150. metadata +374 -0
@@ -0,0 +1,104 @@
1
+ module PuppetGenerator
2
+ module Exceptions
3
+ # exceptions raise because of a wrong usage by the user
4
+ class UserError < Exception; end
5
+
6
+ #raise if a file does not exist in filesystem
7
+ class WrongTemplateChosen < UserError; end
8
+
9
+ #raised if source is not a directory, file etc.
10
+ class InvalidSource < UserError; end
11
+
12
+ #raised if output channel is not directory, file etc.
13
+ class InvalidOutputChannel < UserError; end
14
+
15
+ #raised if template can not be found
16
+ class TemplateNotFound < UserError; end
17
+
18
+ #raised if something goes wrong internally
19
+ #should never happend
20
+ class InternalError < Exception; end
21
+
22
+ #raised if source is empty
23
+ class EmptySource < UserError; end
24
+
25
+ #raised if an unknown input filter was
26
+ #requested by the user
27
+ class UnknownImportFilter < UserError; end
28
+
29
+ #raised if an unknown export filter was
30
+ #requested by the user
31
+ class UnknownExportFilter < UserError; end
32
+
33
+ #raised if an unknown action was
34
+ #requested by the user
35
+ class UnknownAction < UserError; end
36
+
37
+ #raised if an error happened during decoding of yaml
38
+ class InvalidYamlInput < UserError; end
39
+
40
+ #raised if an error happened during decoding of passwd
41
+ class InvalidPasswdInput < UserError; end
42
+
43
+ #raised if an error happened during copying files
44
+ class ErrorDuringCopyFiles < UserError; end
45
+
46
+ #raised if one tries to delete an instance which
47
+ #does not exist
48
+ class InstanceNotFound < InternalError; end
49
+
50
+ #raised if one tries to use an unknown error
51
+ #message
52
+ class UnknownErrorMessage < InternalError; end
53
+
54
+ #raised if one tries to use a keyword whose use
55
+ #is restricted
56
+ class UnauthorizedUseOfKeyword < InternalError; end
57
+
58
+ #raised if one uses invalid search criteria
59
+ class InvalidSearchCriteria < InternalError; end
60
+
61
+ #raised if one uses an invalid template
62
+ class InvalidTemplate < InternalError; end
63
+
64
+ #raised if one uses an invalid action
65
+ class InvalidAction < InternalError; end
66
+
67
+ #raised if the code which defines an import filter
68
+ #which is malformed
69
+ class InvalidImportFilter < InternalError; end
70
+
71
+ #raised if the code which defines an import filter
72
+ #which is malformed
73
+ class InvalidExportFilter < InternalError; end
74
+
75
+ #raised if the code which defines an importer
76
+ #which is malformed
77
+ class InvalidImporter < InternalError; end
78
+
79
+ #raised if the code which defines an exporter
80
+ #which is malformed
81
+ class InvalidExporter < InternalError; end
82
+
83
+ #raised if the code which defines an entry converter
84
+ #which is malformed
85
+ class InvalidEntryConverter < InternalError; end
86
+
87
+ #raised if the code which defines an import filter
88
+ #is malformed
89
+ class MethodNeedsToBeImplemented < InternalError; end
90
+
91
+ #raised if one tries to use an unimplemented exception
92
+ class ExceptionNeedsToBeImplemented < InternalError; end
93
+
94
+ #raised if an file system error occured
95
+ class FilesystemError < Exception ; end
96
+
97
+ #raise if a directory does not exist in filesystem
98
+ class DirectoryDoesNotExist < FilesystemError; end
99
+
100
+ #raise if a file does not exist in filesystem
101
+ class FileDoesNotExist < FilesystemError; end
102
+
103
+ end
104
+ end
@@ -0,0 +1,35 @@
1
+ module PuppetGenerator
2
+ module ExportFilters
3
+ class BuildRoleIncludesForDirectory
4
+
5
+ include PuppetHelper
6
+
7
+ def convert(objects)
8
+
9
+ objects.collect do |o|
10
+ path = o[:name]
11
+
12
+ next unless directory? path
13
+
14
+ result = {
15
+ name: path,
16
+ includes: includes(path),
17
+ }
18
+ end.compact
19
+
20
+ end
21
+
22
+ private
23
+
24
+ def includes(path)
25
+ Dir.glob( File.join( path, '*.pp' ) ).collect { |f| File.basename( f, '.pp') }.sort
26
+ rescue
27
+ PuppetGenerator.logger.warn(self.class.name) { "Directory \"#{o[:name]}\" does not exist. There's no chance to get subentries for it. Therefor I use the name only and do not generate \"include\"-instructions." }
28
+ end
29
+
30
+ def directory?(path)
31
+ FileTest.directory? path
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
1
+ module PuppetGenerator
2
+ module ExportFilters
3
+ class FilesystemAttributes
4
+ def convert(objects)
5
+
6
+ objects.collect do |o|
7
+ path = o[:name]
8
+
9
+ if File.exists? path
10
+ fs_object_info = stats(path)
11
+
12
+ o.merge!( {
13
+ type: type(path),
14
+ owner: owner(fs_object_info),
15
+ mode: mode(fs_object_info),
16
+ } )
17
+ end
18
+
19
+ o
20
+ end
21
+
22
+ end
23
+
24
+ private
25
+
26
+ def stats(path)
27
+ File::Stat.new(path)
28
+ end
29
+
30
+ def mode(fs_object_info)
31
+ "%o" % fs_object_info.mode
32
+ end
33
+
34
+ def owner(fs_object_info)
35
+ Etc.getpwuid(fs_object_info.uid).name
36
+ end
37
+
38
+ def type(path)
39
+ if file?(path)
40
+ return 'file'
41
+ elsif directory?(path)
42
+ return 'directory'
43
+ else
44
+ return 'file'
45
+ end
46
+ end
47
+
48
+ def file?(path)
49
+ FileTest.file? path
50
+ end
51
+
52
+ def directory?(path)
53
+ FileTest.directory? path
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,9 @@
1
+ module PuppetGenerator
2
+ module ExportFilters
3
+ class Null
4
+ def convert(objects)
5
+ objects
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module PuppetGenerator
2
+ module Exporters
3
+ class Directory
4
+
5
+ def write(directory, definitions=[])
6
+ ::Dir.mkdir directory unless ::Dir.exists? directory
7
+
8
+ definitions.each do |d|
9
+ filename = ::File.join( directory, "#{d.file_name}" )
10
+
11
+ ::File.open(filename, "w") do |f|
12
+ f.puts d.text
13
+ end
14
+ end
15
+ end
16
+
17
+ def writes_to?(destination)
18
+ /^(?:dir|directory):/ === destination
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module PuppetGenerator
2
+ module Exporters
3
+ class File
4
+
5
+ def write(filename, definitions)
6
+ ::File.open(filename, "w") do |f|
7
+ definitions.each { |d| f.puts d.text }
8
+ end
9
+ end
10
+
11
+ def writes_to?(destination)
12
+ /^file:/ === destination
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module PuppetGenerator
2
+ module Exporters
3
+ class Stdout
4
+
5
+ def write( output=$stdout , definitions=[] )
6
+ output = definitions.inject( [] ) { |strings, definition| strings << definition.text }
7
+ printf "%s", output.join("\n")
8
+ end
9
+
10
+ def writes_to?(destination)
11
+ /^stdout/ === destination
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module PuppetGenerator
2
+ module ImportFilters
3
+ class Null
4
+ def convert(lines)
5
+ lines.collect { |line| { name: line.strip } }
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module PuppetGenerator
2
+ module ImportFilters
3
+ class Passwd
4
+ def convert(lines)
5
+ lines.collect do |line|
6
+ passwd = line.split(/:/)
7
+ raise Exceptions::InvalidPasswdInput if passwd.size < 7
8
+
9
+ (username,_,uid,gid,_,homedir,shell) = passwd
10
+
11
+ {
12
+ name: username,
13
+ userid: uid,
14
+ groupid: gid,
15
+ homedir: homedir,
16
+ shell: shell,
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module PuppetGenerator
2
+ module ImportFilters
3
+ class Yaml
4
+ def convert(lines)
5
+ hash_keys_from_str_to_sym(Psych.load(lines.join("\n")))
6
+ end
7
+
8
+ private
9
+
10
+ def hash_keys_from_str_to_sym(hash)
11
+ hash.inject([]) do |result,(name,attributes)|
12
+ result << attributes.inject( { name: name} ) { |result, (k,v)| result[k.to_sym] = v; result }
13
+ end
14
+ rescue Exception => e
15
+ raise Exceptions::InvalidYamlInput, e.message
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module PuppetGenerator
2
+ module Importers
3
+ class Directory
4
+ attr_reader :lines
5
+
6
+ def read( directory )
7
+ lines = []
8
+ ::Find.find( directory ) { |path| lines << path }
9
+
10
+ lines
11
+ rescue Exception => e
12
+ raise Exceptions::DirectoryDoesNotExist, e.message
13
+ end
14
+
15
+ def reads_from?(source)
16
+ FileTest.directory? source
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module PuppetGenerator
2
+ module Importers
3
+ class File
4
+
5
+ def read(filename)
6
+ ::File.open(filename).readlines.collect { |l| l.chomp }
7
+ rescue Exception => e
8
+ raise Exceptions::FileDoesNotExist, e.message
9
+ end
10
+
11
+ def reads_from?(source)
12
+ FileTest.file? source
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module PuppetGenerator
2
+ module Importers
3
+ class Stdin
4
+ attr_reader :lines
5
+
6
+ def read( input )
7
+ $stdin.readlines.collect { |l| l.strip.chomp }
8
+ end
9
+
10
+ def reads_from?(source)
11
+ source == 'stdin'
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ class ::Logger
2
+ SILENT = 9999 unless defined? SILENT
3
+ end
4
+
5
+ module PuppetGenerator
6
+ class Logger
7
+
8
+ extend ::Forwardable
9
+ def_delegators :@logger, :fatal, :warn, :debug, :info, :error, :unknown
10
+
11
+ attr_reader :mode
12
+
13
+ def initialize
14
+ @logger = ::Logger.new($stderr)
15
+ self.mode = :info
16
+ end
17
+
18
+ def mode=(m)
19
+ @mode = m
20
+
21
+ case m
22
+ when :debug
23
+ @logger.level = ::Logger::DEBUG
24
+ format_debug
25
+ when :silent
26
+ @logger.level = ::Logger::SILENT
27
+ when :info
28
+ @logger.level = ::Logger::INFO
29
+ format_standard
30
+ else
31
+ @logger.level = ::Logger::INFO
32
+ format_standard
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def format_debug
39
+ @logger.formatter = proc { |severity, datetime, progname , msg|
40
+ debug_msg("%s %s %s: %s\n" % [ datetime, severity, progname, msg ])
41
+ }
42
+ end
43
+
44
+ def format_standard
45
+ @logger.formatter = proc { |severity, datetime, _, msg|
46
+ info_msg( "%s %s: %s\n" % [ datetime, severity, msg ] )
47
+ }
48
+ end
49
+
50
+ def error_msg(msg)
51
+ ANSI.red(msg)
52
+ end
53
+
54
+ def info_msg(msg)
55
+ msg
56
+ end
57
+
58
+ def debug_msg(msg)
59
+ msg
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,36 @@
1
+ module PuppetGenerator
2
+ class << self
3
+ def logger
4
+ @logger ||= Logger.new
5
+ end
6
+
7
+ def library_name
8
+ self.to_s
9
+ end
10
+
11
+ def gem_load_path
12
+ library_name.underscore
13
+ end
14
+
15
+ def root_directory
16
+ File.expand_path('../../../', __FILE__)
17
+ end
18
+
19
+ def enable_coverage_report
20
+ require 'simplecov'
21
+
22
+ SimpleCov.start
23
+ SimpleCov.command_name 'cucumber'
24
+ SimpleCov.root PuppetGenerator.root_directory
25
+ rescue
26
+ self.logger.warn(self) { "Please note, this code is just for coverage testing. You need to install the \"simplecov\"-gem to make it work" }
27
+ end
28
+
29
+ def enable_debugging_libraries
30
+ require 'pry'
31
+ require 'debugger'
32
+ rescue
33
+ self.logger.warn(self) { "Please note, this code is just for debugging the library. You need to install the \"ruby-debug19\"- and \"pry\"-gem to make it work" }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class ApplyExportFilters
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(task)
10
+ PuppetGenerator.logger.debug(self.class.name){ "Filter the output with filter \"#{task.meta[:requested_export_filter].join(", ")}\"" }
11
+
12
+ task.body = apply_filters(task.meta[:requested_export_filter], task.body)
13
+
14
+ PuppetGenerator.logger.debug(self.class.name) { "Count output lines: #{task.body.size}" }
15
+
16
+ @app.call(task)
17
+ end
18
+
19
+ private
20
+
21
+ def apply_filters(filters, data, &block)
22
+
23
+ filters.inject(data) do |data,filter|
24
+ active_filter = Models::ExportFilter.find filter
25
+ raise Exceptions::UnknownExportFilter unless active_filter
26
+
27
+ data = active_filter.convert(data)
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class CheckForEmptySource
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(task)
9
+ PuppetGenerator.logger.debug(self.class.name){ "Checking if given source \"#{task.meta[:source]}\" is empty." }
10
+ raise PuppetGenerator::Exceptions::EmptySource if task.body.empty?
11
+
12
+ @app.call(task)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class ConfigureLogging
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(task)
9
+ case task.meta[:logging_mode]
10
+ when :debug
11
+ PuppetGenerator.logger.mode = :debug
12
+ when :silent
13
+ PuppetGenerator.logger.mode = :silent
14
+ else
15
+ PuppetGenerator.logger.mode = :info
16
+ end
17
+
18
+ PuppetGenerator.logger.debug(self.class.name){ "Logging is configured with mode \"#{PuppetGenerator.logger.mode}\"." }
19
+
20
+ @app.call(task)
21
+ end
22
+
23
+ private
24
+
25
+ def bold_red(msg)
26
+ ANSI.bold(ANSI.red(msg))
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,42 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class CreateModuleDirectories
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(task)
9
+ module_name = task.meta[:module]
10
+ module_dirs = %w[ manifests files ].collect { |d| File.join(module_name, d) }
11
+ module_file = File.join(module_name, 'manifests', 'init.pp')
12
+
13
+ PuppetGenerator.logger.info(self.class.name) { "Create the module directory for module \"#{module_name}\" at \"#{Dir.pwd}\"." }
14
+
15
+ mkdir module_name
16
+ mkdir module_dirs
17
+ mkfile module_file, <<-EOF
18
+ class #{module_name} {
19
+
20
+ }
21
+ EOF
22
+
23
+ end
24
+
25
+ private
26
+
27
+ def mkdir(*dirs)
28
+ FileUtils.mkdir_p( dirs.flatten.collect { |d| File.join(root_path, d ) } )
29
+ end
30
+
31
+ def mkfile(file, content)
32
+ File.open(file, "w") do |f|
33
+ f.write content
34
+ end
35
+ end
36
+
37
+ def root_path
38
+ Dir.pwd
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class CreateOutput
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(task)
9
+ destination = task.meta[:destination].split(/:/)
10
+ channel = destination[0]
11
+ sink = destination[1]
12
+
13
+ PuppetGenerator.logger.info(self.class.name){ "Puppet definitions will be output to \"#{task.meta[:destination]}\"." }
14
+ PuppetGenerator.logger.debug(self.class.name){ "Render template for channel \"#{channel}\" and sink \"#{sink}\"." }
15
+
16
+ unless task.meta[:template_tagged_with]
17
+ task.meta[:template_tagged_with] = case channel
18
+ when /file/
19
+ [ :many_per_file ]
20
+ when /^(?:dir|directory)/
21
+ [ :one_per_file ]
22
+ when /stdout/
23
+ [ :many_per_file ]
24
+ else
25
+ [ :many_per_file ]
26
+ end
27
+ end
28
+
29
+ template = Models::Template.find(name: task.meta[:command], is_suitable_for: channel.to_sym, is_tagged_with: task.meta[:template_tagged_with] )
30
+ raise Exceptions::WrongTemplateChosen unless template
31
+ PuppetGenerator.logger.debug(self.class.name){ "Chosen template: #{template.name} (#{template.path})." }
32
+
33
+ definitions = template.render(task.body)
34
+
35
+ exporter = Models::Exporter.find(writes_to: task.meta[:destination])
36
+ raise Exceptions::InvalidExporter unless exporter
37
+ PuppetGenerator.logger.debug(self.class.name){ "Chosen exporter: #{exporter.name}." }
38
+
39
+ exporter.write(sink, definitions)
40
+
41
+ @app.call(task)
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class CreatePuppetObjectFromEntry
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(task)
10
+ converter = Models::EntryConverter.find( task.meta[ :command ] )
11
+
12
+ raise Exceptions::InvalidEntryConverter, "I was not able to find an entry converter for command \"#{ task.meta[ :command ] }\". Available converters are: #{ Models::EntryConverter.all_names_as_string }" unless converter
13
+
14
+ PuppetGenerator.logger.debug(self.class.name){ "Convert entries for command \"#{ task.meta[:command] }\"" }
15
+ PuppetGenerator.logger.debug(self.class.name){ "Convert entries to puppet objects using converter \"#{ converter.name }.\"" }
16
+
17
+
18
+ task.body = converter.convert( task.body, task.meta[:module] , task.meta[:class] )
19
+
20
+ @app.call(task)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module PuppetGenerator
2
+ module Middleware
3
+ class EnableDebuggingLibraries
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(task)
9
+ PuppetGenerator.enable_debugging_libraries if task.meta[:debug] and not ENV['TRAVIS_CI'] == 'true'
10
+
11
+ @app.call(task)
12
+ end
13
+ end
14
+ end
15
+ end