github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/rollback.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"strconv"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/helm"
    27  )
    28  
    29  const rollbackDesc = `
    30  This command rolls back a release to a previous revision.
    31  
    32  The first argument of the rollback command is the name of a release, and the
    33  second is a revision (version) number. To see revision numbers, run 
    34  'helm history RELEASE'.
    35  `
    36  
    37  type rollbackCmd struct {
    38  	name         string
    39  	revision     int32
    40  	dryRun       bool
    41  	recreate     bool
    42  	disableHooks bool
    43  	out          io.Writer
    44  	client       helm.Interface
    45  	timeout      int64
    46  	wait         bool
    47  }
    48  
    49  func newRollbackCmd(c helm.Interface, out io.Writer) *cobra.Command {
    50  	rollback := &rollbackCmd{
    51  		out:    out,
    52  		client: c,
    53  	}
    54  
    55  	cmd := &cobra.Command{
    56  		Use:               "rollback [flags] [RELEASE] [REVISION]",
    57  		Short:             "roll back a release to a previous revision",
    58  		Long:              rollbackDesc,
    59  		PersistentPreRunE: setupConnection,
    60  		RunE: func(cmd *cobra.Command, args []string) error {
    61  			if err := checkArgsLength(len(args), "release name", "revision number"); err != nil {
    62  				return err
    63  			}
    64  
    65  			rollback.name = args[0]
    66  
    67  			v64, err := strconv.ParseInt(args[1], 10, 32)
    68  			if err != nil {
    69  				return fmt.Errorf("invalid revision number '%q': %s", args[1], err)
    70  			}
    71  
    72  			rollback.revision = int32(v64)
    73  			rollback.client = ensureHelmClient(rollback.client)
    74  			return rollback.run()
    75  		},
    76  	}
    77  
    78  	f := cmd.Flags()
    79  	f.BoolVar(&rollback.dryRun, "dry-run", false, "simulate a rollback")
    80  	f.BoolVar(&rollback.recreate, "recreate-pods", false, "performs pods restart for the resource if applicable")
    81  	f.BoolVar(&rollback.disableHooks, "no-hooks", false, "prevent hooks from running during rollback")
    82  	f.Int64Var(&rollback.timeout, "timeout", 300, "time in seconds to wait for any individual kubernetes operation (like Jobs for hooks)")
    83  	f.BoolVar(&rollback.wait, "wait", false, "if set, will wait until all Pods, PVCs, and Services are in a ready state before marking the release as successful. It will wait for as long as --timeout")
    84  
    85  	return cmd
    86  }
    87  
    88  func (r *rollbackCmd) run() error {
    89  	_, err := r.client.RollbackRelease(
    90  		r.name,
    91  		helm.RollbackDryRun(r.dryRun),
    92  		helm.RollbackRecreate(r.recreate),
    93  		helm.RollbackDisableHooks(r.disableHooks),
    94  		helm.RollbackVersion(r.revision),
    95  		helm.RollbackTimeout(r.timeout),
    96  		helm.RollbackWait(r.wait))
    97  	if err != nil {
    98  		return prettyError(err)
    99  	}
   100  
   101  	fmt.Fprintf(r.out, "Rollback was a success! Happy Helming!\n")
   102  
   103  	return nil
   104  }