Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #733 from tiwillia/rhcappdeployssh
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Dec 3, 2015
2 parents 5219177 + 418952c commit f59f54e
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 278 deletions.
2 changes: 1 addition & 1 deletion lib/rhc/commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def deploy(ref)

raise RHC::DeploymentsNotSupportedException.new if !rest_app.supports? "DEPLOY"

deploy_artifact(rest_app, ref, options.hot_deploy, options.force_clean_build)
deploy_artifact(rest_app, ref)

0
end
Expand Down
5 changes: 1 addition & 4 deletions lib/rhc/commands/port_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ def run(app)
else
debug "Using #{ssh_executable} to determine forwarding ports."
ssh_cmd = "#{ssh_executable} #{ssh_uri.user}@#{ssh_uri.host} '#{list_ports_cmd} 2>&1'"
status, output = exec(ssh_cmd)
if status != 0
raise RHC::SSHCommandFailed.new(status, output)
end
status, output = run_with_system_ssh(ssh_cmd)
end

output.each_line do |line|
Expand Down
56 changes: 44 additions & 12 deletions lib/rhc/deployment_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module DeploymentHelpers

protected

def deploy_artifact(rest_app, artifact, hot_deploy, force_clean_build)
def deploy_artifact(rest_app, artifact)
is_file = File.file?(artifact)
is_url = URI::ABS_URI.match(artifact).present?

if rest_app.deployment_type == 'binary'
if is_file
deploy_local_file(rest_app, artifact, hot_deploy, force_clean_build)
deploy_local_file(rest_app, artifact, options.hot_deploy, options.force_clean_build)
elsif is_url
deploy_file_from_url(rest_app, artifact, hot_deploy, force_clean_build)
deploy_file_from_url(rest_app, artifact, options.hot_deploy, options.force_clean_build)
else
paragraph do
warn "The application '#{rest_app.name}' is configured for binary deployments but the artifact "\
Expand All @@ -36,21 +36,28 @@ def deploy_artifact(rest_app, artifact, hot_deploy, force_clean_build)
end
raise IncompatibleDeploymentTypeException
else
deploy_git_ref(rest_app, artifact, hot_deploy, force_clean_build)
deploy_git_ref(rest_app, artifact, options.hot_deploy, options.force_clean_build)
end
end

def deploy_git_ref(rest_app, ref, hot_deploy, force_clean_build)
say "Deployment of git ref '#{ref}' in progress for application #{rest_app.name} ..."

ssh_executable = check_ssh_executable! options.ssh

ssh_url = URI(rest_app.ssh_url)
remote_cmd = "gear deploy #{ref}#{hot_deploy ? ' --hot-deploy' : ''}#{force_clean_build ? ' --force-clean-build' : ''}"
ssh_cmd = "#{ssh_executable} -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"

begin
ssh_ruby(ssh_url.host, ssh_url.user, remote_cmd)
if options.ssh
debug "Running #{ssh_cmd}"
run_with_system_ssh(ssh_cmd)
else
ssh_ruby(ssh_url.host, ssh_url.user, remote_cmd)
end
success "Success"
rescue
ssh_cmd = "ssh -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
warn "Error deploying git ref. You can try to deploy manually with:\n#{ssh_cmd}"
raise
end
Expand All @@ -60,14 +67,25 @@ def deploy_local_file(rest_app, filename, hot_deploy, force_clean_build)
filename = File.expand_path(filename)
say "Deployment of file '#{filename}' in progress for application #{rest_app.name} ..."

ssh_executable = check_ssh_executable! options.ssh

ssh_url = URI(rest_app.ssh_url)
remote_cmd = "oo-binary-deploy#{hot_deploy ? ' --hot-deploy' : ''}#{force_clean_build ? ' --force-clean-build' : ''}"
if windows?
ssh_cmd = "type #{filename} | #{ssh_executable} -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
else
ssh_cmd = "cat #{filename} | #{ssh_executable} -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
end

begin
ssh_send_file_ruby(ssh_url.host, ssh_url.user, remote_cmd, filename)
if options.ssh
debug "Running #{ssh_cmd}"
run_with_system_ssh(ssh_cmd)
else
ssh_send_file_ruby(ssh_url.host, ssh_url.user, remote_cmd, filename)
end
success "Success"
rescue
ssh_cmd = "ssh -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
warn "Error deploying local file. You can try to deploy manually with:\n#{ssh_cmd}"
raise
end
Expand All @@ -76,16 +94,23 @@ def deploy_local_file(rest_app, filename, hot_deploy, force_clean_build)
def deploy_file_from_url(rest_app, file_url, hot_deploy, force_clean_build)
say "Deployment of file '#{file_url}' in progress for application #{rest_app.name} ..."

ssh_executable = check_ssh_executable! options.ssh

ssh_url = URI(rest_app.ssh_url)
file_url = URI(file_url)

remote_cmd = "oo-binary-deploy#{hot_deploy ? ' --hot-deploy' : ''}#{force_clean_build ? ' --force-clean-build' : ''}"
ssh_cmd = "#{ssh_executable} -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"

begin
ssh_send_url_ruby(ssh_url.host, ssh_url.user, remote_cmd, file_url)
if options.ssh
debug "Running #{ssh_cmd}"
run_with_system_ssh(ssh_cmd)
else
ssh_send_url_ruby(ssh_url.host, ssh_url.user, remote_cmd, file_url)
end
success "Success"
rescue
ssh_cmd = "ssh -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
warn "Error deploying file from url. You can try to deploy manually with:\n#{ssh_cmd}"
raise
end
Expand All @@ -94,14 +119,21 @@ def deploy_file_from_url(rest_app, file_url, hot_deploy, force_clean_build)
def activate_deployment(rest_app, deployment_id)
say "Activating deployment '#{deployment_id}' on application #{rest_app.name} ..."

ssh_executable = check_ssh_executable! options.ssh

ssh_url = URI(rest_app.ssh_url)
remote_cmd = "gear activate --all #{deployment_id}"
ssh_cmd = "#{ssh_executable} -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"

begin
ssh_ruby(ssh_url.host, ssh_url.user, remote_cmd)
if options.ssh
debug "Running #{ssh_cmd}"
run_with_system_ssh(ssh_cmd)
else
ssh_ruby(ssh_url.host, ssh_url.user, remote_cmd)
end
success "Success"
rescue
ssh_cmd = "ssh -t #{ssh_url.user}@#{ssh_url.host} '#{remote_cmd}'"
warn "Error activating deployment. You can try to activate manually with:\n#{ssh_cmd}"
raise
end
Expand Down
30 changes: 17 additions & 13 deletions lib/rhc/ssh_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,10 @@ def save_snapshot(app, filename, for_deployment=false, ssh_executable=nil)
if !RHC::Helpers.windows? || options.ssh
debug "Using user specified SSH: #{ssh_executable}" if options.ssh

status = 1
output = ""
Timeout::timeout(exec_timeout) do
status, output = exec(ssh_cmd)
end
if status != 0
debug output
raise RHC::SnapshotSaveException.new "Error in trying to save snapshot. You can try to save manually by running:\n#{ssh_cmd}"
debug "Running #{ssh_cmd}"
status, output = run_with_system_ssh(ssh_cmd)
end
else
Timeout::timeout(exec_timeout) do
Expand All @@ -294,11 +290,13 @@ def save_snapshot(app, filename, for_deployment=false, ssh_executable=nil)
end
end
rescue Timeout::Error => e
debug e.message
debug e.backtrace
raise RHC::SnapshotSaveException.new "Save operation took longer than the timeout of #{exec_timeout} seconds.\n" +
"You can use the --timeout option to extend this timeout.\n" +
"Alternatively, you can try to save the snapshot manually by running:\n#{ssh_cmd}"
rescue Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
rescue Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed, RHC::SSHCommandFailed => e
debug e.message
debug e.backtrace
raise RHC::SnapshotSaveException.new "Error in trying to save snapshot. You can try to save manually by running:\n#{ssh_cmd}"
end
Expand Down Expand Up @@ -326,11 +324,8 @@ def restore_snapshot(app, filename, ssh_executable=nil)
begin
if !RHC::Helpers.windows? || options.ssh
debug "Using user specified SSH: #{ssh_executable}" if options.ssh
status, output = exec(ssh_cmd)
if status != 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
debug "Running #{ssh_cmd}"
status, output = run_with_system_ssh(ssh_cmd)
else
ssh = Net::SSH.start(ssh_uri.host, ssh_uri.user)
ssh.open_channel do |channel|
Expand All @@ -354,7 +349,8 @@ def restore_snapshot(app, filename, ssh_executable=nil)
end
ssh.loop
end
rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed => e
rescue Timeout::Error, Errno::EADDRNOTAVAIL, Errno::EADDRINUSE, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Net::SSH::AuthenticationFailed, RHC::SSHCommandFailed => e
debug e.message
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
Expand Down Expand Up @@ -510,6 +506,14 @@ def check_ssh_executable!(path)
end
end

def run_with_system_ssh(ssh_cmd)
status, output = exec(ssh_cmd)
if status != 0
raise RHC::SSHCommandFailed.new(status, output)
end
return status, output
end

private

def ssh_add
Expand Down
Loading

0 comments on commit f59f54e

Please sign in to comment.