Class: Net::Openvpn::ClientConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/net/openvpn/client_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ ClientConfig

Returns a new instance of ClientConfig.



5
6
7
8
# File 'lib/net/openvpn/client_config.rb', line 5

def initialize(hostname)
  @hostname = hostname
  load if exists?
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/net/openvpn/client_config.rb', line 21

def exists?
  File.exists? path
end

#ip=(ip) ⇒ Object



25
26
27
# File 'lib/net/openvpn/client_config.rb', line 25

def ip=(ip)
  @ip = ip
end

#loadObject



10
11
12
13
14
15
# File 'lib/net/openvpn/client_config.rb', line 10

def load
  ccd = File.read(path)
  matches = ccd.match /ifconfig-push ([0-9\.]+) ([0-9\.]+)/
  @ip = matches[1]
  @network = matches[2]
end

#network=(network) ⇒ Object



29
30
31
# File 'lib/net/openvpn/client_config.rb', line 29

def network=(network)
  @network = network
end

#pathObject



17
18
19
# File 'lib/net/openvpn/client_config.rb', line 17

def path
  Net::Openvpn.ccdpath @hostname
end

#removeObject



38
39
40
# File 'lib/net/openvpn/client_config.rb', line 38

def remove
  File.delete path
end

#saveObject



42
43
44
45
46
47
48
# File 'lib/net/openvpn/client_config.rb', line 42

def save
  validate!
  
  File.open(path, "w") do |f|
    f.puts "ifconfig-push #{@ip} #{@network}"
  end
end

#validate!Object

Raises:

  • (ArgumentError)


33
34
35
36
# File 'lib/net/openvpn/client_config.rb', line 33

def validate!
  raise ArgumentError, "No IP set!" if @ip.nil? or @ip.empty?
  raise ArgumentError, "No network set!" if @network.nil? or @network.empty?
end