Class: Stax::Cmd::Ecs

Inherits:
SubCommand show all
Defined in:
lib/stax/mixin/ecs.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from SubCommand

#info, stax_info, stax_info_tasks

Instance Method Details

#clustersObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stax/mixin/ecs.rb', line 97

def clusters
  print_table Aws::Ecs.clusters(my.ecs_cluster_name).map { |c|
    [
      c.cluster_name,
      color(c.status, COLORS),
      "instances:#{c.registered_container_instances_count}",
      "pending:#{c.pending_tasks_count}",
      "running:#{c.running_tasks_count}",
    ]
  }
end

#containersObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/stax/mixin/ecs.rb', line 212

def containers
  my.ecs_services.each do |s|
    Aws::Ecs.tasks(
      cluster: my.ecs_cluster_name,
      service_name: s.physical_resource_id,
      desired_status: options[:status].upcase,
    ).each do |t|
      task = t.task_arn.split('/').last
      debug("Containers for task #{task}")
      print_table t.containers.map { |c|
        [
          c.name,
          c.container_arn.split('/').last,
          color(c.last_status, COLORS),
          c.network_interfaces.map(&:private_ipv_4_address).join(','),
          t.task_definition_arn.split('/').last,
          c.exit_code,
          c.reason,
        ]
      }
    end
  end
end

#definitionsObject



156
157
158
159
160
161
# File 'lib/stax/mixin/ecs.rb', line 156

def definitions
  print_table my.ecs_task_definitions.map { |r|
    t = Aws::Ecs.task_definition(r.physical_resource_id)
    [r.logical_resource_id, t.family, t.revision, color(t.status, COLORS)]
  }
end

#deploymentsObject



145
146
147
148
149
150
151
152
153
# File 'lib/stax/mixin/ecs.rb', line 145

def deployments
  my.ecs_service_objects.each do |s|
    debug("Deployments for #{s.service_name}")
    print_table s.deployments.map { |d|
      count = "#{d.running_count}/#{d.desired_count} (#{d.pending_count})"
      [d.id, d.status, count, d.created_at, d.updated_at, d.task_definition.split('/').last]
    }
  end
end

#envObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/stax/mixin/ecs.rb', line 164

def env
  my.ecs_task_families.each do |family|
    Aws::Ecs.task_definition(family).container_definitions.each do |c|
      debug("Environment for #{family} #{c.name}")
      print_table c.environment.map { |e|
        [e.name, e.value]
      }.sort
    end
  end
end

#eventsObject



118
119
120
121
122
123
# File 'lib/stax/mixin/ecs.rb', line 118

def events
  my.ecs_service_objects.each do |s|
    debug("Events for #{s.service_name}")
    s.events.first(options[:number]).reverse.map(&method(:print_event))
  end
end

#instancesObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/stax/mixin/ecs.rb', line 237

def instances
  print_table Aws::Ecs.instances(my.ecs_cluster_name).map { |i|
    [
      i.container_instance_arn.split('/').last,
      i.ec2_instance_id,
      i.agent_connected,
      color(i.status, COLORS),
      i.running_tasks_count,
      "(#{i.pending_tasks_count})",
      "agent #{i.version_info.agent_version}",
      i.version_info.docker_version,
    ]
  }
end

#run_task(id) ⇒ Object



253
254
255
256
257
# File 'lib/stax/mixin/ecs.rb', line 253

def run_task(id)
  Aws::Ecs.run(my.ecs_cluster_name, Aws::Cfn.id(my.stack_name, id)).tap do |tasks|
    puts tasks.map(&:container_instance_arn)
  end
end

#scale(*ids) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/stax/mixin/ecs.rb', line 268

def scale(*ids)
  my.ecs_services_with_ids(*ids).each do |s|
    debug("Scaling service #{s.physical_resource_id.split('/').last}")
    Aws::Ecs.update_service(
      cluster: my.ecs_cluster_name,
      service: s.physical_resource_id,
      desired_count: options[:desired],
    ).tap do |s|
      puts "desired: #{s.desired_count}"
    end
  end
end

#secretsObject



176
177
178
179
180
181
182
183
184
185
# File 'lib/stax/mixin/ecs.rb', line 176

def secrets
  my.ecs_task_families.each do |family|
    Aws::Ecs.task_definition(family).container_definitions.each do |c|
      debug("Secrets for #{family} #{c.name}")
      print_table c.secrets.map { |e|
        [e.name, e.value_from]
      }.sort
    end
  end
end

#servicesObject



110
111
112
113
114
# File 'lib/stax/mixin/ecs.rb', line 110

def services
  print_table my.ecs_service_objects.map { |s|
    [s.service_name, color(s.status, COLORS), s.task_definition.split('/').last, "#{s.running_count}/#{s.desired_count}"]
  }
end

#stop_task(task) ⇒ Object



260
261
262
263
264
# File 'lib/stax/mixin/ecs.rb', line 260

def stop_task(task)
  Aws::Ecs.stop(my.ecs_cluster_name, task).tap do |task|
    puts task.container_instance_arn
  end
end

#tail(service = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stax/mixin/ecs.rb', line 126

def tail(service = nil)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  service ||= my.ecs_service_names.first
  latest_event = Aws::Ecs.services(my.ecs_cluster_name, [service]).first.events.first
  print_event(latest_event)
  last_seen = latest_event.id
  loop do
    sleep 5
    unseen = []
    Aws::Ecs.services(my.ecs_cluster_name, [service]).first.events.each do |e|
      break if e.id == last_seen
      unseen.unshift(e)
    end
    unseen.each(&method(:print_event))
    last_seen = unseen.last.id unless unseen.empty?
  end
end

#tasksObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/stax/mixin/ecs.rb', line 189

def tasks
  my.ecs_services.each do |s|
    name = s.physical_resource_id.split('/').last
    debug("Tasks for service #{name}")
    Aws::Ecs.tasks(
      cluster: my.ecs_cluster_name,
      service_name: s.physical_resource_id,
      desired_status: options[:status].upcase,
    ).map { |t|
      [
        t.task_arn.split('/').last,
        t.task_definition_arn.split('/').last,
        t.container_instance_arn&.split('/')&.last || '--',
        color(t.last_status, COLORS),
        "(#{t.desired_status})",
        t.started_by,
      ]
    }.tap(&method(:print_table))
  end
end