Class: OpenVZ::Util

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.enforce_sudoObject

Class variable which denotes whether execute commands using sudo should be true or false



42
43
44
# File 'lib/openvz/util.rb', line 42

def enforce_sudo
  @enforce_sudo
end

Class Method Details

.execute(cmd, params = {:cwd => "/tmp"}) ⇒ Object

Execute a System Command



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openvz/util.rb', line 46

def self.execute(cmd, params = {:cwd => "/tmp"})
    cmd = "sudo " + cmd if self.enforce_sudo 
  
    Log.debug("#{cmd}")
    
    s = Shell.new("#{cmd}", params)
    s.runcommand
    if s.status.exitstatus != 0
        raise "Cannot execute: '#{cmd}'. Return code: #{s.status.exitstatus}. Stderr: #{s.stderr}"
    end
    s.stdout
end

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