mirror of
https://github.com/puppetlabs/vmpooler-provider-ec2.git
synced 2026-01-26 02:28:40 -05:00
create_vm method implimented
This commit is contained in:
parent
6fbca839ed
commit
72260b719d
2 changed files with 71 additions and 69 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
require 'bigdecimal'
|
require 'bigdecimal'
|
||||||
require 'bigdecimal/util'
|
require 'bigdecimal/util'
|
||||||
require 'vmpooler/providers/base'
|
require 'vmpooler/providers/base'
|
||||||
|
require 'aws-sdk-ec2'
|
||||||
|
|
||||||
module Vmpooler
|
module Vmpooler
|
||||||
class PoolManager
|
class PoolManager
|
||||||
|
|
@ -14,7 +15,10 @@ module Vmpooler
|
||||||
|
|
||||||
def initialize(config, logger, metrics, redis_connection_pool, name, options)
|
def initialize(config, logger, metrics, redis_connection_pool, name, options)
|
||||||
super(config, logger, metrics, redis_connection_pool, name, options)
|
super(config, logger, metrics, redis_connection_pool, name, options)
|
||||||
|
|
||||||
|
@aws_access_key = ENV['ABS_AWS_ACCESS_KEY']
|
||||||
|
@aws_secret_key = ENV['ABS_AWS_SECRET_KEY']
|
||||||
|
|
||||||
task_limit = global_config[:config].nil? || global_config[:config]['task_limit'].nil? ? 10 : global_config[:config]['task_limit'].to_i
|
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:
|
# The default connection pool size is:
|
||||||
# Whatever is biggest from:
|
# Whatever is biggest from:
|
||||||
|
|
@ -79,6 +83,10 @@ module Vmpooler
|
||||||
return provider_config['zone'] if provider_config['zone']
|
return provider_config['zone'] if provider_config['zone']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def region
|
||||||
|
return provider_config['region'] if provider_config['region']
|
||||||
|
end
|
||||||
|
|
||||||
def machine_type(pool_name)
|
def machine_type(pool_name)
|
||||||
return pool_config(pool_name)['machine_type'] if pool_config(pool_name)['machine_type']
|
return pool_config(pool_name)['machine_type'] if pool_config(pool_name)['machine_type']
|
||||||
return provider_config['machine_type'] if provider_config['machine_type']
|
return provider_config['machine_type'] if provider_config['machine_type']
|
||||||
|
|
@ -172,38 +180,60 @@ module Vmpooler
|
||||||
debug_logger('create_vm')
|
debug_logger('create_vm')
|
||||||
pool = pool_config(pool_name)
|
pool = pool_config(pool_name)
|
||||||
raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil?
|
raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil?
|
||||||
|
|
||||||
|
if zone(pool_name) == 'us-west-2b'
|
||||||
|
subnet_id = 'subnet-0fe90a688844f6f26'
|
||||||
|
else
|
||||||
|
subnet_id = 'subnet-091b436f'
|
||||||
|
end
|
||||||
|
tag = [
|
||||||
|
{
|
||||||
|
resource_type: "instance", # accepts capacity-reservation, client-vpn-endpoint, customer-gateway, carrier-gateway, dedicated-host, dhcp-options, egress-only-internet-gateway, elastic-ip, elastic-gpu, export-image-task, export-instance-task, fleet, fpga-image, host-reservation, image, import-image-task, import-snapshot-task, instance, instance-event-window, internet-gateway, ipam, ipam-pool, ipam-scope, ipv4pool-ec2, ipv6pool-ec2, key-pair, launch-template, local-gateway, local-gateway-route-table, local-gateway-virtual-interface, local-gateway-virtual-interface-group, local-gateway-route-table-vpc-association, local-gateway-route-table-virtual-interface-group-association, natgateway, network-acl, network-interface, network-insights-analysis, network-insights-path, network-insights-access-scope, network-insights-access-scope-analysis, placement-group, prefix-list, replace-root-volume-task, reserved-instances, route-table, security-group, security-group-rule, snapshot, spot-fleet-request, spot-instances-request, subnet, subnet-cidr-reservation, traffic-mirror-filter, traffic-mirror-session, traffic-mirror-target, transit-gateway, transit-gateway-attachment, transit-gateway-connect-peer, transit-gateway-multicast-domain, transit-gateway-route-table, volume, vpc, vpc-endpoint, vpc-endpoint-service, vpc-peering-connection, vpn-connection, vpn-gateway, vpc-flow-log
|
||||||
|
tags: [
|
||||||
|
{
|
||||||
|
key: "lifetime",
|
||||||
|
value: "1d",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "created_by",
|
||||||
|
value: "Tanisha",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "job_url",
|
||||||
|
value: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "organization",
|
||||||
|
value: "engineering",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "portfolio",
|
||||||
|
value: "ds-ci",
|
||||||
|
},
|
||||||
|
|
||||||
# harcoded network info
|
],
|
||||||
network_interfaces = Google::Apis::ComputeV1::NetworkInterface.new(
|
},
|
||||||
network: network_name
|
]
|
||||||
)
|
config = {
|
||||||
network_interfaces.subnetwork = subnetwork_name(pool_name) if subnetwork_name(pool_name)
|
min_count: 1,
|
||||||
init_params = {
|
max_count: 1,
|
||||||
source_image: pool['template'], # The source image to create this disk.
|
image_id: pool['template'],
|
||||||
labels: { 'vm' => new_vmname, 'pool' => pool_name },
|
monitoring: {:enabled => true},
|
||||||
disk_name: "#{new_vmname}-disk0"
|
key_name: 'always-be-scheduling',
|
||||||
|
security_group_ids: ['sg-697fb015'],
|
||||||
|
instance_type: pool['amisize'],
|
||||||
|
disable_api_termination: false,
|
||||||
|
instance_initiated_shutdown_behavior: 'terminate',
|
||||||
|
tag_specifications: tag,
|
||||||
|
subnet_id: subnet_id
|
||||||
}
|
}
|
||||||
disk = Google::Apis::ComputeV1::AttachedDisk.new(
|
|
||||||
auto_delete: true,
|
# if volume_size
|
||||||
boot: true,
|
# config[:block_device_mappings] = get_block_device_mappings(image_id, volume_size)
|
||||||
initialize_params: Google::Apis::ComputeV1::AttachedDiskInitializeParams.new(init_params)
|
# end
|
||||||
)
|
|
||||||
append_domain = domain || global_config[:config]['domain']
|
|
||||||
fqdn = "#{new_vmname}.#{append_domain}" if append_domain
|
|
||||||
|
|
||||||
# Assume all pool config is valid i.e. not missing
|
|
||||||
client = ::Google::Apis::ComputeV1::Instance.new(
|
|
||||||
name: new_vmname,
|
|
||||||
hostname: fqdn,
|
|
||||||
machine_type: pool['machine_type'],
|
|
||||||
disks: [disk],
|
|
||||||
network_interfaces: [network_interfaces],
|
|
||||||
labels: { 'vm' => new_vmname, 'pool' => pool_name },
|
|
||||||
tags: Google::Apis::ComputeV1::Tags.new(items: [project])
|
|
||||||
)
|
|
||||||
|
|
||||||
debug_logger('trigger insert_instance')
|
debug_logger('trigger insert_instance')
|
||||||
result = connection.insert_instance(project, zone(pool_name), client)
|
result = connection.create_instances(config)
|
||||||
wait_for_operation(project, pool_name, result)
|
wait_for_operation(project, pool_name, result)
|
||||||
created_instance = get_vm(pool_name, new_vmname)
|
created_instance = get_vm(pool_name, new_vmname)
|
||||||
dns_setup(created_instance)
|
dns_setup(created_instance)
|
||||||
|
|
@ -665,7 +695,7 @@ module Vmpooler
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensured_aws_connection(connection_pool_object)
|
def ensured_aws_connection(connection_pool_object)
|
||||||
connection_pool_object[:connection] = connect_to_gce unless connection_pool_object[:connection]
|
connection_pool_object[:connection] = connect_to_aws unless connection_pool_object[:connection]
|
||||||
connection_pool_object[:connection]
|
connection_pool_object[:connection]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -674,12 +704,11 @@ module Vmpooler
|
||||||
retry_factor = global_config[:config]['retry_factor'] || 10
|
retry_factor = global_config[:config]['retry_factor'] || 10
|
||||||
try = 1
|
try = 1
|
||||||
begin
|
begin
|
||||||
scopes = ['https://www.googleapis.com/auth/compute', 'https://www.googleapis.com/auth/cloud-platform']
|
compute = ::Aws::EC2::Resource.new(
|
||||||
|
region: region,
|
||||||
authorization = Google::Auth.get_application_default(scopes)
|
credentials: ::Aws::Credentials.new(@aws_access_key, @aws_secret_key),
|
||||||
|
log_level: :debug
|
||||||
compute = ::Google::Apis::ComputeV1::ComputeService.new
|
)
|
||||||
compute.authorization = authorization
|
|
||||||
|
|
||||||
metrics.increment('connect.open')
|
metrics.increment('connect.open')
|
||||||
compute
|
compute
|
||||||
|
|
|
||||||
|
|
@ -12,27 +12,27 @@ describe 'Vmpooler::PoolManager::Provider::Aws' do
|
||||||
let(:poolname) { 'debian-9' }
|
let(:poolname) { 'debian-9' }
|
||||||
let(:provider_options) { { 'param' => 'value' } }
|
let(:provider_options) { { 'param' => 'value' } }
|
||||||
let(:project) { 'vmpooler-test' }
|
let(:project) { 'vmpooler-test' }
|
||||||
let(:zone) { 'us-west1-b' }
|
let(:zone) { 'us-west-2b' }
|
||||||
|
let(:region) { 'us-west-2'}
|
||||||
let(:config) { YAML.load(<<~EOT
|
let(:config) { YAML.load(<<~EOT
|
||||||
---
|
---
|
||||||
:config:
|
:config:
|
||||||
max_tries: 3
|
max_tries: 3
|
||||||
retry_factor: 10
|
retry_factor: 10
|
||||||
:providers:
|
:providers:
|
||||||
:gce:
|
:aws:
|
||||||
connection_pool_timeout: 1
|
connection_pool_timeout: 1
|
||||||
project: '#{project}'
|
|
||||||
zone: '#{zone}'
|
zone: '#{zone}'
|
||||||
network_name: global/networks/default
|
region: '#{region}'
|
||||||
:pools:
|
:pools:
|
||||||
- name: '#{poolname}'
|
- name: '#{poolname}'
|
||||||
alias: [ 'mockpool' ]
|
alias: [ 'mockpool' ]
|
||||||
template: 'projects/debian-cloud/global/images/family/debian-9'
|
amisize: 'a1.large'
|
||||||
|
template: 'ami-03c1b544a7566b3e5'
|
||||||
size: 5
|
size: 5
|
||||||
timeout: 10
|
timeout: 10
|
||||||
ready_ttl: 1440
|
ready_ttl: 1440
|
||||||
provider: 'aws'
|
provider: 'aws'
|
||||||
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
|
|
||||||
EOT
|
EOT
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -51,38 +51,11 @@ EOT
|
||||||
|
|
||||||
subject { Vmpooler::PoolManager::Provider::Aws.new(config, logger, metrics, redis_connection_pool, 'aws', provider_options) }
|
subject { Vmpooler::PoolManager::Provider::Aws.new(config, logger, metrics, redis_connection_pool, 'aws', provider_options) }
|
||||||
|
|
||||||
before(:each) { allow(subject).to receive(:dns).and_return(MockDNS.new()) }
|
|
||||||
|
|
||||||
describe '#manual tests live' do
|
describe '#manual tests live' do
|
||||||
context 'in itsysops' do
|
context 'in itsysops' do
|
||||||
before(:each) { allow(subject).to receive(:dns).and_call_original }
|
before(:each) { allow(subject).to receive(:dns).and_call_original }
|
||||||
let(:vmname) { "instance-24" }
|
let(:vmname) { "instance-24" }
|
||||||
let(:project) { 'vmpooler-test' }
|
let(:project) { 'vmpooler-test' }
|
||||||
let(:config) { YAML.load(<<~EOT
|
|
||||||
---
|
|
||||||
:config:
|
|
||||||
max_tries: 3
|
|
||||||
retry_factor: 10
|
|
||||||
:providers:
|
|
||||||
:gce:
|
|
||||||
connection_pool_timeout: 1
|
|
||||||
project: '#{project}'
|
|
||||||
zone: '#{zone}'
|
|
||||||
network_name: 'projects/itsysopsnetworking/global/networks/shared1'
|
|
||||||
dns_zone_resource_name: 'test-vmpooler-puppet-net'
|
|
||||||
domain: 'test.vmpooler.puppet.net'
|
|
||||||
:pools:
|
|
||||||
- name: '#{poolname}'
|
|
||||||
alias: [ 'mockpool' ]
|
|
||||||
template: 'projects/debian-cloud/global/images/family/debian-9'
|
|
||||||
size: 5
|
|
||||||
timeout: 10
|
|
||||||
ready_ttl: 1440
|
|
||||||
provider: 'gce'
|
|
||||||
subnetwork_name: 'projects/itsysopsnetworking/regions/us-west1/subnetworks/vmpooler-test'
|
|
||||||
machine_type: 'zones/#{zone}/machineTypes/e2-micro'
|
|
||||||
EOT
|
|
||||||
) }
|
|
||||||
it 'gets a vm' do
|
it 'gets a vm' do
|
||||||
result = subject.create_vm(poolname, vmname)
|
result = subject.create_vm(poolname, vmname)
|
||||||
#result = subject.destroy_vm(poolname, vmname)
|
#result = subject.destroy_vm(poolname, vmname)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue