diff options
| author | ruki <[email protected]> | 2022-04-13 23:00:31 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-13 23:00:31 +0800 |
| commit | 260b86994e5e8e75d2f1df1667f5369f47c2bbe4 (patch) | |
| tree | 39b8911bcfdc817fbae83d405939dc4d4c8730c4 | |
| parent | 33de5bfd93c27f28db546d4594bd950cc015637a (diff) | |
add sync files stub
| -rw-r--r-- | xmake/modules/private/service/config.lua | 5 | ||||
| -rw-r--r-- | xmake/modules/private/service/message.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/client.lua | 60 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/server.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/session.lua | 19 |
5 files changed, 72 insertions, 19 deletions
diff --git a/xmake/modules/private/service/config.lua b/xmake/modules/private/service/config.lua index dddda6656..90492392c 100644 --- a/xmake/modules/private/service/config.lua +++ b/xmake/modules/private/service/config.lua @@ -34,7 +34,10 @@ function _generate_configfile() workdir = path.join(servicedir, "remote_build") }, client = { - connect = "127.0.0.1:90091" + connect = "127.0.0.1:90091", + -- ssh user and password for git push + user = "user" + -- pass = "yyyy" } } } diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index 748c1d5a7..73f39683d 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -126,9 +126,10 @@ function new_disconnect(session_id) end -- new sync message -function new_sync(session_id) +function new_sync(session_id, start) return _new({ code = message.CODE_SYNC, + start = start or true, session_id = session_id }) end diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index fee9ef6a6..1031aec60 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -21,6 +21,8 @@ -- imports import("core.base.socket") import("core.project.config", {alias = "project_config"}) +import("devel.git") +import("lib.detect.find_tool") import("private.service.config") import("private.service.message") import("private.service.client") @@ -48,6 +50,9 @@ function remote_build_client:init() else raise("we need enter a project directory with xmake.lua first!") end + + -- check requires + self:_check_requires() end -- get class @@ -66,6 +71,7 @@ function remote_build_client:connect() local sock = socket.connect(addr, port) local session_id = self:session_id() local connected = false + local errors print("%s: connect %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) @@ -76,7 +82,7 @@ function remote_build_client:connect() if msg:success() then connected = true else - print("%s: connect %s:%d failed, %s", self, addr, port, msg:errors() or "unknown") + errors = msg:errors() end end end @@ -84,7 +90,7 @@ function remote_build_client:connect() if connected then print("%s: connected!", self) else - print("%s: connect %s:%d failed", self, addr, port) + print("%s: connect %s:%d failed, %s", self, addr, port, errors or "unknown") end -- update status @@ -111,6 +117,7 @@ function remote_build_client:disconnect() local port = self:port() local sock = socket.connect(addr, port) local session_id = self:session_id() + local errors local disconnected = false print("%s: disconnect %s:%d ..", self, addr, port) if sock then @@ -122,7 +129,7 @@ function remote_build_client:disconnect() if msg:success() then disconnected = true else - print("%s: disconnect %s:%d failed, %s", self, addr, port, msg:errors()) + errors = msg:errors() end end end @@ -130,7 +137,7 @@ function remote_build_client:disconnect() if disconnected then print("%s: disconnected!", self) else - print("%s: disconnect %s:%d failed", self, addr, port) + print("%s: disconnect %s:%d failed, %s", self, addr, port, errors or "unknown") end -- update status @@ -146,26 +153,33 @@ function remote_build_client:sync() local port = self:port() local sock = socket.connect(addr, port) local session_id = self:session_id() + local errors local synced = false print("%s: sync files in %s:%d ..", self, addr, port) if sock then local stream = socket_stream(sock) if stream:send_msg(message.new_sync(session_id)) and stream:flush() then local msg = stream:recv_msg() - if msg then + if msg and msg:success() then vprint(msg:body()) - if msg:success() then - synced = true - else - print("%s: sync files in %s:%d failed, %s", self, addr, port, msg:errors()) + self:_do_syncfiles(msg:body().path, msg:body().branch) + if stream:send_msg(message.new_sync(session_id, false)) and stream:flush() then + msg = stream:recv_msg() + if msg and msg:success() then + synced = true + elseif msg then + errors = msg:errors() + end end + elseif msg then + errors = msg:errors() end end end if synced then print("%s: synced!", self) else - print("%s: sync files in %s:%d failed", self, addr, port) + print("%s: sync files in %s:%d failed, %s", self, addr, port, errors or "unknown") end end @@ -176,6 +190,7 @@ function remote_build_client:clean() local port = self:port() local sock = socket.connect(addr, port) local session_id = self:session_id() + local errors local cleaned = false print("%s: clean files in %s:%d ..", self, addr, port) if sock then @@ -187,7 +202,7 @@ function remote_build_client:clean() if msg:success() then cleaned = true else - print("%s: clean files in %s:%d failed, %s", self, addr, port, msg:errors()) + errors = msg:errors() end end end @@ -195,7 +210,7 @@ function remote_build_client:clean() if cleaned then print("%s: cleaned!", self) else - print("%s: clean files in %s:%d failed", self, addr, port) + print("%s: clean files in %s:%d failed, %s", self, addr, port, errors or "unknown") end end @@ -243,6 +258,27 @@ function remote_build_client:session_id() return self:status().session_id or hash.uuid():split("-", {plain = true})[1]:lower() end +-- check requires +function remote_build_client:_check_requires() + local git = find_tool("git") + assert(git, "git not found!") +end + +-- do syncfiles, e.g. git push user@addr:remote_path branch:remote_branch +function remote_build_client:_do_syncfiles(remote_path, remote_branch) + local user = assert(config.get("remote_build.client.user"), "config(remote_build.client.user): not found!") + local pass = config.get("remote_build.client.pass") + local addr = self:addr() + local branch = git.branch({repodir = os.curdir()}) + assert(branch, "git branch not found!") + assert(remote_path, "git remote path not found!") + assert(remote_branch, "git remote branch not found!") + + -- get remote url + local remote_url = string.format("%s@%s:%s %s:%s", user, addr, remote_path, branch, remote_branch) + print(remote_url) +end + function remote_build_client:__tostring() return "<remote_build_client>" end diff --git a/xmake/modules/private/service/remote_build/server.lua b/xmake/modules/private/service/remote_build/server.lua index 4dec9573a..ae32c869c 100644 --- a/xmake/modules/private/service/remote_build/server.lua +++ b/xmake/modules/private/service/remote_build/server.lua @@ -73,6 +73,7 @@ function remote_build_server:_on_handle(stream, msg) local session = self:_session(session_id) vprint("%s: %s: <session %s>: on handle message(%d)", self, stream:sock(), session_id, msg:code()) vprint(msg:body()) + local respmsg = msg:clone() local session_errs local session_ok = try { @@ -83,7 +84,7 @@ function remote_build_server:_on_handle(stream, msg) session:close() self._SESSIONS[session_id] = nil elseif msg:is_sync() then - session:sync() + session:sync(respmsg) elseif msg:is_clean() then session:clean() end @@ -98,7 +99,6 @@ function remote_build_server:_on_handle(stream, msg) end } } - local respmsg = msg:clone() respmsg:status_set(session_ok) if not session_ok and session_errs then respmsg:errors_set(session_errs) diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua index 2adf73dfa..07978e4f8 100644 --- a/xmake/modules/private/service/remote_build/session.lua +++ b/xmake/modules/private/service/remote_build/session.lua @@ -48,9 +48,22 @@ function session:close() end -- sync files -function session:sync() - vprint("%s: sync files in %s ..", self, self:sourcedir()) - vprint("%s: sync files ok", self) +function session:sync(respmsg) + local body = respmsg:body() + vprint("%s: %s sync files in %s ..", self, self:sourcedir(), body.start and "start" or "finish") + local sourcedir = self:sourcedir() + local source_branch = self:_source_branch() + if body.start then + self:_reset_sourcedir() + body.path = sourcedir + body.branch = source_branch + else + local branch = git.branch({repodir = sourcedir}) + if not branch or branch ~= source_branch then + git.checkout(source_branch, {repodir = sourcedir}) + end + end + vprint("%s: %s sync files ok", self, body.start and "start" or "finish") end -- clean files |
