Skip to content

Commit

Permalink
list all uc3 boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
terrywbrady committed Mar 15, 2024
1 parent 6c391ad commit c742c84
Showing 4 changed files with 202 additions and 0 deletions.
187 changes: 187 additions & 0 deletions src-colladmin/actions/tag_uc3_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# frozen_string_literal: true

require_relative 'action'
require 'aws-sdk-ec2'
require 'aws-sdk-ssm'

# represents information about an EC2 instance
class Ec2InfoUc3

# See https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/EC2/Client.html#describe_instances-instance_method
def initialize(config, inst)
@config = config
@state = inst.state.name
@type = inst.instance_type
@publicip = inst.public_ip_address
@az = inst.placement.availability_zone
inst.tags.each do |tag|
@name = tag.value if tag.key == 'Name'
@subservice = tag.value if tag.key == 'Subservice'
end
end

attr_reader :name

def self.table_headers
[
'Name',
'Subservice',
'Type',
'SERVER State',
'IP',
'AZ'
]
end

def self.table_types
[
'',
'',
'',
'',
'',
''
]
end

def table_row(action)
[
@name,
@subservice,
@type,
@state,
@publicip,
@az
]
end
end

# Collection Admin Task class - see config/actions.yml for description
class TagUc3Action < AdminAction
def initialize(config, action, path, myparams)
super(config, action, path, myparams)
region = ENV['AWS_REGION'] || 'us-west-2'
@ec2 = Aws::EC2::Client.new(
region: region
)
@ssm = Aws::SSM::Client.new(
region: region
)
@title = 'Merritt EC2 Instances'
@instances = {}
@name = myparams.fetch('name', '')
@label = myparams.fetch('label', '')
@list_servers = @name.empty?

data = @ec2.describe_instances({
filters: [
{
name: 'tag:Program',
values: [LambdaBase.tag_program]
},
{
name: 'tag:Environment',
values: [LambdaBase.tag_environment]
}
]
})

data.reservations.each do |res|
res.instances.each do |inst|
ec2 = Ec2InfoUc3.new(config, inst)
@instances[ec2.name] = ec2
end
end
end

def get_ssm(key)
@ssm.get_parameter({ name: "#{LambdaBase.ssm_root_path}#{key}" })[:parameter][:value]
rescue StandardError
''
end

def get_title
@title
end

def table_headers
Ec2InfoUc3.table_headers
end

def table_types
Ec2InfoUc3.table_types
end

def endpoint_call
return unless @instances[@name]

ec2 = @instances[@name]
url = ec2.urls.fetch(@label, '')
return 'No Url found' if url.empty?

cli = HTTPClient.new
cli.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
if @label == 'stop' || @label == 'start'
puts "POST #{url}"
resp = cli.post(url)
else
puts "GET #{url}"
resp = cli.get(url, follow_redirect: true)
end
ret = resp.body
return ret if @label == 'build-info'

if resp.status == 200
begin
JSON.parse(resp.body)
rescue StandardError
ret = { body: resp.dump }.to_json
end
else
ret = {
message: "Status #{resp.status} for #{url}"
}.to_json
end
ret
end

def perform_action
return endpoint_call unless @list_servers

evaluate_status(table_types, get_table_rows)
{
format: 'report',
title: get_title_with_pagination,
breadcrumb: get_breadcrumb,
headers: table_headers,
types: table_types,
data: get_table_rows,
filter_col: nil,
group_col: nil,
show_grand_total: false,
merritt_path: @merritt_path,
alternative_queries: get_alternative_queries_with_pagination,
iterate: false,
saveable: is_saveable?,
report_path: report_path,
chart: nil,
description: get_description
}.to_json
end

def get_table_rows
rows = []
@instances.keys.sort.each do |k|
rows.append(@instances[k].table_row(self))
end
rows
end

def has_table
@list_servers
end

def get_alternative_queries
[]
end
end
13 changes: 13 additions & 0 deletions src-colladmin/config/actions.yml
Original file line number Diff line number Diff line change
@@ -347,6 +347,19 @@ instances:
- instances
documentation: |
Aws: Tag Query
uc3-instances:
link-title: List and Describe UC3 EC2 Servers
breadcrumb: bp_internals
class: TagUc3Action
category: Dev Ops
sensitivity: readonly
testing: automated
description: |
Generate a report describing the EC2 servers used by UC3.
report-datatypes:
- instances
documentation: |
Aws: Tag Query
ssm-describe:
link-title: List and Describe Merritt SSM Variables
breadcrumb: bp_internals
1 change: 1 addition & 0 deletions src-colladmin/lambda_function.rb
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
require_relative 'actions/cognito_action'
require_relative 'actions/storage_action'
require_relative 'actions/tag_action'
require_relative 'actions/tag_uc3_action'
require_relative 'actions/ssm_describe_action'
require_relative 'actions/opensearch_describe_action'
require_relative 'actions/replication_action'
1 change: 1 addition & 0 deletions src-common/template/navmenu.html
Original file line number Diff line number Diff line change
@@ -105,6 +105,7 @@
<a class="reveal" href="{{ADMINTOOL_HOME}}#.nav_merritt_internals;.bp_internals">Merritt Admin Objects</a>
<a class="reveal" href="{{ADMINTOOL_HOME}}#.nav_merritt_users;.bp_users">Merritt User Accounts</a>
<a class="config" href="{{COLLADMIN_HOME}}?path=instances">Merritt Servers</a>
<a class="config" href="{{COLLADMIN_HOME}}?path=uc3-instances">UC3 Servers</a>
<a class="config" href="{{COLLADMIN_HOME}}?path=ssm-describe">SSM Parameter Registry</a>
<a class="config" href="{{COLLADMIN_HOME}}?path=opensearch-describe">OpenSearch Field Registry</a>
</div>

0 comments on commit c742c84

Please sign in to comment.