Class: SimplyGenius::Atmos::Providers::Aws::ContainerManager

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport
Defined in:
lib/simplygenius/atmos/providers/aws/container_manager.rb

Instance Method Summary collapse

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(cluster, name, remote_image) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 79

def deploy(cluster, name, remote_image)
   result = deploy_task(name, remote_image)
   new_taskdef = result[:task_definition]

   # Only trigger restart if name is a service
   ecs = ::Aws::ECS::Client.new
   resp = ecs.describe_services(cluster: cluster, services: [name])

   if resp.services.size > 0
     logger.info "Updating service with new task definition: #{new_taskdef}"

     resp = ecs.update_service(cluster: cluster, service: name, task_definition: new_taskdef)

     logger.info "Updated service=#{name} on cluster=#{cluster} to #{new_taskdef} with image #{remote_image}"
   else
     logger.info "#{name} is not a service"
   end

   return result
end

#deploy_task(name, 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(name, remote_image)
  result = {}

  ecs = ::Aws::ECS::Client.new
  resp = nil

  resp = ecs.list_task_definitions(family_prefix: name, sort: 'DESC')
  latest_defn_arn = resp.task_definition_arns.first

  logger.info "Current task definition for #{name}: #{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=#{name} to #{result[:task_definition]} with image #{remote_image}"

  return result
end

#list_image_tags(cluster, name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 112

def list_image_tags(cluster, name)
  result = {tags: [], latest: nil, current: nil}
  latest_digest = nil

  ecs = ::Aws::ECS::Client.new
  ecr = ::Aws::ECR::Client.new

  resp = ecs.describe_services(services: [name], cluster: cluster)
  if resp.services.size == 1
    task_def = resp.services.first.task_definition
    resp = ecs.describe_task_definition(task_definition: task_def)
    image = resp.task_definition.container_definitions.first.image
    result[:current] = image.sub(/^.*:/, '')
  else
    raise "No services found for '#{name}' in cluster '#{cluster}'"
  end

  # TODO: handle pagination?
  resp = ecr.list_images(repository_name: name, filter: {tag_status: "TAGGED"}, max_results: 1000)
  if resp.image_ids.size > 0

    images = resp.image_ids

    images.each do |i|
      if i.image_tag == 'latest'
        latest_digest = i.image_digest
      end

      result[:tags] << i.image_tag
    end

    if latest_digest
      images.each do |i|
        if i.image_digest == latest_digest && i.image_tag != 'latest'
          result[:latest] = i.image_tag
          break
        end
      end
      # Handle if latest tag isn't some other tag
      result[:latest] = 'latest' if result[:latest].nil?
    end

    result[:tags].sort!
  else
    raise "No images found for '#{name}'"
  end

  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.get_authorization_token
  auth_data = resp.authorization_data.first
  token = auth_data.authorization_token
  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}"

  tags = ['latest', revision]
  logger.info "Tagging local image '#{local_image}' with #{tags}"
  tags.each {|t| run("docker", "tag", local_image, "#{ecs_image}:#{t}") }

  logger.info "Pushing tagged image to ECR repo #{ecs_image}"
  tags.each {|t| run("docker", "push", "#{ecs_image}:#{t}") }

  result[:remote_image] = "#{ecs_image}:#{revision}"
  return result
end

#remote_image(name, tag) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/simplygenius/atmos/providers/aws/container_manager.rb', line 100

def remote_image(name, tag)
  ecr = ::Aws::ECR::Client.new

  resp = ecr.get_authorization_token
  endpoint = resp.authorization_data.first.proxy_endpoint

  ecs_image="#{endpoint.sub(/https?:\/\//, '')}/#{name}"
  tagged_image = "#{ecs_image}:#{tag}"

  return tagged_image
end