site_network.rb 1.95 KB
Newer Older
1
# coding: utf-8
2
3
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib')))
require 'wiki_generator'
4

5
require_relative '../../../input-validators/check-network-description'
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# This class generates the network description of each site, in .dot
# and .png format
class SiteNetworkGenerator < WikiGenerator

  def initialize(page_name, site)
    super(page_name)
    @site = site
    name = "#{@site.capitalize}Network"
    @files = [
      {	'content-type' => 'text/plain',
        'filename' => "#{name}.dot",
        'path' => "./#{name}.dot",
        'comment' => "#{site.capitalize} network description" },
      { 'content-type' => 'image/png',
        'filename' => "#{name}.png",
        'path' => "./#{name}.png",
        'comment' => "#{site.capitalize} network description" }
    ]
  end

27
28
29
30
31
  def generate_equipments
    h = G5K::get_global_hash['sites'][@site]
    return h['networks'].to_a.map { |e| "* #{e[0]}: #{e[1]['model']}" }.sort.join("\n")
  end

32
  def generate_content
33
    @generated_content = "__NOTOC__\n__NOEDITSECTION__\n"
34
35
36
    @generated_content += "= Network devices models =\n"
    @generated_content += generate_equipments + "\n"
    @generated_content += "More details (including address ranges are available from the [[Grid5000:Network]] page.\n"
37
38
    @generated_content += MW::LINE_FEED

39
40
    # this will generate dot and png network maps in the current directory
    check_network_description({:sites => [@site], :dot => true})
41
42
43
  end
end

Lucas Nussbaum's avatar
Lucas Nussbaum committed
44
45
if __FILE__ == $0
  options = WikiGenerator::parse_options
46

Lucas Nussbaum's avatar
Lucas Nussbaum committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  if (options)
    ret = 2
    begin
      ret = true
      generators = options[:sites].map{ |site| SiteNetworkGenerator.new('Generated/' + site.capitalize + 'Network', site) }
      generators.each{ |generator|
        ret &= generator.exec(options)
      }
    rescue MediawikiApi::ApiError => e
      puts e, e.backtrace
      ret = 3
    rescue StandardError => e
      puts e, e.backtrace
      ret = 4
    ensure
      exit(ret)
    end
64
  end
65
end