Skip to content

Commit

Permalink
Change open-method for external-as-topology script to eliminate conta…
Browse files Browse the repository at this point in the history
…iner reload when the script was updated
  • Loading branch information
corestate55 committed Jan 3, 2024
1 parent 1e1952a commit 0b0be82
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions lib/api/topologies/network/snapshot/external_as_topology.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require 'grape'
require 'json'
require 'open3'

module NetomoxExp
module ApiRoute
Expand All @@ -10,26 +12,17 @@ class ExternalAsTopology < Grape::API
resource 'external_as_topology' do
get do
network, snapshot = %i[network snapshot].map { |key| params[key] }
# NOTE: to `load` topology script, it must be absolute-path
# see also: https://docs.ruby-lang.org/ja/latest/method/Kernel/m/load.html
# NOTE: absolute-path of external_as_script
ext_topo_dir = File.join(CONFIGS_DIR, network, snapshot, 'external_as_topology')
puts "# DEBUG: ext_topo_dir=#{ext_topo_dir}"
error!("#{network}/#{snapshot} does not have external-AS topology dir", 404) unless Dir.exist?(ext_topo_dir)

ext_topo_script = File.join(ext_topo_dir, 'main.rb')
error!("External-AS topology script is not found in #{ext_topo_dir}", 404) unless File.exist?(ext_topo_script)

# param [String] script
# return [String] external-AS topology data (RFC8345 json string)
generate_topology_proc = lambda do |script|
# (re)load every call
load script
# the script defines `generate_topology` function that returns RFC8345 topology json string as external-AS
generate_topology
end

# response
generate_topology_proc.call(ext_topo_script)
command = "ruby #{ext_topo_script}"
logger.info "Call external script: #{command}"
output, _status = Open3.capture2(command)
JSON.parse(output)
end
end
end
Expand Down

0 comments on commit 0b0be82

Please sign in to comment.