Class: CookbooksFetcher

Inherits:
EasyLogger show all
Defined in:
lib/cookbooks_fetcher.rb

Instance Method Summary collapse

Methods inherited from EasyLogger

#log, #log_level

Constructor Details

#initialize(facility_log_level) ⇒ CookbooksFetcher

constructor method



4
5
6
7
# File 'lib/cookbooks_fetcher.rb', line 4

def initialize(facility_log_level)
  @facility_log_level = facility_log_level
  super(facility_log_level)
end

Instance Method Details

#fetch(cookbooks_src, cookbooks_dest = '/usr/src/chef-cookbooks', archive = true) ⇒ Object



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

def fetch(cookbooks_src, cookbooks_dest='/usr/src/chef-cookbooks', archive=true)
  logger = EasyLogger.new(@facility_log_level)    
  if Process.uid != 0
    cookbooks_dest = "#{File.expand_path("~")}/chef-cookbooks"
  end
  system("mkdir -p #{cookbooks_dest}")
  logger.log "Cookbooks destination: #{cookbooks_dest}.", [ 'verbose', 'debug' ]
  repos_name = File.basename(cookbooks_src).gsub('.git', '')
  logger.log "Inspecting repos, '#{repos_name}'..."
  if File.exists?("#{cookbooks_dest}/#{repos_name}/.git")
    logger.log "Pulling '#{repos_name}' (instead of cloning)..", 'verbose'
    pull_cmd = "cd #{cookbooks_dest}/#{repos_name} && git pull 2>&1"
    pull = "[git] " + `#{pull_cmd}`; result=$?.success?
    if result
      logger.log pull
    else
      logger.log pull, 'error'
      raise "Failed to pull git repository!"
    end
  elsif archive
    system("cd #{cookbooks_dest}; git clone --depth=1 #{cookbooks_src}")
    #system("git archive --format=tar --remote=#{opts[:fetch]} master | tar -xf -")
  else
    system("cd #{cookbooks_dest}; git clone #{cookbooks_src}")
  end
end