github.com/pachyderm/pachyderm@v1.13.4/src/server/admin/cmds/cmds.go (about)

     1  package cmds
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/pachyderm/pachyderm/src/client"
     8  	"github.com/pachyderm/pachyderm/src/client/pkg/config"
     9  	"github.com/pachyderm/pachyderm/src/client/pkg/errors"
    10  	"github.com/pachyderm/pachyderm/src/server/pkg/cmdutil"
    11  	"github.com/pachyderm/pachyderm/src/server/pkg/uuid"
    12  
    13  	"github.com/golang/snappy"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  // Cmds returns a slice containing admin commands.
    18  func Cmds() []*cobra.Command {
    19  	var commands []*cobra.Command
    20  
    21  	var noObjects, noEnterprise, noAuth bool
    22  	var url string
    23  	extract := &cobra.Command{
    24  		Short: "Extract Pachyderm state to stdout or an object store bucket.",
    25  		Long:  "Extract Pachyderm state to stdout or an object store bucket.",
    26  		Example: `
    27  # Extract into a local file:
    28  $ {{alias}} > backup
    29  
    30  # Extract to s3:
    31  $ {{alias}} -u s3://bucket/backup`,
    32  		Run: cmdutil.RunFixedArgs(0, func(args []string) (retErr error) {
    33  			c, err := client.NewOnUserMachine("user")
    34  			if err != nil {
    35  				return err
    36  			}
    37  
    38  			defer c.Close()
    39  			if url != "" {
    40  				return c.ExtractURL(url, !noEnterprise, !noAuth)
    41  			}
    42  			w := snappy.NewBufferedWriter(os.Stdout)
    43  			defer func() {
    44  				if err := w.Close(); err != nil && retErr == nil {
    45  					retErr = err
    46  				}
    47  			}()
    48  			return c.ExtractWriter(!noObjects, !noEnterprise, !noAuth, w)
    49  		}),
    50  	}
    51  	extract.Flags().BoolVar(&noObjects, "no-objects", false, "don't extract from object storage, only extract data from etcd")
    52  	extract.Flags().BoolVar(&noEnterprise, "no-enterprise", false, "don't extract the enterprise license information")
    53  	extract.Flags().BoolVar(&noAuth, "no-auth", false, "don't extract the authentication information")
    54  	extract.Flags().StringVarP(&url, "url", "u", "", "An object storage url (i.e. s3://...) to extract to.")
    55  	commands = append(commands, cmdutil.CreateAlias(extract, "extract"))
    56  
    57  	var noToken bool
    58  	restore := &cobra.Command{
    59  		Short: "Restore Pachyderm state from stdin or an object store.",
    60  		Long:  "Restore Pachyderm state from stdin or an object store.",
    61  		Example: `
    62  # Restore from a local file:
    63  $ {{alias}} < backup
    64  
    65  # Restore from s3:
    66  $ {{alias}} -u s3://bucket/backup`,
    67  		Run: cmdutil.RunFixedArgs(0, func(args []string) error {
    68  			c, err := client.NewOnUserMachine("user")
    69  			if err != nil {
    70  				return err
    71  			}
    72  
    73  			// Generate a root auth token and cache it in the current context. If the restore operation
    74  			// activates auth this will become the new root token.
    75  			if !noToken {
    76  				authToken := uuid.NewWithoutDashes()
    77  				if err := config.WritePachTokenToConfig(authToken); err != nil {
    78  					return err
    79  				}
    80  				c.SetAuthToken(authToken)
    81  
    82  				defer func() {
    83  					authActive, err := c.IsAuthActive()
    84  					if err != nil {
    85  						fmt.Fprintf(os.Stderr, "Unable to detect whether auth is active on the restored cluster - %v", err)
    86  					}
    87  
    88  					if authActive || err != nil {
    89  						fmt.Fprintf(os.Stderr, `
    90  Generated a new root auth token. This token has permanent admin access to the restored cluster.
    91  It cannot be recreated - keep it safe for the lifetime of the cluster.
    92  
    93  Token: %v
    94  `[1:], authToken)
    95  					}
    96  				}()
    97  			}
    98  
    99  			defer c.Close()
   100  			if url != "" {
   101  				err = c.RestoreURL(url)
   102  			} else {
   103  				err = c.RestoreReader(snappy.NewReader(os.Stdin))
   104  			}
   105  			if err != nil {
   106  				return errors.Wrapf(err, "WARNING: Your cluster might be in an invalid "+
   107  					"state--consider deleting partially-restored data before continuing")
   108  			}
   109  
   110  			return nil
   111  		}),
   112  	}
   113  	restore.Flags().StringVarP(&url, "url", "u", "", "An object storage url (i.e. s3://...) to restore from.")
   114  	restore.Flags().BoolVar(&noToken, "no-token", false, "Don't generate a new auth token at the beginning of the restore.")
   115  	commands = append(commands, cmdutil.CreateAlias(restore, "restore"))
   116  
   117  	inspectCluster := &cobra.Command{
   118  		Short: "Returns info about the pachyderm cluster",
   119  		Long:  "Returns info about the pachyderm cluster",
   120  		Run: cmdutil.RunFixedArgs(0, func(args []string) error {
   121  			c, err := client.NewOnUserMachine("user")
   122  			if err != nil {
   123  				return err
   124  			}
   125  			defer c.Close()
   126  			ci, err := c.InspectCluster()
   127  			if err != nil {
   128  				return err
   129  			}
   130  			fmt.Println(ci.ID)
   131  			return nil
   132  		}),
   133  	}
   134  	commands = append(commands, cmdutil.CreateAlias(inspectCluster, "inspect cluster"))
   135  
   136  	return commands
   137  }