(DIO-2768) Initial gce provider

This commit is contained in:
Samuel Beaulieu 2021-12-01 16:16:40 -06:00
commit 588e29b6e1
No known key found for this signature in database
GPG key ID: 12030F74136D0F34
11 changed files with 663 additions and 0 deletions

8
.github/dependabot.yml vendored Normal file
View file

@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: bundler
directory: "/"
schedule:
interval: daily
time: "13:00"
open-pull-requests-limit: 10

47
.github/workflows/testing.yml vendored Normal file
View file

@ -0,0 +1,47 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
name: Testing
on:
pull_request:
branches:
- main
jobs:
rubocop:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version:
- '2.5.8'
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run Rubocop
run: bundle exec rake rubocop
spec_tests:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version:
- '2.5.8'
- 'jruby-9.2.12.0'
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run spec tests
run: bundle exec rake test

10
.gitignore vendored Normal file
View file

@ -0,0 +1,10 @@
.bundle/
.vagrant/
coverage/
vendor/
.dccache
.ruby-version
Gemfile.local
results.xml
/vmpooler.yaml
.idea

10
CODEOWNERS Normal file
View file

@ -0,0 +1,10 @@
# This will cause DIO to be assigned review of any opened PRs against
# the branches containing this file.
# See https://help.github.com/en/articles/about-code-owners for info on how to
# take ownership of parts of the code base that should be reviewed by another
# team.
# DIO will be the default owners for everything in the repo.
* @puppetlabs/dio

13
Gemfile Normal file
View file

@ -0,0 +1,13 @@
source ENV['GEM_SOURCE'] || 'https://rubygems.org'
gemspec
# Evaluate Gemfile.local if it exists
if File.exists? "#{__FILE__}.local"
instance_eval(File.read("#{__FILE__}.local"))
end
# Evaluate ~/.gemfile if it exists
if File.exists?(File.join(Dir.home, '.gemfile'))
instance_eval(File.read(File.join(Dir.home, '.gemfile')))
end

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# vmpooler-provider-gce
This is a WIP - do not use yet. Provider for GCE VMs in vmpooler.
Vm has a label 'pool' that represent the VMpooler OS/pool it is part of
Boot disk had a label 'vm' that represent the vm_name it is attached to

25
Rakefile Normal file
View file

@ -0,0 +1,25 @@
require 'rspec/core/rake_task'
rubocop_available = Gem::Specification::find_all_by_name('rubocop').any?
require 'rubocop/rake_task' if rubocop_available
desc 'Run rspec tests with coloring.'
RSpec::Core::RakeTask.new(:test) do |t|
t.rspec_opts = %w[--color --format documentation]
t.pattern = 'spec/'
end
desc 'Run rspec tests and save JUnit output to results.xml.'
RSpec::Core::RakeTask.new(:junit) do |t|
t.rspec_opts = %w[-r yarjuf -f JUnit -o results.xml]
t.pattern = 'spec/'
end
if rubocop_available
desc 'Run RuboCop'
RuboCop::RakeTask.new(:rubocop) do |task|
task.options << '--display-cop-names'
end
end
task :default => [:test]

View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
module VmpoolerProviderGce
VERSION = '0.1.0'
end

View file

@ -0,0 +1,357 @@
# frozen_string_literal: true
require 'googleauth'
require 'google/apis/compute_v1'
require 'bigdecimal'
require 'bigdecimal/util'
require 'vmpooler/providers/base'
module Vmpooler
class PoolManager
class Provider
class Gce < Vmpooler::PoolManager::Provider::Base
# The connection_pool method is normally used only for testing
attr_reader :connection_pool
def initialize(config, logger, metrics, redis_connection_pool, name, options)
super(config, logger, metrics, redis_connection_pool, name, options)
task_limit = global_config[:config].nil? || global_config[:config]['task_limit'].nil? ? 10 : global_config[:config]['task_limit'].to_i
# The default connection pool size is:
# Whatever is biggest from:
# - How many pools this provider services
# - Maximum number of cloning tasks allowed
# - Need at least 2 connections so that a pool can have inventory functions performed while cloning etc.
default_connpool_size = [provided_pools.count, task_limit, 2].max
connpool_size = provider_config['connection_pool_size'].nil? ? default_connpool_size : provider_config['connection_pool_size'].to_i
# The default connection pool timeout should be quite large - 60 seconds
connpool_timeout = provider_config['connection_pool_timeout'].nil? ? 60 : provider_config['connection_pool_timeout'].to_i
logger.log('d', "[#{name}] ConnPool - Creating a connection pool of size #{connpool_size} with timeout #{connpool_timeout}")
@connection_pool = Vmpooler::PoolManager::GenericConnectionPool.new(
metrics: metrics,
connpool_type: 'provider_connection_pool',
connpool_provider: name,
size: connpool_size,
timeout: connpool_timeout
) do
logger.log('d', "[#{name}] Connection Pool - Creating a connection object")
# Need to wrap the vSphere connection object in another object. The generic connection pooler will preserve
# the object reference for the connection, which means it cannot "reconnect" by creating an entirely new connection
# object. Instead by wrapping it in a Hash, the Hash object reference itself never changes but the content of the
# Hash can change, and is preserved across invocations.
new_conn = connect_to_gce
{ connection: new_conn }
end
@redis = redis_connection_pool
end
# name of the provider class
def name
'gce'
end
# main configuration options
def project
provider_config['project']
end
def network_name
provider_config['network_name']
end
# main configuration options, overridable for each pool
def zone(pool_name)
return pool_config(pool_name)['zone'] if pool_config(pool_name)['zone']
return provider_config['zone'] if provider_config['zone']
end
def machine_type(pool_name)
return pool_config(pool_name)['machine_type'] if pool_config(pool_name)['machine_type']
return provider_config['machine_type'] if provider_config['machine_type']
end
#Base methods that are implemented:
#
#
def vms_in_pool(pool_name)
vms = []
pool = pool_config(pool_name)
raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil?
zone = zone(pool_name)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
filter = "(labels.pool = #{pool_name})"
instance_list = connection.list_instances(project, zone, filter: filter)
return vms if instance_list.items.nil?
instance_list.items.each do |vm|
vms << { 'name' => vm.name }
end
end
vms
end
# inputs
# [String]pool_name : Name of the pool
# [String] vm_name : Name of the VM
# returns
# [String] : Name of the host computer running the vm. If this is not a Virtual Machine, it returns the vm_name
def get_vm_host(_pool_name, _vm_name)
raise("#{self.class.name} does not implement get_vm_host")
end
# inputs
# [String] pool_name : Name of the pool
# [String] vm_name : Name of the VM to find
# returns
# nil if VM doesn't exist
# [Hastable] of the VM
# [String] name : The name of the resource, provided by the client when initially creating the resource
# [String] hostname : Specifies the hostname of the instance. The specified hostname must be RFC1035 compliant. If hostname is not specified,
# the default hostname is [ INSTANCE_NAME].c.[PROJECT_ID].internal when using the global DNS, and
# [ INSTANCE_NAME].[ZONE].c.[PROJECT_ID].internal when using zonal DNS
# [String] template : This is the name of template exposed by the API. It must _match_ the poolname ??? TODO
# [String] poolname : Name of the pool the VM as per labels
# [Time] boottime : Time when the VM was created/booted
# [String] status : One of the following values: PROVISIONING, STAGING, RUNNING, STOPPING, SUSPENDING, SUSPENDED, REPAIRING, and TERMINATED
# [String] zone : URL of the zone where the instance resides.
# [String] machine_type : Full or partial URL of the machine type resource to use for this instance, in the format: zones/zone/machineTypes/machine-type.
def get_vm(pool_name, vm_name)
vm_hash = nil
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
vm_object = connection.get_instance(project, zone(pool_name), vm_name)
return vm_hash if vm_object.nil?
vm_hash = generate_vm_hash(vm_object, pool_name)
end
vm_hash
end
# inputs
# [String] pool : Name of the pool
# [String] new_vmname : Name to give the new VM
# returns
# [Hashtable] of the VM as per get_vm
# Raises RuntimeError if the pool_name is not supported by the Provider
def create_vm(pool_name, new_vmname)
pool = pool_config(pool_name)
raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil?
vm_hash = nil
# harcoded network info
network_interfaces = Google::Apis::ComputeV1::NetworkInterface.new(
:network => network_name
)
initParams = {
:source_image => pool['template'], #The source image to create this disk.
:labels => {'vm' => new_vmname, 'pool' => pool_name}
}
disk = Google::Apis::ComputeV1::AttachedDisk.new(
:auto_delete => true,
:boot => true,
:initialize_params => Google::Apis::ComputeV1::AttachedDiskInitializeParams.new(initParams)
)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
# Assume all pool config is valid i.e. not missing
template_path = pool['template']
client = ::Google::Apis::ComputeV1::Instance.new(
:name => new_vmname,
:machine_type => pool['machine_type'],
:disks => [disk],
:network_interfaces => [network_interfaces],
:labels => {'pool' => pool_name}
)
result = connection.insert_instance(project, zone(pool_name), client)
result = wait_for_operation(project, pool_name, result, connection)
if result.error
error_message = ""
# array of errors, combine them all
result.error.each do |error|
error_message = "#{error_message} #{error.code}:#{error.message}"
end
raise "Pool #{pool_name} operation: #{result.description} failed with error: #{error_message}"
end
vm_hash = get_vm(pool_name, new_vmname)
end
vm_hash
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
end
#TODO
def create_disk(pool_name, vm_name, disk_size)
pool = pool_config(pool_name)
raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil?
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
vm_object = find_vm(pool_name, vm_name, connection)
raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil?
#TODO part 2
end
true
end
def create_snapshot(pool_name, vm_name, new_snapshot_name)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
vm_object = find_vm(pool_name, vm_name, connection)
raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil?
old_snap = find_snapshot(vm_object, new_snapshot_name)
raise("Snapshot #{new_snapshot_name} for VM #{vm_name} in pool #{pool_name} already exists for the provider #{name}") unless old_snap.nil?
snapshot_obj = ::Google::Apis::ComputeV1::Snapshot.new(name: "#{new_snapshot_name}_boot", labels: {"snapshot_name" => new_snapshot_name, "vm" => vm_name})
boot_disk = vm_object.disks[0]
result = connection.create_disk_snapshot(project, zone(pool_name), boot_disk, snapshot_obj)
wait_for_operation(project, pool_name, result, connection)
#TODO snapshot other disks if there are any
end
true
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
end
#TODO
def revert_snapshot(pool_name, vm_name, snapshot_name)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
vm_object = find_vm(pool_name, vm_name, connection)
raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil?
snapshot_object = find_snapshot(vm_object, snapshot_name)
raise("Snapshot #{snapshot_name} for VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if snapshot_object.nil?
#TODO part 2
end
true
end
def destroy_vm(pool_name, vm_name)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
vm_object = find_vm(pool_name, vm_name, connection)
# If a VM doesn't exist then it is effectively deleted
return true if vm_object.nil?
start = Time.now
result = connection.delete_instance(project, @options[:vm_zone], name)
wait_for_operation(project, pool_name, result, connection)
finish = format('%<time>.2f', time: Time.now - start)
logger.log('s', "[-] [#{pool}] '#{vm_name}' destroyed in #{finish} seconds")
metrics.timing("destroy.#{pool}", finish)
end
true
rescue ::Google::Apis::ClientError => e
raise e unless e.status_code == 404
nil
end
def vm_ready?(_pool_name, vm_name)
begin
open_socket(vm_name, global_config[:config]['domain'])
rescue StandardError => _e
return false
end
true
end
#TODO
def purge_unconfigured_folders(base_folders, configured_folders, whitelist)
@connection_pool.with_metrics do |pool_object|
connection = ensured_gce_connection(pool_object)
#TODO: part 2 use labels that are not configured
end
end
# END BASE METHODS
# Compute resource wait for operation to be DONE (synchronous operation)
def wait_for_operation(project, pool_name, result, connection)
retries = 5
while result.status != 'DONE' && retries > 0
retries = retries - 1
result = connection.wait_zone_operation(project, zone(pool_name), result.name)
end
result
end
# Return a hash of VM data
# Provides vmname, hostname, template, poolname, boottime, status, zone, machine_type information
def generate_vm_hash(vm_object, pool_name)
pool_configuration = pool_config(pool_name)
return nil if pool_configuration.nil?
{
'name' => vm_object.name,
'hostname' => vm_object.hostname,
'template' => pool_configuration && pool_configuration.key?('template') ? pool_configuration['template'] : nil, #TODO: get it from the API, not from config, but this is what vSphere does too!
'poolname' => vm_object.labels && vm_object.labels.key?('pool') ? vm_object.labels['pool'] : nil,
'boottime' => vm_object.creation_timestamp,
'status' => vm_object.status, # One of the following values: PROVISIONING, STAGING, RUNNING, STOPPING, SUSPENDING, SUSPENDED, REPAIRING, and TERMINATED
'zone' => vm_object.zone,
'machine_type' => vm_object.machine_type
#'powerstate' => powerstate
}
end
def ensured_gce_connection(connection_pool_object)
connection_pool_object[:connection] = connect_to_gce unless connection_pool_object[:connection]
connection_pool_object[:connection]
end
def connect_to_gce
max_tries = global_config[:config]['max_tries'] || 3
retry_factor = global_config[:config]['retry_factor'] || 10
try = 1
begin
scopes = ["https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/cloud-platform"]
authorization = Google::Auth.get_application_default(scopes)
compute = ::Google::Apis::ComputeV1::ComputeService.new
compute.authorization = authorization
metrics.increment('connect.open')
compute
rescue StandardError => e
metrics.increment('connect.fail')
raise e if try >= max_tries
sleep(try * retry_factor)
try += 1
retry
end
end
# This should supercede the open_socket method in the Pool Manager
def open_socket(host, domain = nil, timeout = 5, port = 22, &_block)
Timeout.timeout(timeout) do
target_host = host
target_host = "#{host}.#{domain}" if domain
sock = TCPSocket.new target_host, port
begin
yield sock if block_given?
ensure
sock.close
end
end
end
def find_snapshot(vm, snapshotname)
#TODO
end
end
end
end
end

View file

@ -0,0 +1,33 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'vmpooler-provider-gce/version'
Gem::Specification.new do |s|
s.name = 'vmpooler-provider-gce'
s.version = VmpoolerProviderGce::VERSION
s.authors = ['Puppet']
s.email = ['support@puppet.com']
s.summary = 'GCE provider for VMPooler'
s.homepage = 'https://github.com/puppetlabs/vmpooler-provider-gce'
s.license = 'Apache-2.0'
s.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
s.files = Dir[ "lib/**/*" ]
s.require_paths = ["lib"]
s.add_dependency "google-apis-compute_v1", "~> 0.14"
s.add_dependency "googleauth", "~> 0.16.2"
s.add_development_dependency 'vmpooler', '~> 1.3', '>= 1.3.0'
# Testing dependencies
s.add_development_dependency 'climate_control', '>= 0.2.0'
s.add_development_dependency 'mock_redis', '>= 0.17.0'
s.add_development_dependency 'pry'
s.add_development_dependency 'rack-test', '>= 0.6'
s.add_development_dependency 'rspec', '>= 3.2'
s.add_development_dependency 'rubocop', '~> 1.1.0'
#s.add_development_dependency 'simplecov', '>= 0.11.2'
s.add_development_dependency 'thor', '~> 1.0', '>= 1.0.1'
s.add_development_dependency 'yarjuf', '>= 2.0'
end

149
vmpooler.yaml.example Normal file
View file

@ -0,0 +1,149 @@
---
:providers:
# :providers:
#
# This section contains the VM providers for VMs and Pools
# The currently supported backing services are:
# - vsphere
# - dummy
# - gce
#
# - provider_class
# For multiple providers, specify one of the supported backing services (vsphere or dummy or gce)
# (optional: will default to it's parent :key: name eg. 'gce')
#
# - purge_unconfigured_resources
# Enable purging of VMs (folders or resources) detected
# vSphere: within the base folder path that are not configured for the provider.
# Only a single layer of folders and their child VMs are evaluated from detected base folder paths
# Nested child folders will not be destroyed.
# A base folder path for 'vmpooler/redhat-7' would be 'vmpooler'
#
# gce: in the project but without a pool label, or a pool label for an nonexistant pool
#
# An optional allowlist can be provided to ignore purging certain folders or pool labels
#
# Setting this on the provider will enable purging for the provider
# Expects a boolean value
# (optional; default: false)
#
# - recources_allowlist
# vSphere: Specify folders that are within the base folder path, not in the configuration, and should not be destroyed
# To exclude 'vmpooler/myfolder' from being destroyed when the base path is 'vmpooler' you would specify 'myfolder' in the allowlist
# gce: Specify pool labels that should be ignored when purging VMs, for pools that are not configured. For example if the pool label is
# set to 'donotdelete' and there is no pool with that name configured, adding that value to the allowlist would not purge the VM.
# This option is only evaluated when 'purge_unconfigured_folders' is enabled
# Expects an array of strings specifying the allowlisted folders by name
# (optional; default: nil)
#
# If you want to support more than one provider with different parameters (server, username or passwords) you have to specify the
# backing service in the provider_class configuration parameter for example 'vsphere' or 'dummy'. Each pool can specify
# the provider to use.
#
# Multiple providers example:
:gce1:
provider_class: 'gce'
project: 'myproject'
zone: 'us-central1-f'
:gce2:
provider_class: 'gce'
project: 'myproject-foo'
zone: 'us-central1-f'
# :gce:
#
# This section contains the global variables for the gce provider
# some of them can be overwritten at the pool level
#
# Available configuration parameters:
#
# - project
# The GCE project name to use when creating/deleting resources
# (required)
# - zone
# The GCE zone name to use when creating/deleting resources (vms, disks etc)
# Can be overwritten at the pool level
# (required)
# - machine_type
# Full or partial URL of the machine type resource to use for this instance, in the format: zones/zone/machineTypes/machine-type
# (required)
# - network_name
# The GCE network_name to use
# (required)
# Example:
:gce:
project: 'myproject'
zone: 'us-central1-f'
machine_type: ''
network_name: ''
# :pools:
#
# This section contains a list of virtual machine 'pools' for vmpooler to
# create and maintain.
#
# Available configuration parameters (per-pool):
#
# - name
# The name of the pool.
# (required)
#
# - alias
# Other names this pool can be requested as.
# (optional)
#
# - template
# The template or virtual machine target to spawn clones from.
# (required)
#
# - size
# The number of waiting VMs to keep in a pool.
# (required)
#
# - provider
# The name of the VM provider which manage this pool. This should match
# a name in the :providers: section above e.g. vsphere
# (required; will default to vsphere for backwards compatibility)
# If you have more than one provider, this is where you would choose which
# one to use for this pool
#
# - clone_target
# Per-pool option to override the global 'clone_target' cluster.
# (optional)
#
# - timeout
# How long (in minutes) before marking a clone in 'pending' queues as 'failed' and retrying.
# This setting overrides any globally-configured timeout setting.
# (optional; default: '15')
#
# - ready_ttl
# How long (in minutes) to keep VMs in 'ready' queues before destroying.
# (optional; default: no limit)
#
# - check_loop_delay_min (optional; default: 5) seconds
# - check_loop_delay_max (optional; default: same as check_loop_delay_min) seconds
# - check_loop_delay_decay (optional; default: 2.0) Must be greater than 1.0
# See the :config: section for information about these settings
#
# Provider specific pool settings
#
# Gce provider
# - zone
# The zone to create the VMs in
# (optional: default is global provider zone value)
# - machine_type
# Full or partial URL of the machine type resource to use for this instance, in the format: zones/zone/machineTypes/machine-type
# Example:
:pools:
- name: 'debian-8-x86_64'
alias: [ 'debian-8-64', 'debian-8-amd64' ]
template: 'global/images/my-custom-image'
size: 5
timeout: 15
ready_ttl: 1440
provider: gce
zone: 'us-new-zone'
machine_type: 'zones/us-central1-f/machineTypes/n1-standard-1'