Class: Awful::Ecs

Inherits:
Cli show all
Defined in:
lib/awful/ecs.rb

Constant Summary collapse

COLORS =
{
  ACTIVE:   :green,
  INACTIVE: :red,
  true:     :green,
  false:    :red,
  RUNNING:  :green,
  STOPPED:  :red,
}

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#create(name) ⇒ Object



57
58
59
60
61
# File 'lib/awful/ecs.rb', line 57

def create(name)
  ecs.create_cluster(cluster_name: name).cluster.output do |cluster|
    puts YAML.dump(stringify_keys(cluster.to_h))
  end
end

#definitions(family = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/awful/ecs.rb', line 104

def definitions(family = nil)
  params = {family_prefix: family, status: options[:inactive] ? 'INACTIVE' : 'ACTIVE'}.reject{|_,v| v.nil?}
  arns = ecs.list_task_definitions(params).task_definition_arns
  if options[:arns]
    arns
  else
    arns.map{|a| a.split('/').last}
  end.output(&method(:puts))
end

#delete(name) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/awful/ecs.rb', line 64

def delete(name)
  if yes?("Really delete stack #{name}?", :yellow)
    ecs.delete_cluster(cluster: name).cluster.output do |cluster|
      puts YAML.dump(stringify_keys(cluster.to_h))
    end
  end
end

#deregister(task) ⇒ Object



140
141
142
# File 'lib/awful/ecs.rb', line 140

def deregister(task)
  ecs.deregister_task_definition(task_definition: task)
end

#dump(task) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/awful/ecs.rb', line 146

def dump(task)
  ecs.describe_task_definition(task_definition: task).task_definition.to_h.output do |hash|
    if options[:json]
      puts JSON.pretty_generate(hash)
    else
      puts YAML.dump(stringify_keys(hash))
    end
  end
end

#events(cluster, service) ⇒ Object



223
224
225
226
227
# File 'lib/awful/ecs.rb', line 223

def events(cluster, service)
  ecs.describe_services(cluster: cluster, services: [service]).services.first.events.output do |events|
    print_table events.map { |e| [e.created_at, e.id, e.message] }
  end
end

#families(prefix = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/awful/ecs.rb', line 115

def families(prefix = nil)
  next_token = nil
  families = []
  loop do
    response = ecs.list_task_definition_families(family_prefix: prefix, next_token: next_token)
    families += response.families
    next_token = response.next_token
    break unless next_token
  end
  families.output(&method(:puts))
end

#instances(cluster) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/awful/ecs.rb', line 74

def instances(cluster)
  arns = ecs.list_container_instances(cluster: cluster).container_instance_arns
  if options[:long]
    container_instances = ecs.describe_container_instances(cluster: cluster, container_instances: arns).container_instances

    ## get hash of tags for each instance id
    tags = ec2.describe_instances(instance_ids: container_instances.map(&:ec2_instance_id)).
             map(&:reservations).flatten.
             map(&:instances).flatten.
             each_with_object({}) do |i,h|
      h[i.instance_id] = tag_name(i, '--')
    end

    print_table container_instances.each_with_index.map { |ins, i|
      [
        tags[ins.ec2_instance_id],
        ins.container_instance_arn.split('/').last,
        ins.ec2_instance_id,
        "agent:#{color(ins.agent_connected.to_s)}",
        color(ins.status),
      ]
    }.sort
  else
    puts arns
  end
end

#ls(name = '.') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/awful/ecs.rb', line 33

def ls(name = '.')
  arns = ecs.list_clusters.cluster_arns.select do |arn|
    arn.split('/').last.match(/#{name}/i)
  end
  ecs.describe_clusters(clusters: arns).clusters.output do |clusters|
    if options[:arns]
      puts arns
    elsif options[:long]
      print_table clusters.map { |c|
        [
          c.cluster_name,
          color(c.status),
          "instances:#{c.registered_container_instances_count}",
          "pending:#{c.pending_tasks_count}",
          "running:#{c.running_tasks_count}",
        ]
      }
    else
      puts clusters.map(&:cluster_name).sort
    end
  end
end

#register(family) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/awful/ecs.rb', line 128

def register(family)
  taskdef = ecs.describe_task_definition(task_definition: family).task_definition # get current newest
  ecs.register_task_definition(
    family: family,
    container_definitions: taskdef.container_definitions,
    volumes: taskdef.volumes
  ).task_definition.output do |td|
    puts td.task_definition_arn
  end
end

#run_task(cluster, task) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/awful/ecs.rb', line 231

def run_task(cluster, task)
  container_overrides = {}
  if options[:command]
    name, command = options[:command].split(':', 2)
    container_overrides.merge!(name: name, command: command.split(','))
  end

  params = {
    cluster: cluster,
    task_definition: task,
    overrides: container_overrides.empty? ? {} : {container_overrides: [container_overrides]}
  }

  ecs.run_task(params).output do |response|
    puts YAML.dump(stringify_keys(response.to_h))
  end
end

#services(cluster) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/awful/ecs.rb', line 192

def services(cluster)
  arns = ecs.list_services(cluster: cluster).service_arns
  if options[:long]
    print_table ecs.describe_services(cluster: cluster, services: arns).services.map { |svc|
      [
        svc.service_name,
        color(svc.status),
        svc.task_definition.split('/').last,
        "#{svc.running_count}/#{svc.desired_count}",
      ]
    }
  else
    arns.output(&method(:puts))
  end
end

#status(cluster, *tasks) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/awful/ecs.rb', line 182

def status(cluster, *tasks)
  ecs.describe_tasks(cluster: cluster, tasks: tasks).tasks.output do |responses|
    responses.each do |response|
      puts YAML.dump(stringify_keys(response.to_h))
    end
  end
end

#stop_task(cluster, id) ⇒ Object



250
251
252
253
254
# File 'lib/awful/ecs.rb', line 250

def stop_task(cluster, id)
  ecs.stop_task(cluster: cluster, task: id).task.output do |response|
    puts YAML.dump(stringify_keys(response.to_h))
  end
end

#tasks(cluster) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/awful/ecs.rb', line 159

def tasks(cluster)
  status = %w[running pending stopped].find{ |s| s.match(/^#{options[:status]}/i) }
  arns = ecs.list_tasks(cluster: cluster, desired_status: status.upcase).task_arns
  if arns.empty?
    []
  elsif options[:long]
    ecs.describe_tasks(cluster: cluster, tasks: arns).tasks.output do |tasks|
      print_table tasks.map { |task|
        [
          task.task_arn.split('/').last,
          task.task_definition_arn.split('/').last,
          task.container_instance_arn.split('/').last,
          "#{color(task.last_status)} (#{task.desired_status})",
          task.started_by,
        ]
      }
    end
  else
    arns.output(&method(:puts))
  end
end

#update(cluster, service) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/awful/ecs.rb', line 211

def update(cluster, service)
  params = {
    cluster:         cluster,
    service:         service,
    desired_count:   options[:desired_count],
    task_definition: options[:task_definition],
  }.reject { |k,v| v.nil? }

  ecs.update_service(params).service
end