Class: Ops::Revision

Inherits:
Object
  • Object
show all
Defined in:
lib/ops/revision.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_headers = {}, opts = Ops.config) ⇒ Revision

Returns a new instance of Revision.



5
6
7
8
9
# File 'lib/ops/revision.rb', line 5

def initialize(new_headers={}, opts = Ops.config)
  @file_root = opts.file_root.to_s # convert to string in case they pass us a Pathname
  @environment = opts.environment
  @headers = new_headers
end

Instance Attribute Details

#branch_source=(value) ⇒ Object

Sets the attribute branch_source

Parameters:

  • value

    the value to set the attribute branch_source to.



3
4
5
# File 'lib/ops/revision.rb', line 3

def branch_source=(value)
  @branch_source = value
end

Instance Method Details

#chomp(file) ⇒ Object



59
60
61
# File 'lib/ops/revision.rb', line 59

def chomp(file)
  File.read(file).chomp
end

#current_dirObject



41
42
43
# File 'lib/ops/revision.rb', line 41

def current_dir
  file_root.split('/').last
end

#deploy_dateObject



95
96
97
98
99
100
101
102
103
# File 'lib/ops/revision.rb', line 95

def deploy_date
  @deploy_date ||= if version_file?
                     get_time version_file
                   elsif development?
                     'Live'
                   else
                     'Unknown (VERSION file is missing)'
                   end
end

#development?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ops/revision.rb', line 75

def development?
  environment == 'development'
end

#environmentObject



71
72
73
# File 'lib/ops/revision.rb', line 71

def environment
  @environment
end

#file_rootObject



67
68
69
# File 'lib/ops/revision.rb', line 67

def file_root
  @file_root
end

#get_previous_by_timeObject



25
26
27
# File 'lib/ops/revision.rb', line 25

def get_previous_by_time
  get_previous_versions.sort { |a, b| a[:time] <=> b[:time] }
end

#get_previous_versionsObject



29
30
31
32
33
34
35
# File 'lib/ops/revision.rb', line 29

def get_previous_versions
  Dir["#{path}/../*"].each_with_object([]) do |dir, array|
    next if dir =~ /#{current_dir}$/
    version, revision = File.join(dir, 'VERSION'), File.join(dir, 'REVISION')
    array << stats_hash(version: version, revision: revision) if File.exists?(version) && File.exists?(revision)
  end
end

#get_revision(file) ⇒ Object



55
56
57
# File 'lib/ops/revision.rb', line 55

def get_revision(file)
  chomp file
end

#get_time(file) ⇒ Object



63
64
65
# File 'lib/ops/revision.rb', line 63

def get_time(file)
  File.stat(file).mtime
end

#get_version(file) ⇒ Object



51
52
53
# File 'lib/ops/revision.rb', line 51

def get_version(file)
  chomp(file).gsub('^{}', '')
end

#headersObject



115
116
117
# File 'lib/ops/revision.rb', line 115

def headers
  @headers.select{|k,v| k.match(/^[-A-Z_].*$/) }
end

#last_commitObject



105
106
107
108
109
110
111
112
113
# File 'lib/ops/revision.rb', line 105

def last_commit
  @last_commit ||= if revision_file?
                     chomp revision_file
                   elsif development? && `git show` =~ /^commit (.*)$/
                     $1
                   else
                     'Unknown (REVISION file is missing)'
                   end
end

#pathObject



37
38
39
# File 'lib/ops/revision.rb', line 37

def path
  File.absolute_path file_root
end

#previous_versionsObject



21
22
23
# File 'lib/ops/revision.rb', line 21

def previous_versions
  @previous_versions ||= get_previous_by_time
end

#revision_fileObject



87
88
89
# File 'lib/ops/revision.rb', line 87

def revision_file
  @revision_file ||= File.join(file_root, 'REVISION')
end

#revision_file?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/ops/revision.rb', line 91

def revision_file?
  File.exists? revision_file
end

#stats_hash(files) ⇒ Object



45
46
47
48
49
# File 'lib/ops/revision.rb', line 45

def stats_hash(files)
  { version:  get_version(files[:version]),
    revision: get_revision(files[:revision]),
    time:     get_time(files[:revision]) }
end

#version_fileObject



79
80
81
# File 'lib/ops/revision.rb', line 79

def version_file
  @version_file ||= File.join(file_root, 'VERSION')
end

#version_file?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ops/revision.rb', line 83

def version_file?
  File.exists? version_file
end

#version_or_branchObject



11
12
13
14
15
16
17
18
19
# File 'lib/ops/revision.rb', line 11

def version_or_branch
  @version ||= if version_file?
                 chomp(version_file).gsub('^{}', '')
               elsif development? && branch_source.call =~ /^\* (.*)$/
                 $1
               else
                 'Unknown (VERSION file is missing)'
               end
end