[go: up one dir, main page]

Skip to content

Commit

Permalink
Support attaching/detaching volumes on create/update instance (#466)
Browse files Browse the repository at this point in the history
* Support attaching/detaching volumes on create/update instance

Signed-off-by: Vishal Anarase <iamvishalanarase@gmail.com>

* fix plural on volume_attachments

* resolve conflicts after the rebase

* remove conflict markers

---------

Signed-off-by: Vishal Anarase <iamvishalanarase@gmail.com>
Co-authored-by: alessandroargentieri <alessandro@civo.com>
  • Loading branch information
vishalanarase and alessandroargentieri authored Sep 25, 2024
1 parent bd54e5e commit c7bf446
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 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.AttachedVolumes = append(config.AttachedVolumes, civogo.AttachedVolume{
ID: volume,
})
}
}

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

Expand Down
11 changes: 11 additions & 0 deletions cmd/kubernetes/kubernetes_nodepool_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ var kubernetesNodePoolCreateCmd = &cobra.Command{
os.Exit(1)
}

poolConfig := civogo.KubernetesClusterPoolUpdateConfig{ID: poolID, Count: &numTargetNodesPool, Size: targetNodesPoolSize}
if publicIpNodePool {
if config.Current.RegionToFeatures != nil {
if !config.Current.RegionToFeatures[client.Region].PublicIPNodePools {
utility.Error("The region \"%s\" does not support \"Public IP Node Pools\" feature", client.Region)
os.Exit(1)
}
}
poolConfig.PublicIPNodePool = publicIpNodePool
}

kubernetesCluster, err := client.CreateKubernetesClusterPool(kubernetesFindCluster.ID, &civogo.KubernetesClusterPoolConfig{
Region: client.Region,
ID: poolID,
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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ github.com/briandowns/spinner v1.11.1/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c h1:aprLqMn7gSPT+vdDSl+/E6NLEuArwD/J7IWd8bJt5lQ=
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c/go.mod h1:Ie6SubJv/NTO9Q0UBH0QCl3Ve50lu9hjbi5YJUw03TE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/civo/civogo v0.3.77 h1:1rl5cpQruPhh+w8BBMpGQsaovjDvA44udPoDTAa45rk=
github.com/civo/civogo v0.3.77/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
github.com/civo/civogo v0.3.79 h1:Z1MbEG9CsGqSZV7UFBA0xsjk7TBGUPHjW9sM7cS5yZM=
github.com/civo/civogo v0.3.79/go.mod h1:7UCYX+qeeJbrG55E1huv+0ySxcHTqq/26FcHLVelQJM=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down

0 comments on commit c7bf446

Please sign in to comment.