github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/cmd/attach-volume.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/emc-advanced-dev/pkg/errors"
    10  	"github.com/solo-io/unik/pkg/client"
    11  )
    12  
    13  var mountPoint string
    14  
    15  var attachCmd = &cobra.Command{
    16  	Use:     "attach-volume",
    17  	Aliases: []string{"attach"},
    18  	Short:   "Attach a volume to a stopped instance",
    19  	Long: `Attaches a volume to a stopped instance at a specified mount point.
    20  You specify the volume by name or id.
    21  
    22  The volume must be attached to an available mount point on the instance.
    23  Mount points are image-specific, and are determined when the image is compiled.
    24  
    25  For a list of mount points on the image for this instance, run unik images, or
    26  unik describe image
    27  
    28  If the specified mount point is occupied by another volume, the command will result
    29  in an error
    30  `,
    31  	Run: func(cmd *cobra.Command, args []string) {
    32  		if err := func() error {
    33  			if err := readClientConfig(); err != nil {
    34  				return err
    35  			}
    36  			if volumeName == "" {
    37  				return errors.New("must specify --volume", nil)
    38  			}
    39  			if instanceName == "" {
    40  				return errors.New("must specify --instanceName", nil)
    41  			}
    42  			if mountPoint == "" {
    43  				return errors.New("must specify --mountPoint", nil)
    44  			}
    45  			if host == "" {
    46  				host = clientConfig.Host
    47  			}
    48  			logrus.WithFields(logrus.Fields{"host": host, "instanceName": instanceName, "volume": volumeName, "mountPoint": mountPoint}).Info("attaching volume")
    49  			if err := client.UnikClient(host).Volumes().Attach(volumeName, instanceName, mountPoint); err != nil {
    50  				return err
    51  			}
    52  			return nil
    53  		}(); err != nil {
    54  			logrus.Errorf("failed deleting volume: %v", err)
    55  			os.Exit(-1)
    56  		}
    57  	},
    58  }
    59  
    60  func init() {
    61  	RootCmd.AddCommand(attachCmd)
    62  	attachCmd.Flags().StringVar(&volumeName, "volume", "", "<string,required> name or id of volume to attach. unik accepts a prefix of the name or id")
    63  	attachCmd.Flags().StringVar(&instanceName, "instance", "", "<string,required> name or id of instance to attach to. unik accepts a prefix of the name or id")
    64  	attachCmd.Flags().StringVar(&mountPoint, "mountPoint", "", "<string,required> mount path for volume. this should reflect the mappings specified on the image. run 'unik describe-image' to see expected mount points for the image")
    65  	attachCmd.Flags().BoolVar(&force, "force", false, "<bool, optional> force deleting volume in the case that it is running")
    66  }