-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
gen_puppet.rb
executable file
·155 lines (131 loc) · 4.63 KB
/
gen_puppet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env ruby
# frozen_string_literal: true
# this tool was used to create the stub of all resource defines in bareos
# json source files were generated by 'bareos-<component> -xs'
require 'json'
class String
def underscore
gsub(%r{::}, '/').gsub(%r{([A-Z]+)([A-Z][a-z])}, '\1_\2').gsub(%r{([a-z\d])([A-Z])}, '\1_\2').tr('-', '_').downcase
end
def titelize
gsub(%r{([A-Z]+)([A-Z][a-z])}, '\1 \2').gsub(%r{([a-z\d])([A-Z])}, '\1 \2')
# split(/(?=[A-Z])/).join(' ')
end
end
def datatype(type)
type.downcase
end
def bareos_option(directive, schema)
schema['required'] = false unless schema['required']
type = datatype(schema['datatype'])
" [$#{directive.underscore}, '#{directive.titelize}', '#{type}', #{schema['required']}]"
end
spec = JSON.parse File.read(ARGV[0])
resource = ARGV[1]
# for c in Catalog Job Storage Client Director Pool FileSet JobDefs Schedule Messages Console Profile Counter Device; do ./gen_puppet.rb bareos-dir.json bareos-dir $c > ../puppet/environments/sandbox/tech/modules/bareos/manifests/director/$(echo $c | awk '{print tolower($0)}').pp; don
if resource == '--list'
puts spec['resource'][spec['component']].keys.join ' '
exit 0
end
res = spec['resource'][spec['component']][resource].sort.to_h.delete_if { |_, schema| schema['deprecated'] }
comp_name = case spec['component']
when 'bareos-dir'
'director'
when 'bareos-fd'
'client'
when 'bareos-sd'
'storage'
when 'bareos-tray-monitor'
'monitor'
else
spec['component']
end
# print header/doc
puts "# == Define: bareos::#{comp_name}::#{resource.downcase}
#
# == Parameters
# [*ensure*]
# present or absent the config file.
#"
res.each do |directive, schema|
next if directive == 'Name'
schema['required'] = false unless schema['required']
default = 'Not set'
default = schema['default_value'] if schema['default_value']
description = directive.titelize
description = "#{directive.titelize}: #{schema['description']}" if schema['description']
puts "# [*#{directive.underscore}*]"
puts "# #{description}"
puts '#'
puts '# May be specified as Array.' if schema['datatype'] =~ %r{_LIST$} || schema['datatype'] == 'MESSAGES' || schema['datatype'] == 'SCHEDULE_RUN_COMMAND'
puts "# Bareos Datatype: #{datatype schema['datatype']}"
puts "# Bareos Default: #{default}"
puts "# Required: #{schema['required']}"
puts '#'
end
# print
puts "define bareos::#{comp_name}::#{resource.downcase} (
$ensure = present,
"
# prepare enum/type checks and gen parameter
enums = {}
res_requires = {}
res.each do |directive, schema|
next if directive == 'Name'
schema['datatype'] = "#{schema['datatype']}_LIST" if schema['datatype'] == 'MESSAGES' || schema['datatype'] == 'SCHEDULE_RUN_COMMAND'
if schema['datatype'] =~ %r{_TYPE$}
enums[directive] = schema
# overwrite datatype for type checking
schema['datatype'] = 'type'
end
res_requires[directive] = schema if schema['datatype'] == 'RES' && schema['required']
value = 'undef'
value = schema['default_value'] if schema['default_value'] && schema['required']
puts " $#{directive.underscore} = #{value},"
end
# print end of params
puts ") {
include bareos::#{comp_name}
$_resource = '#{resource}'
$_resource_dir = '#{resource.downcase}'
unless $ensure in [ 'present', 'absent' ] {
fail('Invalid value for ensure')
}
if $ensure == 'present' {
"
# pre-check enums
enums.each do |directive, _schema|
puts " unless $#{directive.underscore} == undef or $#{directive.underscore} in [ undef ] {"
puts " fail(\"Invalid value for #{directive.underscore}: ${#{directive.underscore}}\")"
puts ' }'
end
unless res_requires.empty?
puts ' $_require_resource = ['
res_requires.map do |directive, _schema|
puts " Bareos::#{comp_name.capitalize}::#{directive.downcase.capitalize}[$#{directive.underscore}],"
end
puts ' ]'
end
# print option parser/validater
puts ' $_options = bareos_options('
puts res.map { |directive, schema| bareos_option(directive, schema) }.join(",\n")
puts ' )'
# print footer
if res_requires.empty?
puts ' }'
else
puts ' } else {
$_require_resource = undef
}'
end
puts "
file { \"${::bareos::#{comp_name}::config_dir}/${_resource_dir}/${name}.conf\":
ensure => $ensure,
mode => $bareos::file_mode,
owner => $bareos::file_owner,
group => $bareos::file_group,
content => template('bareos/resource.erb'),
notify => Service[$bareos::#{comp_name}::service_name],"
puts ' require => $_require_resource' unless res_requires.empty?
puts ' }
}'