Class: Proxy::RemoteExecution::Ssh::Actions::PullScript

Inherits:
Dynflow::Action::Runner
  • Object
show all
Defined in:
lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb

Constant Summary collapse

JobDelivered =
Class.new

Instance Method Summary collapse

Instance Method Details

#cleanup(_plan = nil) ⇒ Object



35
36
37
38
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 35

def cleanup(_plan = nil)
  job_storage.drop_job(execution_plan_id, run_step_id)
  Proxy::Dynflow::OtpManager.passwords.delete(execution_plan_id)
end

#host_nameObject



90
91
92
93
94
95
96
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 90

def host_name
  alternative_names = input.fetch(:alternative_names, {})

  alternative_names[:consumer_uuid] ||
    alternative_names[:fqdn] ||
    input[:hostname]
end

#init_runObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 24

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

#job_storageObject



106
107
108
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 106

def job_storage
  Proxy::RemoteExecution::Ssh.job_storage
end

#kill_runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 49

def kill_run
  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

#mqtt_notify(payload) ⇒ Object



84
85
86
87
88
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 84

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

#mqtt_start(otp_password) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 65

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

#mqtt_topicObject



98
99
100
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 98

def mqtt_topic
  "yggdrasil/#{host_name}/data/in"
end

#plan(action_input, mqtt: false) ⇒ Object



10
11
12
13
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 10

def plan(action_input, mqtt: false)
  super(action_input)
  input[:with_mqtt] = mqtt
end

#process_external_event(event) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 40

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

#run(event = nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 15

def run(event = nil)
  if event == JobDelivered
    output[:state] = :delivered
    suspend
  else
    super
  end
end

#settingsObject



102
103
104
# File 'lib/smart_proxy_remote_execution_ssh/actions/pull_script.rb', line 102

def settings
  Proxy::RemoteExecution::Ssh::Plugin.settings
end