Module: Lxc::Helpers

Included in:
Lxc, Lxc
Defined in:
lib/vagabond/cookbooks/lxc/libraries/lxc.rb

Instance Method Summary collapse

Instance Method Details

#command(*args) ⇒ Object



39
40
41
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc.rb', line 39

def command(*args)
  run_command(*args)
end

#detect_home(set_if_missing = false) ⇒ Object

Detect HOME environment variable. If not an acceptable value, set to /root or /tmp



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc.rb', line 57

def detect_home(set_if_missing=false)
  if(ENV['HOME'] && Pathname.new(ENV['HOME']).absolute?)
    ENV['HOME']
  else
    home = File.directory?('/root') && File.writable?('/root') ? '/root' : '/tmp'
    if(set_if_missing)
      ENV['HOME'] = home
    end
    home
  end
end

#logObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc.rb', line 43

def log
  if(defined?(Chef::Log))
    Chef::Log
  else
    unless(@logger)
      require 'logger'
      @logger = Logger.new('/dev/null')
    end
    @logger
  end
end

#run_command(cmd, args = {}) ⇒ Object

Simple helper to shell out



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
# File 'lib/vagabond/cookbooks/lxc/libraries/lxc.rb', line 12

def run_command(cmd, args={})
  retries = args[:allow_failure_retry].to_i
  begin
    shlout = Mixlib::ShellOut.new(cmd, 
      :logger => defined?(Chef::Log) ? Chef::Log.logger : log,
      :live_stream => args[:livestream] ? STDOUT : nil,
      :timeout => args[:timeout] || 1200,
      :environment => {'HOME' => detect_home}
    )
    shlout.run_command
    shlout.error!
    shlout
  rescue Mixlib::ShellOut::ShellCommandFailed, CommandFailed, Mixlib::ShellOut::CommandTimeout
    if(retries > 0)
      log.warn "LXC run command failed: #{cmd}"
      log.warn "Retrying command. #{args[:allow_failure_retry].to_i - retries} of #{args[:allow_failure_retry].to_i} retries remain"
      sleep(0.3)
      retries -= 1
      retry
    elsif(args[:allow_failure])
      true
    else
      raise
    end
  end
end