Class: SimplyGenius::Atmos::Providers::Aws::ContainerManager
- Inherits:
-
Object
- Object
- SimplyGenius::Atmos::Providers::Aws::ContainerManager
- Includes:
- GemLogger::LoggerSupport
- Defined in:
- lib/simplygenius/atmos/providers/aws/container_manager.rb
Instance Method Summary collapse
- #deploy_service(cluster, service, remote_image) ⇒ Object
- #deploy_task(task, remote_image) ⇒ Object
-
#initialize(provider) ⇒ ContainerManager
constructor
A new instance of ContainerManager.
- #push(ecs_name, local_image, ecr_repo: ecs_name, revision: nil) ⇒ Object
Constructor Details
#initialize(provider) ⇒ ContainerManager
Returns a new instance of ContainerManager.
14 15 16 |
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 14 def initialize(provider) @provider = provider end |
Instance Method Details
#deploy_service(cluster, service, remote_image) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 79 def deploy_service(cluster, service, remote_image) result = {} ecs = ::Aws::ECS::Client.new resp = nil # Get current task definition name from service resp = ecs.describe_services(cluster: cluster, services: [service]) current_defn_arn = resp.services.first.task_definition defn_name = current_defn_arn.split("/").last.split(":").first logger.info "Current task definition (name=#{defn_name}): #{current_defn_arn}" result = deploy_task(defn_name, remote_image) new_taskdef = result[:task_definition] logger.info "Updating service with new task definition: #{new_taskdef}" resp = ecs.update_service(cluster: cluster, service: service, task_definition: new_taskdef) logger.info "Updated service=#{service} on cluster=#{cluster} to #{new_taskdef} with image #{remote_image}" return result end |
#deploy_task(task, remote_image) ⇒ Object
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 |
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 50 def deploy_task(task, remote_image) result = {} ecs = ::Aws::ECS::Client.new resp = nil resp = ecs.list_task_definitions(family_prefix: task, sort: 'DESC') latest_defn_arn = resp.task_definition_arns.first logger.info "Latest task definition: #{latest_defn_arn}" resp = ecs.describe_task_definition(task_definition: latest_defn_arn) latest_defn = resp.task_definition new_defn = latest_defn.to_h [:revision, :status, :task_definition_arn, :requires_attributes, :compatibilities].each do |attr| new_defn.delete(attr) end new_defn[:container_definitions].each {|c| c[:image] = remote_image} resp = ecs.register_task_definition(**new_defn) result[:task_definition] = resp.task_definition.task_definition_arn logger.info "Updated task=#{task} to #{result[:task_definition]} with image #{remote_image}" return result end |
#push(ecs_name, local_image, ecr_repo: ecs_name, revision: nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 18 def push(ecs_name, local_image, ecr_repo: ecs_name, revision: nil) revision = Time.now.strftime('%Y%m%d%H%M%S') unless revision.present? result = {} ecr = ::Aws::ECR::Client.new resp = nil resp = ecr. auth_data = resp..first token = auth_data. endpoint = auth_data.proxy_endpoint user, password = Base64.decode64(token).split(':') # docker login into the ECR repo for the current account so that we can pull/push to it run("docker", "login", "-u", user, "-p", password, endpoint)#, stdin_data: token) image="#{ecs_name}:latest" ecs_image="#{endpoint.sub(/https?:\/\//, '')}/#{ecr_repo}" = ['latest', revision] logger.info "Tagging local image '#{local_image}' with #{}" .each {|t| run("docker", "tag", local_image, "#{ecs_image}:#{t}") } logger.info "Pushing tagged image to ECR repo" .each {|t| run("docker", "push", "#{ecs_image}:#{t}") } result[:remote_image] = "#{ecs_image}:#{revision}" return result end |