Class: AvoDeploy::Task::RemoteTaskExecutionEnvironment

Inherits:
TaskExecutionEnvironment show all
Defined in:
lib/avodeploy/task/remote_task_execution_environment.rb

Instance Attribute Summary collapse

Attributes inherited from TaskExecutionEnvironment

#options, #scm

Instance Method Summary collapse

Methods inherited from TaskExecutionEnvironment

#get, #handle_abort, #initialize, #log, #run, #run_nodeps, #set

Constructor Details

This class inherits a constructor from AvoDeploy::Task::TaskExecutionEnvironment

Instance Attribute Details

#configObject

Returns the value of attribute config.



23
24
25
# File 'lib/avodeploy/task/remote_task_execution_environment.rb', line 23

def config
  @config
end

Instance Method Details

#check_util_availability(utils) ⇒ Object

Checks, if all utilities are available for the deployment process to be executed

Parameters:

  • utils (Array)

    array with utilities to check



40
41
42
# File 'lib/avodeploy/task/remote_task_execution_environment.rb', line 40

def check_util_availability(utils)
  super(utils, 'remotely')
end

#command(cmd) ⇒ CommandExecutionResult

Executes a command on the remote system

Parameters:

  • cmd (String)

    the command to execute

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/avodeploy/task/remote_task_execution_environment.rb', line 86

def command(cmd)
  AvoDeploy::Deployment.instance.log.info "Executing [" + cmd.yellow + "] on remote " + get(:name).to_s.cyan

  result = AvoDeploy::CommandExecutionResult.new

  begin
    result = ssh_exec!(@session, cmd)

    if result.stdout.nil? == false && result.stdout.empty? == false
      AvoDeploy::Deployment.instance.log.debug "Stdout@#{get(:host)}: ".cyan + result.stdout.green
    end

    if result.stderr.nil? == false && result.stderr.empty? == false
      AvoDeploy::Deployment.instance.log.debug "Stderr@#{get(:host)}: ".cyan + result.stderr.red
    end
  rescue Exception => e
    handle_abort e
  end

  result
end

#establish_connectionObject

Creates a connection between the local and the remote system over ssh



26
27
28
29
30
31
32
33
34
# File 'lib/avodeploy/task/remote_task_execution_environment.rb', line 26

def establish_connection
  AvoDeploy::Deployment.instance.log.debug "connecting to #{get(:user)}@#{get(:host)}..."

  begin
    @session = ::Net::SSH.start(get(:host), get(:user), port: get(:port), timeout: 30, auth_methods: [ 'publickey', 'hostbased' ])
  rescue ::Net::SSH::AuthenticationFailed => e
    handle_abort e
  end
end

#ssh_exec!(ssh, command) ⇒ Object

Executes a command via ssh

 @param command [String] the command to execute

Parameters:

  • ssh (Net::SSH::Connection::Session)

    ssh session



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
# File 'lib/avodeploy/task/remote_task_execution_environment.rb', line 48

def ssh_exec!(ssh, command)
  stdout_data = ""
  stderr_data = ""
  exit_code = 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 |ch, data|
        stdout_data+=data
      end

      channel.on_extended_data do |ch, type, data|
        stderr_data+=data
      end

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

  result = AvoDeploy::CommandExecutionResult.new
  result.stdin = command
  result.stdout = stdout_data
  result.stderr = stderr_data
  result.retval = exit_code

  result
end