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.



20
21
22
23
# File 'lib/ufo/ecr/auth.rb', line 20

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

Instance Method Details

#docker_configObject



53
54
55
# File 'lib/ufo/ecr/auth.rb', line 53

def docker_config
  "#{ENV['HOME']}/.docker/config.json"
end

#ecr_image?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ufo/ecr/auth.rb', line 45

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

#ensure_dotdocker_existsObject



57
58
59
60
# File 'lib/ufo/ecr/auth.rb', line 57

def ensure_dotdocker_exists
  dirname = File.dirname(docker_config)
  FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
end

#fetch_auth_tokenObject



49
50
51
# File 'lib/ufo/ecr/auth.rb', line 49

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

#updateObject



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

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
  if File.exist?(docker_config)
    data = JSON.load(IO.read(docker_config))
    data["auths"][@repo_domain] = {auth: auth_token}
  else
    data = {"auths" => {@repo_domain => {auth: auth_token}}}
  end

  # Handle legacy docker clients that still have old format with https://
  legacy_entry = "https://#{@repo_domain}"
  data["auths"][legacy_entry] = {auth: auth_token}

  ensure_dotdocker_exists
  IO.write(docker_config, JSON.pretty_generate(data))
end