Class: Chake::Backend

Inherits:
Struct
  • Object
show all
Defined in:
lib/chake/backend.rb,
lib/chake/backend/ssh.rb,
lib/chake/backend/local.rb

Direct Known Subclasses

Local, Ssh

Defined Under Namespace

Classes: Local, Ssh

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nodeObject

Returns the value of attribute node

Returns:

  • (Object)

    the current value of node



3
4
5
# File 'lib/chake/backend.rb', line 3

def node
  @node
end

Class Method Details

.backend_nameObject



36
37
38
# File 'lib/chake/backend.rb', line 36

def self.backend_name
  name.split("::").last.downcase
end

.get(name) ⇒ Object



45
46
47
48
# File 'lib/chake/backend.rb', line 45

def self.get(name)
  backend = @backends.find { |b| b.backend_name == name }
  backend || raise(ArgumentError.new("Invalid backend name: #{name}"))
end

.inherited(subclass) ⇒ Object



40
41
42
43
# File 'lib/chake/backend.rb', line 40

def self.inherited(subclass)
  @backends ||= []
  @backends << subclass
end

Instance Method Details

#rsync_destObject



5
6
7
# File 'lib/chake/backend.rb', line 5

def rsync_dest
  node.path + '/'
end

#run(cmd) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chake/backend.rb', line 9

def run(cmd)
  puts "#{node.hostname}: $ #{cmd}"
  output = IO.popen(command_runner + [cmd])
  output.each_line do |line|
    puts [node.hostname, line.strip].join(': ')
  end
  output.close
  if $?
    status = $?.exitstatus
    if status != 0
      puts [node.hostname, 'FAILED with exit status %d' % status].join(': ')
    end
  end
end

#run_as_root(cmd) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/chake/backend.rb', line 24

def run_as_root(cmd)
  if node.username == 'root'
    run(cmd)
  else
    run('sudo sh -c "' + cmd + '"')
  end
end

#to_sObject



32
33
34
# File 'lib/chake/backend.rb', line 32

def to_s
  self.class.backend_name
end