Class: Avocado::LocalTaskExecutionEnvironment

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

Instance Method Summary collapse

Methods inherited from TaskExecutionEnvironment

#get, #handle_abort, #log, #scm_provider=, #set

Constructor Details

#initialize(config) ⇒ LocalTaskExecutionEnvironment

Initialized the environment

Parameters:

  • config (Hash)

    deployment configuration



25
26
27
28
29
# File 'lib/avocado/task/local_task_execution_environment.rb', line 25

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



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

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



35
36
37
# File 'lib/avocado/task/local_task_execution_environment.rb', line 35

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:



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

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

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

  result = Avocado::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
  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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/avocado/task/local_task_execution_environment.rb', line 68

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

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

    Net::SSH.start(
        target.config[:host],
        target.config[:user]
    ) 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
            Avocado::Deployment.instance.handle_abort(e)
          end

          #log.info "\r#{name}: #{percentage}%"
    end
  end

  log.info "upload completed"
end

#cwdString

Returns the current working directory

Returns:

  • (String)

    current working directory



52
53
54
# File 'lib/avocado/task/local_task_execution_environment.rb', line 52

def cwd
  @dir
end

#targetsHash

Returns all target systems to deploy to

Returns:

  • (Hash)

    hash of target systems



59
60
61
# File 'lib/avocado/task/local_task_execution_environment.rb', line 59

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