Class: Ecr::Auth

Inherits:
Object
  • Object
show all
Includes:
Ufo::AwsService
Defined in:
lib/ufo/ecr/auth.rb

Instance Method Summary collapse

Methods included from Ufo::AwsService

#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb

Constructor Details

#initialize(full_image_name) ⇒ Auth

Returns a new instance of Auth.



22
23
24
25
# File 'lib/ufo/ecr/auth.rb', line 22

def initialize(full_image_name)
  @full_image_name = full_image_name
  @repo_domain = "#{full_image_name.split('/').first}"
end

Instance Method Details

#ecr_image?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ufo/ecr/auth.rb', line 43

def ecr_image?
  !!(@full_image_name =~ /\.amazonaws\.com/)
end

#fetch_auth_tokenObject



47
48
49
# File 'lib/ufo/ecr/auth.rb', line 47

def fetch_auth_token
  ecr.get_authorization_token.authorization_data.first.authorization_token
end

#updateObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ufo/ecr/auth.rb', line 27

def update
  # wont update auth token unless the image being pushed in the ECR image format
  return unless ecr_image?

  auth_token = fetch_auth_token
  username, password = Base64.decode64(auth_token).split(':')

  command = "docker login -u #{username} --password-stdin #{@repo_domain}"
  puts "=> #{command}".color(:green)
  *, status = Open3.capture3(command, stdin_data: password)
  unless status.success?
    puts "ERROR: The docker failed to login.".color(:red)
    exit 1
  end
end