This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathelastic_agent_deploy.go
159 lines (130 loc) · 4.98 KB
/
elastic_agent_deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"runtime"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/installer"
log "github.com/sirupsen/logrus"
)
var deployedAgentsCount = 0
// this step infers the installer type from the underlying OS image
// supported Docker images: centos and debian
func (fts *FleetTestSuite) anAgentIsDeployedToFleet(image string) error {
installerType := "rpm"
if image == "debian" {
installerType = "deb"
}
return fts.deployAgentToFleet(InstallerType(installerType))
}
func (fts *FleetTestSuite) anAgentIsDeployedToFleetOnTopOfBeat(beatsProcess string) error {
return fts.deployAgentToFleet(InstallerType("tar"), BeatsProcess(beatsProcess))
}
// supported installers: tar, rpm, deb
func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstaller(installerType string) error {
return fts.deployAgentToFleet(InstallerType(installerType))
}
// supported installers: tar, rpm, deb
func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndTags(installerType string, flags string) error {
return fts.deployAgentToFleet(InstallerType(installerType), Flags(flags))
}
func (fts *FleetTestSuite) deployAgentToFleet(opts ...DeploymentOpt) error {
// Default Options
args := &DeploymentOpts{
beatsProcess: "",
installerType: "tar",
flags: "",
boostrapFleetServer: false,
}
for _, opt := range opts {
opt(args)
log.Info("<<< configuration to agent deployment applied")
}
log.WithFields(log.Fields{
"installer": args.installerType,
}).Trace("Deploying an agent to Fleet with base image using an already bootstrapped Fleet Server")
deployedAgentsCount++
fts.InstallerType = args.installerType
fts.BeatsProcess = args.beatsProcess
fts.ElasticAgentFlags = args.flags
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName).
WithScale(deployedAgentsCount).
WithVersion(fts.Version)
if fts.BeatsProcess != "" {
agentService = agentService.WithBackgroundProcess(fts.BeatsProcess)
}
services := []deploy.ServiceRequest{
agentService,
}
env := fts.getProfileEnv()
err := fts.getDeployer().Add(fts.currentContext, deploy.NewServiceRequest(common.FleetProfileName), services, env)
if err != nil {
return err
}
agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)
err = deploymentLifecycle(fts.currentContext, agentInstaller, fts.CurrentToken, fts.ElasticAgentFlags)
if err != nil {
return err
}
return err
}
// DeploymentOpts options to be applied to a deployment of the elastic-agent
type DeploymentOpts struct {
beatsProcess string
boostrapFleetServer bool
installerType string
flags string
}
// DeploymentOpt an option to be applied to a deployment of the elastic-agent
type DeploymentOpt func(*DeploymentOpts)
// BeatsProcess option to start a Beats process before the agent is running. Default is empty
func BeatsProcess(beatsProcess string) DeploymentOpt {
return func(args *DeploymentOpts) {
log.Tracef(">>> applying configuration to agent deployment [BeatsProcess]: %s", beatsProcess)
args.beatsProcess = beatsProcess
}
}
// BootstrapFleetServer option to bootstrap a Fleet Server, otherwise the stack one will be used. Default is false
func BootstrapFleetServer(boostrapFleetServer bool) DeploymentOpt {
return func(args *DeploymentOpts) {
log.Tracef(">>> applying configuration to agent deployment [BootstrapFleetServer]: %t", boostrapFleetServer)
args.boostrapFleetServer = boostrapFleetServer
}
}
// InstallerType option to define the installer to use for the agent. Default is "tar"
func InstallerType(installerType string) DeploymentOpt {
// FIXME: We need to cleanup the steps to support different operating systems
// for now we will force the zip installer type when the agent is running on windows
if runtime.GOOS == "windows" && common.Provider == "remote" {
installerType = "zip"
}
return func(args *DeploymentOpts) {
log.Tracef(">>> applying configuration to agent deployment [InstallerType]: %s", installerType)
args.installerType = installerType
}
}
// Flags option to pass flags to the enrollment of the agent. Default is empty
func Flags(flags string) DeploymentOpt {
return func(args *DeploymentOpts) {
log.Tracef(">>> applying configuration to agent deployment [Flags]: %s", flags)
args.flags = flags
}
}
func deploymentLifecycle(ctx context.Context, agentInstaller deploy.ServiceOperator, token string, flags string) error {
err := agentInstaller.Preinstall(ctx)
if err != nil {
return err
}
err = agentInstaller.Install(ctx)
if err != nil {
return err
}
err = agentInstaller.Enroll(ctx, token, flags)
if err != nil {
return err
}
return agentInstaller.Postinstall(ctx)
}