foreman_templates 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b73812edc9e1e160c23eb201ca2b207488ef2857
4
- data.tar.gz: 1b6def802713308391f6bd3439dd0d15c362cc51
3
+ metadata.gz: acae354280dfe45ef7fffd417add76632686f5c6
4
+ data.tar.gz: 64324ccdd05c546ff663fa2eb2f2059a0f809e54
5
5
  SHA512:
6
- metadata.gz: d227eb8fd8b8765b07e1910df3956ca8dbac472312cce92fa078ff4784f0288701ebc4eef10abb218eed8bb4f7017a325e0bc8c500b3679ba0ffed411b0393ad
7
- data.tar.gz: 5dde067950478119a01ae2e35ff9bc3859f9f86303c758b97281f49fb31ac00af41aa144dc89d8cd77f23dc8bda079ba491950f8e9498f70a7ec04c9617a46e1
6
+ metadata.gz: a96282ae4f045fc3642df1f532cfa113c93bd7960b0ad8a1b79a8eb5d022d1908174231c0834fd338dbeca4d9248cf28a7786c8f755faf9afb8a1e3e2390abab
7
+ data.tar.gz: 0c30b8e9eb049e4c8f510efd7e97b795b5b4d17445697bb6a58bc1f2b7b57af9baf4a8b2931516d62d784b9a607b1118a7891b7010f8973de1b681809439a575
data/README.md CHANGED
@@ -44,6 +44,7 @@ template will be automatically associated with the OS
44
44
 
45
45
  * verbose => Print extra information during the run [false]
46
46
  * repo => Sync templates from a different Git repo [https://github.com/theforeman/community-templates]
47
+ * branch => Branch in Git repo [default branch]
47
48
  * prefix => The string all imported templates should begin with [Community]
48
49
  * dirname => The directory within the git tree containing the templates [/]
49
50
  * filter => Import names matching this regex (case-insensitive; snippets are not filtered)
@@ -1,3 +1,5 @@
1
+ require 'diffy'
2
+
1
3
  module ForemanTemplates
2
4
  #Inherit from the Rails module of the parent app (Foreman), not the plugin.
3
5
  #Thus, inhereits from ::Rails::Engine and not from Rails::Engine
@@ -1,3 +1,3 @@
1
1
  module ForemanTemplates
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/templates.rake CHANGED
@@ -1,114 +1,166 @@
1
- require 'open4'
1
+ require 'yaml'
2
+ require 'diffy'
3
+
4
+ class NoKindError < Exception ; end
5
+
6
+ def map_oses
7
+ oses = if @metadata['oses']
8
+ @metadata['oses'].map do |os|
9
+ @db_oses.map { |db| db.to_label =~ /^#{os}/ ? db : nil}
10
+ end.flatten.compact
11
+ else
12
+ []
13
+ end
14
+ puts " Operatingsystem Associations: #{oses.map(&:fullname).join(',')}" if verbose
15
+ return oses
16
+ end
17
+
18
+ def update_template
19
+ # Get template type
20
+ unless kind = TemplateKind.find_by_name(@metadata['kind'])
21
+ puts " Error: Unknown template type '#{@metadata['kind']}'"
22
+ raise NoKindError
23
+ end
24
+
25
+ db_template = ConfigTemplate.find_or_initialize_by_name(@name)
26
+ data = {
27
+ :template => @text,
28
+ :snippet => false,
29
+ :template_kind_id => kind.id
30
+ }
31
+
32
+ if db_template.new_record?
33
+ data[:operatingsystem_ids] = map_oses.map(&:id)
34
+ string = "Created"
35
+ else
36
+ string = "Updated"
37
+ end
38
+
39
+ if @text == db_template.template
40
+ puts " No change to Template #{ ( 'id' + db_template.id ) rescue ''}:#{@name}"
41
+ 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
46
+ end
47
+ end
48
+
49
+ def update_ptable
50
+ oses = map_oses
51
+
52
+ db_ptable = Ptable.find_or_initialize_by_name(@name)
53
+ data = { :layout => @text }
54
+ if db_ptable.new_record?
55
+ data[:os_family] = oses.map(&:family).uniq.first
56
+ #no idea why this fails...
57
+ #data[:operatingsystems] = oses,
58
+ string = "Created"
59
+ else
60
+ string = "Updated"
61
+ end
62
+
63
+ if @text == db_ptable.layout
64
+ puts " No change to Ptable #{ ( 'id' + db_ptable.id ) rescue ''}:#{@name}"
65
+ 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
70
+ end
71
+
72
+ end
73
+
74
+ def update_snippet
75
+ db_snippet = ConfigTemplate.find_or_initialize_by_name(@name)
76
+ data = {
77
+ :template => @text,
78
+ :snippet => true
79
+ }
80
+ string = db_snippet.new_record? ? "Created" : "Updated"
81
+
82
+ if @text == db_snippet.template
83
+ puts " No change to Snippet #{ 'id' + db_snippet.id rescue ''}:#{@name}" if verbose
84
+ 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
89
+ end
90
+ end
2
91
 
3
92
  desc <<-END_DESC
4
93
  Synchronize templates from a git repo
5
94
  END_DESC
6
95
  namespace :templates do
7
96
  task :sync => :environment do
8
-
9
97
  # Available options:
10
98
  #* verbose => Print extra information during the run [false]
11
99
  #* repo => Sync templates from a different Git repo [https://github.com/theforeman/community-templates]
100
+ #* branch => Branch in Git repo [default branch]
12
101
  #* prefix => The string all imported templates should begin with [Community]
13
102
  #* dirname => The directory within the git tree containing the templates [/]
14
103
  #* filter => Import names matching this regex (case-insensitive; snippets are not filtered)
15
104
 
16
105
  verbose = ( ENV['verbose'] and ENV['verbose'] != 'false' ) ? true : false
17
106
  repo = ENV['repo'] ? ENV['repo'] : "https://github.com/theforeman/community-templates.git"
107
+ branch = ENV['branch'] ? "-b #{ENV['branch']}" : ""
18
108
  prefix = ENV['prefix'] ? ENV['prefix'] : 'Community'
19
109
  dirname = ENV['dirname'] ? ENV['dirname'] : '/'
20
110
  filter = ENV['filter'] ? ENV['filter'] : nil
21
111
 
22
112
  # Check out the community templates to a temp location
23
113
  dir = Dir.mktmpdir
24
- command = "git clone #{repo} #{dir}"
114
+ command = "git clone #{branch} #{repo} #{dir}"
25
115
 
26
116
  status = `#{command}`
27
117
  puts "#{status}" if verbose
28
118
 
29
- TemplateKind.all.each do |kind|
30
- Dir["#{dir}#{dirname}/**/*#{kind.name}.erb"].each do |template|
31
- os = File.expand_path('../..',template).split('/').last.capitalize
32
- release = File.expand_path('..',template).split('/').last.capitalize
33
- filename = template.split('/').last
34
- title = filename.split('.').first
35
- name = "#{prefix} #{os}(#{release}) #{title}"
36
- next if filter and not name.match(/#{filter}/i)
37
-
38
- oses = begin
39
- by_type = Operatingsystem.find_all_by_type(os)
40
- by_type.empty? ? Operatingsystem.where("name LIKE ?", os) : by_type
41
- rescue
42
- []
43
- end.delete_if {|o| !(o.major =~ /#{release}/i or o.release_name =~ /#{release}/i )}
44
-
45
- osfamily = oses.map{|o|o.type}.uniq.first
46
-
47
- db_template = ConfigTemplate.find_or_initialize_by_name(name)
48
- data = {
49
- :template => File.read(template),
50
- :snippet => false,
51
- :template_kind_id => kind.id
52
- }
53
- if db_template.new_record?
54
- data[:operatingsystem_ids] = oses.map {|o| o.id}
55
- string = "Created"
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]
56
131
  else
57
- string = "Updated"
132
+ break
58
133
  end
59
-
60
- puts "#{string} Template id #{db_template.id || "new"}:#{name}" if verbose
61
- db_template.update_attributes(data)
62
134
  end
63
- end
135
+ @metadata = options == "" ? {} : YAML.load(options)
136
+ @text = File.read(template).gsub(/#{strip_metadata}/,'')
64
137
 
65
- Dir["#{dir}#{dirname}/**/*disklayout.erb"].each do |ptable|
66
- os = File.expand_path('../..',ptable).split('/').last.capitalize
67
- release = File.expand_path('..',ptable).split('/').last.capitalize
68
- filename = ptable.split('/').last
138
+ # Get the name and filter
139
+ filename = template.split('/').last
69
140
  title = filename.split('.').first
70
- name = "#{prefix} #{os}(#{release}) #{title}"
141
+ @name = @metadata ['name'] || "#{prefix} #{title}"
71
142
  next if filter and not name.match(/#{filter}/i)
72
143
 
73
- oses = begin
74
- by_type = Operatingsystem.find_all_by_type(os)
75
- by_type.empty? ? Operatingsystem.where("name LIKE ?", os) : by_type
76
- rescue
77
- []
78
- end.delete_if {|o| !(o.major =~ /#{release}/i or o.release_name =~ /#{release}/i )}
79
-
80
- osfamily = oses.map{|o|o.type}.uniq.first
81
-
82
- db_ptable = Ptable.find_or_initialize_by_name(name)
83
- data = { :layout => File.read(ptable) }
84
- if db_ptable.new_record?
85
- data[:os_family] = osfamily
86
- data[:operatingsystems] = oses
87
- string = "Created"
88
- else
89
- string = "Updated"
90
- end
144
+ puts "Parsing: " + template.gsub(/#{dir}#{dirname}/,'') if verbose
91
145
 
92
- puts "#{string} Ptable id #{db_ptable.id || 'new'}:#{name}" if verbose
93
- db_ptable.update_attributes(data)
94
- end
95
-
96
- Dir["#{dir}#{dirname}/snippets/*.erb"].each do |snippet|
97
- name = snippet.split('/').last.gsub(/.erb$/,'')
98
-
99
- db_snippet = ConfigTemplate.find_or_initialize_by_name(name)
100
- data = {
101
- :template => File.read(snippet),
102
- :snippet => true
103
- }
104
- string = db_snippet.new_record? ? "Created" : "Updated"
146
+ unless @metadata['kind']
147
+ puts " Error: Must specify template kind"
148
+ next
149
+ end
105
150
 
106
- puts "#{string} Snippet id #{db_snippet.id || 'new'}:#{name}" if verbose
107
- db_snippet.update_attributes(data)
151
+ case @metadata['kind']
152
+ when 'ptable'
153
+ update_ptable
154
+ when 'snippet'
155
+ update_snippet
156
+ else
157
+ begin
158
+ update_template
159
+ rescue NoKindError
160
+ next
161
+ end
162
+ end
108
163
  end
109
164
 
110
- # TODO do this in ruby
111
- `rm -rf #{dir}`
112
-
113
165
  end
114
166
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sutcliffe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-15 00:00:00.000000000 Z
11
+ date: 2013-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: open4
14
+ name: diffy
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  requirements: []
61
61
  rubyforge_project:
62
- rubygems_version: 2.0.0
62
+ rubygems_version: 2.0.3
63
63
  signing_key:
64
64
  specification_version: 4
65
65
  summary: Template-syncing engine for Foreman