lib/contexto/contextualize.rb in contexto-0.3.1 vs lib/contexto/contextualize.rb in contexto-0.4.0

- old
+ new

@@ -3,136 +3,104 @@ # Context module Contexto # ECS class class Contextualize - attr_reader :display, :cluster, :service, :container, :connection_type + attr_reader :display, :cluster, :service def initialize(params = {}) @cluster = params.fetch(:cluster) if params[:cluster] @service = params.fetch(:service) if params[:service] - @container = params.fetch(:container) if params[:container] - @connection_type = params.fetch(:connection_type) if params[:connection_type] @display = Contexto::Display.new end def run - show - if (connection_type && container) - connect_to_endpoint - elsif container - prompt_endpoint + if (cluster || service) + if (cluster && !service) + select_services(cluster) + elsif (cluster && service) + select_tasks(cluster, service) + end + else + select_clusters end end - def show - if !cluster - show_clusters - return - elsif cluster - show_cluster - puts "\n" - end - if !service - show_services - return - elsif service - puts "\n" - show_service - end - if !container - show_tasks - return - elsif container - puts "\n" - show_tasks - end + def select_clusters + clusters = list_clusters + cli.choose do |menu| + puts "\n" + menu.header = "Clusters " + menu.prompt = "Please choose a cluster? " + clusters.each do |cluster| + c = esplit(cluster) + menu.choice(c) { select_services(c) } + end + menu.choice(:nah) { return } + end end - def connect - task = describe_tasks - ec2_instance_id = describe_container_instance(task[:container_instance_arn]) - Contexto::SSH.new(describe_instances(ec2_instance_id), cluster, service, container) - end - - def connect_to_endpoint - connnect.connection_type.to_sym - return - end - - def prompt_endpoint - cli = HighLine.new + def select_services(cluster) + services = list_services(cluster) cli.choose do |menu| puts "\n" - menu.header = "Do you want to connect to container #{container}'s? " - menu.prompt = "Please choose a connection type? " - menu.choice(:console) { connect.console } - menu.choice(:bash) { connect.bash } - menu.choice(:ssh) { connect.ssh } + menu.header = "Services " + menu.prompt = "Please choose a service? " + services.each do |service| + s = esplit(service) + menu.choice(s) { select_tasks(cluster, s) } + end menu.choice(:nah) { return } end end - def show_clusters - title = "Clusters" - headings = %w(Name) - clusters = list_clusters - rows = [] - clusters.each do |cluster| - rows << [cluster.split('/')[-1]] + def select_tasks(cluster, service) + show_tasks(cluster, service) + task = describe_tasks(cluster, service) + containers = task[:containers] + cli.choose do |menu| + puts "\n" + menu.header = "Containers " + menu.prompt = "Please choose a container? " + containers.each do |container| + menu.choice(container[:name]) { prompt_endpoint(cluster, service, container[:name]) } + end + menu.choice(:nah) { return } end - @display.create_display(title, headings, rows) end - 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 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 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 show_tasks - task = describe_tasks - if container - containers = task[:containers].select { |c| c[:name] == container } - else - containers = task[:containers] - end - ec2_instance_id = describe_container_instance(task[:container_instance_arn]) + def show_tasks(cluster, service) + task = describe_tasks(cluster, service) + containers = task[:containers] + ec2_instance_id = describe_container_instance(cluster, task[:container_instance_arn]) title = 'Containers' headings = %w(Container Status IP) 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 prompt_endpoint(cluster, service, container) + cli.choose do |menu| + puts "\n" + menu.header = "Do you want to connect to container #{container}? " + menu.prompt = "Please choose a connection type? " + menu.choice(:console) { connect(cluster, service, container).console } + menu.choice(:bash) { connect(cluster, service, container).bash } + menu.choice(:ssh) { connect(cluster, service, container).ssh } + menu.choice(:nah) { return } + end + end + + def connect(cluster, service, container) + task = describe_tasks(cluster, service) + ec2_instance_id = describe_container_instance(cluster, task[:container_instance_arn]) + Contexto::SSH.new(describe_instances(ec2_instance_id), cluster, service, container) + end + def list_clusters resp = ecs_client.list_clusters resp.cluster_arns end @@ -143,11 +111,11 @@ ] ) resp.clusters[0].to_h end - def list_services + def list_services(cluster) resp = ecs_client.list_services( cluster: cluster ) resp.service_arns end @@ -160,27 +128,27 @@ ] ) resp.services[0].to_h end - def describe_tasks + def describe_tasks(cluster, service) resp = ecs_client.describe_tasks( cluster: cluster, - tasks: [task_arn] + tasks: [task_arn(cluster, service)] ) resp.tasks[0].to_h end - def task_arn + def task_arn(cluster, service) resp = ecs_client.list_tasks( cluster: cluster, service_name: service ) resp.task_arns[0] end - def describe_container_instance(container_instance_arn) + def describe_container_instance(cluster, container_instance_arn) resp = ecs_client.describe_container_instances( cluster: cluster, container_instances: [ container_instance_arn ] @@ -191,9 +159,17 @@ def describe_instances(ec2_instance_id) resp = ec2_client.describe_instances( instance_ids: [ec2_instance_id] ) resp.reservations[0].instances[0].private_ip_address + end + + def esplit(name) + name.split('/')[-1] + end + + def cli + HighLine.new end def region_name ENV['AWS_REGION'] || 'us-east-1' end