Last active
December 19, 2017 01:36
-
-
Save SegFaultAX/eb9c6f81ca586fe8aeabdef71d0e3a65 to your computer and use it in GitHub Desktop.
Git Remote Version Check [Ruby] [Sensu]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# check-git-remote-version.rb | |
# | |
# DESCRIPTION: | |
# Ensure git repo is in-sync with remote | |
# This check verifies that the current version of a git repo reflects the | |
# latest version of a particular ref on a named remote repository. | |
# | |
# PLATFORMS: | |
# Linux | |
# | |
# DEPENDENCIES: | |
# gem: sensu-plugin | |
# | |
# USAGE: | |
# This will return OK if the clone at `/foo/bar` matches the version on the | |
# named remote called `origin` at ref `refs/heads/master` | |
# check-git-remote-version.rb -c /foo/bar -r origin -b master | |
# | |
# NOTES: | |
# This check depends on the locally configured remotes rather than explicitly | |
# passing the remote repository via eg command line arguments. | |
# | |
# LICENSE: | |
# Michael-Keith Bernard <[email protected]> | |
# Released under the same terms as Sensu (the MIT license); see LICENSE for | |
# details. | |
# | |
require 'open3' | |
require 'sensu-plugin/check/cli' | |
class CheckGitRemoteVersion < Sensu::Plugin::Check::CLI | |
option :checkout_dir, | |
short: '-c CHECKOUT', | |
long: '--checkout CHECKOUT', | |
description: 'Path to local git checkout', | |
required: true | |
option :remote, | |
short: '-r REMOTE', | |
long: '--remote REMOTE', | |
description: 'Git remote to check', | |
default: 'origin' | |
option :branch, | |
short: '-b BRANCH', | |
long: '--branch BRANCH', | |
description: 'Remote branch that should be tracked', | |
default: 'master' | |
def run | |
if local_version == remote_version | |
ok "Local git version is up-to-date with #{remote_ref}" | |
end | |
critical "Local git version is out of date with #{remote_ref}! Local: #{local_version}, Remote: #{remote_version}" | |
end | |
def local_version | |
@local_version ||= in_checkout('git', 'rev-parse', '--verify', 'HEAD') do |version| | |
if present?(version) | |
version.strip | |
else | |
"<local version unknown>" | |
end | |
end | |
end | |
def remote_version | |
@remote_version ||= in_checkout('git', 'ls-remote', '--exit-code', '--heads', config[:remote], config[:branch]) do |version| | |
if present?(version) | |
version.strip.split[0] | |
else | |
'<remote version unknown>' | |
end | |
end | |
end | |
def remote_ref | |
"#{config[:remote]}/#{config[:branch]}" | |
end | |
private | |
def in_checkout(command, *args, &block) | |
result = Dir.chdir(config[:checkout_dir]) do | |
stdout, stderr, status = Open3.capture3(command, *args) | |
if status != 0 | |
full_cmd = "`#{command} #{args.join(" ")}`" | |
reason = if present?(stderr) | |
", Reason: #{stderr}" | |
else | |
"" | |
end | |
critical "Failed to run command: #{full_cmd}#{reason}" | |
end | |
stdout | |
end | |
block.call(result) | |
end | |
def present?(s) | |
!(s.empty? || /\A[[:space:]]*\z/.match(s)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment