Skip to content

Commit

Permalink
Support attaching/detaching volumes on create/update instance
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarase <iamvishalanarase@gmail.com>
  • Loading branch information
vishalanarase committed Sep 24, 2024
1 parent 8626fb1 commit 6e5ffb8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {
instanceCreateCmd.Flags().StringVarP(&reservedIPv4, "reservedip", "", "", "Reserved IPv4 address")
instanceCreateCmd.Flags().StringVar(&script, "script", "", "path to a script that will be uploaded to /usr/local/bin/civo-user-init-script on your instance, read/write/executable only by root and then will be executed at the end of the cloud initialization")
instanceCreateCmd.Flags().BoolVar(&skipShebangCheck, "skip-shebang-check", false, "skip the shebang line check when passing a user init script")
instanceCreateCmd.Flags().StringSliceVarP(&volumes, "volumes", "v", []string{}, "List of volumes to attach at boot")

instanceStopCmd.Flags().BoolVarP(&waitStop, "wait", "w", false, "wait until the instance's is stoped")
}
9 changes: 9 additions & 0 deletions cmd/instance/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var wait bool
var hostnameCreate, size, diskimage, publicip, initialuser, sshkey, tags, network, privateIPv4, reservedIPv4, firewall string
var script string
var skipShebangCheck bool
var volumes []string

var instanceCreateCmd = &cobra.Command{
Use: "create",
Expand Down Expand Up @@ -258,6 +259,14 @@ If you wish to use a custom format, the available fields are:
config.Tags = strings.Split(tags, ",")
}

if len(volumes) > 0 {
for _, volume := range volumes {
config.AttachedVolume = append(config.AttachedVolume, civogo.AttachedVolume{
ID: volume,
})
}
}

var executionTime, publicIP string
startTime := utility.StartTime()

Expand Down
1 change: 1 addition & 0 deletions cmd/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
volumeResizeCmd.MarkFlagRequired("size-gb")

volumeAttachCmd.Flags().BoolVarP(&waitVolumeAttach, "wait", "w", false, "wait until the volume is attached")
volumeAttachCmd.Flags().BoolVarP(&attachAtBoot, "attach-at-boot", "a", false, "Attach the volume at boot to instance")

volumeDetachCmd.Flags().BoolVarP(&waitVolumeDetach, "wait", "w", false, "wait until the volume is detached")
}
15 changes: 13 additions & 2 deletions cmd/volume/volume_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

var waitVolumeAttach bool
var attachAtBoot bool

var volumeAttachCmdExamples = []string{
"civo volume attach VOLUME_NAME INSTANCE_HOSTNAME",
Expand Down Expand Up @@ -62,10 +63,15 @@ var volumeAttachCmd = &cobra.Command{
os.Exit(1)
}

_, err = client.AttachVolume(volume.ID, civogo.VolumeAttachConfig{
cfg := civogo.VolumeAttachConfig{
InstanceID: instance.ID,
Region: client.Region,
})
}
if attachAtBoot {
cfg.AttachAtBoot = true
}

_, err = client.AttachVolume(volume.ID, cfg)
if err != nil {
utility.Error("error attaching the volume: %s", err)
os.Exit(1)
Expand Down Expand Up @@ -93,6 +99,11 @@ var volumeAttachCmd = &cobra.Command{
}
}

if attachAtBoot {
out := utility.Yellow(fmt.Sprintf("To use the volume %s you need reboot the instance %s once the volume is in attaching/detaching state", volume.Name, instance.Hostname))
fmt.Println(out)
}

ow := utility.NewOutputWriterWithMap(map[string]string{"id": volume.ID, "name": volume.Name})

switch common.OutputFormat {
Expand Down

0 comments on commit 6e5ffb8

Please sign in to comment.