Class: Chake::Connection

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

Direct Known Subclasses

Local, Ssh

Defined Under Namespace

Classes: CommandFailed, 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



4
5
6
# File 'lib/chake/connection.rb', line 4

def node
  @node
end

Class Method Details

.connection_nameObject



66
67
68
# File 'lib/chake/connection.rb', line 66

def self.connection_name
  name.split('::').last.downcase
end

.get(name) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
# File 'lib/chake/connection.rb', line 76

def self.get(name)
  connection = @connections.find { |b| b.connection_name == name }
  raise ArgumentError, "Invalid connection name: #{name}" unless connection

  connection
end

.inherited(subclass) ⇒ Object



70
71
72
73
74
# File 'lib/chake/connection.rb', line 70

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

Instance Method Details

#read_output(io) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chake/connection.rb', line 33

def read_output(io)
  io.each_line do |line|
    node.log(line.gsub(/\s*$/, ''))
  end
  io.close
  if $CHILD_STATUS
    status = $CHILD_STATUS.exitstatus
    if status != 0
      raise CommandFailed, [node.hostname, 'FAILED with exit status %<status>d' % { status: status }].join(': ')
    end
  end
end

#rsyncObject



17
18
19
# File 'lib/chake/connection.rb', line 17

def rsync
  ['rsync']
end

#rsync_destObject



21
22
23
# File 'lib/chake/connection.rb', line 21

def rsync_dest
  "#{node.path}/"
end

#run(cmd) ⇒ Object



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

def run(cmd)
  node.log('$ %<command>s' % { command: cmd })
  io = IO.popen(command_runner + ['/bin/sh'], 'w+', err: %i[child out])
  io.write(cmd)
  io.close_write
  read_output(io)
end

#run_as_root(cmd) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/chake/connection.rb', line 50

def run_as_root(cmd)
  if node.remote_username == 'root'
    run(cmd)
  else
    run("sudo #{cmd}")
  end
end

#run_shellObject



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

def run_shell
  system(*shell_command)
end

#scpObject



9
10
11
# File 'lib/chake/connection.rb', line 9

def scp
  ['scp']
end

#scp_destObject



13
14
15
# File 'lib/chake/connection.rb', line 13

def scp_dest
  ''
end

#skip?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/chake/connection.rb', line 62

def skip?
  false
end

#to_sObject



58
59
60
# File 'lib/chake/connection.rb', line 58

def to_s
  self.class.connection_name
end