Class: Fastlane::Actions::SshAction

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

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, method_missing, other_action, return_value, sh, step_text

Class Method Details

.authorsObject



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

def self.authors
  ["hjanuschka"]
end

.available_optionsObject



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

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

.descriptionObject



71
72
73
# File 'lib/fastlane/actions/ssh.rb', line 71

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

.detailsObject



75
76
77
# File 'lib/fastlane/actions/ssh.rb', line 75

def self.details
  "Lets you execute remote commands via ssh using username/password or ssh-agent"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/fastlane/actions/ssh.rb', line 138

def self.is_supported?(platform)
  true
end

.outputObject



127
128
129
130
131
132
# File 'lib/fastlane/actions/ssh.rb', line 127

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



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

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.important(['[SSH COMMAND]', cmd].join(': ')) if params[:log]
      return_value = ssh_exec!(ssh, cmd)
      UI.error("SSH Command failed '#{cmd}' Exit-Code: #{return_value[:exit_code]}") if return_value[:exit_code] > 0
      UI.user_error!("SSH Command failed") if return_value[:exit_code] > 0

      stderr << return_value[:stderr]
      stdout << return_value[:stdout]
    end
  end
  UI.message("Succesfully executed #{params[:commands].count} commands on host: #{params[:host]}")
  UI.message("\n########### \n #{stdout} \n###############".magenta) if params[:log]
  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) ⇒ 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
# File 'lib/fastlane/actions/ssh.rb', line 9

def self.ssh_exec!(ssh, command)
  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
      end

      channel.on_extended_data do |ch2, type, data|
        stderr_data += data
      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
  ssh.loop
  {stdout: stdout_data, stderr: stderr_data, exit_code: exit_code, exit_signal: exit_signal}
end