Class: Hamal::RemoteExecutor
Defined Under Namespace
Classes: ExecResult
Instance Method Summary collapse
-
#initialize(remote_user, remote_dir) ⇒ RemoteExecutor
constructor
A new instance of RemoteExecutor.
- #sh(command, interactive: false, abort_on_error: false) ⇒ Object
- #sh!(command, interactive: false) ⇒ Object
- #ssh(remote_command, abort_on_error:, interactive: false) ⇒ Object
Methods included from Helpers
Methods included from Config
#app_local_ports, #app_name, #app_repo, #config_file, #deploy_config, #deploy_env, #deployed_image, #deployed_revision, #project_root, #server
Constructor Details
#initialize(remote_user, remote_dir) ⇒ RemoteExecutor
Returns a new instance of RemoteExecutor.
43 44 45 46 47 48 |
# File 'lib/hamal.rb', line 43 def initialize(remote_user, remote_dir) @remote_user = remote_user @remote_dir = remote_dir raise "Invalid remote user #{@remote_user}" unless [:root, :rails].include? @remote_user end |
Instance Method Details
#sh(command, interactive: false, abort_on_error: false) ⇒ Object
50 51 52 53 54 |
# File 'lib/hamal.rb', line 50 def sh(command, interactive: false, abort_on_error: false) dir_override = "cd #{@remote_dir};" if @remote_dir user_override = "runuser -u rails" if @remote_user == :rails ssh "#{dir_override} #{user_override} #{command}", interactive:, abort_on_error: end |
#sh!(command, interactive: false) ⇒ Object
56 |
# File 'lib/hamal.rb', line 56 def sh!(command, interactive: false) = sh command, interactive:, abort_on_error: true |
#ssh(remote_command, abort_on_error:, interactive: false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/hamal.rb', line 58 def ssh(remote_command, abort_on_error:, interactive: false) remote_command = remote_command.gsub "'", %q('"'"') output = if interactive spawn "ssh -tt root@#{server} '#{remote_command}'", out: $stdout, err: $stderr, in: $stdin Process.wait nil else `ssh root@#{server} '#{remote_command}'`.strip end abort "Failed to execute `#{remote_command}` on `#{server}`" if abort_on_error && !$CHILD_STATUS.success? ExecResult.new output:, exit_code: $CHILD_STATUS.exitstatus end |