diff options
| author | ruki <[email protected]> | 2022-05-13 00:47:27 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-13 00:47:27 +0800 |
| commit | b7e4be37dc71c3d0d5217f32b0960bc042455d30 (patch) | |
| tree | ff503c407ea4e0b98a2487d3d079cd4eda324e0e | |
| parent | 9287baf136e9c347da35d27cd7c6c1c1c54b89a2 (diff) | |
rewrite service config
24 files changed, 157 insertions, 117 deletions
diff --git a/xmake/actions/service/main.lua b/xmake/actions/service/main.lua index 36fa3f368..0a3557503 100644 --- a/xmake/actions/service/main.lua +++ b/xmake/actions/service/main.lua @@ -20,7 +20,8 @@ -- imports import("core.base.option") -import("private.service.config") +import("private.service.server_config") +import("private.service.client_config") import("private.service.start_service") import("private.service.restart_service") import("private.service.stop_service") @@ -29,7 +30,6 @@ import("private.service.reconnect_service") import("private.service.disconnect_service") import("private.service.clean_files") import("private.service.sync_files") -import("private.service.import_config") import("private.service.add_user") import("private.service.rm_user") import("private.service.gen_token") @@ -37,7 +37,8 @@ import("private.service.show_logs") import("private.service.show_status") function main() - config.load() + server_config.load() + client_config.load() if option.get("start") then start_service({daemon = true}) elseif option.get("restart") then @@ -54,8 +55,6 @@ function main() clean_files() elseif option.get("sync") then sync_files() - elseif option.get("config") then - import_config(option.get("config")) elseif option.get("gen-token") then gen_token() elseif option.get("add-user") then diff --git a/xmake/actions/service/xmake.lua b/xmake/actions/service/xmake.lua index 4f8fd4cf7..8a0ce6bdd 100644 --- a/xmake/actions/service/xmake.lua +++ b/xmake/actions/service/xmake.lua @@ -45,9 +45,6 @@ task("service") {nil, "distcc", "k", nil, "Start or connect the distributed build service." }, {nil, "sync", "k", nil, "Sync current project files in the remote daemon service." }, {nil, "clean", "k", nil, "Clean current project files in the remote daemon service." }, - {nil, "config", "kv", nil, "Import the configuration file. (default: ~/.xmake/service.conf)", - "e.g.", - " - xmake service --config=/tmp/config.lua" }, {nil, "add-user", "kv", nil, "Add user in the server.", "e.g.", " - xmake service --add-user=root" }, diff --git a/xmake/modules/private/service/add_user.lua b/xmake/modules/private/service/add_user.lua index 8e5512b5d..eebf1decc 100644 --- a/xmake/modules/private/service/add_user.lua +++ b/xmake/modules/private/service/add_user.lua @@ -24,7 +24,7 @@ import("core.base.base64") import("core.base.bytes") import("core.base.hashset") import("private.service.service") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) function main(user) assert(user, "empty user name!") @@ -41,10 +41,9 @@ function main(user) -- save to configs local configs = assert(config.configs(), "configs not found!") - configs.server = configs.server or {} - configs.server.tokens = configs.server.tokens or {} - if not hashset.from(configs.server.tokens):has(token) then - table.insert(configs.server.tokens, token) + configs.tokens = configs.tokens or {} + if not hashset.from(configs.tokens):has(token) then + table.insert(configs.tokens, token) else cprint("User ${bright}%s${clear} has been added!", user) return diff --git a/xmake/modules/private/service/clean_files.lua b/xmake/modules/private/service/clean_files.lua index caacbeda4..2e70c6c64 100644 --- a/xmake/modules/private/service/clean_files.lua +++ b/xmake/modules/private/service/clean_files.lua @@ -20,7 +20,6 @@ -- imports import("core.base.option") -import("private.service.config") import("private.service.remote_build.client", {alias = "remote_build_client"}) function main() diff --git a/xmake/modules/private/service/client_config.lua b/xmake/modules/private/service/client_config.lua new file mode 100644 index 000000000..58aaf65e5 --- /dev/null +++ b/xmake/modules/private/service/client_config.lua @@ -0,0 +1,98 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file client_config.lua +-- + +-- imports +import("core.base.global") +import("core.base.base64") +import("core.base.bytes") +import("private.service.server_config") + +-- get a local server token +function _get_local_server_token() + local tokens = server_config.get("tokens") + if tokens then + return tokens[1] + end +end + +-- generate a default config file +function _generate_configfile() + local filepath = configfile() + assert(not _g.configs and not os.isfile(filepath)) + local token = _get_local_server_token() + print("generating the config file to %s ..", filepath) + local configs = { + remote_build = { + -- without authorization: "127.0.0.1:9691" + -- with user authorization: "[email protected]:9691" + connect = "127.0.0.1:9691", + -- with token authorization + token = token + }, + distcc_build = { + hosts = { + {connect = "127.0.0.1:9692", token = token} + } + } + + } + save(configs) +end + +-- get config file path +function configfile() + return path.join(global.directory(), "service", "client.conf") +end + +-- get all configs +function configs() + return _g.configs +end + +-- get the given config, e.g. client_config.get("remote_build.connect") +function get(name) + local value = configs() + for _, key in ipairs(name:split('.', {plain = true})) do + if type(value) == "table" then + value = value[key] + else + value = nil + break + end + end + return value +end + +-- load configs +function load() + assert(not _g.configs, "config has been loaded!") + local filepath = configfile() + if not os.isfile(filepath) then + _generate_configfile() + end + assert(os.isfile(filepath), "%s not found!", filepath) + _g.configs = io.load(filepath) +end + +-- save configs +function save(configs) + local filepath = configfile() + io.save(filepath, configs, {orderkeys = true}) +end diff --git a/xmake/modules/private/service/connect_service.lua b/xmake/modules/private/service/connect_service.lua index a2c9e2ee8..8559c66c6 100644 --- a/xmake/modules/private/service/connect_service.lua +++ b/xmake/modules/private/service/connect_service.lua @@ -21,7 +21,7 @@ -- imports import("core.base.option") import("core.base.scheduler") -import("private.service.config") +import("private.service.client_config", {alias = "config"}) import("private.service.remote_build.client", {alias = "remote_build_client"}) import("private.service.distcc_build.client", {alias = "distcc_build_client"}) @@ -40,7 +40,7 @@ function main(...) elseif option.get("distcc") then table.insert(connectors, _connect_distcc_build_server) else - if config.get("remote_build.client") then + if config.get("remote_build") then table.insert(connectors, _connect_remote_build_server) end end diff --git a/xmake/modules/private/service/disconnect_service.lua b/xmake/modules/private/service/disconnect_service.lua index 3f2abaa13..3ee503dfa 100644 --- a/xmake/modules/private/service/disconnect_service.lua +++ b/xmake/modules/private/service/disconnect_service.lua @@ -21,7 +21,7 @@ -- imports import("core.base.option") import("core.base.scheduler") -import("private.service.config") +import("private.service.client_config", {alias = "config"}) import("private.service.remote_build.client", {alias = "remote_build_client"}) import("private.service.distcc_build.client", {alias = "distcc_build_client"}) @@ -40,7 +40,7 @@ function main(...) elseif option.get("distcc") then table.insert(disconnectors, _disconnect_distcc_build_server) else - if config.get("remote_build.client") then + if config.get("remote_build") then table.insert(disconnectors, _disconnect_remote_build_server) end end diff --git a/xmake/modules/private/service/distcc_build/client.lua b/xmake/modules/private/service/distcc_build/client.lua index 4e7f14e48..9f85e3d3e 100644 --- a/xmake/modules/private/service/distcc_build/client.lua +++ b/xmake/modules/private/service/distcc_build/client.lua @@ -26,7 +26,7 @@ import("core.base.option") import("core.base.scheduler") import("core.project.config", {alias = "project_config"}) import("lib.detect.find_tool") -import("private.service.config") +import("private.service.client_config", {alias = "config"}) import("private.service.message") import("private.service.client") import("private.service.stream", {alias = "socket_stream"}) @@ -40,7 +40,7 @@ function distcc_build_client:init() super.init(self) -- init hosts - local hosts = assert(config.get("distcc_build.client.hosts"), "config(distcc_build.client.hosts): not found!") + local hosts = assert(config.get("distcc_build.hosts"), "config(distcc_build.hosts): not found!") self:hosts_set(hosts) -- get project directory @@ -97,7 +97,7 @@ function distcc_build_client:connect() end -- we need user authorization? - local token = config.get("distcc_build.client.token") + local token = config.get("distcc_build.token") if not token and self:user() then -- get user password diff --git a/xmake/modules/private/service/distcc_build/server.lua b/xmake/modules/private/service/distcc_build/server.lua index 7031a65ba..0c38687d8 100644 --- a/xmake/modules/private/service/distcc_build/server.lua +++ b/xmake/modules/private/service/distcc_build/server.lua @@ -19,7 +19,7 @@ -- -- imports -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.message") import("private.service.server") import("private.service.stream", {alias = "socket_stream"}) @@ -35,7 +35,7 @@ function distcc_build_server:init(daemon) super.init(self, daemon) -- init address - local address = assert(config.get("distcc_build.server.listen"), "config(distcc_build.server.listen): not found!") + local address = assert(config.get("distcc_build.listen"), "config(distcc_build.listen): not found!") super.address_set(self, address) -- init handler diff --git a/xmake/modules/private/service/distcc_build/session.lua b/xmake/modules/private/service/distcc_build/session.lua index a43a021fc..0bc33a846 100644 --- a/xmake/modules/private/service/distcc_build/session.lua +++ b/xmake/modules/private/service/distcc_build/session.lua @@ -26,7 +26,7 @@ import("core.base.global") import("core.base.option") import("core.base.hashset") import("core.base.scheduler") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.message") -- define module @@ -80,9 +80,9 @@ end -- get work directory function session:workdir() - local workdir = config.get("distcc_build.server.workdir") + local workdir = config.get("distcc_build.workdir") if not workdir then - workdir = path.join(global.directory(), "service", "distcc_build") + workdir = path.join(global.directory(), "service", "server", "distcc_build") end return path.join(workdir, "sessons", self:id()) end diff --git a/xmake/modules/private/service/gen_token.lua b/xmake/modules/private/service/gen_token.lua index 6da8ea33f..a6326f33f 100644 --- a/xmake/modules/private/service/gen_token.lua +++ b/xmake/modules/private/service/gen_token.lua @@ -24,7 +24,7 @@ import("core.base.base64") import("core.base.bytes") import("core.base.hashset") import("private.service.service") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) -- generate a token function _generate_token() @@ -38,10 +38,9 @@ function main() -- save to configs local configs = assert(config.configs(), "configs not found!") - configs.server = configs.server or {} - configs.server.tokens = configs.server.tokens or {} - if not hashset.from(configs.server.tokens):has(token) then - table.insert(configs.server.tokens, token) + configs.tokens = configs.tokens or {} + if not hashset.from(configs.tokens):has(token) then + table.insert(configs.tokens, token) else cprint("Token ${yellow bright}%s${clear} has been added!", token) return diff --git a/xmake/modules/private/service/import_config.lua b/xmake/modules/private/service/import_config.lua deleted file mode 100644 index 2a4fcebef..000000000 --- a/xmake/modules/private/service/import_config.lua +++ /dev/null @@ -1,30 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, TBOOX Open Source Group. --- --- @author ruki --- @file import_config.lua --- - --- imports -import("core.base.option") -import("private.service.service") -import("private.service.config") - -function main(filepath) - os.cp(filepath, config.configfile()) - print("import %s ok!", filepath) -end - diff --git a/xmake/modules/private/service/remote_build/action.lua b/xmake/modules/private/service/remote_build/action.lua index 3ac67fd6c..c7519bf5c 100644 --- a/xmake/modules/private/service/remote_build/action.lua +++ b/xmake/modules/private/service/remote_build/action.lua @@ -19,7 +19,7 @@ -- -- imports -import("private.service.config") +import("private.service.client_config", {alias = "config"}) import("private.service.remote_build.client", {alias = "remote_build_client"}) -- is enabled? diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index 66c7c2791..143ec3d9d 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -26,7 +26,7 @@ import("core.base.option") import("core.base.scheduler") import("core.project.config", {alias = "project_config"}) import("lib.detect.find_tool") -import("private.service.config") +import("private.service.client_config", {alias = "config"}) import("private.service.message") import("private.service.client") import("private.service.stream", {alias = "socket_stream"}) @@ -41,7 +41,7 @@ function remote_build_client:init() super.init(self) -- init address - local address = assert(config.get("remote_build.client.connect"), "config(remote_build.client.connect): not found!") + local address = assert(config.get("remote_build.connect"), "config(remote_build.connect): not found!") self:address_set(address) -- get project directory @@ -74,7 +74,7 @@ function remote_build_client:connect() end -- we need user authorization? - local token = config.get("remote_build.client.token") + local token = config.get("remote_build.token") if not token and self:user() then -- get user password diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua index f66051822..f3c0dc94d 100644 --- a/xmake/modules/private/service/remote_build/server.lua +++ b/xmake/modules/private/service/remote_build/server.lua @@ -19,7 +19,7 @@ -- -- imports -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.message") import("private.service.server") import("private.service.stream", {alias = "socket_stream"}) @@ -35,7 +35,7 @@ function remote_build_server:init(daemon) super.init(self, daemon) -- init address - local address = assert(config.get("remote_build.server.listen"), "config(remote_build.server.listen): not found!") + local address = assert(config.get("remote_build.listen"), "config(remote_build.listen): not found!") super.address_set(self, address) -- init handler diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua index 0ba605077..5483062cd 100644 --- a/xmake/modules/private/service/remote_build/session.lua +++ b/xmake/modules/private/service/remote_build/session.lua @@ -26,7 +26,7 @@ import("core.base.global") import("core.base.option") import("core.base.hashset") import("core.base.scheduler") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.message") import("private.service.remote_build.filesync", {alias = "new_filesync"}) @@ -212,9 +212,9 @@ end -- get work directory function session:workdir() - local workdir = config.get("remote_build.server.workdir") + local workdir = config.get("remote_build.workdir") if not workdir then - workdir = path.join(global.directory(), "service", "remote_build") + workdir = path.join(global.directory(), "service", "server", "remote_build") end return path.join(workdir, "sessons", self:id()) end diff --git a/xmake/modules/private/service/rm_user.lua b/xmake/modules/private/service/rm_user.lua index c1ed7a7fb..b68ff4f7b 100644 --- a/xmake/modules/private/service/rm_user.lua +++ b/xmake/modules/private/service/rm_user.lua @@ -24,7 +24,7 @@ import("core.base.base64") import("core.base.bytes") import("core.base.hashset") import("private.service.service") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) function main(user) assert(user, "empty user name!") @@ -41,12 +41,11 @@ function main(user) -- save to configs local configs = assert(config.configs(), "configs not found!") - configs.server = configs.server or {} - configs.server.tokens = configs.server.tokens or {} - local tokenset = hashset.from(configs.server.tokens) + configs.tokens = configs.tokens or {} + local tokenset = hashset.from(configs.tokens) if tokenset:has(token) then tokenset:remove(token) - configs.server.tokens = tokenset:to_array() + configs.tokens = tokenset:to_array() else cprint("User ${bright}%s${clear} not found!", user) return diff --git a/xmake/modules/private/service/server.lua b/xmake/modules/private/service/server.lua index 3365effc2..a3971899a 100644 --- a/xmake/modules/private/service/server.lua +++ b/xmake/modules/private/service/server.lua @@ -25,7 +25,7 @@ import("core.base.base64") import("core.base.hashset") import("core.base.socket") import("core.base.scheduler") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.message") import("private.service.stream", {alias = "socket_stream"}) @@ -37,11 +37,11 @@ function server:init(daemon) self._DAEMON = daemon -- init authorizations - local tokens = config.get("server.tokens") + local tokens = config.get("tokens") self:tokens_set(tokens) -- init known hosts - local known_hosts = config.get("server.known_hosts") + local known_hosts = config.get("known_hosts") self:known_hosts_set(known_hosts) end @@ -106,7 +106,7 @@ end -- verify user function server:verify_user(token, peeraddr) if not token then - return false, "client has no authorization, we need add username to connect address or token to `remote_build.client.token`!" + return false, "client has no authorization, we need add username to connect address or token to `remote_build.token`!" end -- check authorization diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/server_config.lua index fff082234..93a06c61b 100644 --- a/xmake/modules/private/service/config.lua +++ b/xmake/modules/private/service/server_config.lua @@ -15,7 +15,7 @@ -- Copyright (C) 2015-present, TBOOX Open Source Group. -- -- @author ruki --- @file config.lua +-- @file server_config.lua -- -- imports @@ -32,43 +32,25 @@ end function _generate_configfile() local filepath = configfile() assert(not _g.configs and not os.isfile(filepath)) - local servicedir = path.join(global.directory(), "service") + local servicedir = path.join(global.directory(), "service", "server") local token = _generate_token() print("generating the config file to %s ..", filepath) cprint("an token(${bright yellow}%s${clear}) is generated, we can use this token to connect service.", token) local configs = { logfile = path.join(servicedir, "logs.txt"), - server = { - known_hosts = { - -- "127.0.0.1" - }, - tokens = { - token - } + known_hosts = { + -- "127.0.0.1" + }, + tokens = { + token }, remote_build = { - server = { - listen = "0.0.0.0:9691", - workdir = path.join(servicedir, "remote_build"), - }, - client = { - -- without authorization: "127.0.0.1:9691" - -- with user authorization: "[email protected]:9691" - connect = "127.0.0.1:9691", - -- with token authorization - token = token - } + listen = "0.0.0.0:9691", + workdir = path.join(servicedir, "remote_build"), }, distcc_build = { - server = { - listen = "0.0.0.0:9692", - workdir = path.join(servicedir, "distcc_build"), - }, - client = { - hosts = { - {connect = "127.0.0.1:9692", token = token} - } - } + listen = "0.0.0.0:9692", + workdir = path.join(servicedir, "distcc_build"), } } @@ -77,7 +59,7 @@ end -- get config file path function configfile() - return path.join(global.directory(), "service.conf") + return path.join(global.directory(), "service", "server.conf") end -- get all configs @@ -85,7 +67,7 @@ function configs() return _g.configs end --- get the given config, e.g. config.get("remote_build.server.listen") +-- get the given config, e.g. server_config.get("remote_build.listen") function get(name) local value = configs() for _, key in ipairs(name:split('.', {plain = true})) do diff --git a/xmake/modules/private/service/service.lua b/xmake/modules/private/service/service.lua index 446cfbee8..192adc539 100644 --- a/xmake/modules/private/service/service.lua +++ b/xmake/modules/private/service/service.lua @@ -21,7 +21,7 @@ -- imports import("core.base.option") import("core.base.scheduler") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.remote_build.server", {alias = "remote_build_server"}) import("private.service.distcc_build.server", {alias = "distcc_build_server"}) @@ -43,10 +43,10 @@ function main(daemon, ...) elseif option.get("distcc") then table.insert(starters, _start_distcc_build_server) else - if config.get("remote_build.server") then + if config.get("remote_build") then table.insert(starters, _start_remote_build_server) end - if config.get("distcc_build.server") then + if config.get("distcc_build") then table.insert(starters, _start_distcc_build_server) end end diff --git a/xmake/modules/private/service/show_logs.lua b/xmake/modules/private/service/show_logs.lua index 0875b1515..52decc54b 100644 --- a/xmake/modules/private/service/show_logs.lua +++ b/xmake/modules/private/service/show_logs.lua @@ -20,7 +20,7 @@ -- imports import("core.base.option") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) function main() local log diff --git a/xmake/modules/private/service/start_service.lua b/xmake/modules/private/service/start_service.lua index f75c98a10..a0670ed81 100644 --- a/xmake/modules/private/service/start_service.lua +++ b/xmake/modules/private/service/start_service.lua @@ -21,7 +21,7 @@ -- imports import("core.base.option") import("core.project.project") -import("private.service.config") +import("private.service.server_config", {alias = "config"}) import("private.service.service") function main(opt) diff --git a/xmake/modules/private/service/stop_service.lua b/xmake/modules/private/service/stop_service.lua index 0575db0d5..6e0971885 100644 --- a/xmake/modules/private/service/stop_service.lua +++ b/xmake/modules/private/service/stop_service.lua @@ -20,7 +20,6 @@ -- imports import("core.base.option") -import("private.service.config") import("private.service.remote_build.server", {alias = "remote_build_server"}) import("private.service.distcc_build.server", {alias = "distcc_build_server"}) diff --git a/xmake/modules/private/service/sync_files.lua b/xmake/modules/private/service/sync_files.lua index da002f8a4..5fdee61f0 100644 --- a/xmake/modules/private/service/sync_files.lua +++ b/xmake/modules/private/service/sync_files.lua @@ -20,7 +20,6 @@ -- imports import("core.base.option") -import("private.service.config") import("private.service.remote_build.client", {alias = "remote_build_client"}) function main() |
