lib/contexto/contextualize.rb in contexto-0.2.4 vs lib/contexto/contextualize.rb in contexto-0.3.0
- old
+ new
@@ -1,63 +1,79 @@
require 'aws-sdk'
+require 'highline'
# Context
module Contexto
# ECS class
class Contextualize
- attr_reader :display, :cluster, :service, :container, :console, :bash, :ssh, :rake
+ attr_reader :display, :cluster, :service, :container, :connection_type
def initialize(params = {})
@cluster = params.fetch(:cluster) if params[:cluster]
@service = params.fetch(:service) if params[:service]
@container = params.fetch(:container) if params[:container]
- @rake = params.fetch(:rake) if params[:rake]
- @console = params.fetch(:console)
- @bash = params.fetch(:bash)
- @ssh = params.fetch(:ssh)
+ @connection_type = params.fetch(:connection_type) if params[:connection_type]
@display = Contexto::Display.new
end
def run
- if connect?
- task = describe_tasks
- ec2_instance_id = describe_container_instance(task[:container_instance_arn])
- @connection = Contexto::SSH.new(describe_instances(ec2_instance_id), cluster, service, container)
- if console
- @connection.console
- elsif bash
- @connection.bash
- elsif ssh
- @connection.ssh
- elsif rake
- @connection.rake(rake)
- end
- return
+ show
+ if (connection_type && container)
+ connect_to_endpoint
+ elsif container
+ prompt_endpoint
end
+ end
+
+ def show
if !cluster
show_clusters
return
+ elsif cluster
+ show_cluster
+ puts "\n"
end
- show_cluster
- puts "\n"
if !service
show_services
- end
- if service
+ return
+ elsif service
puts "\n"
show_service
- puts "\n"
+ end
+ if !container
show_tasks
+ return
+ elsif container
+ puts "\n"
+ show_tasks(container)
end
end
- private
+ 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?
- (console || bash || ssh || rake)
+ def connect_to_endpoint
+ connnect.connection_type.to_sym
+ return
end
+ def prompt_endpoint
+ cli = HighLine.new
+ 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.choice(:nah) { return }
+ end
+ end
+
def show_clusters
title = "Clusters"
headings = %w(Name)
clusters = list_clusters
rows = []
@@ -96,12 +112,16 @@
@display.create_display(title, headings, rows)
rescue Aws::ECS::Errors::ServiceNotFoundException
puts 'Service not found'
end
- def show_tasks
+ def show_tasks(container = '')
task = describe_tasks
- containers = task[:containers]
+ if container
+ containers = task[:containers].select { |c| container == c[:name] }
+ else
+ containers = task[:containers]
+ end
ec2_instance_id = describe_container_instance(task[:container_instance_arn])
title = 'Containers'
headings = %w(Container Status IP)
rows = []
containers.each do |container|