Class: Tomo::Host

Inherits:
Object
  • Object
show all
Includes:
Testing::HostExtensions
Defined in:
lib/tomo/host.rb

Instance Attribute Summary collapse

Attributes included from Testing::HostExtensions

#helper_values, #mocks, #release, #scripts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Testing::HostExtensions

#mock, #result_for

Constructor Details

#initialize(address:, port: nil, log_prefix: nil, roles: nil, user: nil, privileged_user: "root") ⇒ Host

Returns a new instance of Host.



18
19
20
21
22
23
24
25
26
# File 'lib/tomo/host.rb', line 18

def initialize(address:, port: nil, log_prefix: nil, roles: nil, user: nil, privileged_user: "root")
  @user = user.freeze
  @port = (port || 22).to_i.freeze
  @address = address.freeze
  @log_prefix = log_prefix.freeze
  @roles = Array(roles).map(&:freeze).freeze
  @as_privileged = privileged_copy(privileged_user)
  freeze
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



8
9
10
# File 'lib/tomo/host.rb', line 8

def address
  @address
end

#as_privilegedObject (readonly)

Returns the value of attribute as_privileged.



8
9
10
# File 'lib/tomo/host.rb', line 8

def as_privileged
  @as_privileged
end

#log_prefixObject (readonly)

Returns the value of attribute log_prefix.



8
9
10
# File 'lib/tomo/host.rb', line 8

def log_prefix
  @log_prefix
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/tomo/host.rb', line 8

def port
  @port
end

#rolesObject (readonly)

Returns the value of attribute roles.



8
9
10
# File 'lib/tomo/host.rb', line 8

def roles
  @roles
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/tomo/host.rb', line 8

def user
  @user
end

Class Method Details

.parse(host) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/tomo/host.rb', line 10

def self.parse(host, **)
  host = host.to_s.strip
  user, address = host.match(PATTERN).captures
  raise ArgumentError, "host cannot be blank" if address.empty?

  new(user:, address:, **)
end

Instance Method Details

#to_sObject



34
35
36
37
38
# File 'lib/tomo/host.rb', line 34

def to_s
  str = user ? "#{user}@#{address}" : address
  str += ":#{port}" unless port == 22
  str
end

#to_ssh_argsObject



40
41
42
43
44
# File 'lib/tomo/host.rb', line 40

def to_ssh_args
  args = [user ? "#{user}@#{address}" : address]
  args.push("-p", port.to_s) unless port == 22
  args
end

#with_log_prefix(prefix) ⇒ Object



28
29
30
31
32
# File 'lib/tomo/host.rb', line 28

def with_log_prefix(prefix)
  copy = dup
  copy.instance_variable_set(:@log_prefix, prefix)
  copy.freeze
end