-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of hypervisor #10 #11
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d05129
Initial implementation of hypervisor #10
oneiros 3968bee
configure the hostname with the desired hostname
bastelfreak 1c66d13
Prefix environment variable with `BEAKER_` #10
oneiros 913b477
Merge pull request #1 from bastelfreak/fqdn
oneiros ddbeed0
Satisfy rubocop #10
oneiros d265185
Satisfy rubocop #10
oneiros 50a8a4a
Try to establish ruby2.7 compatibility #10
oneiros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ed25519' | ||
require 'ssh_data' | ||
|
||
# Patches for the 'ssh_data' gem to allow serialization of | ||
# ed25519 private keys in OpenSSH format. | ||
module BeakerHcloud | ||
module SSHDataPatches | ||
# Add encoding methods for OpenSSH's PEM-like format to | ||
# store private keys. | ||
module EncodingPatch | ||
def encode_pem(data, type) | ||
encoded_data = Base64.strict_encode64(data) | ||
.scan(/.{1,70}/m) | ||
.join("\n") | ||
.chomp | ||
<<~PEM | ||
-----BEGIN #{type}----- | ||
#{encoded_data} | ||
-----END #{type}----- | ||
PEM | ||
end | ||
|
||
def encode_openssh_private_key(private_key, comment = '') | ||
public_key = private_key.public_key | ||
private_key_data = [ | ||
(SecureRandom.random_bytes(4) * 2), | ||
public_key.rfc4253, | ||
encode_string(private_key.ed25519_key.seed + public_key.ed25519_key.to_str), | ||
encode_string(comment), | ||
].join | ||
unpadded = private_key_data.bytesize % 8 | ||
private_key_data << Array(1..(8 - unpadded)).pack('c*') unless unpadded.zero? | ||
[ | ||
::SSHData::Encoding::OPENSSH_PRIVATE_KEY_MAGIC, | ||
encode_string('none'), | ||
encode_string('none'), | ||
encode_string(''), | ||
encode_uint32(1), | ||
encode_string(public_key.rfc4253), | ||
encode_string(private_key_data), | ||
].join | ||
end | ||
end | ||
|
||
# Add method to emit OpenSSH-encoded string | ||
module Ed25519PrivateKeyPatch | ||
def openssh(comment: '') | ||
encoded_key = ::SSHData::Encoding.encode_openssh_private_key( | ||
self, | ||
comment | ||
) | ||
::SSHData::Encoding.encode_pem( | ||
encoded_key, | ||
'OPENSSH PRIVATE KEY' | ||
) | ||
end | ||
end | ||
end | ||
end | ||
|
||
if defined?(SSHData) | ||
SSHData::Encoding.extend BeakerHcloud::SSHDataPatches::EncodingPatch | ||
SSHData::PrivateKey::ED25519.prepend BeakerHcloud::SSHDataPatches::Ed25519PrivateKeyPatch | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'hcloud' | ||
require 'ed25519' | ||
require 'bcrypt_pbkdf' | ||
|
||
require_relative '../../beaker-hcloud/ssh_data_patches' | ||
|
||
module Beaker | ||
# beaker extenstion to manage cloud instances from https://www.hetzner.com/cloud | ||
# beaker extension to manage cloud instances from https://www.hetzner.com/cloud | ||
class Hcloud < Beaker::Hypervisor | ||
# @param [Host, Array<Host>, String, Symbol] hosts One or more hosts to act upon, or a role (String or Symbol) that identifies one or more hosts. | ||
# @param [Hash{Symbol=>String}] options Options to pass on to the hypervisor | ||
def initialize(hosts, options) # rubocop:disable Lint/MissingSuper | ||
require 'hcloud' | ||
@options = options | ||
@logger = options[:logger] || Beaker::Logger.new | ||
@hosts = hosts | ||
|
||
raise 'You need to pass a token as HCLOUD_TOKEN environment variable' unless ENV['HCLOUD_TOKEN'] | ||
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('HCLOUD_TOKEN')) | ||
@client = ::Hcloud::Client.new(token: ENV.fetch('BEAKER_HCLOUD_TOKEN')) | ||
end | ||
|
||
def provision | ||
@logger.notify 'Provisioning docker' | ||
@logger.notify 'Provisioning hcloud' | ||
create_ssh_key | ||
@hosts.each do |host| | ||
@logger.notify "provisioning #{host.name}" | ||
create_server(host) | ||
end | ||
@logger.notify 'Done provisioning hcloud' | ||
end | ||
|
||
def cleanup | ||
@logger.notify 'Cleaning up docker' | ||
@logger.notify 'Cleaning up hcloud' | ||
@hosts.each do |host| | ||
# @logger.debug("stop VM #{container.id}") | ||
@logger.debug("Deleting hcloud server #{host.name}") | ||
@client.servers.find(host[:hcloud_id]).destroy | ||
end | ||
@logger.notify 'Deleting hcloud SSH key' | ||
@client.ssh_keys.find(@options[:ssh][:hcloud_id]).destroy | ||
File.unlink(@key_file.path) | ||
@logger.notify 'Done cleaning up hcloud' | ||
end | ||
|
||
private | ||
|
||
def ssh_key_name | ||
safe_hostname = Socket.gethostname.gsub('.', '-') | ||
[ | ||
'Beaker', | ||
ENV.fetch('USER', nil), | ||
safe_hostname, | ||
@options[:aws_keyname_modifier], | ||
@options[:timestamp].strftime('%F_%H_%M_%S_%N'), | ||
].join('-') | ||
end | ||
|
||
def create_ssh_key | ||
@logger.notify 'Generating SSH keypair' | ||
ssh_key = SSHData::PrivateKey::ED25519.generate | ||
@key_file = Tempfile.create(ssh_key_name) | ||
File.write(@key_file.path, ssh_key.openssh(comment: ssh_key_name)) | ||
@logger.notify 'Creating hcloud SSH key' | ||
hcloud_ssh_key = @client.ssh_keys.create( | ||
name: ssh_key_name, | ||
public_key: ssh_key.public_key.openssh(comment: ssh_key_name) | ||
) | ||
@options[:ssh][:hcloud_id] = hcloud_ssh_key.id | ||
hcloud_ssh_key | ||
end | ||
|
||
def create_server(host) | ||
@logger.notify "provisioning #{host.name}" | ||
location = host[:location] || 'nbg1' | ||
server_type = host[:server_type] || 'cx11' | ||
action, server = @client.servers.create( | ||
name: host.hostname, | ||
location: location, | ||
server_type: server_type, | ||
image: host[:image], | ||
ssh_keys: [ssh_key_name] | ||
) | ||
while action.status == 'running' | ||
sleep 5 | ||
action = @client.actions.find(action.id) | ||
server = @client.servers.find(server.id) | ||
end | ||
host[:ip] = server.public_net['ipv4']['ip'] | ||
host[:vmhostname] = server.public_net['ipv4']['dns_ptr'] | ||
host[:hcloud_id] = server.id | ||
host.options[:ssh][:keys] = [@key_file.path] | ||
server | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bit dissapointing that the ed25519 isn't sufficient here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, the
ed25519
gem is only about the algorithm itself. It knows nothing about SSH. The (to me) disappointing bit was that no existing gem seems to support the key generation / serialization of ed25519 keys.net-ssh
does not include key generation at all. There is a gem calledsshkey
which would be perfect but only supports RSA/DSA (with partial support for reading ed25519 keys).ssh_data
was the closest thing I could find for what we need, but it still lacked the serialization of the private key.