lib/contexto/contextualize.rb in contexto-0.2.0 vs lib/contexto/contextualize.rb in contexto-0.2.1

- old
+ new

@@ -2,11 +2,11 @@ # Context module Contexto # ECS class class Contextualize - attr_reader :display, :cluster, :service + attr_reader :display, :cluster, :service, :container, :console, :rake def initialize(params = {}) @cluster = params.fetch(:cluster) if params[:cluster] @service = params.fetch(:service) if params[:service] @container = params.fetch(:container) if params[:container] @@ -14,83 +14,87 @@ @console = params.fetch(:console) @display = Contexto::Display.new end def run - if (@console || @rake) + if ssh? task = describe_tasks ec2_instance_id = describe_container_instance(task[:container_instance_arn]) - @ssh = Contexto::SSH.new(describe_instances(ec2_instance_id), @cluster, @service, @container) - if @console + @ssh = Contexto::SSH.new(describe_instances(ec2_instance_id), cluster, service, container) + if console @ssh.console - elsif @rake - @ssh.rake(@rake) + elsif rake + @ssh.rake(rake) end return end - if !@cluster - clusters + if !cluster + show_clusters return end - cluster + show_cluster puts "\n" - if !@service - services + if !service + show_services end - if @service + if service puts "\n" - service + show_service puts "\n" - tasks + show_tasks end end private - def clusters + def ssh? + (console || rake) + end + + def show_clusters title = "Clusters" headings = %w(Name) clusters = list_clusters rows = [] clusters.each do |cluster| rows << [cluster.split('/')[-1]] end @display.create_display(title, headings, rows) end - def cluster - title = "Cluster #{@cluster}" + def show_cluster + title = "Cluster #{cluster}" headings = %w(Name Status Instances Services RunningTasks PendingTasks) cluster = describe_clusters rows = [] rows << [cluster[:cluster_name], cluster[:status], cluster[:registered_container_instances_count], cluster[:active_services_count], cluster[:running_tasks_count], cluster[:pending_tasks_count]] @display.create_display(title, headings, rows) end - def services + def show_services title = "Services" headings = %w(Name) services = list_services rows = [] services.each do |service| rows << [service.split('/')[-1]] end @display.create_display(title, headings, rows) end - def service - title = "Service #{@service}" + def show_service + title = "Service #{service}" headings = %w(Name Status Desired Running Pending) service = describe_services rows = [] rows << [service[:service_name], service[:status], service[:desired_count], service[:running_count], service[:pending_count]] @display.create_display(title, headings, rows) rescue Aws::ECS::Errors::ServiceNotFoundException puts 'Service not found' end - def tasks + def show_tasks task = describe_tasks containers = task[:containers] ec2_instance_id = describe_container_instance(task[:container_instance_arn]) title = 'Containers' headings = %w(Container Status IP) @@ -107,51 +111,51 @@ end def describe_clusters resp = ecs_client.describe_clusters( clusters: [ - @cluster + cluster ] ) resp.clusters[0].to_h end def list_services resp = ecs_client.list_services( - cluster: @cluster + cluster: cluster ) resp.service_arns end def describe_services resp = ecs_client.describe_services( - cluster: @cluster, + cluster: cluster, services: [ - @service + service ] ) resp.services[0].to_h end def describe_tasks resp = ecs_client.describe_tasks( - cluster: @cluster, + cluster: cluster, tasks: [task_arn] ) resp.tasks[0].to_h end def task_arn resp = ecs_client.list_tasks( - cluster: @cluster, - service_name: @service + cluster: cluster, + service_name: service ) resp.task_arns[0] end def describe_container_instance(container_instance_arn) resp = ecs_client.describe_container_instances( - cluster: @cluster, + cluster: cluster, container_instances: [ container_instance_arn ] ) resp.container_instances[0].ec2_instance_id