Class: OpenVZ::Util

Inherits:
Object show all
Defined in:
lib/openvz/util.rb

Instance Method Summary collapse

Instance Method Details

#generate_mac(ctid, vlanid, for_host) ⇒ Object

Generate a mac address based upon three different variables



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/openvz/util.rb', line 4

def generate_mac(ctid, vlanid, for_host)
    ctid_str     = '%06i' % ctid
    vlanid_str   = '%04i' % vlanid

    bridgemac    = [0,0,0,0,0,0]
    bridgemac[1] = ctid_str[0..1]
    bridgemac[2] = ctid_str[2..3]
    bridgemac[3] = ctid_str[4..5]
    bridgemac[4] = vlanid_str[0..1]
    bridgemac[5] = vlanid_str[2..3]

    if for_host
        bridgemac[0] = '12'
    else
        bridgemac[0] = '02'
    end

    # assemble macstring   
    '%s:%s:%s:%s:%s:%s' % bridgemac[0,6]
end

#searchandreplace(file, pattern, replace) ⇒ Object

Search for a specific pattern and replace it with string in file.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openvz/util.rb', line 27

def searchandreplace(file, pattern, replace)
    if File.writeable?(file)
        File.open(file, 'w') do |f|
            $<.each_line do |line|
                f.puts line.gsub(Regexp.new(pattern), replace)
            end
        end
    else
        raise "File not writeable: #{file}."
    end
end