Class: SSHKit::Backend::Abstract

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

Direct Known Subclasses

Local, Netssh, Printer, Skipper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, &block) ⇒ Abstract


34
35
36
37
38
39
40
41
42
43
# File 'lib/sshkit/backends/abstract.rb', line 34

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

  @pwd   = nil
  @env   = nil
  @user  = nil
  @group = nil
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host


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

def host
  @host
end

Class Method Details

.configObject


123
124
125
# File 'lib/sshkit/backends/abstract.rb', line 123

def config
  @config ||= OpenStruct.new
end

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

Yields:


127
128
129
# File 'lib/sshkit/backends/abstract.rb', line 127

def configure
  yield config
end

Instance Method Details

#as(who, &_block) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sshkit/backends/abstract.rb', line 102

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 "if ! sudo -u \#{@user} whoami > /dev/null\nthen echo \"You cannot switch to user '\#{@user}' using sudo, please check the sudoers file\" 1>&2\nfalse\nfi\n", verbosity: Logger::DEBUG
  yield
ensure
  remove_instance_variable(:@user)
  remove_instance_variable(:@group)
end

#background(*args) ⇒ Object


68
69
70
71
72
73
74
# File 'lib/sshkit/backends/abstract.rb', line 68

def background(*args)
  SSHKit.config.deprecation_logger.log(
    'The background method is deprecated. Blame badly behaved pseudo-daemons!'
  )
  options = args.extract_options!.merge(run_in_background: true)
  create_command_and_execute(args, options).success?
end

#capture(*args) ⇒ Object


62
63
64
65
66
# File 'lib/sshkit/backends/abstract.rb', line 62

def capture(*args)
  options = { verbosity: Logger::DEBUG, strip: true }.merge(args.extract_options!)
  result = create_command_and_execute(args, options).full_stdout
  options[:strip] ? result.strip : result
end

#download!(_remote, _local = nil, _options = {}) ⇒ Object


134
# File 'lib/sshkit/backends/abstract.rb', line 134

def download!(_remote, _local=nil, _options = {}) raise MethodUnavailableError end

#execute(*args) ⇒ Object


76
77
78
79
# File 'lib/sshkit/backends/abstract.rb', line 76

def execute(*args)
  options = args.extract_options!
  create_command_and_execute(args, options).success?
end

#make(commands = []) ⇒ Object


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

def make(commands=[])
  execute :make, commands
end

#rake(commands = []) ⇒ Object


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

def rake(commands=[])
  execute :rake, commands
end

#redact(arg) ⇒ Object

Used in execute_command to hide redact() args a user passes in


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

def redact(arg) # Used in execute_command to hide redact() args a user passes in
  arg.to_s.extend(Redaction) # to_s due to our inability to extend Integer, etc
end

#runObject


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

def run
  Thread.current["sshkit_backend"] = self
  instance_exec(@host, &@block)
ensure
  Thread.current["sshkit_backend"] = nil
end

#test(*args) ⇒ Object


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

def test(*args)
  options = { verbosity: Logger::DEBUG, raise_on_non_zero_exit: false }.merge(args.extract_options!)
  create_command_and_execute(args, options).success?
end

#upload!(_local, _remote, _options = {}) ⇒ Object

Backends which extend the Abstract backend should implement the following methods:


133
# File 'lib/sshkit/backends/abstract.rb', line 133

def upload!(_local, _remote, _options = {}) raise MethodUnavailableError end

#with(environment, &_block) ⇒ Object


94
95
96
97
98
99
100
# File 'lib/sshkit/backends/abstract.rb', line 94

def with(environment, &_block)
  env_old = (@env ||= {})
  @env = env_old.merge environment
  yield
ensure
  @env = env_old
end

#within(directory, &_block) ⇒ Object


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

def within(directory, &_block)
  (@pwd ||= []).push directory.to_s
  execute "if test ! -d \#{File.join(@pwd)}\nthen echo \"Directory does not exist '\#{File.join(@pwd)}'\" 1>&2\nfalse\nfi\n", verbosity: Logger::DEBUG
    yield
ensure
  @pwd.pop
end