From 2dfb30a3b8eb3da1335bc095df468fca9934c8a8 Mon Sep 17 00:00:00 2001 From: enggnr <129082112+enggnr@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:13:34 +0530 Subject: [PATCH] Script to install elastic agent --- home/.chezmoi.yaml.tmpl | 1 + ...un_onchange_after_57-elastic-agent.sh.tmpl | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 home/.chezmoiscripts/universal/run_onchange_after_57-elastic-agent.sh.tmpl diff --git a/home/.chezmoi.yaml.tmpl b/home/.chezmoi.yaml.tmpl index bc2469e8..0949b084 100644 --- a/home/.chezmoi.yaml.tmpl +++ b/home/.chezmoi.yaml.tmpl @@ -140,6 +140,7 @@ data: docker: doRegion: nyc1 domain: "{{ $domain }}" + elastic_fleet_server: "https://es-fleet.megabyte.space:8220" headless: {{ $headless }} home: "{{ .chezmoi.homeDir }}" homeParentFolder: "{{ if eq .chezmoi.os "linux" }}/home{{ else if eq .chezmoi.os "darwin" }}/Users{{ else }}C:\Users{{ end }}" diff --git a/home/.chezmoiscripts/universal/run_onchange_after_57-elastic-agent.sh.tmpl b/home/.chezmoiscripts/universal/run_onchange_after_57-elastic-agent.sh.tmpl new file mode 100644 index 00000000..fd73f44f --- /dev/null +++ b/home/.chezmoiscripts/universal/run_onchange_after_57-elastic-agent.sh.tmpl @@ -0,0 +1,29 @@ +{{- if and (ne .host.distro.family "windows") (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (or (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "ELASTIC_TOKEN")) (env "ELASTIC_TOKEN")) -}} +#!/usr/bin/env bash +# @file Netdata +# @brief Enrolls Elastic Agent with Elastic Fleet Server +# @description +# This script enrolls Elastic Agent with Elastic Fleet Server if Elastic Agent is installed, the `ELASTIC_TOKEN` is provided, and the +# `elastic_fleet_server` is defined. +# +# Fleet provides a web-based UI in Kibana for centrally managing Elastic Agents and their policies. Fleet Server is the mechanism +# to connect Elastic Agents to Fleet. All communication between the Fleet UI and Fleet Server happens through Elasticsearch. +# +# [Integrations](https://www.elastic.co/guide/en/fleet/current/integrations.html) and [Policies](https://www.elastic.co/guide/en/fleet/current/agent-policy.html) +# are configured using Fleet UI in Kibana. The [enrollment-token](https://www.elastic.co/guide/en/fleet/current/install-fleet-managed-elastic-agent.html) +# can be obtained from Fleet UI, and, one token can be used to add multiple agents to the corresponding policy. + +{{ includeTemplate "universal/profile" }} +{{ includeTemplate "universal/logg" }} + +### Enroll Elastic Agent with Elastic Fleet Server +if command -v elastic-agent > /dev/null; then + ELASTIC_TOKEN="{{- if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "ELASTIC_TOKEN")) -}}{{- includeTemplate "secrets/ELASTIC_TOKEN" | decrypt | trim -}}{{- else -}}{{- env "ELASTIC_TOKEN" -}}{{- end -}}" + FLEET_URL="{{ .host.data.elastic_fleet_server }}" + logg info 'Enrolling Elastic Agent' + sudo elastic-agent install --url="$FLEET_URL" --enrollment-token="$ELASTIC_TOKEN" +else + logg info '`elastic-agent` is not installed or it is not available in the PATH' +fi + +{{ end -}}