kafo 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of kafo might be problematic. Click here for more details.
- data/README.md +1 -1
- data/bin/kafo-configure +1 -1
- data/bin/kafofy +3 -2
- data/config/kafo.yaml +7 -0
- data/config/kafo.yaml.example +15 -4
- data/lib/kafo/configuration.rb +50 -46
- data/lib/kafo/kafo_configure.rb +87 -30
- data/lib/kafo/logger.rb +40 -35
- data/lib/kafo/params/password.rb +1 -1
- data/lib/kafo/puppet_command.rb +37 -0
- data/lib/kafo/puppet_module.rb +2 -2
- data/lib/kafo/puppet_module_parser.rb +1 -0
- data/lib/kafo/string_helper.rb +1 -1
- data/lib/kafo/version.rb +1 -1
- data/modules/create_resources/Gemfile +11 -0
- data/modules/create_resources/LICENSE +202 -0
- data/modules/create_resources/Modulefile +11 -0
- data/modules/create_resources/README.md +37 -0
- data/modules/create_resources/Rakefile +1 -0
- data/modules/create_resources/lib/puppet/parser/functions/create_resources.rb +75 -0
- data/modules/create_resources/spec/spec_helper.rb +1 -0
- data/modules/create_resources/spec/unit/puppet/parser/functions/create_resources_spec.rb +116 -0
- data/modules/create_resources/tests/users.pp +48 -0
- data/modules/kafo_configure/lib/puppet/parser/functions/class_name.rb +3 -6
- data/modules/kafo_configure/lib/puppet/parser/functions/dump_values.rb +2 -2
- data/modules/kafo_configure/lib/puppet/parser/functions/kafo_ordered.rb +1 -1
- data/modules/kafo_configure/lib/puppet/parser/functions/load_kafo_answer_file.rb +13 -0
- data/modules/kafo_configure/lib/puppet/parser/functions/load_kafo_password.rb +1 -1
- data/modules/kafo_configure/manifests/init.pp +2 -6
- metadata +14 -2
data/README.md
CHANGED
@@ -251,7 +251,7 @@ file. Also we want to name our module as puppetmaster. We add following mapping
|
|
251
251
|
to kafo.yaml
|
252
252
|
|
253
253
|
```yaml
|
254
|
-
:mapping
|
254
|
+
:mapping:
|
255
255
|
:puppetmaster: # a module name, so we'll have puppetmaster: true in answer file
|
256
256
|
:dir_name: 'puppet' # the subdirectory in modules/
|
257
257
|
:manifest_name: 'server' # manifest filename without .pp extension
|
data/bin/kafo-configure
CHANGED
data/bin/kafofy
CHANGED
@@ -23,6 +23,7 @@ if ARGV.size > 0
|
|
23
23
|
#!/usr/bin/env ruby
|
24
24
|
require 'rubygems'
|
25
25
|
require 'kafo'
|
26
|
+
CONFIG_FILE = "/etc/#{script_name}/#{script_name}"
|
26
27
|
KafoConfigure.run
|
27
28
|
EOS
|
28
29
|
File.open(script_name, 'w') { |file| file.write(content) }
|
@@ -32,6 +33,6 @@ end
|
|
32
33
|
puts "Your directory was kafofied"
|
33
34
|
|
34
35
|
puts "Now you should:"
|
35
|
-
puts " 1. upload your puppet modules to modules directory"
|
36
|
-
puts " 2. create default config/answers.yaml"
|
36
|
+
puts " 1. upload your puppet modules to modules directory (you can use librarian-puppet project)"
|
37
|
+
puts " 2. create default config/answers.yaml or modify config/kafo.yaml to laod another answer file"
|
37
38
|
puts " 3. run #{script_name}"
|
data/config/kafo.yaml
ADDED
data/config/kafo.yaml.example
CHANGED
@@ -1,16 +1,27 @@
|
|
1
1
|
# kafo main configuration file example
|
2
2
|
# you can rename it to kafo.yaml so it overwrite default kafo settings
|
3
|
-
# note current configuration is written
|
3
|
+
# note current configuration is written to kafo.yaml every time kafo is run
|
4
4
|
|
5
|
-
##
|
6
|
-
#
|
7
|
-
# :
|
5
|
+
## Installer configuration
|
6
|
+
# Path to answer file, if the file does not exist a $pwd/config/answers.yaml is used as a fallback
|
7
|
+
# :answer_file: /etc/kafo/kafo.yaml
|
8
|
+
# Uncomment if you want to load puppet modules from a specific path, $pwd/modules is used by default
|
9
|
+
# :installer_dir: /usr/share/kafo/
|
8
10
|
|
9
11
|
## Kafo tuning, customization of core functionality
|
10
12
|
# :no_prefix: false
|
11
13
|
# :mapping: {}
|
12
14
|
# :order:
|
13
15
|
|
16
|
+
## Useful for development, e.g. when you want to move log files to local directory
|
17
|
+
# :log_dir: /var/log/kafo
|
18
|
+
# :log_level: :info
|
19
|
+
|
20
|
+
# Change if you want to debug default answers for you modules, this directory holds default answers
|
21
|
+
# :default_values_dir: /tmp
|
22
|
+
|
14
23
|
## Advanced configuration - if not set it's ignored
|
15
24
|
# :log_owner: root
|
16
25
|
# :log_group: root
|
26
|
+
# :config_header_file:
|
27
|
+
# :dont_save_answers:
|
data/lib/kafo/configuration.rb
CHANGED
@@ -3,52 +3,56 @@ require 'kafo/puppet_module'
|
|
3
3
|
require 'kafo/password_manager'
|
4
4
|
|
5
5
|
class Configuration
|
6
|
-
attr_reader :config_file
|
6
|
+
attr_reader :config_file, :answer_file
|
7
|
+
|
8
|
+
DEFAULT = {
|
9
|
+
:log_dir => '/var/log/kafo',
|
10
|
+
:log_level => :info,
|
11
|
+
:no_prefix => false,
|
12
|
+
:mapping => {},
|
13
|
+
:answer_file => '/etc/kafo/kafo.yaml',
|
14
|
+
:installer_dir => '.',
|
15
|
+
:default_values_dir => '/tmp'
|
16
|
+
}
|
7
17
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def self.save_configuration(configuration)
|
13
|
-
File.open(application_config_file, 'w') { |file| file.write(YAML.dump(configuration)) }
|
14
|
-
end
|
18
|
+
def initialize(file)
|
19
|
+
@config_file = file
|
20
|
+
configure_application
|
21
|
+
@logger = Logging.logger.root
|
15
22
|
|
16
|
-
|
23
|
+
@answer_file = app[:answer_file]
|
17
24
|
begin
|
18
|
-
|
19
|
-
rescue => e
|
20
|
-
|
25
|
+
@data = YAML.load_file(@answer_file)
|
26
|
+
rescue Errno::ENOENT => e
|
27
|
+
puts "No answers file at #{@answer_file} found, can not continue"
|
28
|
+
exit(:no_answer_file)
|
21
29
|
end
|
22
30
|
|
23
|
-
|
24
|
-
|
25
|
-
:log_level => :info,
|
26
|
-
:no_prefix => false,
|
27
|
-
:mapping => {}
|
28
|
-
}
|
29
|
-
|
30
|
-
result = default.merge(configuration || {})
|
31
|
-
result[:password] ||= PasswordManager.new.password
|
32
|
-
save_configuration(result)
|
31
|
+
@config_dir = File.dirname(@config_file)
|
32
|
+
end
|
33
33
|
|
34
|
-
|
34
|
+
def save_configuration(configuration)
|
35
|
+
File.open(@config_file, 'w') { |file| file.write(YAML.dump(configuration)) }
|
35
36
|
end
|
36
37
|
|
37
|
-
|
38
|
+
def configure_application
|
39
|
+
result = app
|
40
|
+
save_configuration(result)
|
41
|
+
result
|
42
|
+
end
|
38
43
|
|
39
|
-
def
|
40
|
-
@
|
41
|
-
|
44
|
+
def app
|
45
|
+
@app ||= begin
|
46
|
+
begin
|
47
|
+
configuration = YAML.load_file(@config_file)
|
48
|
+
rescue => e
|
49
|
+
configuration = {}
|
50
|
+
end
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
puts "No answers file at #{file} found, can not continue"
|
47
|
-
exit(23)
|
52
|
+
result = DEFAULT.merge(configuration || {})
|
53
|
+
result[:password] ||= PasswordManager.new.password
|
54
|
+
result
|
48
55
|
end
|
49
|
-
|
50
|
-
@config_file = file
|
51
|
-
@config_dir = File.join(Dir.pwd, 'config')
|
52
56
|
end
|
53
57
|
|
54
58
|
def modules
|
@@ -58,12 +62,11 @@ class Configuration
|
|
58
62
|
def params_default_values
|
59
63
|
@params_default_values ||= begin
|
60
64
|
@logger.info "Parsing default values from puppet modules..."
|
61
|
-
|
62
|
-
|
63
|
-
@logger.debug `echo '#{includes} dump_values(#{params})' | puppet apply --modulepath #{modules_path} 2>&1`
|
65
|
+
command = PuppetCommand.new("#{includes} dump_values(#{params})").append('2>&1').command
|
66
|
+
@logger.debug `#{command}`
|
64
67
|
unless $?.exitstatus == 0
|
65
68
|
@logger.error "Could not get default values, cannot continue"
|
66
|
-
exit(
|
69
|
+
exit(:default_error)
|
67
70
|
end
|
68
71
|
@logger.info "... finished"
|
69
72
|
YAML.load_file(File.join(@config_dir, 'default_values.yaml'))
|
@@ -83,15 +86,16 @@ class Configuration
|
|
83
86
|
end
|
84
87
|
|
85
88
|
def config_header
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
def gem_root_path
|
90
|
-
@gem_root_path ||= File.join(File.dirname(__FILE__), '../../')
|
89
|
+
files = [ app[:config_header_file], File.join(KafoConfigure.gem_root, '/config/config_header.txt') ].compact
|
90
|
+
file = files.select { |f| File.exists?(f) }.first
|
91
|
+
@config_header ||= file.nil? ? '' : File.read(file)
|
91
92
|
end
|
92
93
|
|
93
|
-
def store(data)
|
94
|
-
|
94
|
+
def store(data, file = nil)
|
95
|
+
filename = file || answer_file
|
96
|
+
FileUtils.touch filename
|
97
|
+
File.chmod 0600, filename
|
98
|
+
File.open(filename, 'w') { |file| file.write(config_header + YAML.dump(data)) }
|
95
99
|
end
|
96
100
|
|
97
101
|
private
|
data/lib/kafo/kafo_configure.rb
CHANGED
@@ -6,22 +6,23 @@ require 'kafo/logger'
|
|
6
6
|
require 'kafo/string_helper'
|
7
7
|
require 'kafo/wizard'
|
8
8
|
require 'kafo/system_checker'
|
9
|
+
require 'kafo/puppet_command'
|
9
10
|
|
10
11
|
class KafoConfigure < Clamp::Command
|
11
12
|
include StringHelper
|
12
13
|
attr_reader :logger
|
13
14
|
|
14
15
|
class << self
|
15
|
-
attr_accessor :config, :root_dir
|
16
|
+
attr_accessor :config, :root_dir, :config_file, :gem_root, :temp_config_file
|
16
17
|
end
|
17
18
|
|
18
19
|
def initialize(*args)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
self.class.config_file = config_file
|
21
|
+
self.class.config = Configuration.new(self.class.config_file)
|
22
|
+
self.class.root_dir = File.expand_path(self.class.config.app[:installer_dir])
|
23
|
+
self.class.gem_root = File.join(File.dirname(__FILE__), '../../')
|
24
|
+
Logger.setup
|
25
|
+
@logger = Logging.logger.root
|
25
26
|
set_env
|
26
27
|
super
|
27
28
|
set_parameters
|
@@ -33,38 +34,85 @@ class KafoConfigure < Clamp::Command
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def execute
|
36
|
-
|
37
|
+
catch :exit do
|
38
|
+
parse_cli_arguments
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
+
if verbose?
|
41
|
+
logger.appenders = logger.appenders << ::Logging.appenders.stdout(:layout => Logger::COLOR_LAYOUT)
|
42
|
+
end
|
43
|
+
|
44
|
+
unless SystemChecker.check
|
45
|
+
puts "Your system does not meet configuration criteria"
|
46
|
+
exit(:invalid_system)
|
47
|
+
end
|
48
|
+
|
49
|
+
if interactive?
|
50
|
+
wizard = Wizard.new
|
51
|
+
wizard.run
|
52
|
+
else
|
53
|
+
unless validate_all
|
54
|
+
puts "Error during configuration, exiting"
|
55
|
+
exit(:invalid_values)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if dont_save_answers?
|
60
|
+
self.class.temp_config_file = temp_config_file
|
61
|
+
store_params(temp_config_file)
|
62
|
+
else
|
63
|
+
store_params
|
64
|
+
end
|
65
|
+
run_installation
|
40
66
|
end
|
67
|
+
return self
|
68
|
+
end
|
41
69
|
|
42
|
-
|
43
|
-
|
44
|
-
|
70
|
+
def self.run
|
71
|
+
catch :exit do
|
72
|
+
return super
|
45
73
|
end
|
74
|
+
Kernel.exit(self.exit_code) # fail in initialize
|
75
|
+
end
|
76
|
+
|
77
|
+
def exit_code
|
78
|
+
self.class.exit_code
|
79
|
+
end
|
46
80
|
|
47
|
-
|
48
|
-
|
49
|
-
|
81
|
+
def self.exit(code)
|
82
|
+
@exit_code = translate_exit_code(code)
|
83
|
+
throw :exit
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.exit_code
|
87
|
+
@exit_code ||= 0
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.translate_exit_code(code)
|
91
|
+
return code if code.is_a? Fixnum
|
92
|
+
error_codes = { :invalid_system => 20,
|
93
|
+
:invalid_values => 21,
|
94
|
+
:manifest_error => 22,
|
95
|
+
:no_answer_file => 23,
|
96
|
+
:unknown_module => 24,
|
97
|
+
:defaults_error => 25 }
|
98
|
+
if error_codes.has_key? code
|
99
|
+
return error_codes[code]
|
50
100
|
else
|
51
|
-
|
52
|
-
puts "Error during configuration, exiting"
|
53
|
-
exit(21)
|
54
|
-
end
|
101
|
+
raise "Unknown code #{code}"
|
55
102
|
end
|
56
|
-
|
57
|
-
store_params unless dont_save_answers?
|
58
|
-
run_installation
|
59
103
|
end
|
60
104
|
|
61
105
|
private
|
62
106
|
|
107
|
+
def exit(code)
|
108
|
+
self.class.exit(code)
|
109
|
+
end
|
110
|
+
|
63
111
|
def params
|
64
112
|
@params ||= config.modules.map(&:params).flatten
|
65
113
|
rescue ModuleName => e
|
66
114
|
puts e
|
67
|
-
exit(
|
115
|
+
exit(:unknown_module)
|
68
116
|
end
|
69
117
|
|
70
118
|
def set_parameters
|
@@ -81,7 +129,7 @@ class KafoConfigure < Clamp::Command
|
|
81
129
|
self.class.option ['-v', '--verbose'], :flag, 'Display log on STDOUT instead of progressbar'
|
82
130
|
self.class.option ['-n', '--noop'], :flag, 'Run puppet in noop mode?', :default => false
|
83
131
|
self.class.option ['-d', '--dont-save-answers'], :flag, 'Skip saving answers to answers.yaml?',
|
84
|
-
:default =>
|
132
|
+
:default => !!config.app[:dont_save_answers]
|
85
133
|
|
86
134
|
config.modules.each do |mod|
|
87
135
|
self.class.option d("--[no-]enable-#{mod.name}"),
|
@@ -110,9 +158,9 @@ class KafoConfigure < Clamp::Command
|
|
110
158
|
end
|
111
159
|
end
|
112
160
|
|
113
|
-
def store_params
|
161
|
+
def store_params(file = nil)
|
114
162
|
data = Hash[config.modules.map { |mod| [mod.name, mod.enabled? ? mod.params_hash : false] }]
|
115
|
-
config.store(data)
|
163
|
+
config.store(data, file)
|
116
164
|
end
|
117
165
|
|
118
166
|
def validate_all(logging = true)
|
@@ -127,9 +175,7 @@ class KafoConfigure < Clamp::Command
|
|
127
175
|
|
128
176
|
def run_installation
|
129
177
|
exit_code = 0
|
130
|
-
modules_path = "modules:#{File.join(File.dirname(__FILE__), '../../modules')}"
|
131
178
|
options = [
|
132
|
-
"--modulepath #{modules_path}",
|
133
179
|
'--verbose',
|
134
180
|
'--debug',
|
135
181
|
'--color=false',
|
@@ -138,7 +184,8 @@ class KafoConfigure < Clamp::Command
|
|
138
184
|
]
|
139
185
|
options.push '--noop' if noop?
|
140
186
|
begin
|
141
|
-
|
187
|
+
command = PuppetCommand.new('include kafo_configure', options).command
|
188
|
+
PTY.spawn(command) do |stdin, stdout, pid|
|
142
189
|
begin
|
143
190
|
stdin.each { |line| puppet_log(line) }
|
144
191
|
rescue Errno::EIO
|
@@ -154,6 +201,7 @@ class KafoConfigure < Clamp::Command
|
|
154
201
|
exit_code = e.status.exitstatus
|
155
202
|
end
|
156
203
|
logger.info "Puppet has finished, bye!"
|
204
|
+
FileUtils.rm(temp_config_file, :force => true)
|
157
205
|
exit(exit_code)
|
158
206
|
end
|
159
207
|
|
@@ -185,4 +233,13 @@ class KafoConfigure < Clamp::Command
|
|
185
233
|
ENV['FACTER_fqdn'] = facter_hostname
|
186
234
|
end
|
187
235
|
|
236
|
+
def config_file
|
237
|
+
return CONFIG_FILE if defined?(CONFIG_FILE) && File.exists?(CONFIG_FILE)
|
238
|
+
return '/etc/kafo/kafo.yaml' if File.exists?('/etc/kafo/kafo.yaml')
|
239
|
+
File.join(Dir.pwd, 'config', 'kafo.yaml')
|
240
|
+
end
|
241
|
+
|
242
|
+
def temp_config_file
|
243
|
+
@temp_config_file ||= "/tmp/kafo_answers_#{rand(1_000_000)}.yaml"
|
244
|
+
end
|
188
245
|
end
|
data/lib/kafo/logger.rb
CHANGED
@@ -1,41 +1,46 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
require 'logging'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
:error => :red,
|
9
|
-
:fatal => [:white, :on_red]
|
10
|
-
},
|
11
|
-
:date => :blue,
|
12
|
-
:logger => :cyan,
|
13
|
-
:line => :yellow,
|
14
|
-
:file => :yellow,
|
15
|
-
:method => :yellow
|
16
|
-
)
|
17
|
-
pattern = "[%5l %d %c] %m\n"
|
18
|
-
COLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => 'bright')
|
19
|
-
NOCOLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => nil)
|
4
|
+
class Logger
|
5
|
+
pattern = "[%5l %d %c] %m\n"
|
6
|
+
COLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => 'bright')
|
7
|
+
NOCOLOR_LAYOUT = Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => nil)
|
20
8
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
:truncate => true
|
9
|
+
Logging.color_scheme('bright',
|
10
|
+
:levels => {
|
11
|
+
:info => :green,
|
12
|
+
:warn => :yellow,
|
13
|
+
:error => :red,
|
14
|
+
:fatal => [:white, :on_red]
|
15
|
+
},
|
16
|
+
:date => :blue,
|
17
|
+
:logger => :cyan,
|
18
|
+
:line => :yellow,
|
19
|
+
:file => :yellow,
|
20
|
+
:method => :yellow
|
34
21
|
)
|
35
|
-
# set owner and group (it's ignored if attribute is nil)
|
36
|
-
FileUtils.chown Configuration::KAFO[:log_owner], Configuration::KAFO[:log_group], filename
|
37
|
-
rescue ArgumentError => e
|
38
|
-
puts "File #{filename} not writeable, won't log anything to file!"
|
39
|
-
end
|
40
22
|
|
41
|
-
|
23
|
+
def self.setup
|
24
|
+
begin
|
25
|
+
FileUtils.mkdir_p(KafoConfigure.config.app[:log_dir], :mode => 0750)
|
26
|
+
rescue Errno::EACCES => e
|
27
|
+
puts "No permissions to create log dir #{KafoConfigure.config.app[:log_dir]}"
|
28
|
+
end
|
29
|
+
|
30
|
+
logger = Logging.logger.root
|
31
|
+
filename = "#{KafoConfigure.config.app[:log_dir]}/configure.log"
|
32
|
+
begin
|
33
|
+
logger.appenders = ::Logging.appenders.rolling_file('configure',
|
34
|
+
:filename => filename,
|
35
|
+
:layout => NOCOLOR_LAYOUT,
|
36
|
+
:truncate => true
|
37
|
+
)
|
38
|
+
# set owner and group (it's ignored if attribute is nil)
|
39
|
+
FileUtils.chown KafoConfigure.config.app[:log_owner], KafoConfigure.config.app[:log_group], filename
|
40
|
+
rescue ArgumentError => e
|
41
|
+
puts "File #{filename} not writeable, won't log anything to file!"
|
42
|
+
end
|
43
|
+
|
44
|
+
logger.level = KafoConfigure.config.app[:log_level]
|
45
|
+
end
|
46
|
+
end
|
data/lib/kafo/params/password.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
class PuppetCommand
|
2
|
+
def initialize(command, options = [])
|
3
|
+
@command = command
|
4
|
+
@options = options.push("--modulepath #{modules_path}")
|
5
|
+
@logger = Logging.logger.root
|
6
|
+
end
|
7
|
+
|
8
|
+
def command
|
9
|
+
custom_answer_file = if KafoConfigure.temp_config_file.nil?
|
10
|
+
''
|
11
|
+
else
|
12
|
+
"$kafo_answer_file=\"#{KafoConfigure.temp_config_file}\""
|
13
|
+
end
|
14
|
+
|
15
|
+
result = [
|
16
|
+
"echo '$kafo_config_file=\"#{KafoConfigure.config_file}\" #{custom_answer_file} #{@command}'",
|
17
|
+
" | ",
|
18
|
+
"puppet apply #{@options.join(' ')} #{@suffix}"
|
19
|
+
].join
|
20
|
+
@logger.debug result
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
24
|
+
def append(suffix)
|
25
|
+
@suffix = suffix
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def modules_path
|
32
|
+
[
|
33
|
+
KafoConfigure.config.app[:installer_dir] + '/modules',
|
34
|
+
File.join(KafoConfigure.gem_root, 'modules')
|
35
|
+
].join(':')
|
36
|
+
end
|
37
|
+
end
|
data/lib/kafo/puppet_module.rb
CHANGED
@@ -43,7 +43,7 @@ class PuppetModule
|
|
43
43
|
rescue ConfigurationException => e
|
44
44
|
puts "Unable to continue because of:"
|
45
45
|
puts e.message
|
46
|
-
exit(
|
46
|
+
KafoConfigure.exit(:manifest_error)
|
47
47
|
end
|
48
48
|
|
49
49
|
def validations(param = nil)
|
@@ -64,7 +64,7 @@ class PuppetModule
|
|
64
64
|
|
65
65
|
# mapping from configuration with stringified keys
|
66
66
|
def mapping
|
67
|
-
@mapping ||= Hash[
|
67
|
+
@mapping ||= Hash[KafoConfigure.config.app[:mapping].map { |k, v| [k.to_s, v] }]
|
68
68
|
end
|
69
69
|
|
70
70
|
# custom module directory name
|
data/lib/kafo/string_helper.rb
CHANGED
@@ -10,7 +10,7 @@ module StringHelper
|
|
10
10
|
alias :u :underscore
|
11
11
|
|
12
12
|
def with_prefix(param)
|
13
|
-
prefix =
|
13
|
+
prefix = KafoConfigure.config.app[:no_prefix] ? '' : "#{d(param.module_name)}-"
|
14
14
|
"#{prefix}#{d(param.name)}"
|
15
15
|
end
|
16
16
|
|
data/lib/kafo/version.rb
CHANGED
@@ -0,0 +1,202 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
202
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
name 'puppetlabs-create_resources'
|
2
|
+
version '0.0.1'
|
3
|
+
source 'git://github.com/puppetlabs/puppetlabs-create_resources.git'
|
4
|
+
author 'puppetlabs'
|
5
|
+
license 'Apache Version 2.0'
|
6
|
+
summary 'Function to dynamically create resources from hashes'
|
7
|
+
description 'Function to dynamically create resources from hashes'
|
8
|
+
project_page 'https://github.com/puppetlabs/puppetlabs-create_resources'
|
9
|
+
|
10
|
+
## Add dependencies, if any:
|
11
|
+
# dependency 'username/name', '>= 1.2.0'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
build status of unit tests:
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/puppetlabs/puppetlabs-create_resources.png?branch=master)](https://travis-ci.org/puppetlabs/puppetlabs-create_resources)
|
4
|
+
|
5
|
+
|
6
|
+
- License - Apache Version 2.0
|
7
|
+
- Copyright - Puppetlabs 2011
|
8
|
+
|
9
|
+
*NOTE* - this has exists in 2.7.x core, it has been published seperately
|
10
|
+
so that it can be used with 2.6.x
|
11
|
+
|
12
|
+
This module contains a custom function for puppet that can be used to dynamically add resources to the catalog.
|
13
|
+
|
14
|
+
I wrote this to use with an external node classifier that consumes YAML.
|
15
|
+
|
16
|
+
The yaml specifies classes and passes hashes to those classes as parameters
|
17
|
+
|
18
|
+
classes:
|
19
|
+
webserver::instances:
|
20
|
+
instances:
|
21
|
+
instance1:
|
22
|
+
foo: bar
|
23
|
+
instance2:
|
24
|
+
foo: blah
|
25
|
+
|
26
|
+
Then puppet code can consume the hash parameters and convert then into resources
|
27
|
+
|
28
|
+
class webserver::instances (
|
29
|
+
$instances = {}
|
30
|
+
) {
|
31
|
+
create_resources('webserver::instance', $instances)
|
32
|
+
}
|
33
|
+
|
34
|
+
Now I can dynamically determine how webserver instances are deployed to nodes
|
35
|
+
by updating the YAML files.
|
36
|
+
|
37
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'puppetlabs_spec_helper/rake_tasks'
|
@@ -0,0 +1,75 @@
|
|
1
|
+
Puppet::Parser::Functions::newfunction(:create_resources, :doc => <<-'ENDHEREDOC') do |args|
|
2
|
+
Converts a hash into a set of resources and adds them to the catalog.
|
3
|
+
|
4
|
+
This function takes two mandatory arguments: a resource type, and a hash describing
|
5
|
+
a set of resources. The hash should be in the form `{title => {parameters} }`:
|
6
|
+
|
7
|
+
# A hash of user resources:
|
8
|
+
$myusers = {
|
9
|
+
'nick' => { uid => '1330',
|
10
|
+
group => allstaff,
|
11
|
+
groups => ['developers', 'operations', 'release'], }
|
12
|
+
'dan' => { uid => '1308',
|
13
|
+
group => allstaff,
|
14
|
+
groups => ['developers', 'prosvc', 'release'], }
|
15
|
+
}
|
16
|
+
|
17
|
+
create_resources(user, $myusers)
|
18
|
+
|
19
|
+
A third, optional parameter may be given, also as a hash:
|
20
|
+
|
21
|
+
$defaults = {
|
22
|
+
'ensure' => present,
|
23
|
+
'provider' => 'ldap',
|
24
|
+
}
|
25
|
+
|
26
|
+
create_resources(user, $myusers, $defaults)
|
27
|
+
|
28
|
+
The values given on the third argument are added to the parameters of each resource
|
29
|
+
present in the set given on the second argument. If a parameter is present on both
|
30
|
+
the second and third arguments, the one on the second argument takes precedence.
|
31
|
+
|
32
|
+
This function can be used to create defined resources and classes, as well
|
33
|
+
as native resources.
|
34
|
+
ENDHEREDOC
|
35
|
+
raise ArgumentError, ("create_resources(): wrong number of arguments (#{args.length}; must be 2 or 3)") if args.length < 2 || args.length > 3
|
36
|
+
|
37
|
+
# figure out what kind of resource we are
|
38
|
+
type_of_resource = nil
|
39
|
+
type_name = args[0].downcase
|
40
|
+
if type_name == 'class'
|
41
|
+
type_of_resource = :class
|
42
|
+
else
|
43
|
+
if resource = Puppet::Type.type(type_name.to_sym)
|
44
|
+
type_of_resource = :type
|
45
|
+
elsif resource = find_definition(type_name.downcase)
|
46
|
+
type_of_resource = :define
|
47
|
+
else
|
48
|
+
raise ArgumentError, "could not create resource of unknown type #{type_name}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
# iterate through the resources to create
|
52
|
+
defaults = args[2] || {}
|
53
|
+
args[1].each do |title, params|
|
54
|
+
params = Puppet::Util.symbolizehash(defaults.merge(params))
|
55
|
+
raise ArgumentError, 'params should not contain title' if(params[:title])
|
56
|
+
case type_of_resource
|
57
|
+
# JJM The only difference between a type and a define is the call to instantiate_resource
|
58
|
+
# for a defined type.
|
59
|
+
when :type, :define
|
60
|
+
p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource)
|
61
|
+
{:name => title}.merge(params).each do |k,v|
|
62
|
+
p_resource.set_parameter(k,v)
|
63
|
+
end
|
64
|
+
if type_of_resource == :define then
|
65
|
+
resource.instantiate_resource(self, p_resource)
|
66
|
+
end
|
67
|
+
compiler.add_resource(self, p_resource)
|
68
|
+
when :class
|
69
|
+
klass = find_hostclass(title)
|
70
|
+
raise ArgumentError, "could not find hostclass #{title}" unless klass
|
71
|
+
klass.ensure_in_catalog(self, params)
|
72
|
+
compiler.catalog.add_class(title)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'puppetlabs_spec_helper/module_spec_helper'
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'puppet'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'function for dynamically creating resources' do
|
5
|
+
|
6
|
+
def setup_scope
|
7
|
+
@compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => 'production'))
|
8
|
+
if Puppet.version =~ /^3\./
|
9
|
+
@scope = Puppet::Parser::Scope.new(@compiler)
|
10
|
+
else
|
11
|
+
@scope = Puppet::Parser::Scope.new(:compiler => @compiler)
|
12
|
+
end
|
13
|
+
@topscope = @topscope
|
14
|
+
@scope.parent = @topscope
|
15
|
+
Puppet::Parser::Functions.function(:create_resources)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'basic tests' do
|
19
|
+
|
20
|
+
before :each do
|
21
|
+
setup_scope
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should exist" do
|
25
|
+
Puppet::Parser::Functions.function(:create_resources).should == "function_create_resources"
|
26
|
+
end
|
27
|
+
it 'should require two arguments' do
|
28
|
+
lambda { @scope.function_create_resources(['foo']) }.should raise_error(ArgumentError, 'create_resources(): wrong number of arguments (1; must be 2 or 3)')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'when creating native types' do
|
33
|
+
before :each do
|
34
|
+
Puppet[:code]=
|
35
|
+
'
|
36
|
+
class t {}
|
37
|
+
notify{test:}
|
38
|
+
'
|
39
|
+
setup_scope
|
40
|
+
end
|
41
|
+
it 'empty hash should not cause resources to be added' do
|
42
|
+
|
43
|
+
@scope.function_create_resources(['file', {}])
|
44
|
+
@compiler.catalog.resources.size == 1
|
45
|
+
end
|
46
|
+
it 'should be able to add' do
|
47
|
+
@scope.function_create_resources(['file', {'/etc/foo'=>{'ensure'=>'present'}}])
|
48
|
+
@compiler.catalog.resource(:file, "/etc/foo")['ensure'].should == 'present'
|
49
|
+
end
|
50
|
+
it 'should accept multiple types' do
|
51
|
+
type_hash = {}
|
52
|
+
type_hash['notify'] = {'message' => 'one'}
|
53
|
+
type_hash['user'] = {'home' => true}
|
54
|
+
@scope.function_create_resources(['notify', type_hash])
|
55
|
+
@compiler.catalog.resource(:notify, "notify")['message'].should == 'one'
|
56
|
+
@compiler.catalog.resource(:notify, "user")['home'].should == true
|
57
|
+
end
|
58
|
+
it 'should fail to add non-existing type' do
|
59
|
+
lambda {
|
60
|
+
@scope.function_create_resources(['fooper', {}]) }.should raise_error(ArgumentError, 'could not create resource of unknown type fooper')
|
61
|
+
end
|
62
|
+
before :each do
|
63
|
+
Puppet[:code]=
|
64
|
+
'
|
65
|
+
class t {}
|
66
|
+
define foo($one){notify{$name: message => $one}}
|
67
|
+
notify{test:}
|
68
|
+
'
|
69
|
+
setup_scope
|
70
|
+
Puppet::Parser::Functions.function(:create_resources)
|
71
|
+
end
|
72
|
+
it 'should be able to create defined resoure types' do
|
73
|
+
@scope.function_create_resources(['foo', {'blah'=>{'one'=>'two'}}])
|
74
|
+
# still have to compile for this to work...
|
75
|
+
# I am not sure if this constraint ruins the tests
|
76
|
+
@scope.compiler.compile
|
77
|
+
@compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
|
78
|
+
end
|
79
|
+
it 'should fail if defines are missing params' do
|
80
|
+
@scope.function_create_resources(['foo', {'blah'=>{}}])
|
81
|
+
lambda { @scope.compiler.compile }.should raise_error(Puppet::ParseError, /Must pass one/)
|
82
|
+
end
|
83
|
+
it 'should be able to add multiple defines' do
|
84
|
+
hash = {}
|
85
|
+
hash['blah'] = {'one' => 'two'}
|
86
|
+
hash['blaz'] = {'one' => 'three'}
|
87
|
+
@scope.function_create_resources(['foo', hash])
|
88
|
+
# still have to compile for this to work...
|
89
|
+
# I am not sure if this constraint ruins the tests
|
90
|
+
@scope.compiler.compile
|
91
|
+
@compiler.catalog.resource(:notify, "blah")['message'].should == 'two'
|
92
|
+
@compiler.catalog.resource(:notify, "blaz")['message'].should == 'three'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
describe 'when creating classes' do
|
96
|
+
before :each do
|
97
|
+
Puppet[:code]=
|
98
|
+
'
|
99
|
+
class t {}
|
100
|
+
class bar($one){notify{test: message => $one}}
|
101
|
+
notify{tester:}
|
102
|
+
'
|
103
|
+
setup_scope
|
104
|
+
Puppet::Parser::Functions.function(:create_resources)
|
105
|
+
end
|
106
|
+
it 'should be able to create classes' do
|
107
|
+
@scope.function_create_resources(['class', {'bar'=>{'one'=>'two'}}])
|
108
|
+
@scope.compiler.compile
|
109
|
+
@compiler.catalog.resource(:notify, "test")['message'].should == 'two'
|
110
|
+
@compiler.catalog.resource(:class, "bar").should_not be_nil#['message'].should == 'two'
|
111
|
+
end
|
112
|
+
it 'should fail to create non-existing classes' do
|
113
|
+
lambda { @scope.function_create_resources(['class', {'blah'=>{'one'=>'two'}}]) }.should raise_error(ArgumentError ,'could not find hostclass blah')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
$users3 = {
|
2
|
+
'dannyboy454' => {
|
3
|
+
'user_name'=>'dlfjdslkf',
|
4
|
+
'ensure'=>present,
|
5
|
+
'require' => 'User[foobar]',
|
6
|
+
'before' => 'User[sally-mae]'
|
7
|
+
},
|
8
|
+
'bobby-joe' => {'ensure'=>present}
|
9
|
+
}
|
10
|
+
create_resources('create_resources::user', $users3)
|
11
|
+
|
12
|
+
# TODO - types are not applied in main stage
|
13
|
+
$users = {
|
14
|
+
'sally-mae' =>
|
15
|
+
{'ensure' => 'present',
|
16
|
+
'require' => 'User[bobby-joe]'
|
17
|
+
}
|
18
|
+
}
|
19
|
+
create_resources('user', $users)
|
20
|
+
|
21
|
+
user { 'foobar':
|
22
|
+
ensure => present,
|
23
|
+
require => User['bobby-joe']
|
24
|
+
}
|
25
|
+
|
26
|
+
$classes = {
|
27
|
+
'create_resources' => {
|
28
|
+
'ensure' => 'present'
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
create_resources('class', $classes)
|
33
|
+
|
34
|
+
class create_resources(
|
35
|
+
$ensure,
|
36
|
+
$user_name=$name
|
37
|
+
){
|
38
|
+
user{$name: ensure => $ensure}
|
39
|
+
notify{$user_name:}
|
40
|
+
}
|
41
|
+
|
42
|
+
define create_resources::user(
|
43
|
+
$ensure,
|
44
|
+
$user_name=$operatingsystem
|
45
|
+
){
|
46
|
+
user{$name: ensure => $ensure}
|
47
|
+
notify{$user_name:}
|
48
|
+
}
|
@@ -7,12 +7,9 @@
|
|
7
7
|
#
|
8
8
|
module Puppet::Parser::Functions
|
9
9
|
newfunction(:class_name, :type => :rvalue) do |args|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
else
|
14
|
-
args[0]
|
15
|
-
end
|
10
|
+
mapping = YAML.load_file(lookupvar('kafo_config_file'))[:mapping]
|
11
|
+
mod = args[0].to_sym
|
12
|
+
mapping[mod].nil? ? mod : "#{mapping[mod][:dir_name]}::#{mapping[mod][:manifest_name]}"
|
16
13
|
end
|
17
14
|
end
|
18
15
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module Puppet::Parser::Functions
|
4
4
|
newfunction(:dump_values) do |args|
|
5
5
|
data = Hash[args.map { |arg| [arg, lookupvar(arg)] }]
|
6
|
-
|
6
|
+
dump_dir = YAML.load_file(lookupvar('kafo_config_file'))[:default_values_dir]
|
7
|
+
File.open("#{dump_dir}/default_values.yaml", 'w') { |file| file.write(YAML.dump(data)) }
|
7
8
|
end
|
8
9
|
end
|
9
|
-
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Loads a answer file
|
2
|
+
#
|
3
|
+
# it can be specified either as a $kafo_answer_file variable or it's read from config file
|
4
|
+
module Puppet::Parser::Functions
|
5
|
+
newfunction(:load_kafo_answer_file, :type => :rvalue) do |args|
|
6
|
+
answer_file = lookupvar('kafo_answer_file')
|
7
|
+
if answer_file && !answer_file.empty?
|
8
|
+
answer_file
|
9
|
+
else
|
10
|
+
YAML.load_file(lookupvar('kafo_config_file'))[:answer_file]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -4,14 +4,10 @@
|
|
4
4
|
# $modulepath/config/answers.yaml
|
5
5
|
# /etc/kafo-configure/answers.yaml
|
6
6
|
#
|
7
|
-
class kafo_configure
|
8
|
-
$answers = undef
|
9
|
-
) {
|
7
|
+
class kafo_configure {
|
10
8
|
|
11
9
|
$password = load_kafo_password()
|
12
|
-
$params = loadanyyaml(
|
13
|
-
"/etc/kafo-configure/answers.yaml",
|
14
|
-
"config/answers.yaml")
|
10
|
+
$params = loadanyyaml(load_kafo_answer_file())
|
15
11
|
$keys = kafo_ordered(hash_keys($params))
|
16
12
|
|
17
13
|
kafo_configure::yaml_to_class { $keys: }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kafo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -134,6 +134,7 @@ extra_rdoc_files: []
|
|
134
134
|
files:
|
135
135
|
- bin/kafo-configure
|
136
136
|
- bin/kafofy
|
137
|
+
- config/kafo.yaml
|
137
138
|
- config/kafo.yaml.example
|
138
139
|
- config/config_header.txt
|
139
140
|
- lib/kafo/puppet_module.rb
|
@@ -143,6 +144,7 @@ files:
|
|
143
144
|
- lib/kafo/validator.rb
|
144
145
|
- lib/kafo/exceptions.rb
|
145
146
|
- lib/kafo/system_checker.rb
|
147
|
+
- lib/kafo/puppet_command.rb
|
146
148
|
- lib/kafo/wizard.rb
|
147
149
|
- lib/kafo/params/password.rb
|
148
150
|
- lib/kafo/params/string.rb
|
@@ -158,6 +160,7 @@ files:
|
|
158
160
|
- lib/kafo.rb
|
159
161
|
- modules/kafo_configure/lib/puppet/parser/functions/decrypt.rb
|
160
162
|
- modules/kafo_configure/lib/puppet/parser/functions/is_hash.rb
|
163
|
+
- modules/kafo_configure/lib/puppet/parser/functions/load_kafo_answer_file.rb
|
161
164
|
- modules/kafo_configure/lib/puppet/parser/functions/class_name.rb
|
162
165
|
- modules/kafo_configure/lib/puppet/parser/functions/dump_values.rb
|
163
166
|
- modules/kafo_configure/lib/puppet/parser/functions/load_kafo_password.rb
|
@@ -166,6 +169,15 @@ files:
|
|
166
169
|
- modules/kafo_configure/lib/puppet/parser/functions/loadanyyaml.rb
|
167
170
|
- modules/kafo_configure/manifests/yaml_to_class.pp
|
168
171
|
- modules/kafo_configure/manifests/init.pp
|
172
|
+
- modules/create_resources/README.md
|
173
|
+
- modules/create_resources/Modulefile
|
174
|
+
- modules/create_resources/tests/users.pp
|
175
|
+
- modules/create_resources/Gemfile
|
176
|
+
- modules/create_resources/Rakefile
|
177
|
+
- modules/create_resources/lib/puppet/parser/functions/create_resources.rb
|
178
|
+
- modules/create_resources/LICENSE
|
179
|
+
- modules/create_resources/spec/unit/puppet/parser/functions/create_resources_spec.rb
|
180
|
+
- modules/create_resources/spec/spec_helper.rb
|
169
181
|
- LICENSE.txt
|
170
182
|
- Rakefile
|
171
183
|
- README.md
|