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

cmd: Move elasticsearch create to deployment API #67

Merged
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
136 changes: 82 additions & 54 deletions cmd/deployment/elasticsearch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,116 @@
package cmdelasticsearch

import (
"encoding/json"
"os"
"path/filepath"
"fmt"

"github.com/elastic/cloud-sdk-go/pkg/input"
"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/spf13/cobra"

cmdutil "github.com/elastic/ecctl/cmd/util"
"github.com/elastic/ecctl/pkg/deployment/elasticsearch"
"github.com/elastic/ecctl/pkg/deployment/elasticsearch/plan"
"github.com/elastic/ecctl/pkg/deployment"
"github.com/elastic/ecctl/pkg/deployment/depresource"
"github.com/elastic/ecctl/pkg/ecctl"
"github.com/elastic/ecctl/pkg/util"
)

const (
esCreateExample = `
* Create a single node elasticsearch cluster
ecctl deployment elasticsearch create --version 5.6.0 --zones 1 --capacity 2048

* Create an elasticsearch cluster from a plan definition.
ecctl deployment elasticsearch create --file definition.json
`
)

var createElasticsearchCmd = &cobra.Command{
Use: "create [--file] [--capacity|--version|--zones] [name]",
Short: "Creates an Elasticsearch cluster",
PreRunE: cobra.MaximumNArgs(1),
Use: "create {--file|--size <int> --version <string> --zones <string>|--topology-element <obj>}",
Short: "Creates a deployment with (only) an Elasticsearch resource in it",
PreRunE: cobra.MaximumNArgs(0),
Long: esCreateLong,
Example: esCreateExample,
RunE: func(cmd *cobra.Command, args []string) error {
var name string
if len(args) == 1 {
name = args[0]
}

var track, _ = cmd.Flags().GetBool("track")
var generatePayload, _ = cmd.Flags().GetBool("generate-payload")
var zoneCount, _ = cmd.Flags().GetInt32("zones")
var capacity, _ = cmd.Flags().GetInt32("capacity")
var size, _ = cmd.Flags().GetInt32("size")
var plugin, _ = cmd.Flags().GetStringSlice("plugin")
var def *models.ElasticsearchClusterPlan
var name, _ = cmd.Flags().GetString("name")
var RefID, _ = cmd.Flags().GetString("ref-id")
var version, _ = cmd.Flags().GetString("version")
var dt, _ = cmd.Flags().GetString("deployment-template")
var te, _ = cmd.Flags().GetStringArray("topology-element")
var region string
if ecctl.Get().Config.Region == "" {
region = cmdutil.DefaultECERegion
}

var p *models.ElasticsearchPayload
if err := cmdutil.FileOrStdin(cmd, "file"); err == nil {
reader, _ := input.NewFileOrReader(os.Stdin, cmd.Flag("file").Value.String())
if reader != nil {
if err := json.NewDecoder(reader).Decode(&def); err != nil {
return err
}
err := cmdutil.DecodeDefinition(cmd, "file", &p)
if err != nil && err != cmdutil.ErrNodefinitionLoaded {
return err
}
}

r, err := elasticsearch.Create(elasticsearch.CreateParams{
API: ecctl.Get().API,
ClusterName: name,
PlanDefinition: def,
LegacyParams: plan.LegacyParams{
Version: cmd.Flag("version").Value.String(),
ZoneCount: zoneCount,
Capacity: capacity,
Plugins: plugin,
},
TrackParams: util.TrackParams{
Track: track,
Output: ecctl.Get().Config.OutputDevice,
payload, err := depresource.ParseElasticsearchInput(depresource.ParseElasticsearchInputParams{
NewElasticsearchParams: depresource.NewElasticsearchParams{
API: ecctl.Get().API,
RefID: RefID,
Version: version,
Plugins: plugin,
Region: region,
TemplateID: dt,
},
Size: size,
ZoneCount: zoneCount,
Payload: p,
Writer: ecctl.Get().Config.ErrorDevice,
TopologyElements: te,
})
if err != nil {
return err
}
return ecctl.Get().Formatter.Format(
filepath.Join("elasticsearch", "create"),
r,
)

// Returns the ElasticsearchPayload skipping the creation of the resources.
if generatePayload {
return ecctl.Get().Formatter.Format("", payload)
}

var createParams = deployment.CreateParams{
API: ecctl.Get().API,
Request: &models.DeploymentCreateRequest{
Name: name,
Resources: &models.DeploymentCreateResources{
Elasticsearch: []*models.ElasticsearchPayload{payload},
},
},
}

res, err := deployment.Create(createParams)
if err != nil {
return err
}

if err := ecctl.Get().Formatter.Format("", res); err != nil {
if !track {
return err
}
fmt.Fprintln(ecctl.Get().Config.OutputDevice, err)
}

if !track {
return nil
}

return depresource.TrackResources(depresource.TrackResourcesParams{
API: ecctl.Get().API,
Resources: res.Resources,
OutputDevice: ecctl.Get().Config.OutputDevice,
})
},
}

func init() {
Command.AddCommand(createElasticsearchCmd)
createElasticsearchCmd.Flags().String("file", "", "JSON plan definition file location")
createElasticsearchCmd.Flags().StringP("version", "v", "", "Filter per version")
createElasticsearchCmd.Flags().Int32P("zones", "z", 0, "Number of zones for the cluster")
createElasticsearchCmd.Flags().Int32P("capacity", "c", 0, "Capacity per node")
createElasticsearchCmd.Flags().String("file", "", "ElasticsearchPayload file definition. See help for more information")
createElasticsearchCmd.Flags().String("deployment-template", "default", "Deployment template ID on which to base the deployment from")
createElasticsearchCmd.Flags().StringArrayP("topology-element", "e", nil, "Topology element definition. See help for more information")
createElasticsearchCmd.Flags().String("version", "", "Version to use, if not specified, the latest available stack version will be used")
createElasticsearchCmd.Flags().String("name", "", "Optional name for the Elasticsearch deployment")
createElasticsearchCmd.Flags().String("ref-id", "elasticsearch", "RefId for the Elasticsearch deployment")
createElasticsearchCmd.Flags().Int32("zones", 1, "Number of zones the deployment will span")
createElasticsearchCmd.Flags().Int32("size", 4096, "Memory (RAM) in MB that each of the deployment nodes will have")
createElasticsearchCmd.Flags().BoolP("track", "t", false, cmdutil.TrackFlagMessage)
createElasticsearchCmd.Flags().StringSlice("plugin", nil, "Additional plugins to add to the Elasticsearch cluster")
createElasticsearchCmd.Flags().Bool("generate-payload", false, "Returns the ElasticsearchPayload without actually creating the deployment resources")
createElasticsearchCmd.Flags().StringSlice("plugin", nil, "Additional plugins to add to the Elasticsearch deployment")
}
143 changes: 143 additions & 0 deletions cmd/deployment/elasticsearch/create_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cmdelasticsearch

const (
esCreateLong = `Creates an Elasticsearch deployment, limitting the creation scope to Elasticsearch resources.
There's a few ways to create an Elasticsearch deployment, sane default values are provided, making
the command work out of the box even when no parameters are set. When version is not specified,
the latest available stack version will automatically be used. These are the available options:

* Simplified flags: --zones <zone count> --size <node memory in MB>
* Advanced flags: --topology-element <json obj> (shorthand: -e).
Note that the flag can be specified multiple times for complex topologies.
The JSON object has the following format:
{
"name": "["data", "master", "ml"]" # type string
"size": 1024 # type int32
"zone_count": 1 # type int32
}
* File definition: --file=<file path> (shorthand: -f). The definition can be found in:
https://www.elastic.co/guide/en/cloud-enterprise/current/definitions.html#ElasticsearchPayload

As an option "--generate-payload" can be used in order to obtain the generated ElasticsearchPayload
that would be sent as a request, save it, update or extend the topology and create an Elasticsearch
deployment using the saved payload with the "--file" flag.`

esCreateExample = `## Create a single node cluster. The command will exit after the API response has been returned,
## without waiting until the deployment resources have been created.
$ ecctl deployment elasticsearch create --name example-cluster --size 2048
Obtained latest stack version: 7.4.2
{
"created": true,
"id": "439fdd1d1b6e4713b6a86847f5a6a199",
"name": "example-cluster",
"resources": [
"id": "a95f5c474fba482b989f790a1f8475b3",
"kind": "elasticsearch",
"ref_id": "elasticsearch",
"region": "ece-region"
}
]
}

## To make the command wait until the resources have been created use the "--track" flag, which will
## output the current stage on which the deployment resources are in.
$ deployment elasticsearch create --name example-cluster --size 2048 --track
Obtained latest stack version: 7.4.2
[...]
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: running step "wait-until-running" (Plan duration 1.348361695s)...
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: running step "verify-non-split" (Plan duration 51.296428148s)...
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: running step "set-quorum-size" (Plan duration 57.381950576s)...
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: running step "set-maintenance" (Plan duration 58.296756321s)...
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: running step "apply-hot-warm-default-allocation" (Plan duration 1m3.285855089s)...
Cluster [6be1a417f8bc408cafead7d9db724886][Elasticsearch]: finished running all the plan steps (Total plan duration: 1m4.756486638s)

## Additionally, a more advanced topology can be created through "--topology-element" or shorthand "-e".
$ ecctl deployment elasticsearch create --name my-cluster --topology-element '{"size": 1024, "zone_count": 2, "name": "data"}' --topology-element '{"size": 1024, "zone_count": 1, "name": "ml"}' --generate-payload
Obtained latest stack version: 7.4.2
{
"plan": {
"cluster_topology": [
{
"instance_configuration_id": "data.default",
"node_type": {
"data": true,
"ingest": true,
"master": true
},
"size": {
"resource": "memory",
"value": 1024
},
"zone_count": 2
},
{
"instance_configuration_id": "ml",
"node_type": {
"data": false,
"ingest": false,
"master": false,
"ml": true
},
"size": {
"resource": "memory",
"value": 1024
},
"zone_count": 1
}
],
"deployment_template": {
"id": "default"
},
"elasticsearch": {
"version": "7.4.2"
}
},
"ref_id": "elasticsearch",
"region": "ece-region"
}

## Save the definition to a file for later use.
$ ecctl deployment elasticsearch create --name my-cluster --size 1024 --track --generate-payload --zones 2 > elasticsearch_create_example.json
Obtained latest stack version: 7.4.2

## Create an Elasticsearch deployment through the file definition and track the progress
$ ecctl deployment elasticsearch create --file elasticsearch_create_example.json --track
[...]
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "wait-until-running" (Plan duration 3.165747696s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "verify-non-split" (Plan duration 1m2.476847682s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "set-quorum-size" (Plan duration 1m7.575588825s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "set-maintenance" (Plan duration 1m8.464692293s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "apply-hot-warm-default-allocation" (Plan duration 1m13.631385049s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "apply-plan-settings" (Plan duration 1m14.335030452s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: running step "post-plan-cleanup" (Plan duration 1m15.463009785s)...
Cluster [9c96d8a0df1c47f8a45cd154fc0e3c83][Elasticsearch]: finished running all the plan steps (Total plan duration: 1m16.89854434s)

## Create the deployment piping through the file contents tracking the creation progress
$ cat elasticsearch_create_example.json | dev-cli deployment elasticsearch create --track
[...]
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "wait-until-running" (Plan duration 3.955507371s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "verify-non-split" (Plan duration 1m2.434546366s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "set-quorum-size" (Plan duration 1m7.269306003s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "set-maintenance" (Plan duration 1m10.321987044s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "apply-hot-warm-default-allocation" (Plan duration 1m15.337019401s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "apply-plan-settings" (Plan duration 1m16.346500871s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: running step "post-plan-cleanup" (Plan duration 1m18.334419179s)...
Cluster [7dcaeb621dba4248b6a4efc8080a055c][Elasticsearch]: finished running all the plan steps (Total plan duration: 1m20.043525071s)`
)
28 changes: 28 additions & 0 deletions cmd/util/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,40 @@ package cmdutil
import (
"encoding/json"
"errors"
"os"
"path"
"reflect"

"github.com/elastic/cloud-sdk-go/pkg/input"
"github.com/elastic/cloud-sdk-go/pkg/models"
"github.com/spf13/cobra"
)

var (
// ErrNodefinitionLoaded is returned by DecodeDefinition when no reader
// has been returned from either File or Stdin.
ErrNodefinitionLoaded = errors.New("failed obtaining a reader from file or stdin")
)

// DecodeDefinition takes a cobra command, a flagname and the desired structure
// on which to decode the contents of either the os.Stdin or the file contents.
// If both are empty, an error is returned.
func DecodeDefinition(cmd *cobra.Command, flagname string, v interface{}) error {
if reflect.ValueOf(v).Kind() != reflect.Ptr {
return errors.New("decode file: passed structure is not a pointer")
}

reader, _ := input.NewFileOrReader(os.Stdin, cmd.Flag("file").Value.String())
if reader != nil {
if err := json.NewDecoder(reader).Decode(&v); err != nil {
return err
}
return nil
}

return ErrNodefinitionLoaded
}

// DecodeFile takes a filename and the pointer to a structure, opening the file
// and dumping the contents into the desired structure. Make sure a pointer is
// passed rather than the copy of a structure.
Expand Down
2 changes: 1 addition & 1 deletion docs/ecctl_deployment_elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ecctl deployment elasticsearch [flags]

* [ecctl deployment](ecctl_deployment.md) - Manages deployments
* [ecctl deployment elasticsearch console](ecctl_deployment_elasticsearch_console.md) - Starts an interactive console with the cluster
* [ecctl deployment elasticsearch create](ecctl_deployment_elasticsearch_create.md) - Creates an Elasticsearch cluster
* [ecctl deployment elasticsearch create](ecctl_deployment_elasticsearch_create.md) - Creates a deployment with (only) an Elasticsearch resource in it
* [ecctl deployment elasticsearch delete](ecctl_deployment_elasticsearch_delete.md) - Deletes an Elasticsearch cluster
* [ecctl deployment elasticsearch diagnose](ecctl_deployment_elasticsearch_diagnose.md) - Generates a diagnostics bundle for the cluster
* [ecctl deployment elasticsearch instances](ecctl_deployment_elasticsearch_instances.md) - Manages elasticsearch at the instance level
Expand Down
Loading