Skip to content

Commit

Permalink
Attach a label to servers with their desired deletion date
Browse files Browse the repository at this point in the history
This then enables us to periodically scan for images with a unix
timestamp in the label that's smaller than now() and delete them.

Co-authored-by: Ewoud Kohl van Wijngaarden <[email protected]>
  • Loading branch information
bastelfreak and ekohl committed Nov 1, 2023
1 parent 41229a0 commit 549d4d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ are being created:
# Cleanup

In cases where the beaker process is killed before finishing, it may leave resources in Hetzner cloud. These will need to be manually deleted.

Look for servers in your project named exactly as the ones in your beaker host configuration and SSH keys with names beginning with `Beaker-`.

Every created cloud instance gets a label `delete_vm_after: 1698792887`. By
default this is the UNIX timestamp during VM creation + an hour.

You can modify the default of an hour by setting the `BEAKER_HCLOUD_DELETE_VM_AFTER`
environment variable to any positive integer. It will be interpreted as hours.

It's up to the user to scan for VMs where the `delete_vm_after` points to a
past timestamp and delete them.

# Contributing

Please refer to voxpupuli/beaker's [contributing](https://github.com/voxpupuli/beaker/blob/master/CONTRIBUTING.md) guide.
12 changes: 12 additions & 0 deletions lib/beaker/hypervisor/hcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'hcloud'
require 'ed25519'
require 'bcrypt_pbkdf'
require 'time'

require_relative '../../beaker-hcloud/ssh_data_patches'

Expand All @@ -15,7 +16,9 @@ def initialize(hosts, options) # rubocop:disable Lint/MissingSuper
@options = options
@logger = options[:logger] || Beaker::Logger.new
@hosts = hosts
@delete_vm_after = ENV.fetch('BEAKER_HCLOUD_DELETE_VM_AFTER', 1).to_i

raise 'BEAKER_HCLOUD_DELETE_VM_AFTER needs to be a positive integer' unless @delete_vm_after.positive?
raise 'You need to pass a token as BEAKER_HCLOUD_TOKEN environment variable' unless ENV['BEAKER_HCLOUD_TOKEN']

@client = ::Hcloud::Client.new(token: ENV.fetch('BEAKER_HCLOUD_TOKEN'))
Expand Down Expand Up @@ -69,6 +72,14 @@ def create_ssh_key
hcloud_ssh_key
end

# we need to save the date as unix timestamp. Hetzner Cloud labels only support:
# "alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between."
# https://docs.hetzner.cloud/#labels
def vm_deletion_date
delete_vm_after_seconds = @delete_vm_after * 60 * 60
Time.now.to_i + delete_vm_after_seconds
end

def create_server(host)
@logger.notify "provisioning #{host.name}"
location = host[:location] || 'nbg1'
Expand All @@ -79,6 +90,7 @@ def create_server(host)
server_type: server_type,
image: host[:image],
ssh_keys: [ssh_key_name],
labels: { delete_vm_after: vm_deletion_date },
)
while action.status == 'running'
sleep 5
Expand Down
6 changes: 4 additions & 2 deletions spec/beaker/hypervisor/hcloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
'dns_ptr' => 'server1.example.com',
},
},
destroy: true)
destroy: true,
labels: { vm_delete_after: '1695385549' })
end
let(:server2) do
double(:server2,
Expand All @@ -56,7 +57,8 @@
'dns_ptr' => 'server2.example.com',
},
},
destroy: true)
destroy: true,
labels: { vm_delete_after: '1695385549' })
end
let(:action_double) do
double(:action, status: 'success')
Expand Down

0 comments on commit 549d4d3

Please sign in to comment.