-
Notifications
You must be signed in to change notification settings - Fork 26
Groundwork for pull provider #64
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
Merged
+416
−6
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c839d90
Fixes #33682 - Groundwork for pull provider
adamruzicka 54efb6b
Fixes #33682 - Make it configurable
adamruzicka ca28a30
Fixes #33682 - Rework storage
adamruzicka 470ecca
Fixes #33682 - Extract job store operations into a separate class
adamruzicka b117ce7
Fixes #33682 - Add support for password based auth
adamruzicka 870f2fc
Fixes #33682 - Cleaner event dispatch
adamruzicka dc26154
Fixes #33682 - Apply suggestions from code review
adamruzicka c1eee2e
Fixes #33682 - Apply suggestions from code review
adamruzicka 6462ce5
Fixes #33682 - Prefer consumer uuid over hostname, if available
adamruzicka 0f8a2be
Fixes #33682 - Slightly better handling of async_ssh setting
adamruzicka 3a2c9de
Fixes #33682 - Drop cancellation for now
adamruzicka 0873377
Fixes #33682 - Address review comments
adamruzicka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module Proxy::RemoteExecution::Ssh | ||
| module Actions | ||
| require 'smart_proxy_remote_execution_ssh/actions/run_script' | ||
| require 'smart_proxy_remote_execution_ssh/actions/pull_script' | ||
| end | ||
| end |
110 changes: 110 additions & 0 deletions
110
lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| require 'mqtt' | ||
| require 'json' | ||
|
|
||
| module Proxy::RemoteExecution::Ssh::Actions | ||
| class PullScript < Proxy::Dynflow::Action::Runner | ||
| JobDelivered = Class.new | ||
|
|
||
| execution_plan_hooks.use :cleanup, :on => :stopped | ||
|
|
||
| def plan(action_input, mqtt: false) | ||
| super(action_input) | ||
| input[:with_mqtt] = mqtt | ||
| end | ||
|
|
||
| def run(event = nil) | ||
| if event == JobDelivered | ||
| output[:state] = :delivered | ||
| suspend | ||
| else | ||
| super | ||
| end | ||
| end | ||
|
|
||
| def init_run | ||
| otp_password = if input[:with_mqtt] | ||
| ::Proxy::Dynflow::OtpManager.generate_otp(execution_plan_id) | ||
| end | ||
| input[:job_uuid] = job_storage.store_job(host_name, execution_plan_id, run_step_id, input[:script]) | ||
| output[:state] = :ready_for_pickup | ||
| output[:result] = [] | ||
| mqtt_start(otp_password) if input[:with_mqtt] | ||
| suspend | ||
| end | ||
|
|
||
| def cleanup(_plan = nil) | ||
| job_storage.drop_job(execution_plan_id, run_step_id) | ||
| Proxy::Dynflow::OtpManager.passwords.delete(execution_plan_id) | ||
| end | ||
|
|
||
| def process_external_event(event) | ||
| output[:state] = :running | ||
| data = event.data | ||
| continuous_output = Proxy::Dynflow::ContinuousOutput.new | ||
| Array(data['output']).each { |line| continuous_output.add_output(line, 'stdout') } if data.key?('output') | ||
| exit_code = data['exit_code'].to_i if data['exit_code'] | ||
| process_update(Proxy::Dynflow::Runner::Update.new(continuous_output, exit_code)) | ||
| end | ||
|
|
||
| def kill_run | ||
ezr-ondrej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case output[:state] | ||
| when :ready_for_pickup | ||
| # If the job is not running yet on the client, wipe it from storage | ||
| cleanup | ||
| # TODO: Stop the action | ||
| when :notified, :running | ||
| # Client was notified or is already running, dealing with this situation | ||
| # is only supported if mqtt is available | ||
| # Otherwise we have to wait it out | ||
| # TODO | ||
| # if input[:with_mqtt] | ||
| end | ||
| suspend | ||
| end | ||
|
|
||
| def mqtt_start(otp_password) | ||
| payload = { | ||
| type: 'data', | ||
| message_id: SecureRandom.uuid, | ||
| version: 1, | ||
| sent: DateTime.now.iso8601, | ||
| directive: 'foreman', | ||
| metadata: { | ||
| 'job_uuid': input[:job_uuid], | ||
| 'username': execution_plan_id, | ||
| 'password': otp_password, | ||
| 'return_url': "#{input[:proxy_url]}/ssh/jobs/#{input[:job_uuid]}/update", | ||
| }, | ||
| content: "#{input[:proxy_url]}/ssh/jobs/#{input[:job_uuid]}", | ||
| } | ||
| mqtt_notify payload | ||
| output[:state] = :notified | ||
| end | ||
|
|
||
| def mqtt_notify(payload) | ||
| MQTT::Client.connect(settings.mqtt_broker, settings.mqtt_port) do |c| | ||
| c.publish(mqtt_topic, JSON.dump(payload), false, 1) | ||
| end | ||
| end | ||
|
|
||
| def host_name | ||
| alternative_names = input.fetch(:alternative_names, {}) | ||
|
|
||
| alternative_names[:consumer_uuid] || | ||
| alternative_names[:fqdn] || | ||
| input[:hostname] | ||
| end | ||
|
|
||
| def mqtt_topic | ||
| "yggdrasil/#{host_name}/data/in" | ||
| end | ||
|
|
||
| def settings | ||
| Proxy::RemoteExecution::Ssh::Plugin.settings | ||
| end | ||
|
|
||
| def job_storage | ||
| Proxy::RemoteExecution::Ssh.job_storage | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # lib/job_storage.rb | ||
| require 'sequel' | ||
|
|
||
| module Proxy::RemoteExecution::Ssh | ||
| class JobStorage | ||
| def initialize | ||
| @db = Sequel.sqlite | ||
| @db.create_table :jobs do | ||
| DateTime :timestamp, null: false, default: Sequel::CURRENT_TIMESTAMP | ||
| String :uuid, fixed: true, size: 36, primary_key: true, null: false | ||
| String :hostname, null: false, index: true | ||
| String :execution_plan_uuid, fixed: true, size: 36, null: false, index: true | ||
| Integer :run_step_id, null: false | ||
| String :job, text: true | ||
| end | ||
| end | ||
|
|
||
| def find_job(uuid) | ||
| jobs.where(uuid: uuid).first | ||
| end | ||
|
|
||
| def job_uuids_for_host(hostname) | ||
| jobs_for_host(hostname).order(:timestamp) | ||
| .select_map(:uuid) | ||
| end | ||
|
|
||
| def store_job(hostname, execution_plan_uuid, run_step_id, job, uuid: SecureRandom.uuid, timestamp: Time.now.utc) | ||
| jobs.insert(timestamp: timestamp, | ||
| uuid: uuid, | ||
| hostname: hostname, | ||
| execution_plan_uuid: execution_plan_uuid, | ||
| run_step_id: run_step_id, | ||
| job: job) | ||
| uuid | ||
| end | ||
|
|
||
| def drop_job(execution_plan_uuid, run_step_id) | ||
| jobs.where(execution_plan_uuid: execution_plan_uuid, run_step_id: run_step_id).delete | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def jobs_for_host(hostname) | ||
| jobs.where(hostname: hostname) | ||
| end | ||
|
|
||
| def jobs | ||
| @db[:jobs] | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.