Class: Simp::Rake::Build::Deps

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Pager
Defined in:
lib/simp/rake/build/deps.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ Deps

Returns a new instance of Deps.



175
176
177
178
179
# File 'lib/simp/rake/build/deps.rb', line 175

def initialize( base_dir )
  @base_dir = base_dir
  @verbose = ENV.fetch('SIMP_PKG_verbose','no') == 'yes'
  define_tasks
end

Instance Method Details

#define_tasksObject



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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/simp/rake/build/deps.rb', line 181

def define_tasks
  namespace :deps do
    desc <<-EOM
    Checks out all dependency repos.

    This task used R10k to update all dependencies.

    Arguments:
      * :method  => The update method to use (Default => 'tracking')
           tracking => checks out each dep (by branch) according to Puppetfile.tracking
           stable   => checks out each dep (by ref) according to in Puppetfile.stable
    EOM
    task :checkout, [:method] do |t,args|
      args.with_defaults(:method => 'tracking')

      r10k_helper = R10KHelper.new("Puppetfile.#{args[:method]}")

      r10k_issues = Parallel.map(
        Array(r10k_helper.modules),
        :in_processes => get_cpu_limit,
        :progress => 'Submodule Checkout'
      ) do |mod|
        issues = []

        Dir.chdir(@base_dir) do
          unless File.directory?(mod[:path])
            FileUtils.mkdir_p(mod[:path])
          end

          # Only for known modules...
          unless mod[:status] == :unknown
            # Since r10k is destructive, we're enumerating all valid states
            if [
                :absent,
                :mismatched,
                :outdated,
                :insync,
                :dirty
            ].include?(mod[:r10k_module].status)
              unless mod[:r10k_cache].synced?
                mod[:r10k_cache].sync
              end

              if mod[:status] == :known
                mod[:r10k_module].sync
              else
                # If we get here, the module was dirty and should be skipped
                issues << "#{mod[:name]}: Skipped - #{mod[:status]}"
                next
              end
            else
              issues << "#{mod[:name]}: Skipped - Unknown status type #{mod[:r10k_module].status}"
            end
          end
        end

        issues
      end

      r10k_issues.flatten!

      unless r10k_issues.empty?
        $stderr.puts('='*80)

        unless @verbose
          $stderr.puts('Warning: Some repositories were skipped!')
          $stderr.puts('  * If this is a fresh build, this could be an issue')
          $stderr.puts('  * This is expected if re-running a build')
          $stderr.puts('  * Run with SIMP_PKG_verbose=yes for full details')
        else
          $stderr.puts("R10k Checkout Issues:")
          r10k_issues.each do |issue|
            $stderr.puts("  * #{issue}")
          end
        end

        $stderr.puts('='*80)
      end
    end

    desc <<-EOM
    Get the status of the project Git repositories

    Arguments:
      * :method  => The update method to use (Default => 'tracking')
           tracking => checks out each dep (by branch) according to Puppetfile.tracking
           stable   => checks out each dep (by ref) according to in Puppetfile.stable
    EOM
    task :status, [:method] do |t,args|
      args.with_defaults(:method => 'tracking')
      @dirty_repos = nil

      r10k_helper = R10KHelper.new("Puppetfile.#{args[:method]}")

      mods_with_changes = {}

      r10k_helper.each_module do |mod|
        unless File.directory?(mod[:path])
          $stderr.puts("Warning: '#{mod[:path]}' is not a module...skipping") if File.exist?(mod[:path])
          next
        end

        if mod[:status] != :known
          # Clean up the path a bit for printing
          dirty_path = mod[:path].split(r10k_helper.basedir.to_s).last
          if dirty_path[0].chr == File::SEPARATOR
            dirty_path[0] = ''
          end

          mods_with_changes[mod[:name]] = dirty_path
        end
      end

      if mods_with_changes.empty?
        puts "No repositories have changes."
        @dirty_repos = false
      else
        puts "The following repositories have changes:"
        puts mods_with_changes.map{|k,v| "  + #{k} => #{v}"}.join("\n")

        @dirty_repos = true
      end

      unknown_mods = r10k_helper.unknown_modules
      unless unknown_mods.empty?
        puts "The following modules were unknown:"
        puts unknown_mods.map{|k,v| "  ? #{k}"}.join("\n")
      end
    end

    desc <<-EOM
    Records the current dependencies into Puppetfile.stable.

    Arguments:
      * :source => The source Puppetfile to use (Default => 'tracking')
    EOM
    task :record, [:method] do |t,args|
      args.with_defaults(:source => 'tracking')
      r10k_helper = R10KHelper.new("Puppetfile.#{args[:source]}")

      File.open('Puppetfile.stable','w'){|f| f.puts r10k_helper.puppetfile }
    end

    desc <<-EOM
    Provide a log of changes to all modules from the given top level Git reference.

    Arguments:
      * :ref => The top level git ref to use as the oldest point for all logs.
      * :source => The source Puppetfile to use (Default => 'tracking')
    EOM
    task :changelog, [:ref] do |t,args|
      args.with_defaults(:source => 'tracking')

      r10k_helper = R10KHelper.new("Puppetfile.#{args[:source]}")

      git_logs = Hash.new

      Dir.chdir(r10k_helper.basedir) do
        ref = args[:ref]
        refdate = nil
        begin
          refdate = %x(git log -1 --format=%ai '#{ref}')
          refdate = nil unless $?.success?
        rescue Exception
          #noop
        end

        fail("You must specify a valid reference") unless ref
        fail("Could not find a Git log for #{ref}") unless refdate

        mods_with_changes = {}

        log_output = %x(git log --since='#{refdate}' --stat --reverse).chomp
        git_logs['__SIMP CORE__'] = log_output unless log_output.strip.empty?

        r10k_helper.each_module do |mod|
          if File.directory?(mod[:path])
            Dir.chdir(mod[:path]) do
              log_output = %x(git log --since='#{refdate}' --stat --reverse).chomp
              git_logs[mod[:name]] = log_output unless log_output.strip.empty?
            end
          end
        end

        if git_logs.empty?
          puts( "No changes found for any components since #{refdate}")
        else
          page

          git_logs.keys.sort.each do |mod_name|
            puts <<-EOM
  ========
  #{mod_name}:

  #{git_logs[mod_name].gsub(/^/,'  ')}

            EOM
          end
        end
      end
    end
  end
end