Project

General

Profile

Download (3.26 KB) Statistics
| Branch: | Tag: | Revision:
bba6b24b Ohad Levy
class SmartProxy < Sinatra::Base
b35ab467 Ohad Levy
use Rack::MethodOverride
bba6b24b Ohad Levy
def dhcp_setup
raise "Smart Proxy is not configured to support DHCP" unless SETTINGS.dhcp
1728ab70 Paul Kelly
case SETTINGS.dhcp_vendor.downcase
bba6b24b Ohad Levy
when "isc"
require 'proxy/dhcp/server/isc'
unless SETTINGS.dhcp_config and SETTINGS.dhcp_leases \
and File.exist?(SETTINGS.dhcp_config) and File.exist?(SETTINGS.dhcp_leases)
log_halt 400, "Unable to find the DHCP configuration or lease files"
end
1728ab70 Paul Kelly
@server = Proxy::DHCP::ISC.new({:name => "127.0.0.1",
:config => File.read(SETTINGS.dhcp_config),
:leases => File.read(SETTINGS.dhcp_leases)})
when "native_ms"
require 'proxy/dhcp/server/native_ms'
@server = Proxy::DHCP::NativeMS.new(:server => SETTINGS.dhcp_server ? SETTINGS.dhcp_server : "127.0.0.1")
bba6b24b Ohad Levy
else
1728ab70 Paul Kelly
log_halt 400, "Unrecognized or missing DHCP vendor type: #{SETTINGS.dhcp_vendor.nil? ? "MISSING" : SETTINGS.dhcp_vendor}"
bba6b24b Ohad Levy
end
@subnets = @server.subnets
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
46d028cd Ohad Levy
end

bba6b24b Ohad Levy
helpers do
def load_subnet
@subnet = @server.find_subnet(params[:network])
log_halt 404, "Subnet #{params[:network]} not found" unless @subnet
@subnet
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
before do
dhcp_setup if request.path_info =~ /dhcp/
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
get "/dhcp" do
begin
if request.accept.include?("application/json")
content_type :json
2b9164ca Ohad Levy
bba6b24b Ohad Levy
log_halt 404, "No subnets found on server @{name}" unless @subnets
@subnets.map{|s| {:network => s.network, :netmask => s.netmask }}.to_json
else
erb :"dhcp/index"
end
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
46d028cd Ohad Levy
end
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
get "/dhcp/:network" do
begin
load_subnet
3929e459 Ohad Levy
if request.accept.include?("application/json")
content_type :json
{:reservations => @subnet.reservations, :leases => @subnet.leases}.to_json
else
erb :"dhcp/show"
end
bba6b24b Ohad Levy
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
get "/dhcp/:network/unused_ip" do
begin
content_type :json
({:ip => load_subnet.unused_ip}).to_json
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
get "/dhcp/:network/:record" do
begin
content_type :json
record = load_subnet[params[:record]]
log_halt 404, "Record #{params[:network]}/#{params[:record]} not found" unless record
record.options.to_json
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
# create a new record in a network
post "/dhcp/:network" do
begin
50e38ad1 Ohad Levy
content_type :json
f0e9b8bf Ohad Levy
options = params.reject{|k,v| k["network"]}
@server.addRecord(options)
b1b32f74 Paul Kelly
rescue Proxy::DHCP::Collision => e
fcc0d38b Ohad Levy
log_halt 409, e
f0e9b8bf Ohad Levy
rescue Proxy::DHCP::AlreadyExists
# no need to do anything
bba6b24b Ohad Levy
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
50e38ad1 Ohad Levy
end
bba6b24b Ohad Levy
end
50e38ad1 Ohad Levy
bba6b24b Ohad Levy
# delete a record from a network
delete "/dhcp/:network/:record" do
begin
record = load_subnet[params[:record]]
log_halt 404, "Record #{params[:network]}/#{params[:record]} not found" unless record
@server.delRecord @subnet, record
if request.accept.include?("application/json")
content_type :json
a551cc19 Paul Kelly
{}
bba6b24b Ohad Levy
else
redirect "/dhcp/#{params[:network]}"
end
rescue Exception => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
2b9164ca Ohad Levy
end
end