class Argus::Runner

Public Class Methods

new(msg) click to toggle source
# File lib/argus/runner.rb, line 47
def initialize(msg)
  msg = symbolize_keys(msg)
  puts "Received message: #{msg}"

  ## set default
  msg[:branch] ||= 'master'

  ## make working directory
  dir = File.join(ENV.fetch('ARGUS_HOME', '/tmp'), msg[:org], msg[:repo])
  FileUtils.mkdir_p(dir)

  ## github repo to get
  git = Git.new(msg[:org], msg[:repo], msg[:branch], msg.fetch(:sha, nil))

  ## authenticate to registry
  registry = authenticate_ecr

  ## docker image to build
  img = Image.new(image_name(registry, msg))

  Dir.chdir(dir) do
    img.pull              # pull some layers to speed up the build
    git.pull              # get the git repo
    raise ArgusError, "git sha not found: #{git}" unless git.sha

    options = msg.fetch(:build_options, {})
    img.build!(options) # build docker image
    raise ArgusError, 'docker build failed' unless img.is_ok?

    short_sha = git.sha.slice(0,7) # human-readable sha for messages

    notify("build complete for #{img} #{short_sha} (#{img.build_time.round}s)", :good)

    img.tag!(git.sha)       # tag the image
    img.push(git.sha)       # push to registry

    notify("push complete for #{img} #{short_sha} (#{img.push_time.round}s)", :good)
  end
rescue ArgusError => e
  notify(e.message, :danger)
  raise # re-raise for shoryuken to delete failed job
end

Public Instance Methods

authenticate_ecr() click to toggle source

authenticate to AWS ECR

# File lib/argus/runner.rb, line 19
def authenticate_ecr
  ## get token and extract creds
  auth = Aws::ECR::Client.new.get_authorization_token.authorization_data.first
  username, password = Base64.decode64(auth.authorization_token).split(':')

  ## authenticate our docker client
  Docker.authenticate!(
    username:      username,
    password:      password,
    serveraddress: auth.proxy_endpoint
  )

  ## return registry name
  URI.parse(auth.proxy_endpoint).host
end
image_name(registry, msg) click to toggle source

return image tag, or make it out of: repo:branch with / changed to - in branch; prepend registry if none given

# File lib/argus/runner.rb, line 38
def image_name(registry, msg)
  name = msg[:tag] || (msg[:repo] + ':' + msg[:branch].gsub('/', '-'))
  if name.include?('/')
    name
  else
    "#{registry}/#{name}"
  end
end
notify(message, color = nil) click to toggle source

… else notify stdout

# File lib/argus/runner.rb, line 10
def notify(message, color = nil)
  puts message
end
symbolize_keys(hash) click to toggle source
# File lib/argus/runner.rb, line 14
def symbolize_keys(hash)
  Hash[ hash.map { |k,v| [ k.to_sym, v ] }]
end