Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
02a879e
Generate stack files
jsoriano May 3, 2022
97a3ce7
It builds
jsoriano May 3, 2022
935a90d
Fix docker compose file
jsoriano May 3, 2022
a1abf89
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Nov 28, 2022
36af266
Fix profile creation
jsoriano Nov 29, 2022
6096b2d
Allow to select the current profile
jsoriano Nov 29, 2022
987028d
Move kibana healthcheck to stack
jsoriano Nov 29, 2022
8a8f8da
Remove code for service deployers from installation
jsoriano Nov 29, 2022
5fefa3c
Move service tokens and geoip to profile
jsoriano Nov 29, 2022
6a89b90
Fix tests
jsoriano Nov 29, 2022
ae24022
Unify code to load profiles in stack subcommands
jsoriano Nov 30, 2022
3d73ed7
Move stack files to stack package
jsoriano Nov 30, 2022
e164209
Add TODO
jsoriano Nov 30, 2022
01f754b
Use public go-resource
jsoriano Dec 1, 2022
dd40411
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Dec 1, 2022
d22311c
Linting
jsoriano Dec 1, 2022
5c31959
Fix username and password
jsoriano Dec 1, 2022
200b4d7
Compose provider
jsoriano Dec 1, 2022
57c3e97
Persist stack configuration
jsoriano Dec 1, 2022
a24368c
Create a cluster
jsoriano Dec 1, 2022
ebf5d95
Cloud deployment creation
jsoriano Dec 2, 2022
592d610
Tear down and status
jsoriano Dec 2, 2022
118362e
Wait for cluster to be ready
jsoriano Dec 2, 2022
280b0ab
Print urls only when available
jsoriano Dec 2, 2022
092eeee
Get provider from profile, not from flag, except for stack up
jsoriano Dec 2, 2022
210a2f1
Return hard-coded shellinit default for backwards compatibility
jsoriano Dec 2, 2022
315dd92
Fix go mod
jsoriano Dec 5, 2022
7737fec
Linting
jsoriano Dec 5, 2022
1ef359b
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Dec 14, 2022
8b8e82a
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Dec 19, 2022
060b76f
Fix nil pointer dereference
jsoriano Dec 19, 2022
ed1e62b
Update go-resource
jsoriano Dec 19, 2022
aa10373
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Dec 20, 2022
b5dbf6a
Update gobuffalo/packr
jsoriano Dec 20, 2022
d400e9d
Fix custom agent deployer
jsoriano Dec 21, 2022
a17fcdd
Add variant for Kibana >= 8.7.0 with Fleet experimental toggles enabl…
jsoriano Feb 1, 2023
d964ab5
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Feb 3, 2023
12e4139
Fix terraform deployer
jsoriano Feb 9, 2023
3657f9f
Merge remote-tracking branch 'origin/main' into profile-resources
jsoriano Feb 9, 2023
1b9fb1e
Fix terraform deployer dockerfile
jsoriano Feb 9, 2023
77bc1d5
Remove unused kibana config
jsoriano Feb 9, 2023
0655713
Fix linting
jsoriano Feb 9, 2023
b5b5b26
Fix creation of parent directories
jsoriano Feb 9, 2023
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
Prev Previous commit
Next Next commit
Cloud deployment creation
  • Loading branch information
jsoriano committed Dec 2, 2022
commit ebf5d9547d503cbe32465f9a741ca18359709d4e
148 changes: 137 additions & 11 deletions internal/stack/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
package stack

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"time"

"github.com/elastic/cloud-sdk-go/pkg/api"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/depresourceapi"
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/deptemplateapi"
"github.com/elastic/cloud-sdk-go/pkg/auth"
"github.com/elastic/cloud-sdk-go/pkg/models"

"github.com/elastic/elastic-package/internal/profile"
"github.com/elastic/elastic-package/internal/signal"
)

// Docs: https://www.elastic.co/guide/en/cloud/current/ec-api-deployment-crud.html
Expand Down Expand Up @@ -69,34 +73,156 @@ func (cp *cloudProvider) BootUp(options Options) error {
return err
}

// TODO: Check if the deployment already exists.

// TODO: Parameterize this.
name := "elastic-package-test"
region := "gcp-europe-west3"
templateID := "gcp-storage-optimized"

payload, err := depresourceapi.NewPayload(depresourceapi.NewPayloadParams{
API: api,
Name: name,
Version: options.StackVersion,
Region: region,
DeploymentTemplateID: templateID,
templateID := "gcp-io-optimized"

template, err := deptemplateapi.Get(deptemplateapi.GetParams{
API: api,
TemplateID: templateID,
Region: region,
StackVersion: options.StackVersion,
})
if err != nil {
return fmt.Errorf("failed to initialize payload: %w", err)
return fmt.Errorf("failed to get deployment template %q: %w", templateID, err)
}

payload := template.DeploymentTemplate

// Remove the resources that we don't need.
payload.Resources.Apm = nil
payload.Resources.Appsearch = nil
payload.Resources.EnterpriseSearch = nil

// Initialize the plan with the id of the template, otherwise the create request fails.
if es := payload.Resources.Elasticsearch; len(es) > 0 {
plan := es[0].Plan
if plan.DeploymentTemplate == nil {
plan.DeploymentTemplate = &models.DeploymentTemplateReference{}
}
plan.DeploymentTemplate.ID = &templateID
}

res, err := deploymentapi.Create(deploymentapi.CreateParams{
API: api,
Request: payload,
Overrides: &deploymentapi.PayloadOverrides{
Name: name,
},
})
if err != nil {
return fmt.Errorf("failed to create deployment: %w", err)
}
if created := res.Created; created == nil || !*created {
return fmt.Errorf("request succeeded, but deployment was not created, check in the console UI")
}

var config Config
config.Parameters = map[string]string{
"alias": res.Alias,
}
deploymentID := res.ID
if deploymentID == nil {
return fmt.Errorf("deployment created, but couldn't get its ID, check in the console UI")
}
config.Parameters["id"] = *deploymentID
for _, resource := range res.Resources {
kind := resource.Kind
if kind == nil {
continue
}
if *kind == "elasticsearch" {
if creds := resource.Credentials; creds != nil {
if creds.Username != nil {
config.ElasticsearchUsername = *creds.Username
}
if creds.Password != nil {
config.ElasticsearchPassword = *creds.Password
}
}
}
}

// Storing once before getting the endpoints, so we have the ID.
err = storeConfig(cp.profile, config)
if err != nil {
return fmt.Errorf("failed to store config: %w", err)
}

for {
deployment, err := deploymentapi.Get(deploymentapi.GetParams{
API: api,
DeploymentID: *deploymentID,
})
if err != nil {
return fmt.Errorf("couldn't check deployment health: %w", err)
}

if healthy := deployment.Healthy; healthy == nil || !*healthy {
if signal.SIGINT() {
return fmt.Errorf("wait interrupted")
}
time.Sleep(1 * time.Second)
continue
}

// TODO: Check that resources are healthy too.

config.ElasticsearchHost, err = cp.getServiceURL(deployment.Resources.Elasticsearch)
if err != nil {
return fmt.Errorf("failed to get elasticsearch host: %w", err)
}
config.KibanaHost, err = cp.getServiceURL(deployment.Resources.Kibana)
if err != nil {
return fmt.Errorf("failed to get kibana host: %w", err)
}
config.Parameters["fleet_url"], err = cp.getServiceURL(deployment.Resources.IntegrationsServer)
if err != nil {
return fmt.Errorf("failed to get fleet host: %w", err)
}

break
}

// Store the configuration again now with the service urls.
err = storeConfig(cp.profile, config)
if err != nil {
return fmt.Errorf("failed to store config: %w", err)
}

fmt.Printf("%+v\n", res)
return nil
}

func (*cloudProvider) getServiceURL(resourcesResponse any) (string, error) {
// Converting back and forth for easier access.
var resources []struct {
Info struct {
Metadata struct {
ServiceURL string `json:"service_url"`
} `json:"metadata"`
} `json:"info"`
}

d, err := json.Marshal(resourcesResponse)
if err != nil {
return "", fmt.Errorf("failed to marshal resources: %w", err)
}
err = json.Unmarshal(d, &resources)
if err != nil {
return "", fmt.Errorf("failed to unmarshal resources back: %w", err)
}

for _, resource := range resources {
if serviceURL := resource.Info.Metadata.ServiceURL; serviceURL != "" {
return serviceURL, nil
}
}
return "", fmt.Errorf("url not found")
}

func (*cloudProvider) TearDown(options Options) error {
return fmt.Errorf("not implemented")
}
Expand Down