class BBC::Cosmos::Tools::Commands::Component

Public Instance Methods

instances(component) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 13
def instances(component)
  say banner + "\n"

  ec2 = AWS::EC2.new aws_credentials

  tag = options[:show_tag]

  rows = instance_ids(component).each_with_index.map do |id, i|
    meta = make_request("env/#{options[:env]}/component/#{component}/instances")
    ip = meta[i]["private_ip_address"]
    id = "#{id} (#{ip})"

    [id].tap do |row|
      row << ec2.instances[id].tags[tag] if tag
    end
  end

  headers = ["Instance ID"]
  headers << tag if tag

  print_table build_table(headers, rows)
end
ssh(component) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 37
def ssh(component)
  say banner(component) + "\n"
  meta = make_request("env/#{options[:env]}/component/#{component}/instances")

  meta.count == 1 ? single_instance(component, meta)
                  : multiple_instance(component, meta)
end

Private Instance Methods

api() click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 47
def api
  @api ||= BBC::Cosmos::Tools::API.new(
    "https://api.live.bbc.co.uk", options[:key_path]
  )
end
aws_credentials() click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 126
def aws_credentials
  response = BBC::Cosmos::Tools::API.new(
    "https://wormhole.cloud.bbc.co.uk",
    options[:key_path]
  ).get "/login/credentials"

  fail Exception, response.body if response.status != 200

  credentials = JSON.parse response.body

  { :access_key_id     => credentials["accessKeyId"],
    :secret_access_key => credentials["secretAccessKey"],
    :session_token     => credentials["sessionToken"],
    :region            => "eu-west-1" }
end
check_status(login) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 68
def check_status(login)
  url = JSON.parse(login.body)['url']
  status = "pending_creation"
  say "\n"
  until status == "current"
    sleep 2
    say "Creating Session", :yellow
    status = make_request(url)["status"]
    fail Exception, (say "SSH Failed"; :red) if status == "failed"
  end
end
extract_ipaddress(meta, instance) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 90
def extract_ipaddress(meta, instance)
  meta.detect { |hash| hash.value?(instance) }.fetch("private_ip_address")
end
instance_ids(component) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 113
def instance_ids(component)
  response = BBC::Cosmos::Tools::API.new(
    config.app["api"],
    options[:key_path]
  ).get "/cosmos/env/#{options[:env]}/component/#{component}/instances"

  fail Exception, response.body if response.status != 200

  JSON.parse(response.body).map do |instance|
    instance.fetch "id"
  end
end
make_request(path) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 107
def make_request(path)
  JSON.parse(api.get("/cosmos/#{path}").tap do |request|
    fail Exception, request.body if request.status != 200
  end.body)
end
multiple_instance(component, meta) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 57
def multiple_instance(component, meta)
  instance_id = select_instance(meta)
  session(component, instance_id, extract_ipaddress(meta, instance_id))
end
output_message(component, ip, id) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 94
def output_message(component, ip, id)
  say "\nYour IP for #{id} is #{ip}", :green
end
post_data(component, id) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 98
def post_data(component, id)
  post_data = { "instance_id" => id }

  endpoint = "/cosmos/env/#{options[:env]}/component/#{component}/logins/create"
  api.post(endpoint, post_data.to_json).tap do |response|
    fail Exception, response.body if response.status != 201
  end
end
select_instance(meta) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 80
def select_instance(meta)
  choose do |menu|
    say "Multiple instances detected, please select one", :green

    meta.map do |i|
      menu.choice(i["id"].to_sym)
    end
  end.to_s
end
session(component, instance_id, ip_address) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 62
def session(component, instance_id, ip_address)
  response = post_data(component, instance_id)
  check_status(response)
  output_message(component, ip_address, instance_id)
end
single_instance(component, meta) click to toggle source
# File lib/bbc/cosmos/tools/commands/component.rb, line 53
def single_instance(component, meta)
  session(component, meta[0]["id"], meta[0]["private_ip_address"])
end