Class: Distem::Lib::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/distem/distemlib/shell.rb

Constant Summary

PATH_DISTEMD_LOG_CMD =

The file to save log of the executed commands

File.join(Distem::Node::Admin::PATH_DISTEM_LOGS,"distemd.cmd")
@@count =
0

Class Method Summary (collapse)

Class Method Details

+ (Object) run(cmd, simple = false)

Execute the specified command on the physical node (log the resuls in PATH_DISTEMD_LOG_CMD)

Attributes

  • cmd The command (String)

  • simple Execute the command in simple mode (no logs of stderr)

Raises:



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/distem/distemlib/shell.rb', line 14

def self.run(cmd, simple=false)
  @@count = @@count + 1
  cmdlog = "(#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}-#{@@count}) #{cmd}"

  ret = ""
  log = ""
  error = false
  err = ""

  if simple
    ret = `#{cmd}`
    log = "#{cmdlog}\n#{ret}"
    error = !$?.success?
  else
    Dir::mkdir(Distem::Node::Admin::PATH_DISTEM_LOGS) unless File.exist?(Distem::Node::Admin::PATH_DISTEM_LOGS)
    full_version = RUBY_VERSION.split('.')
    main_version = full_version[0] + '.' + full_version[1]
    case main_version
    when '1.8'
      Open3.popen3(cmd) do |stdin, stdout, stderr|
        ret = stdout.read
        err = stderr.read
        log = "#{cmdlog}\n#{ret}"
        log += "\nError: #{err}" unless err.empty? 
        error = !$?.success? or !err.empty?
      end
    when '1.9','2.0','2.1'
      Open3.popen3(cmd) do |stdin, stdout, stderr, thr|
        ret = stdout.read
        err = stderr.read
        log = "#{cmdlog}\n#{ret}"
        log += "\nError: #{err}" unless err.empty? 
        error = !thr.value.success? or !err.empty?
      end
    else
      raise "Unsupported Ruby version: #{RUBY_VERSION}"
    end
  end
  File.open(PATH_DISTEMD_LOG_CMD,'a+') { |f| f.write(log) }
  raise ShellError.new(cmd,ret,err) if error

  return ret
end

+ (Object) run_without_logging(cmd)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/distem/distemlib/shell.rb', line 58

def self.run_without_logging(cmd)
  res = {}
  full_version = RUBY_VERSION.split('.')
  main_version = full_version[0] + '.' + full_version[1]
  case main_version
  when '1.8'
    Open3.popen3(cmd) do |stdin, stdout, stderr|
      res[:out] = stdout.read
      res[:err] = stderr.read
      res[:success] = ($?.success? and res[:err].empty?) ? 'ok' : 'ko'
    end
  when '1.9','2.0','2.1'
    Open3.popen3(cmd) do |stdin, stdout, stderr, thr|
      res[:out] = stdout.read
      res[:err] = stderr.read
      res[:success] = (thr.value.success? and res[:err].empty?) ? 'ok' : 'ko'
    end
  else
    raise "Unsupported Ruby version: #{RUBY_VERSION}"
  end
  return res
end