Class: SSHKit::Backend::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/backends/abstract.rb

Direct Known Subclasses

Printer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, &block) ⇒ Abstract

Returns a new instance of Abstract.



15
16
17
18
19
# File 'lib/sshkit/backends/abstract.rb', line 15

def initialize(host, &block)
  raise "Must pass a Host object" unless host.is_a? Host
  @host  = host
  @block = block
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/sshkit/backends/abstract.rb', line 9

def host
  @host
end

Class Method Details

.configObject



112
113
114
# File 'lib/sshkit/backends/abstract.rb', line 112

def config
  @config ||= OpenStruct.new
end

.configure {|config| ... } ⇒ Object

Yields:



116
117
118
# File 'lib/sshkit/backends/abstract.rb', line 116

def configure
  yield config
end

Instance Method Details

#as(who, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sshkit/backends/abstract.rb', line 91

def as(who, &block)
  if who.is_a? Hash
    @user  = who[:user]  || who["user"]
    @group = who[:group] || who["group"]
  else
    @user  = who
    @group = nil
  end
  execute <<-EOTEST, verbosity: Logger::DEBUG
    if ! sudo -u #{@user} whoami > /dev/null
      then echo "You cannot switch to user '#{@user}' using sudo, please check the sudoers file" 1>&2
      false
    fi
  EOTEST
  yield
ensure
  remove_instance_variable(:@user)
  remove_instance_variable(:@group)
end

#capture(command, args = []) ⇒ Object



65
66
67
# File 'lib/sshkit/backends/abstract.rb', line 65

def capture(command, args=[])
  raise MethodUnavailableError
end

#debug(messages) ⇒ Object



41
42
43
# File 'lib/sshkit/backends/abstract.rb', line 41

def debug(messages)
  output << LogMessage.new(Logger::DEBUG, messages)
end

#error(messages) ⇒ Object



29
30
31
# File 'lib/sshkit/backends/abstract.rb', line 29

def error(messages)
  output << LogMessage.new(Logger::ERROR, messages)
end

#execute(command, args = []) ⇒ Object



61
62
63
# File 'lib/sshkit/backends/abstract.rb', line 61

def execute(command, args=[])
  raise MethodUnavailableError
end

#fatal(messages) ⇒ Object



25
26
27
# File 'lib/sshkit/backends/abstract.rb', line 25

def fatal(messages)
  output << LogMessage.new(Logger::FATAL, messages)
end

#info(messages) ⇒ Object



37
38
39
# File 'lib/sshkit/backends/abstract.rb', line 37

def info(messages)
  output << LogMessage.new(Logger::INFO, messages)
end

#log(messages) ⇒ Object



21
22
23
# File 'lib/sshkit/backends/abstract.rb', line 21

def log(messages)
  info(messages)
end

#make(commands = []) ⇒ Object



49
50
51
# File 'lib/sshkit/backends/abstract.rb', line 49

def make(commands=[])
  raise MethodUnavailableError
end

#rake(commands = []) ⇒ Object



53
54
55
# File 'lib/sshkit/backends/abstract.rb', line 53

def rake(commands=[])
  raise MethodUnavailableError
end

#runObject



11
12
13
# File 'lib/sshkit/backends/abstract.rb', line 11

def run
  # Nothing to do
end

#test(command, args = []) ⇒ Object



57
58
59
# File 'lib/sshkit/backends/abstract.rb', line 57

def test(command, args=[])
  raise MethodUnavailableError
end

#trace(messages) ⇒ Object



45
46
47
# File 'lib/sshkit/backends/abstract.rb', line 45

def trace(messages)
  output << LogMessage.new(Logger::TRACE, messages)
end

#warn(messages) ⇒ Object



33
34
35
# File 'lib/sshkit/backends/abstract.rb', line 33

def warn(messages)
  output << LogMessage.new(Logger::WARN, messages)
end

#with(environment, &block) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/sshkit/backends/abstract.rb', line 82

def with(environment, &block)
  @_env = (@env ||= {})
  @env = @_env.merge environment
  yield
ensure
  @env = @_env
  remove_instance_variable(:@_env)
end

#within(directory, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sshkit/backends/abstract.rb', line 69

def within(directory, &block)
  (@pwd ||= []).push directory.to_s
  execute <<-EOTEST, verbosity: Logger::DEBUG
    if test ! -d #{File.join(@pwd)}
      then echo "Directory does not exist '#{File.join(@pwd)}'" 1>&2
      false
    fi
    EOTEST
    yield
ensure
  @pwd.pop
end