Class: Jisota::FileScript::Executor
- Inherits:
-
Object
- Object
- Jisota::FileScript::Executor
- Extended by:
- Forwardable
- Defined in:
- lib/jisota/file_script.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Instance Method Summary collapse
- #handle_file_move ⇒ Object
-
#initialize(script:, context:) ⇒ Executor
constructor
A new instance of Executor.
- #make_temp_dir ⇒ Object
- #move_with_callback(callback, callback_not_found_message) ⇒ Object
- #run ⇒ Object
- #same_file ⇒ Object
- #tmp_dir ⇒ Object
- #tmp_file ⇒ Object
- #upload ⇒ Object
Constructor Details
#initialize(script:, context:) ⇒ Executor
Returns a new instance of Executor.
57 58 59 60 |
# File 'lib/jisota/file_script.rb', line 57 def initialize(script: , context: ) @script = script @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
53 54 55 |
# File 'lib/jisota/file_script.rb', line 53 def context @context end |
#script ⇒ Object (readonly)
Returns the value of attribute script.
53 54 55 |
# File 'lib/jisota/file_script.rb', line 53 def script @script end |
Instance Method Details
#handle_file_move ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/jisota/file_script.rb', line 87 def handle_file_move result = ssh_session.command("cmp -s #{tmp_file} #{to}") case result when 0 then same_file when 1 then move_with_callback(update, "File exists and update is not allowed. Skipping.") when 2 then move_with_callback(create, "File does not exist and create is not allowed. Skipping.") end end |
#make_temp_dir ⇒ Object
78 79 80 |
# File 'lib/jisota/file_script.rb', line 78 def make_temp_dir ssh_session.command("mkdir -p #{tmp_dir}", logger) end |
#move_with_callback(callback, callback_not_found_message) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jisota/file_script.rb', line 101 def move_with_callback(callback, ) if callback result = ssh_session.command("sudo mkdir -p `dirname #{to}` && sudo mv #{tmp_file} #{to}", logger) if result == 0 if callback.is_a?(ScriptBlock) callback_script = callback.evaluate(context) callback_script.execute(context) else true end end else logger.() true end end |
#run ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/jisota/file_script.rb', line 70 def run logger.("File #{from} -> #{to}") do make_temp_dir upload handle_file_move end end |
#same_file ⇒ Object
96 97 98 99 |
# File 'lib/jisota/file_script.rb', line 96 def same_file logger.("Files are the same. Skipping move.") true end |
#tmp_dir ⇒ Object
66 67 68 |
# File 'lib/jisota/file_script.rb', line 66 def tmp_dir "tmp/jisota" end |
#tmp_file ⇒ Object
62 63 64 |
# File 'lib/jisota/file_script.rb', line 62 def tmp_file @tmp_file ||= "#{tmp_dir}/#{SecureRandom.hex}" end |
#upload ⇒ Object
82 83 84 85 |
# File 'lib/jisota/file_script.rb', line 82 def upload logger.("Uploading #{from} -> #{tmp_file}") ssh_session.upload(from: from, to: tmp_file) end |