Class: EcsSolo::Deploy

Inherits:
AbstractBase show all
Defined in:
lib/ecs_solo/deploy.rb

Instance Method Summary collapse

Methods included from AwsServices

#cloudformation, #ecs

Constructor Details

#initialize(options = {}) ⇒ Deploy

Returns a new instance of Deploy.



3
4
5
6
7
8
# File 'lib/ecs_solo/deploy.rb', line 3

def initialize(options={})
  super
  @identifier = options[:identifier] # ECS Service or CloudFormation Stack
  @cluster = options[:cluster]
  @command = options[:command]
end

Instance Method Details

#cloudformation_ecs_service_arn(stack_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ecs_solo/deploy.rb', line 48

def cloudformation_ecs_service_arn(stack_name)
  resp = cloudformation.describe_stack_resources(stack_name: stack_name)
  resource = resp.stack_resources.find { |r| r.resource_type == "AWS::ECS::Service" }
  resource.physical_resource_id # IE: arn:aws:ecs:us-west-2:112233445566:service/development/demo-web-development-Ecs-179L598PRC44
rescue Aws::CloudFormation::Errors::ValidationError => e
  if e.message.include?("does not exist")
    return
  else
    raise(e)
  end
end

#ecs_service(service) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ecs_solo/deploy.rb', line 60

def ecs_service(service)
  begin
    resp = ecs.describe_services(services: [service], cluster: @cluster)
  rescue Aws::ECS::Errors::ClusterNotFoundException => e
    puts "#{e.class}: #{e.message}"
    puts "WARN: #{@cluster.color(:green)} not found."
    return
  end

  resp.services.first
end

#find_serviceObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/ecs_solo/deploy.rb', line 37

def find_service
  ecs_service_arn = cloudformation_ecs_service_arn(@identifier)
  if ecs_service_arn
    # IE: arn:aws:ecs:us-west-2:112233445566:service/development/demo-web-development-Ecs-179L598PRC44
    @cluster = ecs_service_arn.split('/')[1] # override @cluster
    ecs_service(ecs_service_arn)
  else
    ecs_service(@identifier)
  end
end

#find_task_definitionObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/ecs_solo/deploy.rb', line 26

def find_task_definition
  puts "Finding Docker image associated with #{@identifier}"
  service = find_service
  return unless service

  task_definition = service.task_definition
  resp = ecs.describe_task_definition(task_definition: task_definition)
  puts "Found task definition with Docker image"
  resp.task_definition
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ecs_solo/deploy.rb', line 10

def run
  if @options[:noop]
    puts "NOOP: Will find task definition associated with #{@identifier.color(:green)} in the cluster #{@cluster.color(:green)}"
    return
  end

  task_definition = find_task_definition
  unless task_definition
    puts "Unable to task definition associated with #{@identifier.color(:green)} in the cluster #{@cluster.color(:green)}"
    exit 1
  end

  @docker = Docker.new(@options.merge(task_definition: task_definition))
  @docker.execute
end