From 054d35b8d3b39f6a1afd9198037e2209585f9f58 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Thu, 4 Nov 2021 10:40:01 +0100 Subject: [PATCH] Add environments method to all strategies --- lib/runner/strategy.rb | 4 ++++ lib/runner/strategy/docker_container_pool.rb | 4 ++++ lib/runner/strategy/null.rb | 4 ++++ lib/runner/strategy/poseidon.rb | 21 ++++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/lib/runner/strategy.rb b/lib/runner/strategy.rb index 2a03f39c..313d35fa 100644 --- a/lib/runner/strategy.rb +++ b/lib/runner/strategy.rb @@ -9,6 +9,10 @@ class Runner::Strategy raise NotImplementedError end + def self.environments + raise NotImplementedError + end + def self.sync_environment(_environment) raise NotImplementedError end diff --git a/lib/runner/strategy/docker_container_pool.rb b/lib/runner/strategy/docker_container_pool.rb index f82394c1..0ec17c2b 100644 --- a/lib/runner/strategy/docker_container_pool.rb +++ b/lib/runner/strategy/docker_container_pool.rb @@ -14,6 +14,10 @@ class Runner::Strategy::DockerContainerPool < Runner::Strategy FileUtils.mkdir_p(File.expand_path(config[:workspace_root])) end + def self.environments + pool_size.keys.map {|key| {id: key} } + end + def self.sync_environment(environment) # Force a database commit and start a new transaction. if environment.class.connection.transaction_open? diff --git a/lib/runner/strategy/null.rb b/lib/runner/strategy/null.rb index 1c5f34a8..4702e6f4 100644 --- a/lib/runner/strategy/null.rb +++ b/lib/runner/strategy/null.rb @@ -5,6 +5,10 @@ class Runner::Strategy::Null < Runner::Strategy def self.initialize_environment; end + def self.environments + raise Runner::Error.new + end + def self.sync_environment(_environment) raise Runner::Error.new end diff --git a/lib/runner/strategy/poseidon.rb b/lib/runner/strategy/poseidon.rb index 8a83e62f..195d32db 100644 --- a/lib/runner/strategy/poseidon.rb +++ b/lib/runner/strategy/poseidon.rb @@ -19,6 +19,27 @@ class Runner::Strategy::Poseidon < Runner::Strategy nil end + def self.environments + url = "#{config[:url]}/execution-environments" + Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Getting list of execution environments at #{url}" } + response = http_connection.get url + + case response.status + when 200 + response_body = parse response + execution_environments = response_body[:executionEnvironments] + execution_environments.presence || raise(Runner::Error::UnexpectedResponse.new("Could not get the list of execution environments in Poseidon, got response: #{response.as_json}")) + when 404 + raise Runner::Error::EnvironmentNotFound.new + else + handle_error response + end + rescue Faraday::Error => e + raise Runner::Error::FaradayError.new("Could not get the list of execution environments because of Faraday error: #{e.inspect}") + ensure + Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished getting the list of execution environments" } + end + def self.sync_environment(environment) url = "#{config[:url]}/execution-environments/#{environment.id}" Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Synchronizing execution environment at #{url}" }