class Salt::Matrix::Gitrepo

Gitrepo class provides the low level interface to a Git repository.

Public Class Methods

new(remote, name, ref, dirname) click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 15
def initialize(remote, name, ref, dirname)
  @ref = ref
  @remote = remote
  @dirname = dirname
  @formuladir = File.join(dirname, name)
  @repodir = File.join(dirname, '.cache', name)
  @name = name
  
  # Grab the last settings if they exist.
  @settings = {}
  if File.exist? "#{@formuladir}/.salt-matrix.yaml"
    @settings = YAML.load_file("#{@formuladir}/.salt-matrix.yaml")
  end
  
  @git = nil
  if Dir.exist? @repodir
    @git = Git.open @repodir
  end
end

Public Instance Methods

status() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 71
def status
  # Reported conditions:
  #
  #   :absent         Nothing currently exists
  #   :mismatched     Formula is not derived from repo
  #   :badrepo        Cloned repo is not what is specified
  #   :outdated       Expected, but needs to be sync'ed
  #   :insync         Expected and upto date
  
  if !Dir.exist? @formuladir or !Dir.exist? @repodir
    return :absent
  end
  
  
    
  if @settings.empty?
    return :mismatched
  elsif @settings[:remote] != @remote
    return :badrepo
  end
  
  # Check the repo settings
  unless @git.nil?
    @git.fetch
    if @settings[:remote] != @git.remote.url
      return :badrepo
    elsif @settings[:sha] != @git.object(@ref).sha
      return :outdated
    else
      return :insync
    end
  else
    # Ideally this should never get returned!
    return :invalid
  end
end
sync() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 36
def sync
  
  case status
  when :absent
    puts "#{"%-30s" % @name}" + "Cloning".yellow
    replicate_repo
    puts "#{"%-30s" % @name}" + "Syncing".cyan
    replicate_formula
  when :mismatched
    puts "#{"%-30s" % @name}" + "Syncing".cyan
    replicate_formula
  when :badrepo
    puts "#{"%-30s" % @name}" + "Kill repo".yellow
    destroy_repo
    puts "#{"%-30s" % @name}" + "Kill formula".yellow
    destroy_formula
    puts "#{"%-30s" % @name}" + "Cloning".yellow
    replicate_repo
    puts "#{"%-30s" % @name}" + "Syncing".cyan
    replicate_formula
  when :outdated
    puts "#{"%-30s" % @name}" + "Updating repo".cyan
    update_repo
    puts "#{"%-30s" % @name}" + "Syncing".cyan
    replicate_formula
  when :insync
    puts "#{"%-30s" % @name}" + "OK".green
  else
    puts "#{"%-30s" % @name}" + "Invalid State".red
  end

  save_settings
end

Private Instance Methods

destroy_formula() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 117
def destroy_formula
  FileUtils.rm_r @formuladir
end
destroy_repo() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 112
def destroy_repo
  FileUtils.rm_r @repodir
  @git = nil
end
replicate_formula() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 153
def replicate_formula
  # Locate the formula directory and copy it
  #    for now we treat the name as the formula directory name
  if Dir.exist? File.join(@repodir, @name)
    system("rsync -a --delete --exclude .salt-matrix.yaml #{File.join(@repodir, @name)} #{@dirname}")
    #FileUtils.cp_r(File.join(@repodir, @name), @dirname)
    @settings[:name] = @name
    @settings[:remote] = @remote
    @settings[:ref] = @ref
  else
    puts "#{"%-30s" % @name}" + "Invalid formula".red
  end
end
replicate_repo() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 121
def replicate_repo
  unless Dir.exists? "#{@dirname}/.cache"
    Dir.mkdir "#{@dirname}/.cache"
  end
  
  # clone to the .cache directory
  Dir.chdir "#{@dirname}/.cache"
  begin
    if @git.nil?
      Salt::Matrix.debug "Cloning: #{@remote}"
      @git = Git.clone(@remote, @name)
    end
  rescue Exception => e
    puts "Exception: #{e}"
    puts e.backtrace.join("\n")
    puts e.inspect
  end
  
  # Checkout to the correct revsion
  Salt::Matrix.debug "Checking out: #{@ref}"
  @git.checkout @ref
  
  # Record the Git ID
  @settings[:sha] = @git.object("HEAD").sha
end
save_settings() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 168
def save_settings
  if Dir.exist? @formuladir
    File.open("#{@formuladir}/.salt-matrix.yaml", 'w') do |fh|
      fh.write @settings.to_yaml
    end
  end
end
update_repo() click to toggle source
# File lib/salt/matrix/gitrepo.rb, line 147
def update_repo
  @git.fetch
  @git.pull
end