Skip to content

Instantly share code, notes, and snippets.

@rrreeeyyy
Created June 17, 2014 17:50
Show Gist options
  • Save rrreeeyyy/13286fa53413fa4d594c to your computer and use it in GitHub Desktop.
Save rrreeeyyy/13286fa53413fa4d594c to your computer and use it in GitHub Desktop.
IPVS Web API using mruby-ipvs and mruby-simplehttpserver.
services = {}
def body_parser(body)
params = {}
body.split('&').each do |x|
tokens = x.split('=', 2)
if tokens && tokens.size == 2
params[tokens[0]] = HTTP::URL::decode(tokens[1])
end
end
params
end
server = SimpleHttpServer.new({
:server_ip => "0.0.0.0",
:port => 8000,
})
server.http do |r|
server.set_response_headers({
"Server" => "mruby-simplehttpserver",
"Date" => server.http_date,
})
end
server.location "/api/v1/service/add" do |r|
params = body_parser r.body
server.response_body = "OK"
service_addr = params['service_addr']
service_port = params['service_port']
service_key = [service_addr, service_port].join(':')
begin
service = IPVS::Service.new({
'addr' => service_addr,
'port' => service_port.to_i
}).add_service
services[service_key] = {obj: service, dests: {}}
rescue => e
server.response_body = "ERROR: #{e.message}"
end
server.create_response
end
server.location "/api/v1/service/remove" do |r|
params = body_parser r.body
server.response_body = "OK"
service_addr = params['service_addr']
service_port = params['service_port']
service_key = [service_addr, service_port].join(':')
service = services[service_key]
begin
service[:obj].del_service
service.delete service_key
rescue => e
server.response_body = "ERROR: #{e.message}"
end
server.create_response
end
server.location "/api/v1/dest/add" do |r|
params = body_parser r.body
server.response_body = "OK"
service_addr = params['service_addr']
service_port = params['service_port']
dest_addr = params['dest_addr']
dest_port = params['dest_port']
service_key = [service_addr, service_port].join(':')
dest_key = [dest_addr, dest_port].join(':')
service = services[service_key]
begin
dest = IPVS::Dest.new({
'addr' => dest_addr,
'port' => dest_port.to_i
})
service[:obj].add_dest dest
service[:dests][dest_key] = dest
rescue => e
server.response_body = "ERROR: #{e.message}"
end
server.create_response
end
server.location "/api/v1/dest/remove" do |r|
params = body_parser r.body
server.response_body = "OK"
service_addr = params['service_addr']
service_port = params['service_port']
dest_addr = params['dest_addr']
dest_port = params['dest_port']
service_key = [service_addr, service_port].join(':')
dest_key = [dest_addr, dest_port].join(':')
service = services[service_key]
dest = service[:dests][dest_key]
begin
service[:obj].del_dest dest
service[:dests].delete dest_key
rescue => e
server.response_body = "ERROR: #{e.message}"
end
server.create_response
end
server.run
@rrreeeyyy
Copy link
Author

Endpoint:

http://#{SERVER_ADDR}/api/v1

Add Service

POST /api/v1/service/add
  • INPUT
    • service_addr, service_port

Remove Service

POST /api/v1/service/remove
  • INPUT
    • service_addr, service_port

Add Dest

POST /api/v1/dest/add
  • INPUT
    • service_addr, service_port, dest_addr, dest_port

Remove Dest

POST /api/v1/dest/remove
  • INPUT
    • service_addr, service_port, dest_addr, dest_port

Usage:

Server

requirements

  • mruby-ipvs
  • mruby-simplehttpserver
    • mruby-io
    • mruby-socket
    • mruby-http
mruby ipvs_http_api.rb

Client

curl -X POST -d "service_addr=192.168.0.1&service_port=80" http://localhost:8000/api/v1/service/add
ipvsadm -L # or cat /proc/net/ip_vs
curl -X POST -d "service_addr=192.168.0.1&service_port=80&dest_addr=192.168.0.2&dest_port=80" http://localhost:8000/api/v1/dest/add
ipvsadm -L # or cat /proc/net/ip_vs
curl -X POST -d "service_addr=192.168.0.1&service_port=80&dest_addr=192.168.0.2&dest_port=80" http://localhost:8000/api/v1/dest/remove
ipvsadm -L # or cat /proc/net/ip_vs
curl -X POST -d "service_addr=192.168.0.1&service_port=80" http://localhost:8000/api/v1/service/remove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment