Class: Argus::Runner

Inherits:
Object
  • Object
show all
Includes:
SlackNotifier
Defined in:
lib/argus/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(msg) ⇒ Runner



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

#authenticate_ecrObject

authenticate to AWS ECR



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object

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



38
39
40
41
42
43
44
45
# 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) ⇒ Object

… else notify stdout



10
11
12
# File 'lib/argus/runner.rb', line 10

def notify(message, color = nil)
  puts message
end

#SlackNotifierObject

override notifier if setup for Slack …



7
# File 'lib/argus/runner.rb', line 7

prepend SlackNotifier

#symbolize_keys(hash) ⇒ Object



14
15
16
# File 'lib/argus/runner.rb', line 14

def symbolize_keys(hash)
  Hash[ hash.map { |k,v| [ k.to_sym, v ] }]
end