一键生成开发环境web环境部署

#!/usr/local/bin/lua
-- 一键生成开发环境web环境部署
-- 调用方式 lua init.lua example [example.local?]

if arg[1] == nil then print("arguments #1 missing") return  end
if arg[2] == nil then arg[2] = arg[1] .. ".local" end

-- 修改host
file = io.open("/etc/hosts", "a")

file:write("127.0.0.1    "..arg[2].."\n")
file:close()

-- 添加nginx配置
conf=io.open("/usr/local/etc/nginx/sites-enabled/template.conf", "r");

html = ""
for line in conf:lines() do
    html = html .. string.gsub(string.gsub(line, 'template.local', arg[2]), 'template', arg[1]) .. "\n"
end

conf:close();

local bak = io.open("/usr/local/etc/nginx/sites-enabled/"..arg[1]..".conf", 'a')
bak:write(html)
bak.close()

-- 重新载入nginx配置
local nginx = io.popen("which nginx")
local path = nginx:read("*all")
local t = io.popen("sudo /usr/local/bin/nginx -s reload")
local r, errMsg = t:read("*all")

if(nil == errMsg)
then
    print(string.format("success! plz visit http://%s to confirm", arg[2]))
else
    print(string.format("failed : %s", errMsg))
end

你可能感兴趣的:(一键生成开发环境web环境部署)