Skip to content

Commit

Permalink
fixed running agent in python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
godfryd committed Nov 16, 2023
1 parent 6a55d35 commit 88d4ad8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
13 changes: 7 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ task :run_agent_in_lxd do
Rake::Task["build_agent"].invoke
Dir.chdir('agent') do
systems = [
['ubuntu:20.04', 'u20'],
#['ubuntu:20.04', 'u20'],
['images:ubuntu/mantic/amd64', 'u2310'],
]

sh 'lxc network delete kk-net || true'
Expand Down Expand Up @@ -324,12 +325,12 @@ task :run_agent_in_lxd do
end

sh "lxc exec #{cntr_name} -- apt-get update"
sh "lxc exec #{cntr_name} -- apt-get install -y python3-docker docker.io"
sh "lxc exec #{cntr_name} -- apt-get install -y python3-docker docker.io wget python3.12"
sh "lxc exec #{cntr_name} -- ln -sf /usr/bin/python3.12 /usr/bin/python3"

sh "lxc exec #{cntr_name} -- curl -o agent http://#{LOCALHOST_IP}:8080/bk/install/agent"
sh "lxc exec #{cntr_name} -- chmod a+x agent"
sh "lxc exec #{cntr_name} -- ./agent install -s http://#{LOCALHOST_IP}:8080"
#sh "lxc exec #{cntr_name} -- journalctl -u kraken-agent.service"
sh "lxc exec #{cntr_name} -- wget http://#{LOCALHOST_IP}:4200/bk/install/kraken-agent-install.sh"
sh "lxc exec #{cntr_name} -- chmod a+x kraken-agent-install.sh"
sh "lxc exec #{cntr_name} -- ./kraken-agent-install.sh"
sh "lxc exec #{cntr_name} -- journalctl -f -u kraken-agent.service"
end
end
Expand Down
22 changes: 5 additions & 17 deletions agent/kraken/agent/jobber.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,12 @@ async def async_handle_request(self, reader, writer):
# data = await reader.read(8192)
data = await reader.readline()
if not data:
writer.close()
await writer.wait_closed()
break
try:
data = data.decode()
log.info("received %s from %s", data, addr)
# log.info("received %s from %s", data, addr)
data = json.loads(data)
except Exception:
log.exception('problem with decoding data %s from %s', data, addr)
Expand All @@ -171,9 +173,7 @@ async def _async_tcp_server(server):
await server.serve_forever()
finally:
server.close()
log.info('jobber closing http server 1')
# await server.wait_closed() # TODO: this hangs indefinitelly, check https://github.com/python/cpython/issues/104344
log.info('jobber closed http server')
await server.wait_closed()


async def _send_keep_alive_to_server(proc_coord):
Expand Down Expand Up @@ -228,7 +228,6 @@ async def _async_exec_tool(exec_ctx, proc_coord, tool_path, command, cwd, timeou

# wait for any task to complete
done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
log.info('jobber async_exec_tool finished tasks: %s', done)

for t in done:
ex = None
Expand All @@ -242,39 +241,28 @@ async def _async_exec_tool(exec_ctx, proc_coord, tool_path, command, cwd, timeou

# stop internal http server if not stopped yet
if tcp_server_task not in done and not tcp_server_task.done():
log.info('jobber async_exec_tool close http server')
server.close()
log.info('jobber async_exec_tool wait for http server')
tcp_server_task.cancel()
log.info('jobber async_exec_tool http server canceled')
try:
await tcp_server_task
except asyncio.CancelledError:
pass
log.info('jobber async_exec_tool waited for http server')

if cancel_task and (cancel_task in done or cancel_task.done()):
log.info('jobber async_exec_tool wait for subprocess')
subprocess_task.cancel()
log.info('jobber async_exec_tool subprocess cancel')
try:
await subprocess_task
except asyncio.CancelledError:
pass
log.info('jobber async_exec_tool waited for subprocess')

log.info('jobber async_exec_tool completed')


def _exec_tool_inner(kk_srv, exec_ctx, tool_path, command, cwd, timeout, user, step, step_file_path, job_id, idx, cancel_event=None):
proc_coord = ProcCoord(kk_srv, command, job_id, idx)
f = _async_exec_tool(exec_ctx, proc_coord, tool_path, command, cwd, timeout, user, step, step_file_path, cancel_event)
try:
if hasattr(asyncio, 'run'):
asyncio.run(f, debug=True) # this is available since Python 3.7
asyncio.run(f) # this is available since Python 3.7
else:
loop = asyncio.get_event_loop()
loop.set_debug()
loop.run_until_complete(f)
loop.close()
except asyncio.CancelledError:
Expand Down
2 changes: 1 addition & 1 deletion agent/kraken/agent/local_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def _async_run_exc(self, proc_coord, tool_path, return_addr, step, step_fi
time.sleep(1)
await proc.wait()
raise
log.info('async run completed, %s', command)
# log.info('async run completed, %s', command)
#proc_coord.proc_retcode = proc.returncode

async def async_run(self, proc_coord, tool_path, return_addr, step, step_file_path, command, cwd, timeout, user): # pylint: disable=unused-argument
Expand Down
2 changes: 1 addition & 1 deletion agent/kraken/agent/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def execute(sock, module, command, step_file_path):
result = {'status': 'error', 'reason': 'exception', 'msg': exc}
sock.send_json(result)

log.info('step tool result sent: %s', result)
# log.info('step tool result sent: %s', result)


class JsonSocket(socket.socket):
Expand Down

0 comments on commit 88d4ad8

Please sign in to comment.