foreman_templates 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51de5fa77409a6ab12be3efe3f653215118edad9
4
- data.tar.gz: 10d6f8bcf9680670d7fee1ff8b40278f8402f22a
3
+ metadata.gz: c93fb9f63e065e944c894b5eee5c54a32861ee11
4
+ data.tar.gz: b12db24a9e3518c844f2a46ca0714088ba470139
5
5
  SHA512:
6
- metadata.gz: e63a4884b56adc0157813d20d3ead11c532046ff641f6d382cb032273bb6a41d33daa10091edc1247aa3093233ffe95cc1f47792b29400bde06a62aced4b89f5
7
- data.tar.gz: b6c7f79969381f0bfd46203ee974dca796966a9fe36c9e62f4a1a113619eac07a229b1ca5a6dfa6d297aea45eb29cbb318e28df4000ac0b90f8fdd33bde50fbd
6
+ metadata.gz: a58d817020f22029463c89edfb7ebde7444e22a6d14492762fd50c11b63b93eb4b9adaf4f14c378fb9a9ef09f540807cf8ff80507c1cf8272cc2201a08ae2cd0
7
+ data.tar.gz: 6bb6a5454bf2bf53b455a7d663815868e44842a736092e4a3c72c839b9bc4489f24ab87c1e5aca754d00da6c35688be81c35827e0da38f8bff4e82f94c9f7422
@@ -1,3 +1,3 @@
1
1
  module ForemanTemplates
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
data/lib/templates.rake CHANGED
@@ -1,24 +1,33 @@
1
+ require 'fileutils'
1
2
  require 'yaml'
2
3
  require 'diffy'
3
4
 
4
5
  class NoKindError < Exception ; end
5
6
 
7
+ def db_oses
8
+ @db_oses || Operatingsystem.all
9
+ end
10
+
11
+ def metadata(text)
12
+ # Pull out the first erb comment only - /m is for a multiline regex
13
+ extracted = text.match(/<%\#(.+?)%>/m)
14
+ extracted == nil ? {} : YAML.load(extracted[1])
15
+ end
16
+
6
17
  def map_oses
7
18
  oses = if @metadata['oses']
8
19
  @metadata['oses'].map do |os|
9
- @db_oses.map { |db| db.to_label =~ /^#{os}/ ? db : nil}
20
+ db_oses.map { |db| db.to_label =~ /^#{os}/ ? db : nil}
10
21
  end.flatten.compact
11
22
  else
12
23
  []
13
24
  end
14
- puts " Operatingsystem Associations: #{oses.map(&:fullname).join(',')}" if verbose
15
25
  return oses
16
26
  end
17
27
 
18
28
  def update_template
19
29
  # Get template type
20
30
  unless kind = TemplateKind.find_by_name(@metadata['kind'])
21
- puts " Error: Unknown template type '#{@metadata['kind']}'"
22
31
  raise NoKindError
23
32
  end
24
33
 
@@ -29,46 +38,49 @@ def update_template
29
38
  :template_kind_id => kind.id
30
39
  }
31
40
 
41
+ oses = map_oses
32
42
  if db_template.new_record?
33
- data[:operatingsystem_ids] = map_oses.map(&:id)
43
+ data[:operatingsystem_ids] = oses.map(&:id)
34
44
  string = "Created"
35
45
  else
36
46
  string = "Updated"
37
47
  end
38
48
 
39
49
  if @text == db_template.template
40
- puts " No change to Template #{ ( 'id' + db_template.id ) rescue ''}:#{@name}"
50
+ diff = nil
51
+ status = true
52
+ result = " No change to Template #{ ( 'id' + db_template.id ) rescue ''}:#{@name}"
41
53
  else
42
- update = Diffy::Diff.new(db_template.template, @text, :include_diff_info => true).to_s(:color)
43
- db_template.update_attributes(data)
44
- puts " #{string} Template #{ 'id' + db_template.id rescue ''}:#{@name}"
45
- puts update if !db_template.new_record? and verbose
54
+ diff = Diffy::Diff.new(db_template.template, @text, :include_diff_info => true).to_s(:color)
55
+ status = db_template.update_attributes(data)
56
+ result = " #{string} Template #{ 'id' + db_template.id rescue ''}:#{@name}"
57
+ result += "\n Operatingsystem Associations:\n - #{oses.map(&:fullname).join("\n - ")}" if !oses.empty?
46
58
  end
59
+ { :diff => diff, :status => status, :result => result }
47
60
  end
48
61
 
49
62
  def update_ptable
50
- oses = map_oses
51
-
52
63
  db_ptable = Ptable.find_or_initialize_by_name(@name)
53
64
  data = { :layout => @text }
65
+
66
+ oses = map_oses
54
67
  if db_ptable.new_record?
55
68
  data[:os_family] = oses.map(&:family).uniq.first
56
- #no idea why this fails...
57
- #data[:operatingsystems] = oses,
58
69
  string = "Created"
59
70
  else
60
71
  string = "Updated"
61
72
  end
62
73
 
63
74
  if @text == db_ptable.layout
64
- puts " No change to Ptable #{ ( 'id' + db_ptable.id ) rescue ''}:#{@name}"
75
+ diff = nil
76
+ status = true
77
+ result = " No change to Ptable #{ ( 'id' + db_ptable.id ) rescue ''}:#{@name}"
65
78
  else
66
- update = Diffy::Diff.new(db_ptable.layout, @text, :include_diff_info => true).to_s(:color)
67
- db_ptable.update_attributes(data)
68
- puts " #{string} Ptable #{ ( 'id' + db_ptable.id ) rescue ''}:#{@name}"
69
- puts update if !db_ptable.new_record? and verbose
79
+ diff = Diffy::Diff.new(db_ptable.layout, @text, :include_diff_info => true).to_s(:color)
80
+ status = db_ptable.update_attributes(data)
81
+ result = " #{string} Ptable #{ ( 'id' + db_ptable.id ) rescue ''}:#{@name}"
70
82
  end
71
-
83
+ { :diff => diff, :status => status, :result => result }
72
84
  end
73
85
 
74
86
  def update_snippet
@@ -80,13 +92,15 @@ def update_snippet
80
92
  string = db_snippet.new_record? ? "Created" : "Updated"
81
93
 
82
94
  if @text == db_snippet.template
83
- puts " No change to Snippet #{ 'id' + db_snippet.id rescue ''}:#{@name}" if verbose
95
+ diff = nil
96
+ status = true
97
+ result = " No change to Snippet #{ 'id' + db_snippet.id rescue ''}:#{@name}"
84
98
  else
85
- update = Diffy::Diff.new(db_snippet.template, @text, :include_diff_info => true).to_s(:color)
86
- db_snippet.update_attributes(data)
87
- puts " #{string} Snippet #{ ('id' + db_snippet.id) rescue ''}:#{@name}" if verbose
88
- puts update if !db_snippet.new_record? and verbose
99
+ diff = Diffy::Diff.new(db_snippet.template, @text, :include_diff_info => true).to_s(:color)
100
+ status = db_snippet.update_attributes(data)
101
+ result = " #{string} Snippet #{ ('id' + db_snippet.id) rescue ''}:#{@name}"
89
102
  end
103
+ { :diff => diff, :status => status, :result => result }
90
104
  end
91
105
 
92
106
  desc <<-END_DESC
@@ -102,65 +116,82 @@ namespace :templates do
102
116
  #* dirname => The directory within the git tree containing the templates [/]
103
117
  #* filter => Import names matching this regex (case-insensitive; snippets are not filtered)
104
118
 
105
- verbose = ( ENV['verbose'] and ENV['verbose'] != 'false' ) ? true : false
119
+ @verbose = ( ENV['verbose'] and ENV['verbose'] != 'false' ) ? true : false
106
120
  repo = ENV['repo'] ? ENV['repo'] : "https://github.com/theforeman/community-templates.git"
107
121
  branch = ENV['branch'] ? "-b #{ENV['branch']}" : ""
108
- prefix = ENV['prefix'] ? ENV['prefix'] : 'Community'
122
+ prefix = ENV['prefix'] ? ENV['prefix'] : nil
109
123
  dirname = ENV['dirname'] ? ENV['dirname'] : '/'
110
124
  filter = ENV['filter'] ? ENV['filter'] : nil
111
125
 
112
126
  # Check out the community templates to a temp location
113
- dir = Dir.mktmpdir
114
- command = "git clone #{branch} #{repo} #{dir}"
115
-
116
- status = `#{command}`
117
- puts "#{status}" if verbose
118
-
119
- # Cache the list of OSes
120
- @db_oses = Operatingsystem.all
121
-
122
- # Build a list of ERB files to parse
123
- Dir["#{dir}#{dirname}/**/*.erb"].each do |template|
124
- # Parse Metadata in the template
125
- options=""
126
- strip_metadata=""
127
- File.readlines(template).each do |line|
128
- if line =~ /^#/
129
- strip_metadata += line
130
- options += line[1..-1]
131
- else
132
- break
133
- end
134
- end
135
- @metadata = options == "" ? {} : YAML.load(options)
136
- @text = File.read(template).gsub(/#{strip_metadata}/,'')
127
+ begin
128
+ dir = Dir.mktmpdir
129
+ command = "git clone #{branch} #{repo} #{dir}"
137
130
 
138
- # Get the name and filter
139
- filename = template.split('/').last
140
- title = filename.split('.').first
141
- @name = @metadata ['name'] || "#{prefix} #{title}"
142
- next if filter and not name.match(/#{filter}/i)
131
+ status = `#{command}`
132
+ puts "#{status}" if @verbose
143
133
 
144
- puts "Parsing: " + template.gsub(/#{dir}#{dirname}/,'') if verbose
134
+ # Build a list of ERB files to parse
135
+ Dir["#{dir}#{dirname}/**/*.erb"].each do |template|
136
+ @text = File.read(template)
137
+ @metadata = metadata(@text)
145
138
 
146
- unless @metadata['kind']
147
- puts " Error: Must specify template kind"
148
- next
149
- end
139
+ # Get the name and filter
140
+ filename = template.split('/').last
141
+ title = filename.split('.').first
142
+ @name = @metadata ['name'] || title
143
+ @name = [prefix, @name].compact.join(' ')
144
+ next if filter and not @name.match(/#{filter}/i)
145
+
146
+ puts "Parsing: " + template.gsub(/#{dir}#{dirname}/,'') if @verbose
147
+
148
+ unless @metadata['kind']
149
+ puts " Error: Must specify template kind"
150
+ next
151
+ end
150
152
 
151
- case @metadata['kind']
152
- when 'ptable'
153
- update_ptable
154
- when 'snippet'
155
- update_snippet
156
- else
157
153
  begin
158
- update_template
154
+ case @metadata['kind']
155
+ when 'ptable'
156
+ data = update_ptable
157
+ when 'snippet'
158
+ data = update_snippet
159
+ else
160
+ data = update_template
161
+ end
162
+
163
+ if data[:status] == true && @verbose
164
+ puts data[:result]
165
+ puts data[:diff]
166
+ elsif data[:status] == false
167
+ puts "Error with #{@name}"
168
+ puts data[:result]
169
+ end
159
170
  rescue NoKindError
171
+ puts " Error: Unknown template type '#{@metadata['kind']}'"
160
172
  next
161
173
  end
162
174
  end
175
+ ensure
176
+ FileUtils.remove_entry_secure(dir)
163
177
  end
164
178
 
165
179
  end
166
180
  end
181
+
182
+ # Setup Tests
183
+ namespace :test do
184
+ desc "Test ForemanTemplates plugin"
185
+ Rake::TestTask.new(:templates) do |t|
186
+ test_dir = File.join(File.dirname(__FILE__), '..', 'test')
187
+ t.libs << ["test",test_dir]
188
+ t.pattern = "#{test_dir}/**/*_test.rb"
189
+ end
190
+ end
191
+ Rake::Task[:test].enhance do
192
+ Rake::Task['test:templates'].invoke
193
+ end
194
+ load 'tasks/jenkins.rake'
195
+ Rake::Task["jenkins:unit"].enhance do
196
+ Rake::Task['test:templates'].invoke
197
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sutcliffe