Skip to content

Commit

Permalink
feat: Refactor vm update (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
bilby91 authored Nov 10, 2024
1 parent d6b9579 commit e9db27a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ resource "crunchloop_vm" "vm" {
If you wish to contribute to the provider, follow these steps:

1. Clone the repository
2. Build the provider using Go: `go build ./...`
2. Build the provider using Go: `go build -o terraform-provider-crunchloop`
3. Use the `examples` folder to test managing resources with your instance

## Documentation

Expand Down
31 changes: 30 additions & 1 deletion docs/resources/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,36 @@ resource "crunchloop_vm" "without_host" {
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = base64encode("echo 'Hello, World!'")
}
# Use a cloud-init configuration to configure the VM
#
data "cloudinit_config" "cloudinit" {
gzip = false
base64_encode = true
part {
filename = "cloud-config.yaml"
content_type = "text/cloud-config"
content = <<-EOF
#cloud-config
cloud_final_modules:
- [scripts-user, always]
runcmd:
- echo "Hello, World!" > /tmp/hello.txt
EOF
}
}
resource "crunchloop_vm" "with_cloudinit" {
name = "terraform-with-cloudinit"
vmi_id = data.crunchloop_vmi.ubuntu.id
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = data.cloudinit_config.cloudinit.rendered
}
```

Expand Down
31 changes: 30 additions & 1 deletion examples/resources/crunchloop_vm/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,34 @@ resource "crunchloop_vm" "without_host" {
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = base64encode("echo 'Hello, World!'")
}

# Use a cloud-init configuration to configure the VM
#
data "cloudinit_config" "cloudinit" {
gzip = false
base64_encode = true

part {
filename = "cloud-config.yaml"
content_type = "text/cloud-config"

content = <<-EOF
#cloud-config
cloud_final_modules:
- [scripts-user, always]
runcmd:
- echo "Hello, World!" > /tmp/hello.txt
EOF
}
}

resource "crunchloop_vm" "with_cloudinit" {
name = "terraform-with-cloudinit"
vmi_id = data.crunchloop_vmi.ubuntu.id
cores = 1
memory_megabytes = 1024
root_volume_size_gigabytes = 10
user_data = data.cloudinit_config.cloudinit.rendered
}
33 changes: 2 additions & 31 deletions internal/services/vm_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,6 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat
return nil, err
}

var wasRunning = *vm.Status == client.VirtualMachineStatusRunning

// We need to stop the vm if it's running to update it.
if wasRunning {
_, err = s.client.StopVm(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to stop vm before updating. Error: %s", err)
}

err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped")
if err != nil {
return nil, err
}
}

// We are ready to update the vm now.
updateResponse, err := s.client.UpdateVmWithResponse(ctx, id, options)
if err != nil {
Expand All @@ -99,27 +84,13 @@ func (s *VmService) UpdateVm(ctx context.Context, id int32, options client.Updat
}

// After we issue an update, the vm is going to transition to `updating` state
// and eventually will be back to `stopped` state, we need to wait for that
// and eventually will be back to `currentStatus` state, we need to wait for that
// state before moving forward.
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "stopped")
err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, *vm.Status)
if err != nil {
return nil, err
}

// If the vm was running we should turn it on again.
//
if wasRunning {
_, err = s.client.StartVm(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to start vm after updating. Error: %s", err)
}

err = utils.WaitForVmStatus(ctx, s.client, *vm.Id, "running")
if err != nil {
return nil, err
}
}

vm, err = s.GetVm(ctx, id)
if err != nil {
return nil, err
Expand Down

0 comments on commit e9db27a

Please sign in to comment.