Class: SwarmClusterCliOpe::Node

Inherits:
Object
  • Object
show all
Includes:
LoggerConcern
Defined in:
lib/swarm_cluster_cli_ope/node.rb

Direct Known Subclasses

Manager, Worker

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggerConcern

#logger

Constructor Details

#initialize(name: nil, connection_uri: nil) ⇒ Node

Returns a new instance of Node.

Parameters:

  • name (String) (defaults to: nil)
  • connection_uri (String) (defaults to: nil)


15
16
17
18
# File 'lib/swarm_cluster_cli_ope/node.rb', line 15

def initialize(name: nil, connection_uri: nil)
  @name = name
  @connection_uri = connection_uri || name
end

Instance Attribute Details

#connection_uriString

Returns nome da utilizzare nella parte DOCKER_HOST=CONNECTION_URI.

Returns:

  • (String)

    nome da utilizzare nella parte DOCKER_HOST=CONNECTION_URI



10
11
12
# File 'lib/swarm_cluster_cli_ope/node.rb', line 10

def connection_uri
  @connection_uri
end

#nameString

Returns nome del nodo.

Returns:

  • (String)

    nome del nodo



8
9
10
# File 'lib/swarm_cluster_cli_ope/node.rb', line 8

def name
  @name
end

Class Method Details

.info(connection_uri) ⇒ Object

Ritorna le info base di un nodo



69
70
71
72
73
74
75
76
# File 'lib/swarm_cluster_cli_ope/node.rb', line 69

def self.info(connection_uri)
  command = Commands::Base.new
  command.docker_host = "DOCKER_HOST=#{connection_uri}"
  result = command.command do |cmd|
    cmd.add("info")
  end.execute.result.first
  result
end

Instance Method Details

#hostnameString

connection_uri senza la parte del protocollo

Returns:

  • (String)


29
30
31
# File 'lib/swarm_cluster_cli_ope/node.rb', line 29

def hostname
  URI(connection_uri).host
end

#idString

ID univoco del nodo

Returns:

  • (String)


44
45
46
# File 'lib/swarm_cluster_cli_ope/node.rb', line 44

def id
  info.Swarm.NodeID
end

#infoOpenStruct

Info del nodo da parte di docker

Returns:

  • (OpenStruct)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/swarm_cluster_cli_ope/node.rb', line 51

def info
  # path al file di cache
  path = Time.now.strftime("/tmp/.swarm_cluster_cli_info_cache_#{name}-%Y%m%d%H")
  if File.exist?(path)
    i = JSON.parse(File.read(path), object_class: OpenStruct)
  else
    i = Node.info(connection_uri)
    #mi salvo in cache le info scaricate
    File.open(path, "w") do |f|
      f.write(i.to_h.to_json)
    end
  end

  i
end

#is_over_ssh_uri?Boolean

Mi definisce se la connessione che stiamo facendo con questo nodo, la facciamo tramite SSH oppure è locale

Returns:

  • (Boolean)


22
23
24
# File 'lib/swarm_cluster_cli_ope/node.rb', line 22

def is_over_ssh_uri?
  connection_uri.match?(/\Assh\:/)
end

#manager?TrueClass, FalseClass

Controlla se questo nodo è un manager

Returns:

  • (TrueClass, FalseClass)


37
38
39
# File 'lib/swarm_cluster_cli_ope/node.rb', line 37

def manager?
  info.Swarm["RemoteManagers"].collect { |n| n["NodeID"] }.include?(info.Swarm["NodeID"])
end