Class: Kdeploy::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/kdeploy/host.rb

Overview

Host class for managing remote host configuration and connection details

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, user: nil, port: nil, ssh_options: {}, roles: [], vars: {}) ⇒ Host

Returns a new instance of Host.



8
9
10
11
12
13
14
15
# File 'lib/kdeploy/host.rb', line 8

def initialize(hostname, user: nil, port: nil, ssh_options: {}, roles: [], vars: {})
  @hostname = hostname
  @user = user || Kdeploy.configuration&.default_user || ENV.fetch('USER', nil)
  @port = port || Kdeploy.configuration&.default_port || 22
  @ssh_options = ssh_options
  @roles = Array(roles)
  @vars = vars || {}
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def hostname
  @hostname
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def port
  @port
end

#rolesObject (readonly)

Returns the value of attribute roles.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def roles
  @roles
end

#ssh_optionsObject (readonly)

Returns the value of attribute ssh_options.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def ssh_options
  @ssh_options
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def user
  @user
end

#varsObject (readonly)

Returns the value of attribute vars.



6
7
8
# File 'lib/kdeploy/host.rb', line 6

def vars
  @vars
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare hosts for equality

Parameters:

  • other (Host)

    Host to compare with

Returns:

  • (Boolean)

    True if hosts are equal



69
70
71
72
73
74
75
# File 'lib/kdeploy/host.rb', line 69

def ==(other)
  return false unless other.is_a?(Host)

  hostname == other.hostname &&
    user == other.user &&
    port == other.port
end

#connection_optionsHash

Get SSH connection options

Returns:

  • (Hash)

    SSH options



47
48
49
50
51
52
# File 'lib/kdeploy/host.rb', line 47

def connection_options
  base_options = Kdeploy.configuration&.merged_ssh_options(@ssh_options) || @ssh_options
  base_options.merge(
    timeout: Kdeploy.configuration&.ssh_timeout || 30
  )
end

#connection_stringString

Get connection string for display

Returns:

  • (String)

    Connection string



41
42
43
# File 'lib/kdeploy/host.rb', line 41

def connection_string
  "#{@user}@#{@hostname}:#{@port}"
end

#has_role?(role) ⇒ Boolean

Check if host has specific role

Parameters:

  • role (String, Symbol)

    Role to check

Returns:

  • (Boolean)

    True if host has role



20
21
22
# File 'lib/kdeploy/host.rb', line 20

def has_role?(role)
  @roles.include?(role.to_s) || @roles.include?(role.to_sym)
end

#hashInteger

Generate hash code for host

Returns:

  • (Integer)

    Hash code



81
82
83
# File 'lib/kdeploy/host.rb', line 81

def hash
  [hostname, user, port].hash
end

#inspectString

Detailed string representation of the host

Returns:



62
63
64
# File 'lib/kdeploy/host.rb', line 62

def inspect
  "#<Kdeploy::Host #{connection_string} roles=#{@roles} vars=#{@vars.keys}>"
end

#set_var(key, value) ⇒ Object

Set variable value

Parameters:

  • key (String, Symbol)

    Variable key

  • value (Object)

    Variable value

Returns:

  • (Object)

    Set value



35
36
37
# File 'lib/kdeploy/host.rb', line 35

def set_var(key, value)
  @vars[key.to_s] = value
end

#to_sString

String representation of the host

Returns:

  • (String)

    Connection string



56
57
58
# File 'lib/kdeploy/host.rb', line 56

def to_s
  connection_string
end

#var(key) ⇒ Object

Get variable value

Parameters:

  • key (String, Symbol)

    Variable key

Returns:

  • (Object)

    Variable value



27
28
29
# File 'lib/kdeploy/host.rb', line 27

def var(key)
  @vars[key.to_s] || @vars[key.to_sym]
end