Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix run_script #591

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions openc3-cosmos-script-runner-api/app/models/running_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ def self.spawn(scope, name, suite_runner = nil, disconnect = false, environment
end
# Load the script specific ENV vars set by the GUI
# These can override the previously defined global env vars
environment.each do |env|
process.environment[env['key']] = env['value']
if environment
environment.each do |env|
process.environment[env['key']] = env['value']
end
end

# Set proper secrets for running script
Expand Down
27 changes: 19 additions & 8 deletions openc3/lib/openc3/script/script_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def script_list(scope: $openc3_scope)

def script_syntax_check(script, scope: $openc3_scope)
endpoint = "/script-api/scripts/syntax"
response = $script_runner_api_server.request('post', endpoint, :data => script, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: script, scope: scope)
if response.nil? || response.code != 200
_script_response_error(response, "Script syntax check request failed", scope: scope)
else
Expand All @@ -64,13 +64,24 @@ def script_body(filename, scope: $openc3_scope)
end
end

def script_run(filename, disconnect: false, scope: $openc3_scope)
def script_run(filename, disconnect: false, environment: nil, scope: $openc3_scope)
if disconnect
endpoint = "/script-api/scripts/#{filename}/run/disconnect"
else
endpoint = "/script-api/scripts/#{filename}/run"
end
response = $script_runner_api_server.request('post', endpoint, scope: scope)

# Encode the environment hash into an array of key values
if environment && !environment.empty?
env_data = []
environment.map do |key, value|
env_data << { "key" => key, "value" => value }
end
else
env_data = []
end
# NOTE: json: true causes json_api_object to JSON generate and set the Content-Type to json
response = $script_runner_api_server.request('post', endpoint, json: true, data: { environment: env_data }, scope: scope)
if response.nil? || response.code != 200
_script_response_error(response, "Failed to run #{filename}", scope: scope)
else
Expand Down Expand Up @@ -111,7 +122,7 @@ def script_unlock(filename, scope: $openc3_scope)

def script_instrumented(filename, script, scope: $openc3_scope)
endpoint = "/script-api/scripts/#{filename}/instrumented"
response = $script_runner_api_server.request('post', endpoint, :data => script, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: script, scope: scope)
if response.nil? || response.code != 200
_script_response_error(response, "Script instrumented request failed", scope: scope)
else
Expand All @@ -127,7 +138,7 @@ def script_instrumented(filename, script, scope: $openc3_scope)

def script_create(filename, script, breakpoints = [], scope: $openc3_scope)
endpoint = "/script-api/scripts/#{filename}"
response = $script_runner_api_server.request('post', endpoint, :data => {text: script, breakpoints: breakpoints}, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: { text: script, breakpoints: breakpoints }, scope: scope)
if response.nil? || response.code != 200
_script_response_error(response, "Script create request failed", scope: scope)
else
Expand Down Expand Up @@ -205,7 +216,7 @@ def running_script_backtrace(id, scope: $openc3_scope)

def running_script_debug(id, debug_code, scope: $openc3_scope)
endpoint = "/script-api/running-script/#{id}/debug"
response = $script_runner_api_server.request('post', endpoint, data: {'args' => debug_code}, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: {'args' => debug_code}, scope: scope)
if response.nil? || response.code != 200
_script_response_error(response, "Running script debug request failed", scope: scope)
else
Expand All @@ -216,9 +227,9 @@ def running_script_debug(id, debug_code, scope: $openc3_scope)
def running_script_prompt(id, method_name, answer, prompt_id, password: nil, scope: $openc3_scope)
endpoint = "/script-api/running-script/#{id}/prompt"
if password
response = $script_runner_api_server.request('post', endpoint, data: {'method' => method_name, 'answer' => answer, 'prompt_id' => prompt_id, 'password' => password}, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: {'method' => method_name, 'answer' => answer, 'prompt_id' => prompt_id, 'password' => password}, scope: scope)
else
response = $script_runner_api_server.request('post', endpoint, data: {'method' => method_name, 'answer' => answer, 'prompt_id' => prompt_id}, scope: scope)
response = $script_runner_api_server.request('post', endpoint, json: true, data: {'method' => method_name, 'answer' => answer, 'prompt_id' => prompt_id}, scope: scope)
end
if response.nil? || response.code != 200
_script_response_error(response, "Running script prompt request failed", scope: scope)
Expand Down