2019-10-04 14:24:15 +01:00
|
|
|
class Pipeline::ContainerRepo
|
|
|
|
|
|
2019-11-11 20:27:07 +00:00
|
|
|
def self.instance_for(container_slug, credentials)
|
2019-10-18 16:45:31 +01:00
|
|
|
Pipeline::ContainerRepo.new(container_slug, credentials)
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-04 14:24:15 +01:00
|
|
|
attr_reader :image_name
|
|
|
|
|
|
2019-10-07 21:28:33 +01:00
|
|
|
def initialize(image_name, credentials=nil)
|
2019-10-04 14:24:15 +01:00
|
|
|
@image_name = image_name
|
2019-10-07 21:28:33 +01:00
|
|
|
@credentials = credentials
|
2019-10-04 14:24:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_if_required
|
|
|
|
|
puts "Checking if repository exists"
|
|
|
|
|
begin
|
|
|
|
|
return lookup_repo
|
|
|
|
|
rescue Aws::ECR::Errors::RepositoryNotFoundException
|
|
|
|
|
puts "Repository #{image_name} not found"
|
|
|
|
|
end
|
|
|
|
|
puts "Creating repository"
|
|
|
|
|
ecr.create_repository({
|
|
|
|
|
repository_name: image_name,
|
|
|
|
|
image_tag_mutability: "MUTABLE"
|
|
|
|
|
})
|
|
|
|
|
lookup_repo
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def lookup_repo
|
|
|
|
|
repos = ecr.describe_repositories({
|
|
|
|
|
repository_names: [image_name]
|
|
|
|
|
})
|
|
|
|
|
repos.repositories.first
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def repository_url
|
|
|
|
|
lookup_repo.repository_uri
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_login_token
|
|
|
|
|
authorization_token = ecr.get_authorization_token.authorization_data[0].authorization_token
|
|
|
|
|
plain = Base64.decode64(authorization_token)
|
|
|
|
|
user,password = plain.split(":")
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-14 09:33:22 +01:00
|
|
|
def list_images
|
2019-11-15 15:49:05 +00:00
|
|
|
puts "credentials #{@credentials}"
|
|
|
|
|
puts "ECR #{ecr}"
|
|
|
|
|
puts create_login_token
|
2019-10-14 09:33:22 +01:00
|
|
|
ecr.list_images({
|
2019-10-04 14:24:15 +01:00
|
|
|
repository_name: image_name
|
|
|
|
|
})
|
2019-10-14 09:33:22 +01:00
|
|
|
end
|
|
|
|
|
|
2019-11-15 15:49:05 +00:00
|
|
|
|
|
|
|
|
def images_info
|
|
|
|
|
images = list_images()
|
|
|
|
|
info = {}
|
|
|
|
|
images.image_ids.each do |image|
|
|
|
|
|
info[image.image_digest] ||= []
|
|
|
|
|
info[image.image_digest] << image.image_tag
|
|
|
|
|
end
|
|
|
|
|
info
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-14 09:33:22 +01:00
|
|
|
def git_shas
|
|
|
|
|
images = list_images()
|
2019-10-04 14:24:15 +01:00
|
|
|
tags = []
|
|
|
|
|
images.image_ids.each do |image|
|
|
|
|
|
tag = image.image_tag
|
2019-11-17 14:49:20 +00:00
|
|
|
next if tag.nil?
|
2019-10-04 14:24:15 +01:00
|
|
|
# Only return git-based shas
|
|
|
|
|
if tag.start_with?("sha-")
|
|
|
|
|
tag = tag.gsub(/sha-/, "")
|
|
|
|
|
tags << tag unless tag.include?("-")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
tags.uniq
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def ecr
|
2019-10-07 21:28:33 +01:00
|
|
|
@ecr ||= begin
|
2019-11-15 15:49:05 +00:00
|
|
|
( @credentials.nil? ) ?
|
|
|
|
|
Aws::ECR::Client.new(region: 'eu-west-1') :
|
|
|
|
|
Aws::ECR::Client.new(region: 'eu-west-1', credentials: @credentials)
|
2019-10-07 21:28:33 +01:00
|
|
|
end
|
2019-10-04 14:24:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|