Skip to content
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

Add parallel instance creation opt-in option #20

Merged
merged 18 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ CONFIG:
openstack_volume_support: <true/false>
security_group: ['default']
preserve_hosts: <always/onfail/onpass/never>
create_in_parallel: true
run_in_parallel: ['configure', 'install']
type: <foss/git/pe>
```
Expand All @@ -154,6 +155,11 @@ Further, you can opt to use a static master by setting the master's hypervisor t
ip: <master_ip>
```

Additionally, you can set instance creation to occur in parallel instead of sequentially via this CONFIG entry:
```yaml
create_in_parallel: true
```

Additional parameter information is available at https://github.com/voxpupuli/beaker/blob/master/docs/concepts/argument_processing_and_precedence.md

There is a simple rake task to invoke acceptance test for the library once the two environment variables are set:
Expand Down
166 changes: 103 additions & 63 deletions lib/beaker/hypervisor/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,83 +250,123 @@ def get_floating_ip
ip
end

#Create new instances in OpenStack
# Create new instances in OpenStack, depending on if create_in_parallel is true or not
def provision
@logger.notify "Provisioning OpenStack"
if @options[:create_in_parallel]
# Enable abort on exception for threads
Thread.abort_on_exception = true
puts "\nCreating instances in parallel\n\n"
provision_parallel
else
puts "\nCreating instances sequentially\n\n"
provision_sequential
end
end

@hosts.each do |host|
if @options[:openstack_floating_ip]
ip = get_floating_ip
hostname = ip.ip.gsub('.','-')
host[:vmhostname] = hostname + '.rfc1918.puppetlabs.net'
else
hostname = ('a'..'z').to_a.shuffle[0, 10].join
host[:vmhostname] = hostname
end

create_or_associate_keypair(host, hostname)
@logger.debug "Provisioning #{host.name} (#{host[:vmhostname]})"
options = {
:flavor_ref => flavor(host[:flavor]).id,
:image_ref => image(host[:image]).id,
:nics => [ {'net_id' => network(@options[:openstack_network]).id } ],
:name => host[:vmhostname],
:hostname => host[:vmhostname],
:user_data => host[:user_data] || "#cloud-config\nmanage_etc_hosts: true\n",
:key_name => host[:keyname],
}
options[:security_groups] = security_groups(@options[:security_group]) unless @options[:security_group].nil?
vm = @compute_client.servers.create(options)

#wait for the new instance to start up
try = 1
attempts = @options[:timeout].to_i / SLEEPWAIT

while try <= attempts
# Parallel creation wrapper
def provision_parallel
# Array to store threads
threads = @hosts.map do |host|
Thread.new do
begin
canihavethisone marked this conversation as resolved.
Show resolved Hide resolved
vm.wait_for(5) { ready? }
break
rescue Fog::Errors::TimeoutError => e
if try >= attempts
@logger.debug "Failed to connect to new OpenStack instance #{host.name} (#{host[:vmhostname]})"
raise e
end
@logger.debug "Timeout connecting to instance #{host.name} (#{host[:vmhostname]}), trying again..."
create_instance_resources(host)
rescue => e
# Handle exceptions in the thread
message = "Thread #{host} failed with error: #{e.message}"
puts message; $stderr.puts message
canihavethisone marked this conversation as resolved.
Show resolved Hide resolved
end
sleep SLEEPWAIT
try += 1
end
end

if @options[:openstack_floating_ip]
# Associate a public IP to the VM
ip.server = vm
host[:ip] = ip.ip
else
# Get the first address of the VM that was just created just like in the
# OpenStack UI
host[:ip] = vm.addresses.first[1][0]["addr"]
# Wait for all threads to finish
threads.each(&:join)
end

# Sequential creation wrapper
def provision_sequential
@hosts.each do |host|
create_instance_resources(host)
end
end

# Create the actual instance resources
def create_instance_resources(host)
@logger.notify "Provisioning OpenStack"
if @options[:openstack_floating_ip]
ip = get_floating_ip
hostname = ip.ip.gsub('.', '-')
host[:vmhostname] = hostname + '.rfc1918.puppetlabs.net'
else
hostname = ('a'..'z').to_a.shuffle[0, 10].join
host[:vmhostname] = hostname
end

create_or_associate_keypair(host, hostname)
@logger.debug "Provisioning #{host.name} (#{host[:vmhostname]})"
options = {
:flavor_ref => flavor(host[:flavor]).id,
:image_ref => image(host[:image]).id,
:nics => [{'net_id' => network(@options[:openstack_network]).id}],
:name => host[:vmhostname],
:hostname => host[:vmhostname],
:user_data => host[:user_data] || "#cloud-config\nmanage_etc_hosts: true\n",
:key_name => host[:keyname],
}
options[:security_groups] = security_groups(@options[:security_group]) unless @options[:security_group].nil?
vm = @compute_client.servers.create(options)

# Wait for the new instance to start up
try = 1
attempts = @options[:timeout].to_i / SLEEPWAIT

while try <= attempts
begin
vm.wait_for(5) { ready? }
break
rescue Fog::Errors::TimeoutError => e
if try >= attempts
@logger.debug "Failed to connect to new OpenStack instance #{host.name} (#{host[:vmhostname]})"
raise e
end
@logger.debug "Timeout connecting to instance #{host.name} (#{host[:vmhostname]}), trying again..."
end
sleep SLEEPWAIT
try += 1
end

@logger.debug "OpenStack host #{host.name} (#{host[:vmhostname]}) assigned ip: #{host[:ip]}"
if @options[:openstack_floating_ip]
# Associate a public IP to the VM
ip.server = vm
host[:ip] = ip.ip
else
# Get the first address of the VM that was just created just like in the
# OpenStack UI
host[:ip] = vm.addresses.first[1][0]["addr"]
end

#set metadata
vm.metadata.update({:jenkins_build_url => @options[:jenkins_build_url].to_s,
:department => @options[:department].to_s,
:project => @options[:project].to_s })
@vms << vm
@logger.debug "OpenStack host #{host.name} (#{host[:vmhostname]}) assigned ip: #{host[:ip]}"

# Set metadata
vm.metadata.update({:jenkins_build_url => @options[:jenkins_build_url].to_s,
:department => @options[:department].to_s,
:project => @options[:project].to_s })
@vms << vm

# Wait for the host to accept ssh logins
host.wait_for_port(22)
# Wait for the host to accept SSH logins
host.wait_for_port(22)

#enable root if user is not root
enable_root(host)
# Enable root if the user is not root
enable_root(host)

provision_storage(host, vm) if @options[:openstack_volume_support]
@logger.notify "OpenStack Volume Support Disabled, can't provision volumes" if not @options[:openstack_volume_support]
end
provision_storage(host, vm) if @options[:openstack_volume_support]
@logger.notify "OpenStack Volume Support Disabled, can't provision volumes" if not @options[:openstack_volume_support]
rescue => e
# Handle exceptions in the thread
message = "Thread #{host} failed with error: #{e.message}"
puts message; $stderr.puts message
canihavethisone marked this conversation as resolved.
Show resolved Hide resolved

hack_etc_hosts @hosts, @options
canihavethisone marked this conversation as resolved.
Show resolved Hide resolved

end

#Destroy any OpenStack instances
Expand Down Expand Up @@ -411,4 +451,4 @@ def create_or_associate_keypair(host, keyname)
host[:keyname] = keyname
end
end
end
end
canihavethisone marked this conversation as resolved.
Show resolved Hide resolved
Loading