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 8285865
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,19 @@ 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.
Every created cloud instance gets a label `delete_vm_after: 1698792887`. By
default this is the UNIX timestamp during VM creation + an hour.

Look for servers in your project named exactly as the ones in your beaker host configuration and SSH keys with names beginning with `Beaker-`.
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 seconds.

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-`.

You can also periodically scan for VMs where the `delete_vm_after` points to a
past timestamp and delete them.

# Contributing

Expand Down
3 changes: 2 additions & 1 deletion beaker-hcloud.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'bcrypt_pbkdf', '~> 1.0'
s.add_runtime_dependency 'beaker', '~> 5.4'
s.add_runtime_dependency 'ed25519', '~> 1.2'
s.add_runtime_dependency 'hcloud', '~> 1.1'
# pin to 1.1. 1.2 uses blank? a lot but without the correct dependency
s.add_runtime_dependency 'hcloud', '~> 1.1.0'
s.add_runtime_dependency 'ssh_data', '~> 1.3'
end
11 changes: 11 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', 60 * 60).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,13 @@ 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
Time.now.to_i + @delete_vm_after
end

def create_server(host)
@logger.notify "provisioning #{host.name}"
location = host[:location] || 'nbg1'
Expand All @@ -79,6 +89,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 8285865

Please sign in to comment.