Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Elastic Agent] Improved mage demo experience #18445

Merged
merged 7 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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