Add token header option for requests to Poseidon
This commit is contained in:

committed by
Sebastian Serth

parent
44395b7792
commit
b51a45e9b1
@ -16,6 +16,7 @@ default: &default
|
|||||||
strategy: poseidon
|
strategy: poseidon
|
||||||
url: https://runners.example.org
|
url: https://runners.example.org
|
||||||
ca_file: /example/certificates/ca.crt
|
ca_file: /example/certificates/ca.crt
|
||||||
|
token: SECRET
|
||||||
unused_runner_expiration_time: 180
|
unused_runner_expiration_time: 180
|
||||||
|
|
||||||
development:
|
development:
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Runner::Strategy::Poseidon < Runner::Strategy
|
class Runner::Strategy::Poseidon < Runner::Strategy
|
||||||
HEADERS = {'Content-Type' => 'application/json'}.freeze
|
|
||||||
ERRORS = %w[NOMAD_UNREACHABLE NOMAD_OVERLOAD NOMAD_INTERNAL_SERVER_ERROR UNKNOWN].freeze
|
ERRORS = %w[NOMAD_UNREACHABLE NOMAD_OVERLOAD NOMAD_INTERNAL_SERVER_ERROR UNKNOWN].freeze
|
||||||
|
|
||||||
ERRORS.each do |error|
|
ERRORS.each do |error|
|
||||||
@ -14,10 +13,14 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
@config ||= CodeOcean::Config.new(:code_ocean).read[:runner_management] || {}
|
@config ||= CodeOcean::Config.new(:code_ocean).read[:runner_management] || {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.headers
|
||||||
|
@headers ||= {'Content-Type' => 'application/json', 'Poseidon-Token' => config[:token]}
|
||||||
|
end
|
||||||
|
|
||||||
def self.sync_environment(environment)
|
def self.sync_environment(environment)
|
||||||
url = "#{config[:url]}/execution-environments/#{environment.id}"
|
url = "#{config[:url]}/execution-environments/#{environment.id}"
|
||||||
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
||||||
response = connection.put url, environment.to_json, HEADERS
|
response = connection.put url, environment.to_json, headers
|
||||||
return true if [201, 204].include? response.status
|
return true if [201, 204].include? response.status
|
||||||
|
|
||||||
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
|
Rails.logger.warn("Could not create execution environment in Poseidon, got response: #{response.as_json}")
|
||||||
@ -34,7 +37,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
inactivityTimeout: config[:unused_runner_expiration_time].seconds,
|
inactivityTimeout: config[:unused_runner_expiration_time].seconds,
|
||||||
}
|
}
|
||||||
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
connection = Faraday.new nil, ssl: {ca_file: config[:ca_file]}
|
||||||
response = connection.post url, body.to_json, HEADERS
|
response = connection.post url, body.to_json, headers
|
||||||
|
|
||||||
case response.status
|
case response.status
|
||||||
when 200
|
when 200
|
||||||
@ -94,7 +97,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
url = "#{runner_url}/files"
|
url = "#{runner_url}/files"
|
||||||
body = {copy: copy}
|
body = {copy: copy}
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
||||||
response = connection.patch url, body.to_json, HEADERS
|
response = connection.patch url, body.to_json, self.class.headers
|
||||||
return if response.status == 204
|
return if response.status == 204
|
||||||
|
|
||||||
Runner.destroy(@allocation_id) if response.status == 400
|
Runner.destroy(@allocation_id) if response.status == 400
|
||||||
@ -112,7 +115,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
|
|
||||||
def destroy_at_management
|
def destroy_at_management
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
||||||
response = connection.delete runner_url, nil, HEADERS
|
response = connection.delete runner_url, nil, self.class.headers
|
||||||
self.class.handle_error response unless response.status == 204
|
self.class.handle_error response unless response.status == 204
|
||||||
rescue Faraday::Error => e
|
rescue Faraday::Error => e
|
||||||
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
|
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
|
||||||
@ -124,7 +127,7 @@ class Runner::Strategy::Poseidon < Runner::Strategy
|
|||||||
url = "#{runner_url}/execute"
|
url = "#{runner_url}/execute"
|
||||||
body = {command: command, timeLimit: @execution_environment.permitted_execution_time}
|
body = {command: command, timeLimit: @execution_environment.permitted_execution_time}
|
||||||
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
connection = Faraday.new nil, ssl: {ca_file: self.class.config[:ca_file]}
|
||||||
response = connection.post url, body.to_json, HEADERS
|
response = connection.post url, body.to_json, self.class.headers
|
||||||
case response.status
|
case response.status
|
||||||
when 200
|
when 200
|
||||||
response_body = self.class.parse response
|
response_body = self.class.parse response
|
||||||
|
Reference in New Issue
Block a user