Skip to content

Commit

Permalink
[Elastic Agent] Improved mage demo experience (#18445)
Browse files Browse the repository at this point in the history
[Elastic Agent] Improved mage demo experience (#18445)
  • Loading branch information
michalpristas authored Jun 15, 2020
1 parent 8745e17 commit 18aed2c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
set -eo pipefail

# Environment variables used
# FLEET_CONFIG_ID - config related to new token [defaul]
# FLEET_ENROLLMENT_TOKEN - existing enrollment token to be used for enroll
# FLEET_ENROLL - if set to 1 enroll will be performed
# FLEET_SETUP - if set to 1 fleet setup will be performed
Expand Down Expand Up @@ -49,8 +48,9 @@ function enroll(){
if [ $exitCode -ne 0 ]; then
exit $exitCode
fi

apikey=$(echo $enrollResp | jq -r '.item.api_key')
fi
apikey=$(echo $enrollResp | jq -r '.item.api_key')
echo $apikey

./{{ .BeatName }} enroll ${KIBANA_HOST:-http://localhost:5601} $apikey -f
Expand Down
31 changes: 28 additions & 3 deletions x-pack/elastic-agent/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ func (Demo) NoEnroll() error {
}

func runAgent(env map[string]string) error {
supportedEnvs := map[string]int{"FLEET_CONFIG_ID": 0, "FLEET_ENROLLMENT_TOKEN": 0, "FLEET_ENROLL": 0, "FLEET_SETUP": 0, "FLEET_TOKEN_NAME": 0, "KIBANA_HOST": 0, "KIBANA_PASSWORD": 0, "KIBANA_USERNAME": 0}
prevPlatforms := os.Getenv("PLATFORMS")
defer os.Setenv("PLATFORMS", prevPlatforms)

// setting this improves build time
os.Setenv("PLATFORMS", "+all linux/amd64")
devtools.Platforms = devtools.NewPlatformList("+all linux/amd64")

supportedEnvs := map[string]int{"FLEET_ENROLLMENT_TOKEN": 0, "FLEET_ENROLL": 0, "FLEET_SETUP": 0, "FLEET_TOKEN_NAME": 0, "KIBANA_HOST": 0, "KIBANA_PASSWORD": 0, "KIBANA_USERNAME": 0}

tag := dockerTag()
dockerImageOut, err := sh.Output("docker", "image", "ls")
Expand All @@ -435,7 +442,6 @@ func runAgent(env map[string]string) error {
if !strings.Contains(dockerImageOut, tag) {
// produce docker package
packageAgent([]string{
"linux-x86.tar.gz",
"linux-x86_64.tar.gz",
}, devtools.UseElasticAgentDemoPackaging)

Expand All @@ -451,7 +457,11 @@ func runAgent(env map[string]string) error {
}

// prepare env variables
var envs []string
envs := []string{
// providing default kibana to be fixed for os-es if not provided
"KIBANA_HOST=http://localhost:5601",
}

envs = append(envs, os.Environ()...)
for k, v := range env {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
Expand All @@ -465,6 +475,9 @@ func runAgent(env map[string]string) error {
continue
}

// fix value
e = fmt.Sprintf("%s=%s", parts[0], fixOsEnv(parts[0], parts[1]))

dockerCmdArgs = append(dockerCmdArgs, "-e", e)
}

Expand Down Expand Up @@ -521,6 +534,18 @@ func dockerTag() string {
return tagBase
}

func fixOsEnv(k, v string) string {
switch k {
case "KIBANA_HOST":
// network host works in a weird way here
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
return strings.Replace(strings.ToLower(v), "localhost", "host.docker.internal", 1)
}
}

return v
}

func buildVars() map[string]string {
vars := make(map[string]string)

Expand Down

0 comments on commit 18aed2c

Please sign in to comment.