summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-30 23:12:39 +0800
committerruki <[email protected]>2022-07-30 23:12:39 +0800
commit5aec161f35f6905af15764a6da985359d25175ad (patch)
tree5cccd627ba6ecec08e81123be66023cd631db825
parentc0f7a2a0af3ded2f704e07225890b9d2e3b3e2ea (diff)
add xmake service --pull
-rw-r--r--xmake/actions/service/main.lua4
-rw-r--r--xmake/actions/service/xmake.lua7
-rw-r--r--xmake/modules/private/service/pull_files.lua29
-rw-r--r--xmake/modules/private/service/remote_build/client.lua51
-rw-r--r--xmake/modules/private/service/remote_build/server.lua2
-rw-r--r--xmake/modules/private/service/remote_build/server_session.lua36
6 files changed, 128 insertions, 1 deletions
diff --git a/xmake/actions/service/main.lua b/xmake/actions/service/main.lua
index 24ed84d80..9b4df5865 100644
--- a/xmake/actions/service/main.lua
+++ b/xmake/actions/service/main.lua
@@ -30,6 +30,7 @@ import("private.service.reconnect_service")
import("private.service.disconnect_service")
import("private.service.clean_files")
import("private.service.sync_files")
+import("private.service.pull_files")
import("private.service.add_user")
import("private.service.rm_user")
import("private.service.gen_token")
@@ -57,6 +58,9 @@ function main()
clean_files()
elseif option.get("sync") then
sync_files()
+ elseif option.get("pull") then
+ local values = option.get("values") or {}
+ pull_files(values[1], values[2])
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 df4cae06b..32b701bf5 100644
--- a/xmake/actions/service/xmake.lua
+++ b/xmake/actions/service/xmake.lua
@@ -45,6 +45,10 @@ task("service")
{nil, "distcc", "k", nil, "Start or connect the distributed build service." },
{nil, "ccache", "k", nil, "Start or connect the remote c/c++ cache service." },
{nil, "sync", "k", nil, "Sync current project files in the remote daemon service." },
+ {nil, "pull", "k", nil, "Pull the given file or directory in the remote daemon service.",
+ "e.g.",
+ " - xmake service --pull build outputdir",
+ " - xmake service --pull 'build/**' outputdir" },
{nil, "clean", "k", nil, "Clean current project files in the remote daemon service." },
{nil, "add-user", "kv", nil, "Add user in the server.",
"e.g.",
@@ -56,5 +60,6 @@ task("service")
"e.g.",
" - xmake service --gen-token" },
{nil, "logs", "k", nil, "Show service logs if the daemon service has been started." },
- {nil, "status", "k", nil, "Show service status if the daemon service has been started." }
+ {nil, "status", "k", nil, "Show service status if the daemon service has been started." },
+ {nil, "values", "vs", nil, "The values list for pull/.. options." }
}}
diff --git a/xmake/modules/private/service/pull_files.lua b/xmake/modules/private/service/pull_files.lua
new file mode 100644
index 000000000..81edc67e1
--- /dev/null
+++ b/xmake/modules/private/service/pull_files.lua
@@ -0,0 +1,29 @@
+--!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 pull_files.lua
+--
+
+-- imports
+import("core.base.option")
+import("private.service.remote_build.client", {alias = "remote_build_client"})
+
+function main(filepattern, outputdir)
+ remote_build_client():pull(filepattern, outputdir)
+end
+
+
diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua
index 95ad5110e..53fc6b1e3 100644
--- a/xmake/modules/private/service/remote_build/client.lua
+++ b/xmake/modules/private/service/remote_build/client.lua
@@ -235,6 +235,57 @@ function remote_build_client:sync()
end
end
+-- pull server files
+function remote_build_client:pull(filepattern, outputdir)
+ assert(self:is_connected(), "%s: has been not connected!", self)
+ local addr = self:addr()
+ local port = self:port()
+ local sock = assert(socket.connect(addr, port, {timeout = self:connect_timeout()}), "%s: server unreachable!", self)
+ local session_id = self:session_id()
+ local errors
+ local ok = false
+ if not filepattern:find("*", 1, true) and os.isdir(filepattern) then
+ filepattern = path.join(filepattern, "**")
+ end
+ print("%s: pull %s in %s:%d ..", self, filepattern, addr, port)
+ local stream = socket_stream(sock, {send_timeout = self:send_timeout(), recv_timeout = self:recv_timeout()})
+ if stream:send_msg(message.new_pull(session_id, filepattern, {token = self:token()})) and stream:flush() then
+ local fileitems
+ local msg = stream:recv_msg({timeout = -1})
+ if msg then
+ dprint(msg:body())
+ if msg:success() then
+ fileitems = msg:body().fileitems
+ else
+ errors = msg:errors()
+ end
+ end
+ if fileitems then
+ for _, fileitem in ipairs(fileitems) do
+ print("recving %s ..", fileitem)
+ if not stream:recv_file(path.normalize(path.join(outputdir, fileitem))) then
+ errors = string.format("recv %s failed", fileitem)
+ break
+ end
+ end
+ msg = stream:recv_msg({timeout = -1})
+ if msg then
+ dprint(msg:body())
+ if msg:success() then
+ ok = true
+ else
+ errors = msg:errors()
+ end
+ end
+ end
+ end
+ if ok then
+ print("%s: pull files to %s!", self, outputdir)
+ else
+ print("%s: pull files failed in %s:%d, %s", self, addr, port, errors or "unknown")
+ end
+end
+
-- clean server files
function remote_build_client:clean()
assert(self:is_connected(), "%s: has been not connected!", self)
diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua
index 52015c7f2..c890e1158 100644
--- a/xmake/modules/private/service/remote_build/server.lua
+++ b/xmake/modules/private/service/remote_build/server.lua
@@ -93,6 +93,8 @@ function remote_build_server:_on_handle(stream, msg)
session:diff(respmsg)
elseif msg:is_sync() then
session:sync(respmsg)
+ elseif msg:is_pull() then
+ session:pull(respmsg)
elseif msg:is_clean() then
session:clean()
elseif msg:is_runcmd() then
diff --git a/xmake/modules/private/service/remote_build/server_session.lua b/xmake/modules/private/service/remote_build/server_session.lua
index b56908456..7031c5095 100644
--- a/xmake/modules/private/service/remote_build/server_session.lua
+++ b/xmake/modules/private/service/remote_build/server_session.lua
@@ -181,6 +181,42 @@ function server_session:sync(respmsg)
vprint("%s: sync files ok", self)
end
+-- pull file
+function server_session:pull(respmsg)
+ local body = respmsg:body()
+ local stream = self:stream()
+ local filepattern = body.filename
+ vprint("pull %s ..", filepattern)
+
+ -- get files
+ local filepaths = os.files(path.join(self:sourcedir(), filepattern))
+ local fileitems = {}
+ for _, filepath in ipairs(filepaths) do
+ local fileitem = path.relative(filepath, self:sourcedir())
+ if is_host("windows") then
+ fileitem = fileitem:gsub("\\", "/")
+ end
+ if fileitem:startswith("./") then
+ fileitem = fileitem:sub(3)
+ end
+ table.insert(fileitems, fileitem)
+ end
+ vprint(fileitems)
+
+ -- send files
+ body.fileitems = fileitems
+ respmsg:status_set(true)
+ if stream:send_msg(respmsg) then
+ for _, fileitem in ipairs(fileitems) do
+ local filepath = path.join(self:sourcedir(), fileitem)
+ vprint("sending %s ..", filepath)
+ if not stream:send_file(filepath, {compress = os.filesize(filepath) > 4096}) then
+ raise("send %s failed!", filepath)
+ end
+ end
+ end
+end
+
-- clean files
function server_session:clean()
vprint("%s: clean files in %s ..", self, self:workdir())