Skip to content

Commit

Permalink
controlplane installer subcommand (#48)
Browse files Browse the repository at this point in the history
* add controlplane (cp) subcommand
* add controlplane to README
  • Loading branch information
bonifaido authored and tarokkk committed May 27, 2019
1 parent 5c201f9 commit 73b2188
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PIPELINE_VERSION = 0.15.4

# Dependency versions
GOTESTSUM_VERSION = 0.3.3
GOLANGCI_VERSION = 1.15.0
GOLANGCI_VERSION = 1.16.0
LICENSEI_VERSION = 0.1.0
GORELEASER_VERSION = 0.98.0
PACKR_VERSION = 2.0.8
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Usage:
Available Commands:
cluster Manage clusters
controlplane Manage controlplane
form Open forms from config, persist provided values and generate templates
help Help about any command
login Configure and log in to a Banzai Cloud context
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package command
import (
"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster"
"github.com/banzaicloud/banzai-cli/internal/cli/command/controlplane"
"github.com/banzaicloud/banzai-cli/internal/cli/command/form"
"github.com/banzaicloud/banzai-cli/internal/cli/command/organization"
"github.com/banzaicloud/banzai-cli/internal/cli/command/secret"
Expand All @@ -32,5 +33,6 @@ func AddCommands(cmd *cobra.Command, banzaiCli cli.Cli) {
form.NewFormCommand(banzaiCli),
organization.NewOrganizationCommand(banzaiCli),
secret.NewSecretCommand(banzaiCli),
controlplane.NewControlPlaneCommand(banzaiCli),
)
}
36 changes: 36 additions & 0 deletions internal/cli/command/controlplane/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright © 2019 Banzai Cloud
//
// Licensed 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 controlplane

import (
"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/spf13/cobra"
)

// NewControlPlaneCommand returns a cobra command for `controlplane` subcommands.
func NewControlPlaneCommand(banzaiCli cli.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "controlplane",
Aliases: []string{"cp"},
Short: "Manage controlplane",
}

cmd.AddCommand(
NewUpCommand(),
NewDownCommand(),
)

return cmd
}
135 changes: 135 additions & 0 deletions internal/cli/command/controlplane/down.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright © 2019 Banzai Cloud
//
// Licensed 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 controlplane

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
)

// NewDownCommand creates a new cobra.Command for `banzai clontrolplane down`.
func NewDownCommand() *cobra.Command {
options := createOptions{}

cmd := &cobra.Command{
Use: "down",
Short: "Destroy the controlplane",
Long: "Destroy a controlplane based on json stdin or interactive session",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runDestroy(options)
},
}

flags := cmd.Flags()

flags.StringVarP(&options.file, "file", "f", "values.yaml", "Control Plane descriptor file")

return cmd
}

func runDestroy(options createOptions) {
var out map[string]interface{}

filename := options.file

if isInteractive() {
var content string

for {
if filename == "" {
_ = survey.AskOne(
&survey.Input{
Message: "Load a JSON or YAML file:",
Default: "values.yaml",
Help: "Give either a relative or an absolute path to a file containing a JSON or YAML Control Plane creation descriptor. Leave empty to cancel.",
},
&filename,
nil,
)
if filename == "skip" || filename == "" {
break
}
}

if raw, err := ioutil.ReadFile(filename); err != nil {

log.Errorf("failed to read file %q: %v", filename, err)

filename = "" // reset fileName so that we can ask for one

continue
} else {
if err := unmarshal(raw, &out); err != nil {
log.Fatalf("failed to parse control plane descriptor: %v", err)
}

break
}
}

if bytes, err := json.MarshalIndent(out, "", " "); err != nil {
log.Debugf("descriptor: %#v", out)
log.Fatalf("failed to marshal descriptor: %v", err)
} else {
content = string(bytes)
_, _ = fmt.Fprintf(os.Stderr, "The current state of the descriptor:\n\n%s\n", content)
}

var destroy bool
_ = survey.AskOne(
&survey.Confirm{
Message: "Do you want to DESTROY the controlplane now?",
Default: true,
},
&destroy,
nil,
)

if !destroy {
log.Fatal("controlplane destroy cancelled")
}
} else { // non-interactive
var raw []byte
var err error

if filename != "" {
raw, err = ioutil.ReadFile(filename)
} else {
raw, err = ioutil.ReadAll(os.Stdin)
filename = "stdin"
}

if err != nil {
log.Fatalf("failed to read %s: %v", filename, err)
}

if err := unmarshal(raw, &out); err != nil {
log.Fatalf("failed to parse controlplane descriptor: %v", err)
}
}

log.Info("controlplane is being destroy")

if err := runInternal("destroy", filename); err != nil {
log.Fatalf("controlplane destroy failed: %v", err)
}
}
Loading

0 comments on commit 73b2188

Please sign in to comment.