Class: Ufo::Ship

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/ship.rb

Instance Method Summary collapse

Methods inherited from Base

#full_service, #info, #no_service_message, #switch_current

Methods included from Ufo::Stack::Helper

#adjust_stack_name, #cfn, #find_stack, #status

Methods included from Util

#default_cluster, #display_params, #execute, #pretty_time, #settings, #task_definition_arns, #user_params

Methods included from AwsService

#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb

Constructor Details

#initialize(service, options = {}) ⇒ Ship

Returns a new instance of Ship.



6
7
8
9
# File 'lib/ufo/ship.rb', line 6

def initialize(service, options={})
  super
  @task_definition = options[:task_definition]
end

Instance Method Details

#deployObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ufo/ship.rb', line 11

def deploy
  message = "Deploying service #{@service.color(:green)} to cluster #{@cluster.color(:green)}..."
  unless @options[:mute]
    if @options[:noop]
      puts "NOOP: #{message}"
      return
    else
      puts message
    end
  end

  ensure_log_group_exist
  ensure_cluster_exist
  stop_old_tasks if @options[:stop_old_tasks]
  success = deploy_stack

  return if @options[:mute] || !@options[:wait]
  if success
    puts "Software shipped!"
  else
    puts "Software fail to ship."
    exit 1
  end
end

#deploy_stackObject



57
58
59
60
61
62
63
64
# File 'lib/ufo/ship.rb', line 57

def deploy_stack
  options = @options.merge(
    service: @service,
    task_definition: @task_definition,
  )
  stack = Stack.new(options)
  stack.deploy
end

#ecs_clustersObject



99
100
101
# File 'lib/ufo/ship.rb', line 99

def ecs_clusters
  ecs.describe_clusters(clusters: [@cluster]).clusters
end

#ensure_cluster_existObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ufo/ship.rb', line 83

def ensure_cluster_exist
  cluster = ecs_clusters.first
  unless cluster && cluster.status == "ACTIVE"
    message = "#{@cluster} cluster created."
    if @options[:noop]
      message = "NOOP #{message}"
    else
      ecs.create_cluster(cluster_name: @cluster)
      # TODO: Add Waiter logic, sometimes the cluster does not exist by the time
      # we create the service
    end

    puts message unless @options[:mute]
  end
end

#ensure_log_group_existObject



53
54
55
# File 'lib/ufo/ship.rb', line 53

def ensure_log_group_exist
  LogGroup.new(@task_definition, @options).create
end

#show_aws_cli_command(action, params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ufo/ship.rb', line 66

def show_aws_cli_command(action, params)
  puts "Equivalent aws cli command:"
  # Use .ufo/data instead of .ufo/output because output files all get looped
  # through as part of `ufo tasks register`
  rel_path = ".ufo/data/#{action}-params.json"
  output_path = "#{Ufo.root}/#{rel_path}"
  FileUtils.rm_f(output_path)

  # Thanks: https://www.mnishiguchi.com/2017/11/29/rails-hash-camelize-and-underscore-keys/
  params = params.deep_transform_keys { |key| key.to_s.camelize(:lower) }
  json = JSON.pretty_generate(params)
  IO.write(output_path, json)

  file_path = "file://#{rel_path}"
  puts "  aws ecs #{action}-service --cli-input-json #{file_path}".color(:green)
end

#stop_old_tasksObject

Start a thread that will poll for ecs deployments and kill of tasks in old deployments. This must be done in a thread because the stack update process is blocking.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ufo/ship.rb', line 39

def stop_old_tasks
  # only works when deployment is blocking
  return unless @options[:wait]

  Thread.new do
    stop = Ufo::Stop.new(@service, @options.merge(mute: true))
    while true
      stop.log "checking for old tasks and waiting for 10 seconds"
      stop.run
      sleep 10
    end
  end
end