Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Sep 3, 2024
1 parent 46678b2 commit c05a959
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/services/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def process
def handle_ecs_event(event)
case event.group
when ENV.fetch("SWITCH_GROUP")
HandleSwitchEvent.call(event:)
HandleSwitchEvent.call(event:, regions: SomlengRegions.regions)
when ENV.fetch("MEDIA_PROXY_GROUP")
HandleMediaProxyEvent.call(event:)
when ENV.fetch("CLIENT_GATEWAY_GROUP")
Expand Down
4 changes: 3 additions & 1 deletion components/services/app/parsers/ecs_event_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ECSEventParser
:public_ip,
:group,
:event_type,
:region,
keyword_init: true
)

Expand All @@ -33,7 +34,8 @@ def parse_event
eni_private_ip:,
private_ip:,
public_ip:,
group:
group:,
region:
)
end

Expand Down
11 changes: 8 additions & 3 deletions components/services/app/workflows/handle_switch_event.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
class HandleSwitchEvent < ApplicationWorkflow
attr_reader :event
attr_reader :event, :regions

def initialize(event:)
def initialize(event:, regions:)
@event = event
@regions = regions
end

def call
if event.task_running? && event.eni_attached?
load_balancer_manager.create_targets
load_balancer_manager.create_targets(group_id: load_balancer_group)
elsif event.task_stopped? && event.eni_deleted?
load_balancer_manager.delete_targets
end
end

private

def load_balancer_group
regions.find_by!(identifier: event.region).group_id
end

def load_balancer_manager
@load_balancer_manager ||= ManageLoadBalancerTargets.new(ip_address: event.private_ip)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ def initialize(ip_address:)
@ip_address = ip_address
end

def create_targets
def create_targets(**)
gateway_databases.each do |database_connection|
database_connection.transaction do
load_balancer_targets.each do |load_balancer_target|
create_opensips_load_balancer_target!(load_balancer_target:, database_connection:)
create_opensips_load_balancer_target!(load_balancer_target:, database_connection:, **)
end
end
end
Expand All @@ -26,15 +26,16 @@ def delete_targets

private

def create_opensips_load_balancer_target!(load_balancer_target:, database_connection:)
def create_opensips_load_balancer_target!(load_balancer_target:, database_connection:, **attributes)
return if OpenSIPSLoadBalancerTarget.exists?(dst_uri: load_balancer_target.dst_uri, database_connection:)

OpenSIPSLoadBalancerTarget.new(
dst_uri: load_balancer_target.dst_uri,
resources: load_balancer_target.resources,
group_id: 1,
probe_mode: 2,
database_connection:
database_connection:,
**attributes
).save!
end

Expand Down
6 changes: 3 additions & 3 deletions components/services/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative "app_settings"
require_relative "initializers/aws_stubs"

Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].sort.each { |f| require f }
Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].each { |f| require f }

EncryptedEnvironmentVariables.new.decrypt

Dir["#{File.dirname(__FILE__)}/**/*.rb"].sort.each { |f| require f }
Dir["#{File.dirname(__FILE__)}/../app/**/*.rb"].sort.each { |f| require f }
Dir["#{File.dirname(__FILE__)}/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/../app/**/*.rb"].each { |f| require f }
3 changes: 3 additions & 0 deletions components/services/config/initializers/somleng_regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SomlengRegions.configure do |config|
config.region_data = ENV.fetch("REGION_DATA")
end
22 changes: 22 additions & 0 deletions components/services/lib/somleng_regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SomlengRegions
class << self
def configure
yield(configuration)
configuration
end

def configuration
@configuration ||= Configuration.new
end
alias config configuration

def regions
@regions ||= Collection.new(Parser.new.parse(configuration.region_data))
end
end
end

require_relative "somleng_regions/configuration"
require_relative "somleng_regions/parser"
require_relative "somleng_regions/collection"
require_relative "somleng_regions/region"
21 changes: 21 additions & 0 deletions components/services/lib/somleng_regions/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module SomlengRegions
class Collection
class RegionNotFoundError < StandardError; end

attr_reader :regions

def initialize(regions)
@regions = regions
end

def find_by(attributes)
regions.find do |region|
attributes.all? { |key, value| region[key] == value }
end
end

def find_by!(*)
find_by(*) || raise(RegionNotFoundError.new)
end
end
end
5 changes: 5 additions & 0 deletions components/services/lib/somleng_regions/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module SomlengRegions
class Configuration
attr_accessor :region_data
end
end
7 changes: 7 additions & 0 deletions components/services/lib/somleng_regions/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SomlengRegions
class Parser
def parse(region_data)
JSON.parse(region_data).map { |region| Region.new(region) }
end
end
end
5 changes: 5 additions & 0 deletions components/services/lib/somleng_regions/region.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "ostruct"

module SomlengRegions
class Region < OpenStruct; end
end
4 changes: 4 additions & 0 deletions components/services/spec/requests/ecs_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
it "handles switch events" do
stub_env("SWITCH_GROUP" => "service:somleng-switch")
payload = build_ecs_event_payload(
region: "us-east-1",
group: "service:somleng-switch",
eni_private_ip: "10.1.1.100",
eni_status: "ATTACHED",
Expand All @@ -14,6 +15,9 @@

expect(public_gateway_load_balancer.count).to eq(2)
expect(client_gateway_load_balancer.count).to eq(2)
expect(public_gateway_load_balancer.first).to include(
group_id: 2
)
end

it "handles client gateway events" do
Expand Down
15 changes: 15 additions & 0 deletions components/services/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
ENV["PUBLIC_GATEWAY_DB_NAME"] = "opensips_public_gateway_test"
ENV["CLIENT_GATEWAY_DB_NAME"] = "opensips_client_gateway_test"
ENV["MEDIA_PROXY_NG_PORT"] = "2223"
ENV["REGION_DATA"] = [
{
"alias" => "hydrogen",
"identifier" => "ap-southeast-1",
"human_name" => "Asia",
"group_id" => 1
},
{
"alias" => "helium",
"identifier" => "us-east-1",
"human_name" => "North America",
"group_id" => 2
}
].to_json


require_relative "../app"

Expand Down
3 changes: 2 additions & 1 deletion infrastructure/modules/services/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ resource "aws_iam_policy" "custom_policy" {
"Resource": [
"${var.freeswitch_event_socket_password_parameter.arn}",
"${aws_ssm_parameter.application_master_key.arn}",
"${var.db_password_parameter.arn}"
"${var.db_password_parameter.arn}",
"${var.region_data_parameter.arn}"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions infrastructure/modules/services/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ resource "aws_lambda_function" "this" {
MEDIA_PROXY_NG_PORT = var.media_proxy_ng_port
DB_PASSWORD_SSM_PARAMETER_NAME = var.db_password_parameter.name
APP_MASTER_KEY_SSM_PARAMETER_NAME = aws_ssm_parameter.application_master_key.name
REGION_DATA_SSM_PARAMETER_NAME = var.region_data_parameter.name
APP_ENV = var.app_environment
DB_HOST = var.db_host
DB_PORT = var.db_port
Expand Down
1 change: 1 addition & 0 deletions infrastructure/modules/services/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ variable "switch_group" {}
variable "media_proxy_group" {}
variable "client_gateway_group" {}
variable "db_password_parameter" {}
variable "region_data_parameter" {}
variable "freeswitch_event_socket_password_parameter" {}
variable "db_security_group" {}
variable "freeswitch_event_socket_port" {}
Expand Down
1 change: 1 addition & 0 deletions infrastructure/staging/services.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module "services" {

db_password_parameter = data.terraform_remote_state.core_infrastructure.outputs.db_master_password_parameter
freeswitch_event_socket_password_parameter = data.aws_ssm_parameter.freeswitch_event_socket_password
region_data_parameter = data.aws_ssm_parameter.region_data

db_security_group = data.terraform_remote_state.core_infrastructure.outputs.db_security_group
db_username = data.terraform_remote_state.core_infrastructure.outputs.db_cluster.master_username
Expand Down
4 changes: 4 additions & 0 deletions infrastructure/staging/ssm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ data "aws_ssm_parameter" "somleng_services_password" {
data "aws_ssm_parameter" "freeswitch_event_socket_password" {
name = "somleng-switch.staging.freeswitch_event_socket_password"
}

data "aws_ssm_parameter" "region_data" {
name = "somleng.staging.region_data"
}

0 comments on commit c05a959

Please sign in to comment.