Class: Salt::Matrix::Gitrepo

Inherits:
Object
  • Object
show all
Defined in:
lib/salt/matrix/gitrepo.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(remote, name, ref, dirname) ⇒ Gitrepo

Returns a new instance of Gitrepo.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Method Details

#statusObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#syncObject



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
62
63
64
65
66
67
68
# 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