Class: Ufo::Ship

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

Instance Method Summary collapse

Methods inherited from Base

#info, #no_service_message, #switch_current

Methods included from Ufo::Stack::Helper

#adjust_stack_name, #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.



9
10
11
12
# File 'lib/ufo/ship.rb', line 9

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

Instance Method Details

#deployObject



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

def deploy
  message = "Deploying #{@service}..."
  unless @options[:mute]
    if @options[:noop]
      puts "NOOP: #{message}"
      return
    else
      puts message.green
    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."
  end
end

#deploy_stackObject



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

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

#ecs_clustersObject



101
102
103
# File 'lib/ufo/ship.rb', line 101

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

#ensure_cluster_existObject



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

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



55
56
57
# File 'lib/ufo/ship.rb', line 55

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

#show_aws_cli_command(action, params) ⇒ Object



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

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}".colorize(: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.



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

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