module Bourdain module Checks class CookbooksCheck < Check usage :check, <<-END Check all the underlying cookbooks cookbooks END def initialize cookbook_config super [] return unless require_chef! log.info 'Checking cookbooks, this may take a while...' check_gitlab_cookbooks! cookbook_config check_local_cookbooks! cookbook_config end private def check_cookbook cookbook notifications = [] path = cookbook[:path] stale = [] unless Dir.exists? path ensure_cookbook_exists cookbook return end log.info "Fetching latest work on %{path} from %{repo}..." % cookbook Dir.chdir(path) { %x| git fetch | } if youre_dirty? path log.warn "Your cookbook %{path} is dirty." % cookbook elsif youre_ahead? path log.warn "Your cookbook %{path} is ahead of %{repo}." % cookbook elsif youre_behind? path ensure_cookbook_up_to_date cookbook else log.info "Your cookbook %{path} looks up-to-date." % cookbook end end def ensure_cookbook_exists cookbook return if Dir.exists? cookbook[:path] log.warn "Populating cookbook %{path} with %{repo}..." % cookbook %x| git clone git@#{GITLAB_HOST}:#{cookbook[:repo]} #{cookbook[:path]} | log.info "Your cookbook %{path} is now up-to-date." % cookbook end def ensure_cookbook_up_to_date cookbook return unless Dir.exists? cookbook[:path] log.warn "Pulling in changes to %{path} from %{repo}..." % cookbook Dir.chdir(cookbook[:path]) { %x| git pull | } log.info "Your cookbook %{path} is now up-to-date." % cookbook end def check_gitlab_cookbooks! cookbook_configs cookbook_configs.sort_by! { |cc| cc[:path] } cookbook_configs.each do |cookbook| check_cookbook cookbook end end def check_local_cookbooks! cookbook_configs known_cookbook_paths = cookbook_configs.map { |cc| cc[:path].sub('forks/bjn_', 'forks/') } local_cookbook_paths = Dir[File.join('{apps,bases,forks,libs,realms}', '*')] unknown_cookbook_paths = local_cookbook_paths - known_cookbook_paths unless unknown_cookbook_paths.empty? log.warn "Head's up, I see some cookbooks aren't on GitLab:" unknown_cookbook_paths.each do |cookbook_path| log.warn " #{cookbook_path}" end end end end end end