netconfgen 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/confgen +8 -5
- data/lib/netconfgen/netconfgen.rb +88 -5
- data/netconfgen.gemspec +1 -1
- data/netconfgen.yaml +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aa9d22b0e9d275cf0ef86fde23168d0453a605d
|
4
|
+
data.tar.gz: aec475534ce8cdae5c7f67469a19ef8c122ad906
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5e043ea9977c620995ac432d23e67860887a112f53fd2948e6de74b1955db2fe768537f8ea3c9c4f3c3b9f6e331306a14879ff348b7f3f7fd30b7fb9a6a318b
|
7
|
+
data.tar.gz: c7f2b03d00e9d483a23a732e523c461aafe95ccbd4dc094b7b683ee658388741c1d90cbeb76ede63efd499e086dde3bc65858d37d77df30b48b27be3be562e28
|
data/bin/confgen
CHANGED
@@ -11,6 +11,7 @@ require 'json'
|
|
11
11
|
|
12
12
|
CONFIG_FILE = ["/etc/netconfgen.yaml", "~/.netconfgen.yaml", "netconfgen.yaml"]
|
13
13
|
config = {}
|
14
|
+
|
14
15
|
CONFIG_FILE.each do |fname|
|
15
16
|
fname = File.expand_path(fname)
|
16
17
|
if File.exists?(fname)
|
@@ -61,6 +62,11 @@ if config[:listen] && config[:port] < 1024 && !Process.euid.zero?
|
|
61
62
|
abort 'Please run tftpd as root via sudo!'
|
62
63
|
end
|
63
64
|
|
65
|
+
|
66
|
+
if config[:verbose]
|
67
|
+
PP.pp(config, STDERR)
|
68
|
+
end
|
69
|
+
|
64
70
|
log = Logger.new(STDOUT)
|
65
71
|
log.level = config[:verbose] ? "DEBUG" : "INFO"
|
66
72
|
log.formatter = lambda do |s, d, p, m|
|
@@ -68,13 +74,10 @@ log.formatter = lambda do |s, d, p, m|
|
|
68
74
|
end
|
69
75
|
config[:logger] = log
|
70
76
|
|
71
|
-
if config[:verbose]
|
72
|
-
PP.pp(config, STDERR)
|
73
|
-
end
|
74
77
|
|
75
78
|
if !config[:listen]
|
76
|
-
STDERR.puts "Rendering block #{config[:name]}" if config[:verbose]
|
77
|
-
br = NetConfGen::BlockEngine.new(config[:root])
|
79
|
+
STDERR.puts "Rendering block #{config[:name]} from #{config[:root]}" if config[:verbose]
|
80
|
+
br = NetConfGen::BlockEngine.new(config[:root], config[:settings])
|
78
81
|
|
79
82
|
config[:variables].each do |k, v|
|
80
83
|
br.set(k, v)
|
@@ -2,7 +2,10 @@
|
|
2
2
|
#
|
3
3
|
|
4
4
|
require 'erb'
|
5
|
-
|
5
|
+
require 'net/http'
|
6
|
+
require 'uri'
|
7
|
+
require 'json'
|
8
|
+
require 'pp'
|
6
9
|
|
7
10
|
module NetConfGen
|
8
11
|
|
@@ -27,20 +30,100 @@ module NetConfGen
|
|
27
30
|
end
|
28
31
|
|
29
32
|
class BlockContext
|
30
|
-
def initialize(blockengine)
|
33
|
+
def initialize(blockengine, settings=nil)
|
31
34
|
@blockengine = blockengine
|
35
|
+
@settings = settings
|
32
36
|
end
|
33
37
|
|
34
38
|
def include(name)
|
35
39
|
block = @blockengine.load(name)
|
36
40
|
return block.render
|
37
41
|
end
|
42
|
+
|
43
|
+
# Creates an array of individual ports from a port range string
|
44
|
+
def portrange(portrange)
|
45
|
+
if m = portrange.match(/((?:Gi|Fa|Te)(?:[0-9]+\/)+)(\d+)(?:-(\d+))?/) # eg. Gi1/0/27-28 or Gi1/0/27
|
46
|
+
ports = []
|
47
|
+
if m[3]
|
48
|
+
(m[2]..m[3]).each do |i|
|
49
|
+
ports << (m[1] + i)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
ports << (m[1] + m[2])
|
53
|
+
end
|
54
|
+
|
55
|
+
return ports
|
56
|
+
elsif m = portrange.match(/(Gi|Fa|Te)\[(\d)(\d)\](\/\d+)/) # eg. Gi[34]/1
|
57
|
+
ports = []
|
58
|
+
(m[2]..m[3]).each do |i|
|
59
|
+
ports << (m[1] + i + m[4])
|
60
|
+
end
|
61
|
+
return ports
|
62
|
+
end
|
63
|
+
return []
|
64
|
+
end
|
65
|
+
|
66
|
+
def megaexcel_find_row_by_column_value(data, column_definition_row_number, column_name, column_value)
|
67
|
+
|
68
|
+
# The 3rd row is known to be containing the column names
|
69
|
+
columns = data["values"][column_definition_row_number]
|
70
|
+
|
71
|
+
# Set i to the INDEX of the column which we are searching
|
72
|
+
i = columns.index(column_name)
|
73
|
+
if i == nil
|
74
|
+
raise "Column #{column_name} not found from megaexcel"
|
75
|
+
end
|
76
|
+
|
77
|
+
# Find the row where the INDEX column contains the value we are looking for
|
78
|
+
row = data["values"].find do |x|
|
79
|
+
x[i] == column_value
|
80
|
+
end
|
81
|
+
|
82
|
+
# Convert the indexed row into an object which keys are the column names
|
83
|
+
obj = {}
|
84
|
+
columns.each_with_index do |key, j|
|
85
|
+
obj[key] = row[j]
|
86
|
+
end
|
87
|
+
|
88
|
+
return obj
|
89
|
+
end
|
90
|
+
|
91
|
+
def megaexcel(name)
|
92
|
+
url = @settings["megaexcel"]["url"]
|
93
|
+
name_field = @settings["megaexcel"]["name_field"]
|
94
|
+
column_definition_row_number = @settings["megaexcel"]["column_definition_row_number"]
|
95
|
+
|
96
|
+
ret = Net::HTTP.get(URI.parse(url))
|
97
|
+
data = JSON.parse(ret)
|
98
|
+
|
99
|
+
row = megaexcel_find_row_by_column_value(data, column_definition_row_number, name_field, name)
|
100
|
+
pp row
|
101
|
+
end
|
102
|
+
|
103
|
+
def megaexcel_vlans(data)
|
104
|
+
vlans_column_name = @settings["megaexcel"]["vlans_column_name"]
|
105
|
+
str = data[vlans_column_name]
|
106
|
+
vlans = []
|
107
|
+
str.each_line do |line|
|
108
|
+
if m = line.match(/((?:Fa|Gi|Te)[^ ]+).+?vlan\s?(\d+)/)
|
109
|
+
vlans << {
|
110
|
+
"ports" => m[1],
|
111
|
+
"vlan" => m[2],
|
112
|
+
"description" => line.chomp.strip,
|
113
|
+
}
|
114
|
+
else
|
115
|
+
# Warning. unknown vlan setup #{line}
|
116
|
+
end
|
117
|
+
end
|
118
|
+
return vlans
|
119
|
+
end
|
38
120
|
end
|
39
121
|
|
40
122
|
class BlockEngine
|
41
|
-
attr_reader :context
|
42
|
-
def initialize(basepath)
|
123
|
+
attr_reader :context, :settings
|
124
|
+
def initialize(basepath, settings=nil)
|
43
125
|
@basepath = basepath
|
126
|
+
@settings = settings
|
44
127
|
if !File.directory?(basepath)
|
45
128
|
raise "Basepath #{basepath} does not exists"
|
46
129
|
end
|
@@ -48,7 +131,7 @@ module NetConfGen
|
|
48
131
|
|
49
132
|
@blocks = {}
|
50
133
|
|
51
|
-
@context = BlockContext.new(self)
|
134
|
+
@context = BlockContext.new(self, @settings)
|
52
135
|
end
|
53
136
|
|
54
137
|
def set(key, value)
|
data/netconfgen.gemspec
CHANGED
data/netconfgen.yaml
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: netconfgen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juho Mäkinen juho.makinen@gmail.com
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubygems-tasks
|