Class: AvoDeploy::Task::LocalTaskExecutionEnvironment

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

Instance Attribute Summary

Attributes inherited from TaskExecutionEnvironment

#options, #scm

Instance Method Summary collapse

Methods inherited from TaskExecutionEnvironment

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

Constructor Details

#initialize(config) ⇒ LocalTaskExecutionEnvironment

Initialized the environment

Parameters:

  • config (Hash)

    deployment configuration



26
27
28
29
30
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 26

def initialize(config)
  super

  @dir = Dir.pwd
end

Instance Method Details

#chdir(dir) ⇒ Object

Changes the directory for commands to be executed in

Parameters:

  • dir (String)

    the directory to change to



43
44
45
46
47
48
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 43

def chdir(dir)
  log.debug "changing directory [#{dir.yellow}] " + "locally".cyan

  Dir.chdir(dir)
  @dir = Dir.pwd
end

#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



36
37
38
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 36

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

#command(cmd) ⇒ CommandExecutionResult

Executes a command locally in the current directory

Parameters:

  • cmd (String)

    the command to execute

Returns:



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
126
127
128
129
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 100

def command(cmd)
  log = AvoDeploy::Deployment.instance.log

  log.info "Executing [" + cmd.yellow + "] " + "locally".cyan

  result = AvoDeploy::CommandExecutionResult.new

  begin
    stdout, stderr, status = ::Open3.capture3(cmd, :chdir => cwd())

    result.stdin = cmd
    result.stdout = stdout
    result.stderr = stderr
    result.retval = status.exitstatus

    if result.stdout.nil? == false && result.stdout.empty? == false
      log.debug 'Stdout: ' + result.stdout.green
    end

    if result.stderr.nil? == false && result.stderr.empty? == false
      log.debug 'Stderr: ' + result.stderr.red
    end

    log.debug 'Retval: ' + result.retval.to_s
  rescue Exception => e
    handle_abort e
  end

  result
end

#copy_to_target(target, file, remote) ⇒ Object

Copies a file to a remote system (= target)

Parameters:

  • target (Target)

    the target system to deploy to

  • file (String)

    the local file to upload

  • remote (String)

    path on the remote system



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

def copy_to_target(target, file, remote)
  log = AvoDeploy::Deployment.instance.log

  log.info "started upload of file #{file} to #{target.name}"

  Net::SSH.start(
      target.config[:host],
      target.config[:user],
      {
        :port => target.config[:port],
        :auth_methods => [ 'publickey', 'hostbased' ],
      }
  ) do |session|
    session.scp.upload!(file, remote, :recursive => true) do |ch, name, sent, total|
      percentage = 0

      begin
        percentage = (sent.to_f * 100 / total.to_f).to_i
      rescue Exception => e
        AvoDeploy::Deployment.instance.handle_abort(e)
      end
    end
  end

  log.info "upload completed"
end

#cwdString

Returns the current working directory

Returns:

  • (String)

    current working directory



53
54
55
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 53

def cwd
  @dir
end

#targetsHash

Returns all target systems to deploy to

Returns:

  • (Hash)

    hash of target systems



60
61
62
# File 'lib/avodeploy/task/local_task_execution_environment.rb', line 60

def targets
  AvoDeploy::Deployment.instance.config.targets
end