Class: Puppetize::Controler

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetize/puppetize.rb

Instance Method Summary collapse

Constructor Details

#initializeControler

Returns a new instance of Controler.



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
# File 'lib/puppetize/puppetize.rb', line 6

def initialize

  # Check distribution dependency
  puts "\nSorry, Distribution not supported, only redhat based at the moment \n\n" unless File.exist?('/etc/redhat-release') 

  # Verify necessary commands are installed:

  commands = ['puppet','git']

  commands.each do |command|

    begin

      test = ENV['PATH'].split(':').map do |folder| 
        File.exists?(File.join(folder,command)) 
      end

      if ! test.include? true 
        puts "ERROR - #{command} is not installed or not available on default path"
        exit 1
      end

    rescue => e

      STDERR.puts "ERROR - #{e}"
      exit 1

    end

  end

  # Create config dir if it doesn't exists:
  @config_dir = File.join(File.expand_path('~'),'.puppetize')
  @module_dir = File.join(File.expand_path('~'),'puppetize-module')
  @rpm_db = File.join(@config_dir,'rpmlist.db')
  @trackdirs = [
    { 
    :path => '/etc',
    :ignoredir => [
      'group',
      'gshadow',
      'ld.so.cache',
      'passwd',
      'shadow',
      'group-',
      'gshadow-',
      'passwd-',
      'shadow-',
      'rc.d' 
      ]
    }
  ]

  FileUtils.mkdir @config_dir if Dir.glob(@config_dir).empty?

end

Instance Method Details

#buildObject



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/puppetize/puppetize.rb', line 90

def build

  ##################################
  # Generate a standard empty module

  cmd "cd /root && puppet module generate puppetize-module"
  FileUtils.mkdir File.join(@module_dir,'files')
  FileUtils.mkdir File.join(@module_dir,'templates')

  #################################
  # Generate the Package resources:

  rpm_list = cmd("rpm -qa").split("\n")
  init_rpm_list = File.read(@rpm_db).split("\n")
  puppet_rpm_list = rpm_list - init_rpm_list 

  packagelist = []

  puppet_rpm_list.each do |rpm|
    name = cmd("rpm --query --qf %{NAME} #{rpm}")
    version = cmd("rpm --query --qf %{VERSION} #{rpm}")
    release = cmd("rpm --query --qf %{RELEASE} #{rpm}")
    packagelist << [name,version,release]
  end

  # Generate install.pp based on ERB template
  
  template = File.join(File.dirname(__FILE__), '..', '..', 'tpl', 'install.pp.erb')
  renderer = ERB.new(File.read(template),nil,'>')
  output = renderer.result(binding)
  File.open(File.join(@module_dir,'manifests','install.pp'),'w') {|f| f.write(output)}


  ######################################
  # Generate the configuration resources

  puppet_files = Array.new

  @trackdirs.each do |track|

    Dir.chdir track[:path] do

      file_list = %x[git status --porcelain].split("\n").map {|f| f[3..-1]}

      flisttmp = []

      # If a file is a directory recurse inside as git does not provide that listing
      file_list.each do |file|
        if File.directory? file 
          Find.find(File.join(track[:path],file)) do |f|
            flisttmp << f.split(File.join(track[:path],''))[1] if File.file? f
          end
        end
      end

      # Merge both lists:
      file_list = (file_list + flisttmp).uniq

      # If the file does not belong to any package or has been modified then include it in the list

      file_list.each do |file|

        filename = File.join(track[:path],file)

        # If file does not belong to any package or it belongs but 
        # it has been modified then puppetize it

        if not system "rpm -qf #{filename} > /dev/null 2>&1" or system "rpm -Vf #{filename}|grep #{filename} > /dev/null 2>&1"
          puppet_files << filename 
        end

      end

    end

  end

  # Copy the files to the files directory:

  puppet_files.each do |file|

    if File.file? file
      FileUtils.cp file, File.join(@module_dir,'files')
      FileUtils.cp file, File.join(@module_dir,'templates',file.split('/').last + ".erb")
    end

  end

  puppet_file_list = Array.new

  puppet_files.each do |file|

    stat = File.stat file
    mode = stat.mode.to_s(8)[-3..-1]
    owner = Etc.getpwuid(stat.uid).name
    group = Etc.getgrgid(stat.gid).name
    type = File.file?(file) ? "present" : "directory"

    puppet_file_list << {
      :path    => file,
      :ensure  => type,
      :owner   => owner,
      :group   => group,
      :mode    => mode,
      :name    => file.split('/').last
    }

  end

  # Generate config.pp based on ERB template
  template = File.join(File.dirname(__FILE__), '..', '..', 'tpl', 'config.pp.erb')
  renderer = ERB.new(File.read(template),nil,'>')
  output = renderer.result(binding)
  File.open(File.join(@module_dir,'manifests','config.pp'),'w') {|f| f.write(output)}

end

#initObject



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
# File 'lib/puppetize/puppetize.rb', line 63

def init

  # Remember what RPMs are installed

  rpm_list = cmd "rpm -qa"
  File.open(@rpm_db, 'w') {|f| f.write(rpm_list) }

  # Add a git repo to track the filesystem

  @trackdirs.each do |track|

    File.open(File.join(track[:path],".gitignore"),'w') do |f|
      f.write(track[:ignoredir].join("\n"))
    end

    Dir.chdir track[:path] do 

      cmd 'git init .'
      cmd 'git add .'
      cmd "git commit -am'puppetize: Initial Commit'"

    end

  end

end

#resetObject



207
208
209
210
211
212
213
214
215
216
# File 'lib/puppetize/puppetize.rb', line 207

def reset

  @trackdirs.each do |track|
    FileUtils.rm_rf File.join(track[:path],'.git') if File.exists? File.join(track[:path],'.git')
  end

  FileUtils.rm @rpm_db if File.exists? @rpm_db
  FileUtils.rm_rf 'puppetize-module' if File.exists? 'puppetize-module'

end