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



95
96
97
98
99
100
101
102
103
# File 'lib/awful/ecs.rb', line 95

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

#deregister(task) ⇒ Object



131
132
133
# File 'lib/awful/ecs.rb', line 131

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

#dump(task) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/awful/ecs.rb', line 137

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



214
215
216
217
218
# File 'lib/awful/ecs.rb', line 214

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



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/awful/ecs.rb', line 106

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/awful/ecs.rb', line 65

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/awful/ecs.rb', line 119

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/awful/ecs.rb', line 222

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/awful/ecs.rb', line 183

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



173
174
175
176
177
178
179
# File 'lib/awful/ecs.rb', line 173

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



241
242
243
244
245
# File 'lib/awful/ecs.rb', line 241

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/awful/ecs.rb', line 150

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



202
203
204
205
206
207
208
209
210
211
# File 'lib/awful/ecs.rb', line 202

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