-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from oneiros/issue-10
Initial implementation of hypervisor #10
- Loading branch information
Showing
8 changed files
with
360 additions
and
18 deletions.
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.