Class: ECSHelper::Command::Deploy

Inherits:
Base
  • Object
show all
Defined in:
lib/ecs_helper/command/deploy.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
300
STEP =
5

Instance Attribute Summary collapse

Attributes inherited from Base

#client, #helper, #option_parser, #options, #type

Instance Method Summary collapse

Methods inherited from Base

#application, #check_bin, #initialize, #printable?, #project, #required, #validate

Methods included from Logging

#console, #error, #log

Constructor Details

This class inherits a constructor from ECSHelper::Command::Base

Instance Attribute Details

#new_task_definitionObject

Returns the value of attribute new_task_definition.



4
5
6
# File 'lib/ecs_helper/command/deploy.rb', line 4

def new_task_definition
  @new_task_definition
end

#repositoriesObject

Returns the value of attribute repositories.



4
5
6
# File 'lib/ecs_helper/command/deploy.rb', line 4

def repositories
  @repositories
end

#service=(value) ⇒ Object

Sets the attribute service

Parameters:

  • value

    the value to set the attribute service to.



4
5
6
# File 'lib/ecs_helper/command/deploy.rb', line 4

def service=(value)
  @service = value
end

#task_definitionObject

Returns the value of attribute task_definition.



4
5
6
# File 'lib/ecs_helper/command/deploy.rb', line 4

def task_definition
  @task_definition
end

Instance Method Details

#cmd_option_parserObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ecs_helper/command/deploy.rb', line 8

def cmd_option_parser
  options = {}
  parser = ::OptionParser.new do |opts|
    opts.banner = "Usage: ecs_helper deploy [options]"
    opts.on("-p VALUE", "--project VALUE", "Set project name, if not specified will look at ENV['PROJECT'], will be used to detect cluster") { |p| options[:project] = processEqual(p) }
    opts.on("-a VALUE", "--application VALUE", "Set application name, if not specified will look at ENV['APPLICATION'], will be used to detect service and task definition") { |a| options[:application] = processEqual(a) }
    opts.on("-e VALUE", "--environment VALUE", "Set environment, if not specified will look at ENV['ENVIRONMENT'], it there is empty will try to detect based on the branch") { |e| options[:environment] = processEqual(e) }
    opts.on("-v VALUE", "--version VALUE", "Set version which will be applied to all containers in the task if tag is present in the repo") { |t| options[:version] = processEqual(t) }
    opts.on("-cl VALUE", "--cluster VALUE", "Set cluster name, could be autodetected if project and environment are specified") { |c| options[:cluster] = processEqual(c) }
    opts.on("-s VALUE", "--service VALUE", "Set service, could be autodetected if application and environment are specified") { |s| options[:service] = processEqual(s) }
    opts.on("-t VALUE", "--timeout VALUE", "Set timeout how long to wait until deployment finished") { |t| options[:timeout] = processEqual(t) }
  end
  [parser, options]
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ecs_helper/command/deploy.rb', line 23

def run
  task_definition_helper = ECSHelper::TaskDefinitionHelper.new(helper, service)
  service_task_definition = task_definition_helper.service_task_definition
  new_task_definition_hash = task_definition_helper.new_task_definition_hash
  new_task_definition = task_definition_helper.register_task_definition(new_task_definition_hash)
  log("Command", type)
  log("Options", options)
  log("Environment", environment)
  log("Cluster", cluster_arn)
  log("Service", service_arn)
  log("Version", version)
  log("Service task definition", service_task_definition.task_definition_arn)
  log("Containers", task_definition_helper.pretty_container_definitions)
  log("New task definition", new_task_definition.task_definition_arn)
  update_service(new_task_definition.task_definition_arn) && log("Update service", "Service task definition was updated")
  log("Waiting for deployment...")
  wait_for_deployment && log("Success", "Application was succesfully deployed", :cyan)
end

#update_service(task_definition_arn) ⇒ Object



42
43
44
# File 'lib/ecs_helper/command/deploy.rb', line 42

def update_service(task_definition_arn)
  helper.update_service(cluster_arn, service_arn, task_definition_arn)
end

#wait_for_deployment(time = 0) ⇒ Object



46
47
48
49
50
51
# File 'lib/ecs_helper/command/deploy.rb', line 46

def wait_for_deployment(time = 0)
  return true if service.deployments.count == 1
  error("Deployment timeout (#{timeout})") if time > timeout
  sleep STEP
  wait_for_deployment(time + STEP)
end