Class: Jisota::FileScript::Executor

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jisota/file_script.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



53
54
55
# File 'lib/jisota/file_script.rb', line 53

def context
  @context
end

#scriptObject (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_moveObject



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_dirObject



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, callback_not_found_message)
  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.system_message(callback_not_found_message)
    true
  end
end

#runObject



70
71
72
73
74
75
76
# File 'lib/jisota/file_script.rb', line 70

def run
  logger.system_message("File #{from} -> #{to}") do
    make_temp_dir
    upload
    handle_file_move
  end
end

#same_fileObject



96
97
98
99
# File 'lib/jisota/file_script.rb', line 96

def same_file
  logger.system_message("Files are the same. Skipping move.")
  true
end

#tmp_dirObject



66
67
68
# File 'lib/jisota/file_script.rb', line 66

def tmp_dir
  "tmp/jisota"
end

#tmp_fileObject



62
63
64
# File 'lib/jisota/file_script.rb', line 62

def tmp_file
  @tmp_file ||= "#{tmp_dir}/#{SecureRandom.hex}"
end

#uploadObject



82
83
84
85
# File 'lib/jisota/file_script.rb', line 82

def upload
  logger.system_message("Uploading #{from} -> #{tmp_file}")
  ssh_session.upload(from: from, to: tmp_file)
end