Class: RightSupport::Deployment::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/deployment/info.rb

Overview

Helper for getting information about an application that has been deployed to a server. Currently this only supports apps that are deployed to git repositories. It also relies on some conventions that have been adopted by the RightScale Operations team. If your deploys do not follow these conventions, then this class is of little use to you!

In order for Info to be correct, your deployment methodology must adhere to the following three conventions:

1. The locally checked-out branch must track some upstream branch (but doesn't need to be named identically)
2. You should not "git fetch" in the deployed repository after deploy-time
3. If a post-deploy commit is made, the committer's email address must begin with "ops@"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_commits = 5, dir = Dir.pwd) ⇒ Info

Returns a new instance of Info.

Parameters:

  • num_commits (Fixnum) (defaults to: 5)

    how many commits to gather information about

  • dir (String, Pathname) (defaults to: Dir.pwd)

    location of the app’s git repo (or any of its subdirectories)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/right_support/deployment/info.rb', line 44

def initialize(num_commits=5, dir=Dir.pwd)
  @num_commits = num_commits
  @dir = dir


  # For all the services run by Ops, they add an extra commit when they checkout the code. This
  # commit does not indicate the last commit that would be seen in the Git repo anywhere else,
  # so we should ignore this commit whe logging the current repositories state.
  commit_diff = `git --git-dir #{Shellwords.escape(@dir.to_s)}/.git log origin/HEAD..HEAD \
                     --pretty=format:'%ae' 2> /dev/null`.split(/\n+/)

  ops_commit_exists = (commit_diff.count == 1 && commit_diff.first =~ /ops@/)
  ($?.success? && ops_commit_exists) ? head_commit = 1 : head_commit = 0

  recent_commits = `git --git-dir #{Shellwords.escape(@dir.to_s)}/.git log \
                         -n #{Shellwords.escape(@num_commits.to_s)} \
                         --pretty=format:'%h %an %ad' 2> /dev/null`.split(/\n+/)[head_commit..-1]
  @recent_commits = $?.success? ? recent_commits : []

  head_commit = `git --git-dir #{Shellwords.escape(@dir.to_s)}/.git log -n 1 \
                      --pretty=format:'%H' #{"HEAD~" if ops_commit_exists} 2> /dev/null`

  @head_commit = $?.success? ? head_commit : 'unknown'
end

Instance Attribute Details

#head_commitString (readonly)

Returns the SHA (or other SCM identifier) of the most recent commit.

Returns:

  • (String)

    the SHA (or other SCM identifier) of the most recent commit



40
41
42
# File 'lib/right_support/deployment/info.rb', line 40

def head_commit
  @head_commit
end

#recent_commitsArray (readonly)

Returns list of String: descriptions of recent SCM commits (most recent first).

Returns:

  • (Array)

    list of String: descriptions of recent SCM commits (most recent first)



37
38
39
# File 'lib/right_support/deployment/info.rb', line 37

def recent_commits
  @recent_commits
end