Class: Fastlane::Actions::SshAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/ssh.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, return_type, return_value, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



131
132
133
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 131

def self.authors
  ["hjanuschka"]
end

.available_optionsObject



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
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 86

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :username,
                                 short_option: "-u",
                                 env_name: "FL_SSH_USERNAME",
                                 description: "Username"),
    FastlaneCore::ConfigItem.new(key: :password,
                                 short_option: "-p",
                                 env_name: "FL_SSH_PASSWORD",
                                 sensitive: true,
                                 description: "Password",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :host,
                                 short_option: "-H",
                                 env_name: "FL_SSH_HOST",
                                 description: "Hostname"),
    FastlaneCore::ConfigItem.new(key: :port,
                                 short_option: "-P",
                                 env_name: "FL_SSH_PORT",
                                 description: "Port",
                                 optional: true,
                                 default_value: "22"),
    FastlaneCore::ConfigItem.new(key: :commands,
                                 short_option: "-C",
                                 env_name: "FL_SSH_COMMANDS",
                                 description: "Commands",
                                 optional: true,
                                 type: Array),
    FastlaneCore::ConfigItem.new(key: :log,
                                 short_option: "-l",
                                 env_name: "FL_SSH_LOG",
                                 description: "Log commands and output",
                                 optional: true,
                                 default_value: true,
                                 type: Boolean)
  ]
end

.categoryObject



152
153
154
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 152

def self.category
  :misc
end

.descriptionObject



78
79
80
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 78

def self.description
  "Allows remote command execution using ssh"
end

.detailsObject



82
83
84
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 82

def self.details
  "Lets you execute remote commands via ssh using username/password or ssh-agent. If one of the commands in command-array returns non 0, it fails."
end

.example_codeObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 139

def self.example_code
  [
    'ssh(
      host: "dev.januschka.com",
      username: "root",
      commands: [
        "date",
        "echo 1 > /tmp/file1"
      ]
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



135
136
137
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 135

def self.is_supported?(platform)
  true
end

.outputObject



124
125
126
127
128
129
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 124

def self.output
  [
    ['SSH_STDOUT_VALUE', 'Holds the standard output of all commands'],
    ['SSH_STDERR_VALUE', 'Holds the standard error of all commands']
  ]
end

.run(params) ⇒ Object



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
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 45

def self.run(params)
  Actions.verify_gem!('net-ssh')
  require "net/ssh"

  Actions.lane_context[SharedValues::SSH_STDOUT_VALUE] = ""
  Actions.lane_context[SharedValues::SSH_STDERR_VALUE] = ""
  stdout = ""
  stderr = ""

  Net::SSH.start(params[:host], params[:username], { port: params[:port].to_i, password: params[:password] }) do |ssh|
    params[:commands].each do |cmd|
      UI.command(cmd) if params[:log]
      return_value = ssh_exec!(ssh, cmd, params[:log])
      if return_value[:exit_code] != 0
        UI.error("SSH Command failed '#{cmd}' Exit-Code: #{return_value[:exit_code]}")
        UI.user_error!("SSH Command failed")
      end

      stderr << return_value[:stderr]
      stdout << return_value[:stdout]
    end
  end
  command_word = params[:commands].count == 1 ? "command" : "commands"
  UI.success("Successfully executed #{params[:commands].count} #{command_word} on host #{params[:host]}")
  Actions.lane_context[SharedValues::SSH_STDOUT_VALUE] = stdout
  Actions.lane_context[SharedValues::SSH_STDERR_VALUE] = stderr
  return { stdout: Actions.lane_context[SharedValues::SSH_STDOUT_VALUE], stderr: Actions.lane_context[SharedValues::SSH_STDERR_VALUE] }
end

.ssh_exec!(ssh, command, log = true) ⇒ Object



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
# File 'fastlane/lib/fastlane/actions/ssh.rb', line 9

def self.ssh_exec!(ssh, command, log = true)
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  exit_signal = nil
  ssh.open_channel do |channel|
    channel.exec(command) do |ch, success|
      unless success
        abort("FAILED: couldn't execute command (ssh.channel.exec)")
      end
      channel.on_data do |ch1, data|
        stdout_data += data
        UI.command_output(data) if log
      end

      channel.on_extended_data do |ch2, type, data|
        # Only type 1 data is stderr (though no other types are defined by the standard)
        # See http://net-ssh.github.io/net-ssh/Net/SSH/Connection/Channel.html#method-i-on_extended_data
        stderr_data += data if type == 1
      end

      channel.on_request("exit-status") do |ch3, data|
        exit_code = data.read_long
      end

      channel.on_request("exit-signal") do |ch4, data|
        exit_signal = data.read_long
      end
    end
  end

  # Wait for all open channels to close
  ssh.loop
  { stdout: stdout_data, stderr: stderr_data, exit_code: exit_code, exit_signal: exit_signal }
end