Module: Construqt::Util

Defined in:
lib/construqt/util.rb

Defined Under Namespace

Modules: Chainable

Constant Summary collapse

PORTS_DEF_REGEXP =
"([^\\d\\s]*[\\d,-]+)+"
PORT_NAME_REGEXP =
"^(.*?)(\\d+)$"

Class Method Summary collapse

Class Method Details

.add_clean_ip_pattern(pat) ⇒ Object



85
86
87
# File 'lib/construqt/util.rb', line 85

def self.add_clean_ip_pattern(pat)
  @clean_if << pat
end

.add_gre_prefix(name) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/construqt/util.rb', line 76

def self.add_gre_prefix(name)
  unless name.start_with?("gt")
    return "gt"+name
  end

  name
end

.clean_bgp(name) ⇒ Object



99
100
101
# File 'lib/construqt/util.rb', line 99

def self.clean_bgp(name)
  name.gsub(/[^a-z0-9]/, '_')
end

.clean_if(prefix, name) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/construqt/util.rb', line 89

def self.clean_if(prefix, name)
  unless name.start_with?(prefix)
    name = prefix+name
  end

  name = name.gsub(/[^a-z0-9]/, '')
  @clean_if.each { |pat| name.gsub!(pat, '') }
  name
end

.createRangeDefinition(ports) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/construqt/util.rb', line 107

def self.createRangeDefinition(ports)
  ranges=[]
  lastPort=nil

  #remove duplicates
  ports.uniq.sort do |l,r|
    fc = l.to_s.length <=> r.to_s.length
    fc!=0 ? fc : l<=>r
  end.each do |port|
    port = port.to_s
    if  (ranges.length>0 && portNeighbors?(port, ranges[ranges.length-1]["to"]))
      ranges[ranges.length-1]["to"] = port
    else
      ranges << {"from" => port, "to" => port}
    end
  end

  ranges = ranges.map do |range|
    range["from"] == range["to"] ? range["from"] : range["from"] +"-"+range["to"]
  end
  #puts "self.createRangeDefinition[#{ports}]=>#{ranges}"
  ranges.join(",")
end

.expandRangeDefinition(list_str) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/construqt/util.rb', line 133

def self.expandRangeDefinition(list_str)
  ports = list_str.strip.split(",").map do |range_def|
    range = range_def.split("-")
    if (range.length==1)
      range
    elsif (range.length==2)
      range[0]=~/#{PORT_NAME_REGEXP}/
      prefix_from=$1
      from = $2
      range[1]=~/#{PORT_NAME_REGEXP}/
      prefix_to=$1
      to = $2
      throw "port prefixes differ" unless prefix_from==prefix_to
      (from.to_i .. to.to_i).map {|n| prefix_from + n.to_s }
    else
      throw "invalid range found #{range_def}"
    end
  end
  ports.flatten
end

.generate_mac_address_from_name(name) ⇒ Object



154
155
156
157
# File 'lib/construqt/util.rb', line 154

def self.generate_mac_address_from_name(name)
  # http://www.iana.org/assignments/ethernet-numbers/ethernet-numbers.xhtml
  '8f:'+Digest::SHA256.hexdigest("#{name}").scan(/../)[0,5].join(':')
end

.indent(body, ident) ⇒ Object



159
160
161
162
163
164
# File 'lib/construqt/util.rb', line 159

def self.indent(body, ident)
  if ident.kind_of?(Fixnum)
    ident = (1..ident).to_a.map{' '}.join('')
  end
  body.lines.map { |line| ident+line.chomp.strip }.join("\n")
end

.password(a) ⇒ Object



72
73
74
# File 'lib/construqt/util.rb', line 72

def self.password(a)
  a
end

.portNeighbors?(port1, port2) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/construqt/util.rb', line 103

def self.portNeighbors?(port1, port2)
  port2.succ == port1 || port1.succ == port2
end

.write_str(str, *path) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/construqt/util.rb', line 64

def self.write_str(str, *path)
  path = File.join("cfgs", *path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'w') {|f| f.write(str) }
  Construqt.logger.info "Write:#{path}"
  return str
end