Module: Bcome::Tree

Includes:
Draw
Included in:
Node::Base
Defined in:
lib/objects/modules/tree.rb

Constant Summary

Constants included from Draw

Draw::BLIP, Draw::BOTTOM_ANCHOR, Draw::BOX_BOTTOM_LEFT, Draw::BOX_BOTTOM_RIGHT, Draw::BOX_HORIZONTAL_LINE, Draw::BOX_SIDE, Draw::BOX_TOP_LEFT, Draw::BOX_TOP_RIGHT, Draw::BRANCH, Draw::INGRESS, Draw::LEFT_PADDING, Draw::MID_SHIPS

Instance Method Summary collapse

Methods included from Draw

#box_it, #max_box_line_length

Instance Method Details

#build_tree(data_build_method, title_prefix) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/objects/modules/tree.rb', line 94

def build_tree(data_build_method, title_prefix)
  data = send(data_build_method)

  @lines = []
  title = "#{title_prefix.informational}\s#{namespace}"
  @lines << "\n"
  @lines << "#{BLIP}\s\s\s#{title}"
  @lines << INGRESS.to_s

  if data.nil?
    parent.build_tree(data_build_method)
    return
  end

  recurse_tree_lines(data)

  @lines.each do |line|
    print "#{LEFT_PADDING}#{line}\n"
  end

  print "\n\n"
  p
end

#deduce_tree_structure(index, number_lines) ⇒ Object



151
152
153
154
155
# File 'lib/objects/modules/tree.rb', line 151

def deduce_tree_structure(index, number_lines)
  return BOTTOM_ANCHOR, "\s" if (index + 1) == number_lines

  [MID_SHIPS, BRANCH]
end

#namespace_tree_lineObject



73
74
75
76
77
# File 'lib/objects/modules/tree.rb', line 73

def namespace_tree_line
  return "#{type.bc_green} #{identifier} (empty set)" if !server? && !resources.has_active_nodes?

  "#{type.bc_green} #{identifier}"
end

#network_namespace_tree_dataObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/objects/modules/tree.rb', line 54

def network_namespace_tree_data
  @tree = {}

  resources.sort_by(&:identifier).each do |resource|
    next if resource.hide?

    if resource.inventory?
      resource.load_nodes unless resource.nodes_loaded?
    end

    unless resource.is_a?(Bcome::Node::Inventory::Merge)
      next if resource.parent && !resource.parent.resources.is_active_resource?(resource)
    end
    @tree[resource.namespace_tree_line] = resource.resources.any? ? resource.network_namespace_tree_data : nil
  end

  @tree
end

#recurse_tree_lines(data, padding = '') ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/objects/modules/tree.rb', line 118

def recurse_tree_lines(data, padding = '')
  # @lines << padding + BRANCH

  data.each_with_index do |config, index|
    key = config[0]
    values = config[1]

    anchor, branch = deduce_tree_structure(index, data.size)

    labels = key.is_a?(Array) ? key : [key]

    labels.each_with_index do |label, index|
      key_string = if index == 0 #  First line
                     "#{anchor}\s#{label}"
                   else # Any subsequent line
                     "#{branch}#{"\s" * 4}\s#{label}"
                   end

      entry_string = "#{padding}#{key_string}"
      @lines << entry_string
    end # End labels group

    @lines << "#{padding}#{branch}" if labels.size > 1

    next unless values&.is_a?(Hash)

    tab_padding = padding + branch + ("\s" * (anchor.length + 4))
    recurse_tree_lines(values, tab_padding)
    @lines << padding + branch
  end
  nil
end

#routesObject



12
13
14
15
16
17
18
19
# File 'lib/objects/modules/tree.rb', line 12

def routes
  if machines.empty?
    puts "\nNo routes are found below this namespace (empty server list)\n".warning
  else
    title_prefix = 'Ssh connection routes'
    build_tree(:routing_tree_data, title_prefix)
  end
end

#routing_tree_dataObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/objects/modules/tree.rb', line 21

def routing_tree_data
  @tree = {}

  # For each namespace, we have many proxy chains
  proxy_chain_link.link.each do |proxy_chain, machines|
    is_direct = proxy_chain.hops.any? ? false : true

    if inventory?
      load_nodes unless nodes_loaded?
    end

    ## Machine data
    machine_data = {}
    machines.each do |machine|
      key = machine.routing_tree_line(is_direct)
      machine_data[key] = nil
    end

    ## Construct Hop data
    hops = proxy_chain.hops
    hop_lines = hops.compact.enum_for(:each_with_index).collect { |hop, index| hop.pretty_proxy_details(index + 1) }

    @tree.merge!(to_nested_hash(hop_lines, machine_data))
  end

  @tree
end

#routing_tree_line(is_direct = true) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/objects/modules/tree.rb', line 79

def routing_tree_line(is_direct = true)
  address = if is_direct && public_ip_address
              public_ip_address
            else
              internal_ip_address
            end

  [
    type.to_s.bc_cyan,
    "namespace:\s".bc_green + keyed_namespace,
    "ip address\s".bc_green + address.to_s,
    "user\s".bc_green + ssh_driver.user
  ]
end

#to_nested_hash(array, data) ⇒ Object



49
50
51
52
# File 'lib/objects/modules/tree.rb', line 49

def to_nested_hash(array, data)
  nested = array.reverse.inject(data) { |a, n| { n => a } }
  nested.is_a?(String) ? { "#{nested}": nil } : nested
end

#treeObject



7
8
9
10
# File 'lib/objects/modules/tree.rb', line 7

def tree
  title_prefix = 'Namespace tree'
  build_tree(:network_namespace_tree_data, title_prefix)
end