puppetize 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ require 'fileutils'
2
2
  require 'erb'
3
3
  require 'pp'
4
4
  require 'open4'
5
+ require 'find'
5
6
  require 'puppetize/puppetize'
6
7
  require 'puppetize/command_line'
7
8
  require 'puppetize/stdlib'
@@ -8,25 +8,7 @@ module Puppetize
8
8
  # Check distribution dependency
9
9
  puts "\nSorry, Distribution not supported, only redhat based at the moment \n\n" unless File.exist?('/etc/redhat-release')
10
10
 
11
- # Create config dir if it doesn't exists:
12
- @config_dir = File.join(File.expand_path('~'),'.puppetize')
13
- @module_dir = File.join(File.expand_path('~'),'puppetize-module')
14
- @rpm_db = File.join(@config_dir,'rpmlist.db')
15
- FileUtils.mkdir @config_dir if Dir.glob(@config_dir).empty?
16
-
17
- end
18
-
19
- def init
20
-
21
- rpm_list = cmd "rpm -qa"
22
- File.open(@rpm_db, 'w') {|f| f.write(rpm_list) }
23
-
24
- end
25
-
26
- def build
27
-
28
11
  # Verify necessary commands are installed:
29
- #
30
12
 
31
13
  commands = ['puppet','git']
32
14
 
@@ -40,26 +22,83 @@ module Puppetize
40
22
 
41
23
  if ! test.include? true
42
24
  puts "ERROR - #{command} is not installed or not available on default path"
43
- exit -1
25
+ exit 1
44
26
  end
45
27
 
46
28
  rescue => e
47
29
 
48
30
  STDERR.puts "ERROR - #{e}"
49
- exit -1
31
+ exit 1
32
+
33
+ end
34
+
35
+ end
36
+
37
+ # Create config dir if it doesn't exists:
38
+ @config_dir = File.join(File.expand_path('~'),'.puppetize')
39
+ @module_dir = File.join(File.expand_path('~'),'puppetize-module')
40
+ @rpm_db = File.join(@config_dir,'rpmlist.db')
41
+ @trackdirs = [
42
+ {
43
+ :path => '/etc',
44
+ :ignoredir => [
45
+ 'group',
46
+ 'gshadow',
47
+ 'ld.so.cache',
48
+ 'passwd',
49
+ 'shadow',
50
+ 'group-',
51
+ 'gshadow-',
52
+ 'passwd-',
53
+ 'shadow-',
54
+ 'rc.d'
55
+ ]
56
+ }
57
+ ]
58
+
59
+ FileUtils.mkdir @config_dir if Dir.glob(@config_dir).empty?
60
+
61
+ end
62
+
63
+ def init
64
+
65
+ # Remember what RPMs are installed
66
+
67
+ rpm_list = cmd "rpm -qa"
68
+ File.open(@rpm_db, 'w') {|f| f.write(rpm_list) }
69
+
70
+ # Add a git repo to track the filesystem
71
+
72
+ @trackdirs.each do |track|
73
+
74
+ File.open(File.join(track[:path],".gitignore"),'w') do |f|
75
+ f.write(track[:ignoredir].join("\n"))
76
+ end
77
+
78
+ Dir.chdir track[:path] do
79
+
80
+ cmd 'git init .'
81
+ cmd 'git add .'
82
+ cmd "git commit -am'puppetize: Initial Commit'"
50
83
 
51
84
  end
52
85
 
53
86
  end
54
87
 
88
+ end
89
+
90
+ def build
91
+
92
+ ##################################
55
93
  # Generate a standard empty module
56
94
 
57
- cmd "puppet module generate puppetize-module"
58
-
95
+ cmd "cd /root && puppet module generate puppetize-module"
96
+ FileUtils.mkdir File.join(@module_dir,'files')
97
+ FileUtils.mkdir File.join(@module_dir,'templates')
59
98
 
60
99
  #################################
61
100
  # Generate the Package resources:
62
-
101
+
63
102
  rpm_list = cmd("rpm -qa").split("\n")
64
103
  init_rpm_list = File.read(@rpm_db).split("\n")
65
104
  puppet_rpm_list = rpm_list - init_rpm_list
@@ -74,19 +113,105 @@ module Puppetize
74
113
  end
75
114
 
76
115
  # Generate install.pp based on ERB template
116
+
77
117
  template = File.join(File.dirname(__FILE__), '..', '..', 'tpl', 'install.pp.erb')
78
- renderer = ERB.new(File.read(template))
118
+ renderer = ERB.new(File.read(template),nil,'>')
79
119
  output = renderer.result(binding)
80
120
  File.open(File.join(@module_dir,'manifests','install.pp'),'w') {|f| f.write(output)}
81
121
 
122
+
82
123
  ######################################
83
124
  # Generate the configuration resources
84
-
125
+
126
+ puppet_files = Array.new
127
+
128
+ @trackdirs.each do |track|
129
+
130
+ Dir.chdir track[:path] do
131
+
132
+ file_list = %x[git status --porcelain].split("\n").map {|f| f[3..-1]}
133
+
134
+ flisttmp = []
135
+
136
+ # If a file is a directory recurse inside as git does not provide that listing
137
+ file_list.each do |file|
138
+ if File.directory? file
139
+ Find.find(File.join(track[:path],file)) do |f|
140
+ flisttmp << f.split(File.join(track[:path],''))[1] if File.file? f
141
+ end
142
+ end
143
+ end
144
+
145
+ # Merge both lists:
146
+ file_list = (file_list + flisttmp).uniq
147
+
148
+ # If the file does not belong to any package or has been modified then include it in the list
149
+
150
+ file_list.each do |file|
151
+
152
+ filename = File.join(track[:path],file)
153
+
154
+ # If file does not belong to any package or it belongs but
155
+ # it has been modified then puppetize it
156
+
157
+ if not system "rpm -qf #{filename} > /dev/null 2>&1" or system "rpm -Vf #{filename}|grep #{filename} > /dev/null 2>&1"
158
+ puppet_files << filename
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+ # Copy the files to the files directory:
168
+
169
+ puppet_files.each do |file|
170
+
171
+ if File.file? file
172
+ FileUtils.cp file, File.join(@module_dir,'files')
173
+ FileUtils.cp file, File.join(@module_dir,'templates',file.split('/').last + ".erb")
174
+ end
175
+
176
+ end
177
+
178
+ puppet_file_list = Array.new
179
+
180
+ puppet_files.each do |file|
181
+
182
+ stat = File.stat file
183
+ mode = stat.mode.to_s(8)[-3..-1]
184
+ owner = Etc.getpwuid(stat.uid).name
185
+ group = Etc.getgrgid(stat.gid).name
186
+ type = File.file?(file) ? "present" : "directory"
187
+
188
+ puppet_file_list << {
189
+ :path => file,
190
+ :ensure => type,
191
+ :owner => owner,
192
+ :group => group,
193
+ :mode => mode,
194
+ :name => file.split('/').last
195
+ }
196
+
197
+ end
198
+
199
+ # Generate config.pp based on ERB template
200
+ template = File.join(File.dirname(__FILE__), '..', '..', 'tpl', 'config.pp.erb')
201
+ renderer = ERB.new(File.read(template),nil,'>')
202
+ output = renderer.result(binding)
203
+ File.open(File.join(@module_dir,'manifests','config.pp'),'w') {|f| f.write(output)}
204
+
85
205
  end
86
206
 
87
207
  def reset
88
208
 
89
- puts "reset"
209
+ @trackdirs.each do |track|
210
+ FileUtils.rm_rf File.join(track[:path],'.git') if File.exists? File.join(track[:path],'.git')
211
+ end
212
+
213
+ FileUtils.rm @rpm_db if File.exists? @rpm_db
214
+ FileUtils.rm_rf 'puppetize-module' if File.exists? 'puppetize-module'
90
215
 
91
216
  end
92
217
 
@@ -1,3 +1,3 @@
1
1
  module Puppetize
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,17 @@
1
+
2
+ class module::config {
3
+
4
+ <% for file in puppet_file_list %>
5
+ file {'<%= file[:path] %>':
6
+ ensure => <%= file[:ensure] %>,
7
+ owner => '<%= file[:owner] %>',
8
+ group => '<%= file[:group] %>',
9
+ mode => '<%= file[:mode] %>',
10
+ <% if file[:ensure] == 'present' %>
11
+ content => template("${module_name}/<%= file[:name] %>.erb")
12
+ source => "puppet:///modules/${module_name}/<%= file[:name] %>"
13
+ <% end %>
14
+ }
15
+ <% end %>
16
+
17
+ }
@@ -0,0 +1,10 @@
1
+
2
+ class module::install {
3
+
4
+ <% for pkg in packagelist %>
5
+ package {'<%= pkg[0] %>':
6
+ ensure => '<%= pkg[1] %>-<%= pkg[2] %>',
7
+ }
8
+ <% end %>
9
+
10
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetize
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
10
- version: 0.0.4
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Juan Breinlinger
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-11-04 00:00:00 +00:00
18
+ date: 2013-11-05 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -33,6 +33,21 @@ dependencies:
33
33
  version: "2.0"
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: open4
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 1
47
+ - 0
48
+ version: "1.0"
49
+ type: :runtime
50
+ version_requirements: *id002
36
51
  description: Puppetize is a tool that can generate complete modules from systems
37
52
  email: juan.brein@breins.net
38
53
  executables:
@@ -48,6 +63,8 @@ files:
48
63
  - lib/puppetize/stdlib.rb
49
64
  - lib/puppetize/command_line.rb
50
65
  - bin/puppetize
66
+ - tpl/config.pp.erb
67
+ - tpl/install.pp.erb
51
68
  - README.md
52
69
  - LICENSE
53
70
  - CONTRIBUTORS