Skip to content

Commit

Permalink
Merge pull request #9 from bobrik/per-upstream-conf
Browse files Browse the repository at this point in the history
Save configs per upstream
  • Loading branch information
bobrik committed Jul 22, 2015
2 parents 49527aa + 688ebda commit 94b5d40
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions lua/zoidberg-state-handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ elseif ngx.req.get_method() == "PUT" or ngx.req.get_method() == "POST" then
local state = cjson.new().decode(ngx.req.get_body_data())
local zoidberg = ngx.shared.zoidberg
local global_directives = zoidberg:get("global_directives")
local enabled = {}
local saved = {}

local current = {}
Expand Down Expand Up @@ -110,34 +109,33 @@ elseif ngx.req.get_method() == "PUT" or ngx.req.get_method() == "POST" then
ngx.log(ngx.NOTICE, "updating " .. name .. ": " .. servers .. " upstreams")
local status, rv = require("ngx.dyups").update(name, upstream)
ngx.log(ngx.NOTICE, "updated " .. name .. ": " .. status .. ", " .. rv)

local temp_upstreams_file = "/etc/nginx/include/dyups/" .. name .. ".conf.temp"
local final_upstreams_file = "/etc/nginx/include/dyups/" .. name .. ".conf"
local dumped, openError = io.open(temp_upstreams_file, "w")
if not dumped then
ngx.log(ngx.ERR, "failed to open temp file for upstreams: " .. openError)
else
dumped:write("# AUTOMATICALLY GENERATED BY ZOIDBERG-NGINX, DO NOT EDIT" .. "\n")
dumped:write("# Comes from group: " .. group .. "\n")
dumped:write("upstream " .. name .. " {\n")
dumped:write(" " .. upstream:gsub("\n", "\n ") .. "\n")
dumped:write("}\n")

io.close(dumped)

local moved, moveError = os.rename(temp_upstreams_file, final_upstreams_file)
if not moved then
ngx.log(ngx.ERR, "failed to move temp file for upstreams to the place: " .. moveError)
end
end
end

saved[name] = upstream
enabled[name] = true
end
end

zoidberg:set(state_key, cjson.new().encode({ state = state, saved = saved, enabled = enabled }))

local temp_upstreams_file = "/etc/nginx/include/dyups/" .. group .. ".conf.temp"
local final_upstreams_file = "/etc/nginx/include/dyups/" .. group .. ".conf"
local dumped, openError = io.open(temp_upstreams_file, "w")
if not dumped then
ngx.log(ngx.ERR, "failed to open temp file for upstreams: " .. openError)
else
for name, directives in pairs(saved) do
dumped:write("upstream " .. name .. " {\n")
dumped:write(" " .. directives:gsub("\n", "\n ") .. "\n")
dumped:write("}\n")
end

io.close(dumped)

local moved, moveError = os.rename(temp_upstreams_file, final_upstreams_file)
if not moved then
ngx.log(ngx.ERR, "failed to move temp file for upstreams to the place: " .. moveError)
end
end
zoidberg:set(state_key, cjson.new().encode({ state = state, saved = saved }))

return ngx.exit(204)
else
Expand Down

0 comments on commit 94b5d40

Please # to comment.