def restore(app)
filename = options.filepath ? options.filepath : "#{app}.tar.gz"
if File.exists? filename
include_git = RHC::Helpers.windows? ? true : RHC::TarGz.contains(filename, './*/git')
rest_app = rest_client.find_application(options.namespace, app)
ssh_uri = URI.parse(rest_app.ssh_url)
ssh_cmd = "cat #{filename} | ssh #{ssh_uri.user}@#{ssh_uri.host} 'restore#{include_git ? ' INCLUDE_GIT' : ''}'"
say "Restoring from snapshot #{filename}..."
debug ssh_cmd
begin
if ! RHC::Helpers.windows?
output = Kernel.%x ssh_cmd
if $?.exitstatus != 0
debug output
raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
end
else
ssh = Net::SSH.start(ssh_uri.host, ssh_uri.user)
ssh.open_channel do |channel|
channel.exec("restore#{include_git ? ' INCLUDE_GIT' : ''}") do |ch, success|
channel.on_data do |ch, data|
say data
end
channel.on_extended_data do |ch, type, data|
say data
end
channel.on_close do |ch|
say "Terminating..."
end
File.open(filename, 'rb') do |file|
file.chunk(1024) do |chunk|
channel.send_data chunk
end
end
channel.eof!
end
end
ssh.loop
end
rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
debug e.backtrace
raise RHC::SnapshotRestoreException.new "Error in trying to restore snapshot. You can try to restore manually by running:\n#{ssh_cmd}"
end
else
raise RHC::SnapshotRestoreException.new "Archive not found: #{filename}"
end
results { say "Success" }
0
end
end
end