[Logs onboarding] Getting elastic-agent state in a more reliably way (#165205)

Closes https://github.com/elastic/kibana/issues/163163.

We were getting `elastic-agent` status using `human`
[output](https://www.elastic.co/guide/en/fleet/current/elastic-agent-cmd-options.html#_options_7),
this way of obtaining the state is very unreliable since human format
it's more likeable to vary from one version to other. e.g

- v8.0.0
<img width="1021" alt="image"
src="7c8102a4-0785-4ab1-b690-ab62ec67644d">

- v8.9.0
<img width="1026" alt="image"
src="b6acdfbd-6efa-4518-8855-0aba3662f07b">

### Changes
- Get `elastic-agent` status from json output.

#### Demo

##### v8.0.0


6c507269-65d5-4c8a-9e9f-420698ca995d


##### v8.9.0


acdab744-0bd7-43c8-9eb8-024e5a2eeae5
This commit is contained in:
Yngrid Coello 2023-08-31 10:46:41 +02:00 committed by GitHub
parent d45347cf68
commit 94bccbc6cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -124,14 +124,20 @@ waitForElasticAgentStatus
if [ "$?" -ne 0 ]; then if [ "$?" -ne 0 ]; then
updateStepProgress "ea-status" "warning" "Unable to determine agent status" updateStepProgress "ea-status" "warning" "Unable to determine agent status"
fi fi
ELASTIC_AGENT_STATE="$(elastic-agent status | grep -m1 State | sed 's/State: //')"
ELASTIC_AGENT_MESSAGE="$(elastic-agent status | grep -m1 Message | sed 's/Message: //')" # https://www.elastic.co/guide/en/fleet/current/elastic-agent-cmd-options.html#elastic-agent-status-command
if [ "${ELASTIC_AGENT_STATE}" = "HEALTHY" ] && [ "${ELASTIC_AGENT_MESSAGE}" = "Running" ]; then ELASTIC_AGENT_STATES=(STARTING CONFIGURING HEALTHY DEGRADED FAILED STOPPING UPGRADING ROLLBACK)
# Get elastic-agent status in json format | removing extra states in the json | finding "state":value | removing , | removing "state": | trimming the result
ELASTIC_AGENT_STATE="$(elastic-agent status --output json | sed -n '/components/q;p' | grep state | sed 's/\(.*\),/\1 /' | sed 's/"state": //' | sed 's/\s//g')"
# Get elastic-agent status in json format | removing extra states in the json | finding "message":value | removing , | removing "message": | trimming the result | removing ""
ELASTIC_AGENT_MESSAGE="$(elastic-agent status --output json | sed -n '/components/q;p' | grep message | sed 's/\(.*\),/\1 /' | sed 's/"message": //' | sed 's/\s//g' | sed 's/\"//g')"
if [ "${ELASTIC_AGENT_STATE}" = "2" ] && [ "${ELASTIC_AGENT_MESSAGE}" = "Running" ]; then
echo "Elastic Agent running" echo "Elastic Agent running"
echo "Download and save configuration to ${cfg}" echo "Download and save configuration to ${cfg}"
updateStepProgress "ea-status" "complete" updateStepProgress "ea-status" "complete"
else else
updateStepProgress "ea-status" "warning" "Expected agent status HEALTHY / Running but got ${ELASTIC_AGENT_STATE} / ${ELASTIC_AGENT_MESSAGE}" updateStepProgress "ea-status" "warning" "Expected agent status HEALTHY / Running but got ${ELASTIC_AGENT_STATES[ELASTIC_AGENT_STATE]} / ${ELASTIC_AGENT_MESSAGE}"
fi fi
downloadElasticAgentConfig() { downloadElasticAgentConfig() {