|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package cmddeployment |
| 19 | + |
| 20 | +import ( |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/elastic/cloud-sdk-go/pkg/models" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + |
| 26 | + cmdutil "github.com/elastic/ecctl/cmd/util" |
| 27 | + "github.com/elastic/ecctl/pkg/deployment" |
| 28 | + "github.com/elastic/ecctl/pkg/ecctl" |
| 29 | +) |
| 30 | + |
| 31 | +const updateLong = `updates a deployment from a file definition, defaulting prune_orphans=false, making the default |
| 32 | +update action safe for partial updates, to override this behavior toggle --prune-orphans. |
| 33 | +
|
| 34 | +Read more about the deployment definition in https://www.elastic.co/guide/en/cloud-enterprise/current/Deployment_-_CRUD.html` |
| 35 | + |
| 36 | +var updateExample = ` |
| 37 | +#### Same base deployment as the create example, changing cluster_topology[0].zone_count to 3. |
| 38 | +$ cat deployment_example_update.json |
| 39 | +{ |
| 40 | + "resources": { |
| 41 | + "elasticsearch": [ |
| 42 | + { |
| 43 | + "display_name": "my elasticsearch cluster", |
| 44 | + "ref_id": "my-es-cluster", |
| 45 | + "plan": { |
| 46 | + "deployment_template": { |
| 47 | + "id": "default" |
| 48 | + }, |
| 49 | + "elasticsearch": { |
| 50 | + "version": "6.8.4" |
| 51 | + }, |
| 52 | + "cluster_topology": [ |
| 53 | + { |
| 54 | + "instance_configuration_id": "data.default", |
| 55 | + "memory_per_node": 1024, |
| 56 | + "node_count_per_zone": 1, |
| 57 | + "node_type": { |
| 58 | + "data": true, |
| 59 | + "ingest": true, |
| 60 | + "master": true, |
| 61 | + "ml": false |
| 62 | + }, |
| 63 | + "zone_count": 3 |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | +} |
| 71 | +$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json |
| 72 | +{ |
| 73 | + "id": "f44c06c3af6f85dac05023cf243f4ab1", |
| 74 | + "name": "my example cluster", |
| 75 | + "resources": [ |
| 76 | + { |
| 77 | + "id": "205745432f6345a4999dd2d77ceb1812", |
| 78 | + "kind": "elasticsearch", |
| 79 | + "ref_id": "my-es-cluster", |
| 80 | + "region": "ece-region" |
| 81 | + }, |
| 82 | + { |
| 83 | + "elasticsearch_cluster_ref_id": "my-es-cluster", |
| 84 | + "id": "3617594f01074b76a5ca4f903f9d33ec", |
| 85 | + "kind": "kibana", |
| 86 | + "ref_id": "my-kibana-instance", |
| 87 | + "region": "ece-region" |
| 88 | + }, |
| 89 | + { |
| 90 | + "elasticsearch_cluster_ref_id": "my-es-cluster", |
| 91 | + "id": "90c3c9566f454861b0dc935c5c7420d8", |
| 92 | + "kind": "apm", |
| 93 | + "ref_id": "my-apm-instance", |
| 94 | + "region": "ece-region" |
| 95 | + } |
| 96 | + ] |
| 97 | +} |
| 98 | +#### Setting --prune-orphans, will cause any non-specified resources to be shut down. |
| 99 | +$ ecctl deployment update f44c06c3af6f85dac05023cf243f4ab1 -f deployment_example_update.json --prune-orphans |
| 100 | +setting --prune-orphans to "true" will cause any resources not specified in the update request to be removed from the deployment, do you want to continue? [y/n]: y |
| 101 | +{ |
| 102 | + "id": "f44c06c3af6f85dac05023cf243f4ab1", |
| 103 | + "name": "my example cluster", |
| 104 | + "resources": [ |
| 105 | + { |
| 106 | + "id": "205745432f6345a4999dd2d77ceb1812", |
| 107 | + "kind": "elasticsearch", |
| 108 | + "ref_id": "my-es-cluster", |
| 109 | + "region": "ece-region" |
| 110 | + } |
| 111 | + ], |
| 112 | + "shutdown_resources": { |
| 113 | + "apm": [ |
| 114 | + "90c3c9566f454861b0dc935c5c7420d8" |
| 115 | + ], |
| 116 | + "appsearch": [], |
| 117 | + "elasticsearch": [], |
| 118 | + "kibana": [ |
| 119 | + "3617594f01074b76a5ca4f903f9d33ec" |
| 120 | + ] |
| 121 | + } |
| 122 | +}`[1:] |
| 123 | + |
| 124 | +var updateCmd = &cobra.Command{ |
| 125 | + Use: `update -f <file definition.json>`, |
| 126 | + Short: "Updates a deployment from a file definition, allowing certain flag overrides", |
| 127 | + Long: updateLong, |
| 128 | + Example: updateExample, |
| 129 | + PreRunE: cmdutil.MinimumNArgsAndUUID(1), |
| 130 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 131 | + filename, _ := cmd.Flags().GetString("file") |
| 132 | + var r models.DeploymentUpdateRequest |
| 133 | + if err := cmdutil.DecodeFile(filename, &r); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + pruneOrphans, _ := cmd.Flags().GetBool("prune-orphans") |
| 138 | + r.PruneOrphans = &pruneOrphans |
| 139 | + |
| 140 | + force, _ := cmd.Flags().GetBool("force") |
| 141 | + var msg = `setting --prune-orphans to "true" will cause any resources not specified in the update request to be removed from the deployment, do you want to continue? [y/n]: ` |
| 142 | + if pruneOrphans && !force && !cmdutil.ConfirmAction(msg, os.Stderr, os.Stdout) { |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + skipSnapshot, _ := cmd.Flags().GetBool("skip-snapshot") |
| 147 | + hidePrunedOrphans, _ := cmd.Flags().GetBool("hide-pruned-orphans") |
| 148 | + |
| 149 | + var region string |
| 150 | + if ecctl.Get().Config.Region == "" { |
| 151 | + region = cmdutil.DefaultECERegion |
| 152 | + } |
| 153 | + |
| 154 | + res, err := deployment.Update(deployment.UpdateParams{ |
| 155 | + DeploymentID: args[0], |
| 156 | + API: ecctl.Get().API, |
| 157 | + Region: region, |
| 158 | + Request: &r, |
| 159 | + SkipSnapshot: skipSnapshot, |
| 160 | + HidePrunedOrphans: hidePrunedOrphans, |
| 161 | + }) |
| 162 | + |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + return ecctl.Get().Formatter.Format("", res) |
| 168 | + }, |
| 169 | +} |
| 170 | + |
| 171 | +func init() { |
| 172 | + Command.AddCommand(updateCmd) |
| 173 | + updateCmd.Flags().Bool("prune-orphans", false, "When set to true, it will remove any resources not specified in the update request, treating the json file contents as the authoritative deployment definition") |
| 174 | + updateCmd.Flags().Bool("skip-snapshot", false, "Skips taking an Elasticsearch snapshot prior to shutting down the deployment") |
| 175 | + updateCmd.Flags().Bool("hide-pruned-orphans", false, "Hides orphaned resources that were shut down (only relevant if --prune-orphans=true)") |
| 176 | + updateCmd.Flags().StringP("file", "f", "", "Partial (default) or full JSON file deployment update payload") |
| 177 | + updateCmd.MarkFlagRequired("file") |
| 178 | + updateCmd.MarkFlagFilename("file", "*.json") |
| 179 | +} |
0 commit comments