Skip to content

Commit

Permalink
Merge pull request #280 from banzaicloud/backup-service
Browse files Browse the repository at this point in the history
Support backup service
  • Loading branch information
colin014 authored Aug 3, 2020
2 parents e56447d + 0a1741d commit eac9d1a
Show file tree
Hide file tree
Showing 14 changed files with 1,690 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/cli/command/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package cluster

import (
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/nodepool"
"github.com/spf13/cobra"

"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/node"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/nodepool"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/restore"
)

// NewClusterCommand returns a cobra command for `cluster` subcommands.
Expand All @@ -43,6 +44,7 @@ func NewClusterCommand(banzaiCli cli.Cli) *cobra.Command {
integratedservice.NewIntegratedServiceCommand(banzaiCli),
node.NewNodeCommand(banzaiCli),
nodepool.NewNodePoolCommand(banzaiCli),
restore.NewRestoreCommand(banzaiCli),
)

return cmd
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/command/cluster/integratedservice/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/banzaicloud/banzai-cli/internal/cli"
clustercontext "github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/context"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice/services"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice/services/backup"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice/services/dns"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice/services/expiry"
"github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/integratedservice/services/ingress"
Expand Down Expand Up @@ -54,6 +55,8 @@ func NewIntegratedServiceCommand(banzaiCli cli.Cli) *cobra.Command {
services.NewServiceCommand(banzaiCli, "monitoring", monitoring.NewManager(banzaiCli)),
services.NewServiceCommand(banzaiCli, "securityscan", securityscan.NewManager(banzaiCli)),
services.NewServiceCommand(banzaiCli, "vault", vault.NewManager(banzaiCli)),

backup.NewBackupCommand(banzaiCli),
)

return cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright © 2020 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 backup

import (
"context"

"emperror.dev/errors"
"github.com/banzaicloud/banzai-cli/.gen/pipeline"
"github.com/banzaicloud/banzai-cli/internal/cli"
"github.com/spf13/cobra"
)

const (
clusterRunningStatus = "RUNNING"
clusterWarningStatus = "WARNING"
)

func NewBackupCommand(banzaiCli cli.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "backup",
Short: "Enable the backup service",
Long: "Allows you to enable the backup service for the cluster, in order to create manual and scheduled automatic backups of the cluster, and also to restore from these backups. You must enable the backup service before it can be used. See the subcommands of `banzai cluster service backup` and `banzai cluster service restore` for details.",
}

cmd.AddCommand(
newStatusCommand(banzaiCli),
newEnableCommand(banzaiCli),
newDisableCommand(banzaiCli),
newListCommand(banzaiCli),
newCreateCommand(banzaiCli),
newDeleteCommand(banzaiCli),
)

return cmd
}

type NotAvailableError struct {
}

func (NotAvailableError) Error() string {
return "This command is not available on this cluster. The following cloud providers are supported: Amazon, Azure, Google. The cluster must be in Running or Warning state to run this command."
}

func isCommandEnabledForCluster(client *pipeline.APIClient, orgID, clusterID int32) (bool, error) {
response, _, err := client.ClustersApi.GetCluster(context.Background(), orgID, clusterID)
if err != nil {
return false, errors.WrapIfWithDetails(err, "failed to get cluster", "clusterID", clusterID)
}

switch response.Cloud {
case amazonType, azureType, googleType:
switch response.Status {
case clusterRunningStatus, clusterWarningStatus:
return true, nil
default:
return false, nil
}
default:
return false, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright © 2020 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 backup

import (
"context"
"encoding/json"
"fmt"
"time"

"emperror.dev/errors"
"github.com/banzaicloud/banzai-cli/.gen/pipeline"
"github.com/banzaicloud/banzai-cli/internal/cli"
clustercontext "github.com/banzaicloud/banzai-cli/internal/cli/command/cluster/context"
"github.com/banzaicloud/banzai-cli/internal/cli/input"
"github.com/banzaicloud/banzai-cli/internal/cli/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type createOptions struct {
clustercontext.Context

filePath string
}

func newCreateCommand(banzaiCli cli.Cli) *cobra.Command {
options := createOptions{}

cmd := &cobra.Command{
Use: "create",
Aliases: []string{"c"},
Short: "Create a manual backup",
Long: "Create a one-time manual backup.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
cmd.SilenceErrors = true

if err := options.Init(args...); err != nil {
return errors.WrapIf(err, "failed to initialize options")
}

return runCreate(banzaiCli, options)
},
}

flags := cmd.Flags()
flags.StringVarP(&options.filePath, "file", "f", "", "Create backup specification file")

options.Context = clustercontext.NewClusterContext(cmd, banzaiCli, "create")

return cmd
}

func runCreate(banzaiCli cli.Cli, options createOptions) error {
client := banzaiCli.Client()
orgID := banzaiCli.Context().OrganizationID()
clusterID := options.ClusterID()

enabled, err := isCommandEnabledForCluster(client, orgID, clusterID)
if err != nil {
return errors.WrapIf(err, "error during checking command availability")
}

if !enabled {
return NotAvailableError{}
}

var request pipeline.CreateBackupRequest
if options.filePath == "" && banzaiCli.Interactive() {
if request, err = buildCreateRequestInteractively(); err != nil {
return errors.WrapIf(err, "failed to build create backup request interactively")
}
} else {
if err = readCreateReqFromFileOrStdin(options.filePath, &request); err != nil {
return errors.WrapIf(err, "failed to read create backup specification")
}
}

_, _, err = client.ArkBackupsApi.CreateARKBackupOfACluster(context.Background(), orgID, clusterID, request)
if err != nil {
return errors.WrapIf(err, "failed to create backup")
}

log.Infof("Backup created for cluster [%d]", clusterID)

return nil
}

func buildCreateRequestInteractively() (pipeline.CreateBackupRequest, error) {
var name string
var ttlLabel string

var currentTime = time.Now()

var defaultBackupName = fmt.Sprintf("manual-backup-%s", currentTime.Format("2006-01-02"))
err := input.DoQuestions([]input.QuestionMaker{
input.QuestionInput{
QuestionBase: input.QuestionBase{
Message: "Name of the backup",
Help: fmt.Sprintf("Name of the backup, for example, `%s`", defaultBackupName),
},
DefaultValue: defaultBackupName,
Output: &name,
},
input.QuestionSelect{
QuestionInput: input.QuestionInput{
QuestionBase: input.QuestionBase{
Message: "Keep backup for",
Help: "Retain backup for the specified period.",
},
DefaultValue: ttl1DayLabel,
Output: &ttlLabel,
},
Options: []string{ttl1DayLabel, ttl2DaysLabel, ttl1WeekLabel},
},
})
if err != nil {
return pipeline.CreateBackupRequest{}, errors.WrapIf(err, "error during getting create options")
}

var selectedTTL string
switch ttlLabel {
case ttl1DayLabel:
selectedTTL = ttl1DayValue
case ttl2DaysLabel:
selectedTTL = ttl2DaysValue
case ttl1WeekLabel:
selectedTTL = ttl1WeekValue
}

return pipeline.CreateBackupRequest{
Name: name,
Ttl: selectedTTL,
}, nil
}

func readCreateReqFromFileOrStdin(filePath string, req *pipeline.CreateBackupRequest) error {
filename, raw, err := utils.ReadFileOrStdin(filePath)
if err != nil {
return errors.WrapIfWithDetails(err, "failed to read", "filename", filename)
}

if err := json.Unmarshal(raw, &req); err != nil {
return errors.WrapIfWithDetails(err,
"failed to unmarshal input",
"fileName", filename,
"raw request", string(raw),
)
}

return nil
}
Loading

0 comments on commit eac9d1a

Please sign in to comment.