Skip to content

Commit

Permalink
only update the container health status each time at the very beginni…
Browse files Browse the repository at this point in the history
…ng of deploy agent starts (#1260)

* only update the container health status each time at the very beginning of deploy agent starts

* import needed packages

* fix test

* add exception check and also check if it is the first time to run deploy-agent

* send map for extraInfo and make sure we get 200 from agent server

* move the external command call into utils.py

* revert test_agent.py

* bump deploy-agent version
  • Loading branch information
liyaqin1 authored Sep 20, 2023
1 parent 6a3eed3 commit 341173f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deploy-agent/deployd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
# 2: puppet applied successfully with changes
PUPPET_SUCCESS_EXIT_CODES = [0, 2]

__version__ = '1.2.46'
__version__ = '1.2.47'
10 changes: 10 additions & 0 deletions deploy-agent/deployd/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ def serve_build(self):
if not self._executor:
self._executor = Executor(callback=PingServer(self), config=self._config)
# start to ping server to get the latest deploy goal
if len(self._envs) > 0:
for status in self._envs.values():
# for each container, we check the health status
try:
healthStatus = utils.get_container_health_info(status.report.envName)
status.report.extraInfo = {'serviceHealth': healthStatus}
except Exception:
status.report.extraInfo = None
log.exception('get exception while trying to check container health: {}'.format(traceback.format_exc()))
continue
self._response = self._client.send_reports(self._envs)

if self._response:
Expand Down
14 changes: 14 additions & 0 deletions deploy-agent/deployd/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@ def get_info_from_facter(keys):
return None


def get_container_health_info(key):
try:
log.info(f"Get health info for container {key}")
cmd = ['docker', 'inspect', '-f', '{{.State.Health.Status}}', key]
output = subprocess.run(cmd, check=True, stdout=subprocess.PIPE).stdout
if output:
return output.decode().strip()
else:
return None
except:
log.error("Failed to get container health info for {}".format(key))
return None


def check_not_none(arg, msg=None):
if arg is None:
raise ValueError(msg)
Expand Down
2 changes: 2 additions & 0 deletions deploy-agent/deployd/types/ping_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ def to_json(self):
ping_report["errorMessage"] = report.errorMessage
ping_report["failCount"] = report.failCount
ping_report["deployAlias"] = report.deployAlias
"""
if report.extraInfo:
ping_report["extraInfo"] = \
json.dumps(report.extraInfo, ensure_ascii=False).encode('utf8')
"""
ping_requests["reports"].append(ping_report)
return ping_requests

Expand Down
2 changes: 1 addition & 1 deletion deploy-agent/tests/unit/deploy/server/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,4 @@ def test_send_deploy_status(self, mock_create_sc):


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit 341173f

Please sign in to comment.