Skip to content

Commit

Permalink
move the external command call into utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
liyaqin1 committed Sep 19, 2023
1 parent d47f55f commit 59aa580
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
13 changes: 3 additions & 10 deletions deploy-agent/deployd/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,10 @@ def serve_build(self):
for status in self._envs.values():
# for each container, we check the health status
try:
cmd = ['docker', 'inspect', status.report.envName]
output = subprocess.run(cmd, check=True, stdout=subprocess.PIPE).stdout
result = json.loads(output)
if result[0].get("State"):
healthStatus = result[0].get("State").get("Health").get("Status")
status.report.extraInfo = {'serviceHealth': healthStatus}
else:
status.report.extraInfo = None
log.info('sidecar name: {}'.format(status.report.envName))
log.info('sidecar extraInfo: {}'.format(status.report.extraInfo))
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)
Expand Down
2 changes: 1 addition & 1 deletion deploy-agent/deployd/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _read_host_info(self):
"Host name: {}, IP: {}, host id: {}, agent_version={}, autoscaling_group: {}, "
"availability_zone: {}, stage_type: {}, group: {}, account id: {}".format(self._hostname, self._ip, self._id,
self._agent_version, self._autoscaling_group, self._availability_zone, self._stage_type, self._hostgroup, self._account_id))

if not self._availability_zone:
log.error("Fail to read host info: availablity zone")
create_sc_increment(name='deploy.failed.agent.hostinfocollection',
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

0 comments on commit 59aa580

Please sign in to comment.