Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Commit

Permalink
Bug 1017878 - Do not fail if the module was not initialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmillner committed Oct 10, 2013
1 parent 55a59e8 commit 362257c
Showing 1 changed file with 47 additions and 19 deletions.
Expand Up @@ -41,26 +41,19 @@ def initialize(*args)
end

def create
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
if not d.has_key?(@fqdn)
d[@fqdn]={
"aliases" => [],
"connections" => {}
}
end
end
with_create
end

def destroy
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
writer_if_exists do |d|
d.delete(@fqdn)
end
end


def connect(*elements)
reported_urls=[]
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
with_create do |d|
elements.each do |path, uri, options|

next unless options["protocols"] and options["protocols"].include?("tls")
Expand Down Expand Up @@ -94,17 +87,18 @@ def connect(*elements)
end

def connections
HaproxySNIProxyDB.open(HaproxySNIProxyDB::READER) do |d|
reader_if_exists do |d|
return d[@fqdn]["connections"].select { |port, backend|
backend.to_s != ""
}.map { |port, backend|
["TLS_PORT_#{@sni_ports.index(port)+1}", backend, { "protocols"=>["tls"] } ]
}
end
[]
end

def disconnect(*paths)
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
writer_if_exists do |d|
paths.each do |path|
reqport = path
if path=~/^TLS_PORT_(\d+)$/
Expand All @@ -119,27 +113,61 @@ def disconnect(*paths)
end

def aliases
HaproxySNIProxyDB.open(HaproxySNIProxyDB::READER) do |d|
begin
return d[@fqdn]["aliases"].clone
rescue NoMethodError
end
reader_if_exists do |d|
return d[@fqdn]["aliases"].clone
end
nil
end

def add_alias(name)
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
with_create do |d|
d[@fqdn]["aliases"] << name
end
end

def remove_alias(name)
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
writer_if_exists do |d|
d[@fqdn]["aliases"].delete(name)
end
end


private

# Private: Create the database entry if it does not already exist.
def with_create
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
if not d.has_key?(@fqdn)
d[@fqdn]={
"aliases" => [],
"connections" => {}
}
end
if block_given?
yield(d)
end
end
end

# Private: Yield the provided block if the record exists
def reader_if_exists
HaproxySNIProxyDB.open(HaproxySNIProxyDB::READER) do |d|
if block_given? and d.has_key?(@fqdn)
yield(d)
end
end
end

# Private: Yield the provided block if the record exists
# and allow modifications.
def writer_if_exists
HaproxySNIProxyDB.open(HaproxySNIProxyDB::WRCREAT) do |d|
if block_given? and d.has_key?(@fqdn)
yield(d)
end
end
end

end

#
Expand Down

0 comments on commit 362257c

Please sign in to comment.