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

- old
+ new

@@ -5,58 +5,84 @@ # ECS class class Contextualize attr_reader :display, :cluster, :service def initialize(params = {}) - @cluster = params.fetch(:cluster) + @cluster = params.fetch(:cluster) if params[:cluster] @service = params.fetch(:service) if params[:service] @container = params.fetch(:container) if params[:container] - @ssh = params.fetch(:ssh) + @rake = params.fetch(:rake) if params[:rake] + @console = params.fetch(:console) @display = Contexto::Display.new end def run - if @ssh + if (@console || @rake) task = describe_tasks ec2_instance_id = describe_container_instance(task[:container_instance_arn]) - @connect = Contexto::SSH.new(describe_instances(ec2_instance_id), @cluster, @service, @container) + @ssh = Contexto::SSH.new(describe_instances(ec2_instance_id), @cluster, @service, @container) + if @console + @ssh.console + elsif @rake + @ssh.rake(@rake) + end return end + if !@cluster + clusters + return + end cluster puts "\n" - return unless @service - service - puts "\n" - tasks + if !@service + services + end + if @service + puts "\n" + service + puts "\n" + tasks + end end private + def 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 - resp = ecs_client.describe_clusters( - clusters: [ - @cluster - ] - ) title = "Cluster #{@cluster}" headings = %w(Name Status Instances Services RunningTasks PendingTasks) - cluster = resp.clusters[0].to_h + 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 + 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 - resp = ecs_client.describe_services( - cluster: @cluster, - services: [ - @service - ] - ) title = "Service #{@service}" headings = %w(Name Status Desired Running Pending) - service = resp.services[0].to_h + 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' @@ -71,9 +97,40 @@ rows = [] containers.each do |container| rows << [container[:name], container[:last_status], describe_instances(ec2_instance_id)] end @display.create_display(title, headings, rows) + end + + def list_clusters + resp = ecs_client.list_clusters + resp.cluster_arns + end + + def describe_clusters + resp = ecs_client.describe_clusters( + clusters: [ + @cluster + ] + ) + resp.clusters[0].to_h + end + + def list_services + resp = ecs_client.list_services( + cluster: @cluster + ) + resp.service_arns + end + + def describe_services + resp = ecs_client.describe_services( + cluster: @cluster, + services: [ + @service + ] + ) + resp.services[0].to_h end def describe_tasks resp = ecs_client.describe_tasks( cluster: @cluster,