Editor: Allow file retrieval after code run

This commit is contained in:
Sebastian Serth
2022-10-04 15:17:16 +02:00
committed by Sebastian Serth
parent fb9672c7a4
commit 60078701f5
22 changed files with 311 additions and 8 deletions

View File

@ -132,6 +132,64 @@ class Runner::Strategy::Poseidon < Runner::Strategy
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished copying files" }
end
def retrieve_files(path: './', recursive: true, privileged_execution: false)
url = "#{runner_url}/files"
params = {
path: path,
recursive: recursive,
privilegedExecution: privileged_execution || @execution_environment.privileged_execution,
}
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Retrieving files at #{runner_url} with #{params}" }
response = self.class.http_connection.get url, params
case response.status
when 200
JSON.parse(response.body)
when 424
raise Runner::Error::WorkspaceError.new("The path #{path} is not available or could not be read.")
else
self.class.handle_error response
end
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished listing files" }
end
def download_file(file, privileged_execution: false, &block)
url = "#{runner_url}/files/raw"
params = {
path: file,
privilegedExecution: privileged_execution || @execution_environment.privileged_execution,
}
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Download file #{params} from #{runner_url}" }
response = self.class.new_http_connection.get url, params do |request|
content_length = nil
content_type = nil
next if block.blank?
request.options.on_data = proc do |chunk, _overall_received_bytes, env|
next unless env.success?
content_length ||= env.response_headers['Content-Length'].presence&.to_i
content_type ||= env.response_headers['Content-Type'].presence || 'application/octet-stream'
yield chunk, content_length, content_type
end
request.options
end
case response.status
when 200
response.body
when 424
raise Runner::Error::WorkspaceError.new("The file #{file} is not available or could not be read.")
else
self.class.handle_error response
end
rescue Faraday::Error => e
raise Runner::Error::FaradayError.new("Request to Poseidon failed: #{e.inspect}")
ensure
Rails.logger.debug { "#{Time.zone.now.getutc.inspect}: Finished downloading file" }
end
def attach_to_execution(command, event_loop, starting_time, privileged_execution: false)
websocket_url = execute_command(command, privileged_execution: privileged_execution)
socket = Connection.new(websocket_url, self, event_loop)
@ -232,6 +290,12 @@ class Runner::Strategy::Poseidon < Runner::Strategy
end
end
def self.new_http_connection
Faraday.new(ssl: {ca_file: config[:ca_file]}, headers: headers) do |faraday|
faraday.adapter :net_http
end
end
def self.parse(response)
JSON.parse(response.body).deep_symbolize_keys
rescue JSON::ParserError => e