File: ssh_helpers.rb

package info (click to toggle)
ruby-mina 0.3.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 444 kB
  • sloc: ruby: 1,630; makefile: 31
file content (125 lines) | stat: -rw-r--r-- 3,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# # Helpers: SSH helpers
# You don't need to invoke these helpers, they're already invoked automatically.

module Mina
  module SshHelpers

    # ### ssh
    # Executes a command via SSH.
    #
    # Returns nothing usually, but if `{ return: true }` is given, returns the
    # STDOUT output of the SSH session.
    #
    # `options` is a hash of options:
    #
    #  - `:pretty` - Prettify the output if true.
    #  - `:return`  - If set to true, returns the output.
    #
    # Example
    #
    #     ssh("ls", return: true)

    def ssh(cmd, options={})
      require 'shellwords'

      cmd.unshift("export #{env_vars}") if env_vars?
      cmd = cmd.join("\n")  if cmd.is_a?(Array)
      script = Shellwords.escape(cmd)

      if options[:return] == true
        `#{ssh_command} -- #{script}`

      elsif simulate_mode?
        Ssh.simulate(cmd, ssh_command)

      else
        result = Ssh.invoke(script, self)
        Ssh.ensure_successful result, self
      end
    end

    # ### ssh_command
    # Returns the SSH command to be executed.
    #
    #     set :domain, 'foo.com'
    #     set :user, 'diggity'
    #
    #     puts ssh_command
    #     #=> 'ssh diggity@foo.com'

    def ssh_command
      args = domain!.dup
      args = "#{user}@#{args}" if user?
      args << " -i #{identity_file}" if identity_file?
      args << " -p #{port}" if port?
      args << " -A" if forward_agent?
      args << " #{ssh_options}" if ssh_options?
      args << " -o StrictHostKeyChecking=no"
      args << " -t"
      "ssh #{args}"
    end

    # ## Private methods
    # `ssh` delegates to these.

    module Ssh

      extend self

      # ### Ssh.simulate
      # __Internal:__ Prints SSH command. Called by `ssh`.

      def simulate(cmd, ssh_command)
        str = "Executing the following via '#{ssh_command}':"
        puts "#!/usr/bin/env bash"
        puts "# #{str}"
        puts "#"

        puts cmd

        0
      end

      # ### Ssh.invoke
      # __Internal:__ Initiates an SSH session with script `script` with given
      # `term_mode`.  Called by `ssh`.

      def invoke(script, this)
        # Ruby 1.8.7 doesn't let you have empty symbols
        term_mode = :"#{this.settings.term_mode}" if this.settings.term_mode
        code = "#{this.ssh_command} -- #{script}"

        # Certain environments can't do :pretty mode.
        term_mode = :exec  if term_mode == :pretty && !pretty_supported?

        case term_mode
        when :pretty
          this.pretty_system(code)
        when :exec
          exec code
        else
          system code
          $?.to_i
        end
      end

      def pretty_supported?
        # open4 is not supported under Windows.
        # https://github.com/nadarei/mina/issues/58
        require 'rbconfig'
        ! (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
      end

      # ### Ssh.ensure_successful
      # __Internal:__ Halts the execution if the given result code is not
      # successful (non-zero).

      def ensure_successful(result, this)
        this.die result if result.is_a?(Fixnum) && result > 0
        result
      end

    end

  end
end