├── CODEOWNERS ├── .rspec ├── lib ├── beaker-vmpooler │ └── version.rb └── beaker │ └── hypervisor │ └── vmpooler.rb ├── .github ├── dependabot.yml └── workflows │ ├── testing.yml │ ├── security.yml │ └── release.yml ├── .simplecov ├── .gitignore ├── spec ├── spec_helper.rb └── beaker │ └── hypervisor │ └── vmpooler_spec.rb ├── Gemfile ├── bin └── beaker-vmpooler ├── beaker-vmpooler.gemspec ├── README.md ├── vmpooler.md ├── Rakefile └── LICENSE /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @puppetlabs/release-engineering 2 | 3 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --tty 4 | -------------------------------------------------------------------------------- /lib/beaker-vmpooler/version.rb: -------------------------------------------------------------------------------- 1 | module BeakerVmpooler 2 | VERSION = '1.4.0' 3 | end 4 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: bundler 4 | directory: "/" 5 | schedule: 6 | interval: weekly 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /.simplecov: -------------------------------------------------------------------------------- 1 | SimpleCov.configure do 2 | add_filter 'spec/' 3 | add_filter 'vendor/' 4 | add_filter do |file| 5 | file.lines_of_code < 10 6 | end 7 | end 8 | 9 | SimpleCov.start if ENV['BEAKER_VMPOOLER_COVERAGE'] 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | log/* 3 | !.gitignore 4 | junit 5 | acceptance-tests 6 | pkg 7 | Gemfile.lock 8 | options.rb 9 | test.cfg 10 | .yardoc 11 | coverage 12 | .bundle 13 | .vendor 14 | _vendor 15 | tmp/ 16 | doc 17 | # JetBrains IDEA 18 | *.iml 19 | .idea/ 20 | # rbenv file 21 | .ruby-version 22 | .ruby-gemset 23 | # Vagrant folder 24 | .vagrant/ 25 | .vagrant_files/ 26 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'simplecov' 2 | require 'rspec/its' 3 | require 'beaker' 4 | require 'beaker/hypervisor/vmpooler' 5 | 6 | # setup & require beaker's spec_helper.rb 7 | beaker_gem_spec = Gem::Specification.find_by_name('beaker') 8 | beaker_gem_dir = beaker_gem_spec.gem_dir 9 | beaker_spec_path = File.join(beaker_gem_dir, 'spec') 10 | $LOAD_PATH << beaker_spec_path 11 | require File.join(beaker_spec_path, 'spec_helper.rb') 12 | 13 | RSpec.configure do |config| 14 | config.include TestFileHelpers 15 | config.include HostHelpers 16 | end 17 | -------------------------------------------------------------------------------- /.github/workflows/testing.yml: -------------------------------------------------------------------------------- 1 | name: Testing 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | spec_tests: 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | ruby-version: 14 | - '2.7' 15 | - '3.0' 16 | - '3.1' 17 | - '3.2' 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Set up Ruby 21 | uses: ruby/setup-ruby@v1 22 | with: 23 | ruby-version: ${{ matrix.ruby-version }} 24 | bundler-cache: true # runs 'bundle install' and caches installed gems automatically 25 | - name: Run spec tests 26 | run: bundle exec rake test 27 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source ENV['GEM_SOURCE'] || "https://rubygems.org" 2 | 3 | gemspec :development_group => :acceptance_testing 4 | 5 | def location_for(place, fake_version = nil) 6 | if place =~ /^(git:[^#]*)#(.*)/ 7 | [fake_version, { :git => $1, :branch => $2, :require => false }].compact 8 | elsif place =~ /^file:\/\/(.*)/ 9 | ['>= 0', { :path => File.expand_path($1), :require => false }] 10 | else 11 | [place, { :require => false }] 12 | end 13 | end 14 | 15 | # We don't put beaker in as a test dependency because we 16 | # don't want to create a transitive dependency 17 | group :acceptance_testing do 18 | gem "beaker", *location_for(ENV['BEAKER_VERSION'] || '>= 5.0', '< 7') 19 | gem "beaker-abs" 20 | end 21 | 22 | if File.exist? "#{__FILE__}.local" 23 | eval(File.read("#{__FILE__}.local"), binding) 24 | end 25 | -------------------------------------------------------------------------------- /bin/beaker-vmpooler: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'rubygems' unless defined?(Gem) 4 | require 'beaker-vmpooler' 5 | 6 | VERSION_STRING = 7 | " 8 | _ .--. 9 | ( ` ) 10 | beaker-vmpooler .-' `--, 11 | _..----.. ( )`-. 12 | .'_|` _|` _|( .__, ) 13 | /_| _| _| _( (_, .-' 14 | ;| _| _| _| '-'__,--'`--' 15 | | _| _| _| _| | 16 | _ || _| _| _| _| %s 17 | _( `--.\\_| _| _| _|/ 18 | .-' )--,| _| _|.` 19 | (__, (_ ) )_| _| / 20 | `-.__.\\ _,--'\\|__|__/ 21 | ;____; 22 | \\YT/ 23 | || 24 | |\"\"| 25 | '==' 26 | " 27 | 28 | 29 | 30 | puts VERSION_STRING % [Beaker::DSL::Helpers::Vmpooler::Version::STRING] 31 | 32 | exit 0 33 | -------------------------------------------------------------------------------- /.github/workflows/security.yml: -------------------------------------------------------------------------------- 1 | name: Security 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | scan: 10 | name: Mend Scanning 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: checkout repo content 14 | uses: actions/checkout@v3 15 | with: 16 | fetch-depth: 1 17 | - name: setup ruby 18 | uses: ruby/setup-ruby@v1 19 | with: 20 | ruby-version: 2.7 21 | # setup a package lock if one doesn't exist, otherwise do nothing 22 | - name: check lock 23 | run: '[ -f "Gemfile.lock" ] && echo "package lock file exists, skipping" || bundle lock' 24 | # install java 25 | - uses: actions/setup-java@v3 26 | with: 27 | distribution: 'temurin' # See 'Supported distributions' for available options 28 | java-version: '17' 29 | # download mend 30 | - name: download_mend 31 | run: curl -o wss-unified-agent.jar https://unified-agent.s3.amazonaws.com/wss-unified-agent.jar 32 | - name: run mend 33 | run: java -jar wss-unified-agent.jar 34 | env: 35 | WS_APIKEY: ${{ secrets.MEND_API_KEY }} 36 | WS_WSS_URL: https://saas-eu.whitesourcesoftware.com/agent 37 | WS_USERKEY: ${{ secrets.MEND_TOKEN }} 38 | WS_PRODUCTNAME: RE 39 | WS_PROJECTNAME: ${{ github.event.repository.name }} 40 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: workflow_dispatch 4 | 5 | jobs: 6 | release: 7 | runs-on: ubuntu-latest 8 | if: github.repository == 'puppetlabs/beaker-vmpooler' 9 | steps: 10 | - uses: actions/checkout@v3 11 | - name: Get Version 12 | id: gv 13 | run: | 14 | version=$(grep VERSION lib/beaker-vmpooler/version.rb |rev |cut -d "'" -f2 |rev) 15 | echo "version=$version" >> $GITHUB_OUTPUT 16 | echo "Found version $version from lib/beaker-vmpooler/version.rb" 17 | - name: Tag Release 18 | uses: ncipollo/release-action@v1 19 | with: 20 | tag: ${{ steps.gv.outputs.version }} 21 | token: ${{ secrets.GITHUB_TOKEN }} 22 | draft: false 23 | prerelease: false 24 | generateReleaseNotes: true 25 | - name: Install Ruby 2.7 26 | uses: ruby/setup-ruby@v1 27 | with: 28 | ruby-version: '2.7' 29 | - name: Build gem 30 | run: gem build *.gemspec 31 | - name: Publish gem 32 | run: | 33 | mkdir -p $HOME/.gem 34 | touch $HOME/.gem/credentials 35 | chmod 0600 $HOME/.gem/credentials 36 | printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials 37 | gem push *.gem 38 | env: 39 | GEM_HOST_API_KEY: '${{ secrets.RUBYGEMS_AUTH_TOKEN }}' 40 | -------------------------------------------------------------------------------- /beaker-vmpooler.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $LOAD_PATH.unshift File.expand_path("../lib", __FILE__) 3 | require 'beaker-vmpooler/version' 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "beaker-vmpooler" 7 | s.version = BeakerVmpooler::VERSION 8 | s.authors = ["Rishi Javia, Kevin Imber, Tony Vu"] 9 | s.email = ["rishi.javia@puppet.com, kevin.imber@puppet.com, tony.vu@puppet.com"] 10 | s.homepage = "https://github.com/puppetlabs/beaker-vmpooler" 11 | s.summary = %q{Beaker DSL Extension Helpers!} 12 | s.description = %q{For use for the Beaker acceptance testing tool} 13 | s.license = 'Apache2' 14 | 15 | s.files = `git ls-files`.split("\n") 16 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 17 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 18 | s.require_paths = ["lib"] 19 | 20 | # Testing dependencies 21 | s.add_development_dependency 'rspec', '~> 3.0' 22 | s.add_development_dependency 'rspec-its' 23 | s.add_development_dependency 'fakefs', '~> 2.4' 24 | s.add_development_dependency 'rake', '~> 13.0' 25 | s.add_development_dependency 'simplecov' 26 | s.add_development_dependency 'pry', '~> 0.10' 27 | 28 | # Documentation dependencies 29 | s.add_development_dependency 'yard' 30 | s.add_development_dependency 'markdown' 31 | s.add_development_dependency 'thin' 32 | 33 | # Run time dependencies 34 | s.add_runtime_dependency 'stringify-hash', '~> 0.0.0' 35 | 36 | end 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # beaker-vmpooler 2 | 3 | Beaker library to use vmpooler hypervisor 4 | 5 | # How to use this wizardry 6 | 7 | This is a gem that allows you to use hosts with [vmpooler](vmpooler.md) hypervisor with [beaker](https://github.com/puppetlabs/beaker). 8 | 9 | Beaker will automatically load the appropriate hypervisors for any given hosts file, so as long as your project dependencies are satisfied there's nothing else to do. No need to `require` this library in your tests. 10 | 11 | ## With Beaker 3.x 12 | 13 | This library is included as a dependency of Beaker 3.x versions, so there's nothing to do. 14 | 15 | ## With Beaker 4.x 16 | 17 | As of Beaker 4.0, all hypervisor and DSL extension libraries have been removed and are no longer dependencies. In order to use a specific hypervisor or DSL extension library in your project, you will need to include them alongside Beaker in your Gemfile or project.gemspec. E.g. 18 | 19 | ~~~ruby 20 | # Gemfile 21 | gem 'beaker', '~>4.0' 22 | gem 'beaker-vmpooler' 23 | # project.gemspec 24 | s.add_runtime_dependency 'beaker', '~>4.0' 25 | s.add_runtime_dependency 'beaker-vmpooler' 26 | ~~~ 27 | 28 | # Spec tests 29 | 30 | Spec test live under the `spec` folder. There are the default rake task and therefore can run with a simple command: 31 | ```bash 32 | bundle exec rake test:spec 33 | ``` 34 | 35 | # Acceptance tests 36 | 37 | We run beaker's base acceptance tests with this library to see if the hypervisor is working with beaker. There is a simple rake task to invoke acceptance test for the library: 38 | ```bash 39 | bundle exec rake test:acceptance 40 | ``` 41 | 42 | # Contributing 43 | 44 | Please refer to puppetlabs/beaker's [contributing](https://github.com/puppetlabs/beaker/blob/master/CONTRIBUTING.md) guide. 45 | 46 | If you are making changes in beaker-vmpooler and simultaneously in beaker, please *comment and link* your beaker fork repo and branch name in your PR of beaker-vmpooler for testing on CI. 47 | -------------------------------------------------------------------------------- /vmpooler.md: -------------------------------------------------------------------------------- 1 | [vmpooler](https://github.com/puppetlabs/vmpooler) is a puppet-built abstraction 2 | layer over vSphere infrastructure that pools VMs to be used by beaker & other 3 | systems. 4 | 5 | beaker's vmpooler hypervisor interacts with vmpooler to get Systems Under Test 6 | (SUTs) for testing purposes. 7 | 8 | **Note** that if you're a puppet-internal user, you'll have to setup your SSH 9 | keys to communicate with vmpooler SUTs. To do that, refer to our 10 | [internal doc](https://confluence.puppetlabs.com/display/SRE/SSH+access+to+vmpooler+VMs). 11 | 12 | # Tokens 13 | 14 | Using tokens will allow you to extend your VMs lifetime, as well as interact 15 | with vmpooler and your VMs in more complex ways. You can have beaker do these 16 | same things by providing your `vmpooler_token` in the `~/.fog` file. For more 17 | info about how the `.fog` file works, please refer to the beaker 18 | [hypervisor README](https://github.com/puppetlabs/beaker/blob/master/docs/how_to/hypervisors/README.md). 19 | 20 | An example of a `.fog` file with just the vmpooler details is below: 21 | ```yaml 22 | :default: 23 | :vmpooler_token: 'randomtokentext' 24 | ``` 25 | # Additional Disks 26 | Using the vmpooler API, Beaker enables you to attach additional storage disks in the host configuration file. The disks are added at the time the VM is created. Logic for using the disk must go into your tests. 27 | 28 | Simply add the `disks` key and a list containing the sizes(in GB) of the disks you want to create and attach to that host. 29 | For example, to create 2 disks sized 8GB and 16GB to example-box: 30 | 31 | ```yaml 32 | example-box: 33 | disks: 34 | - 8 35 | - 16 36 | roles: 37 | - satellite 38 | platform: el-7-x86_64 39 | hypervisor: vmpooler 40 | template: redhat-7-x86_64 41 | ``` 42 | 43 | Users with Puppet credentials can follow our instructions for getting & using 44 | vmpooler tokens in our 45 | [internal documentation](https://confluence.puppetlabs.com/pages/viewpage.action?spaceKey=SRE&title=Generating+and+using+vmpooler+tokens). 46 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rspec/core/rake_task' 2 | require 'beaker' 3 | require 'beaker/hypervisor/vmpooler' 4 | 5 | namespace :test do 6 | 7 | namespace :spec do 8 | desc "Run spec tests" 9 | RSpec::Core::RakeTask.new(:run) do |t| 10 | t.rspec_opts = ['--color'] 11 | t.pattern = 'spec/' 12 | end 13 | end 14 | 15 | desc <<-EOS 16 | Runs the base beaker acceptance test using the hypervisor library 17 | EOS 18 | task :acceptance do 19 | # setup & load_path of beaker's acceptance base and lib directory 20 | beaker_gem_spec = Gem::Specification.find_by_name('beaker') 21 | beaker_gem_dir = beaker_gem_spec.gem_dir 22 | beaker_test_base_dir = File.join(beaker_gem_dir, 'acceptance/tests/base') 23 | beaker_hosts = ENV['BEAKER_HOSTS'] || 'redhat7-64af-64default.mdcal' 24 | load_path_option = File.join(beaker_gem_dir, 'acceptance/lib') 25 | sh("beaker", 26 | "--tests", beaker_test_base_dir, 27 | "--log-level", "verbose", 28 | "--hosts", beaker_hosts, 29 | "--load-path", load_path_option, 30 | "--keyfile", ENV['KEY'] || "#{ENV['HOME']}/.ssh/id_rsa-acceptance") 31 | end 32 | 33 | end 34 | 35 | # namespace-named default tasks. 36 | # these are the default tasks invoked when only the namespace is referenced. 37 | # they're needed because `task :default` in those blocks doesn't work as expected. 38 | task 'test:spec' => 'test:spec:run' 39 | 40 | # global defaults 41 | task :test => 'test:spec' 42 | task :default => :test 43 | 44 | ########################################################### 45 | # 46 | # Documentation Tasks 47 | # 48 | ########################################################### 49 | DOCS_DAEMON = "yard server --reload --daemon --server thin" 50 | FOREGROUND_SERVER = 'bundle exec yard server --reload --verbose --server thin lib/beaker' 51 | 52 | def running?( cmdline ) 53 | ps = `ps -ef` 54 | found = ps.lines.grep( /#{Regexp.quote( cmdline )}/ ) 55 | if found.length > 1 56 | raise StandardError, "Found multiple YARD Servers. Don't know what to do." 57 | end 58 | 59 | yes = found.empty? ? false : true 60 | return yes, found.first 61 | end 62 | 63 | def pid_from( output ) 64 | output.squeeze(' ').strip.split(' ')[1] 65 | end 66 | 67 | desc 'Start the documentation server in the foreground' 68 | task :docs => 'docs:clear' do 69 | original_dir = Dir.pwd 70 | Dir.chdir( File.expand_path(File.dirname(__FILE__)) ) 71 | sh FOREGROUND_SERVER 72 | Dir.chdir( original_dir ) 73 | end 74 | 75 | namespace :docs do 76 | 77 | desc 'Clear the generated documentation cache' 78 | task :clear do 79 | original_dir = Dir.pwd 80 | Dir.chdir( File.expand_path(File.dirname(__FILE__)) ) 81 | sh 'rm -rf docs' 82 | Dir.chdir( original_dir ) 83 | end 84 | 85 | desc 'Generate static documentation' 86 | task :gen => 'docs:clear' do 87 | original_dir = Dir.pwd 88 | Dir.chdir( File.expand_path(File.dirname(__FILE__)) ) 89 | output = `bundle exec yard doc` 90 | puts output 91 | if output =~ /\[warn\]|\[error\]/ 92 | fail "Errors/Warnings during yard documentation generation" 93 | end 94 | Dir.chdir( original_dir ) 95 | end 96 | 97 | desc 'Run the documentation server in the background, alias `bg`' 98 | task :background => 'docs:clear' do 99 | yes, output = running?( DOCS_DAEMON ) 100 | if yes 101 | puts "Not starting a new YARD Server..." 102 | puts "Found one running with pid #{pid_from( output )}." 103 | else 104 | original_dir = Dir.pwd 105 | Dir.chdir( File.expand_path(File.dirname(__FILE__)) ) 106 | sh "bundle exec #{DOCS_DAEMON}" 107 | Dir.chdir( original_dir ) 108 | end 109 | end 110 | 111 | task(:bg) { Rake::Task['docs:background'].invoke } 112 | 113 | desc 'Check the status of the documentation server' 114 | task :status do 115 | yes, output = running?( DOCS_DAEMON ) 116 | if yes 117 | pid = pid_from( output ) 118 | puts "Found a YARD Server running with pid #{pid}" 119 | else 120 | puts "Could not find a running YARD Server." 121 | end 122 | end 123 | 124 | desc "Stop a running YARD Server" 125 | task :stop do 126 | yes, output = running?( DOCS_DAEMON ) 127 | if yes 128 | pid = pid_from( output ) 129 | puts "Found a YARD Server running with pid #{pid}" 130 | `kill #{pid}` 131 | puts "Stopping..." 132 | yes, output = running?( DOCS_DAEMON ) 133 | if yes 134 | `kill -9 #{pid}` 135 | yes, output = running?( DOCS_DAEMON ) 136 | if yes 137 | puts "Could not Stop Server!" 138 | else 139 | puts "Server stopped." 140 | end 141 | else 142 | puts "Server stopped." 143 | end 144 | else 145 | puts "Could not find a running YARD Server" 146 | end 147 | end 148 | end 149 | -------------------------------------------------------------------------------- /spec/beaker/hypervisor/vmpooler_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | module Beaker 4 | describe Vmpooler do 5 | 6 | before :each do 7 | stub_const( "Net", MockNet ) 8 | allow_any_instance_of(MockNet::HTTP).to receive(:use_ssl=) 9 | allow( JSON ).to receive( :parse ) do |arg| 10 | arg 11 | end 12 | allow( Socket ).to receive( :getaddrinfo ).and_return( true ) 13 | allow_any_instance_of( Beaker::Vmpooler ).to \ 14 | receive(:load_credentials).and_return(fog_file_contents) 15 | end 16 | 17 | describe '#get_template_url' do 18 | 19 | it 'works returns the valid url when passed valid pooling_api and template name' do 20 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 21 | uri = vmpooler.get_template_url("http://pooling.com", "template") 22 | expect( uri ).to be === "http://pooling.com/vm/template" 23 | end 24 | 25 | it 'adds a missing scheme to a given URL' do 26 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 27 | uri = vmpooler.get_template_url("pooling.com", "template") 28 | expect( URI.parse(uri).scheme ).to_not be === nil 29 | end 30 | 31 | it 'raises an error on an invalid pooling api url' do 32 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 33 | expect{ vmpooler.get_template_url("pooling### ", "template")}.to raise_error ArgumentError 34 | end 35 | 36 | it 'raises an error on an invalide template name' do 37 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 38 | expect{ vmpooler.get_template_url("pooling.com", "t!e&m*p(l\\a/t e")}.to raise_error ArgumentError 39 | end 40 | end 41 | 42 | describe '#add_tags' do 43 | let(:vmpooler) { Beaker::Vmpooler.new(make_hosts({:host_tags => {'test_tag' => 'test_value'}}), make_opts) } 44 | 45 | it 'merges tags correctly' do 46 | vmpooler.instance_eval { 47 | @options = @options.merge({:project => 'vmpooler-spec'}) 48 | } 49 | host = vmpooler.instance_variable_get(:@hosts)[0] 50 | merged_tags = vmpooler.add_tags(host) 51 | expected_hash = { 52 | test_tag: 'test_value', 53 | beaker_version: Beaker::Version::STRING, 54 | project: 'vmpooler-spec' 55 | } 56 | expect(merged_tags).to include(expected_hash) 57 | end 58 | end 59 | 60 | describe '#disk_added?' do 61 | let(:vmpooler) { Beaker::Vmpooler.new(make_hosts, make_opts) } 62 | let(:response_hash_no_disk) { 63 | { 64 | "ok" => "true", 65 | "hostname" => { 66 | "template"=>"redhat-7-x86_64", 67 | "domain"=>"delivery.puppetlabs.net" 68 | } 69 | } 70 | } 71 | let(:response_hash_disk) { 72 | { 73 | "ok" => "true", 74 | "hostname" => { 75 | "disk" => [ 76 | '+16gb', 77 | '+8gb' 78 | ], 79 | "template"=>"redhat-7-x86_64", 80 | "domain"=>"delivery.puppetlabs.net" 81 | } 82 | } 83 | } 84 | it 'returns false when there is no disk' do 85 | host = response_hash_no_disk['hostname'] 86 | expect(vmpooler.disk_added?(host, "8", 0)).to be(false) 87 | end 88 | 89 | it 'returns true when there is a disk' do 90 | host = response_hash_disk["hostname"] 91 | expect(vmpooler.disk_added?(host, "16", 0)).to be(true) 92 | expect(vmpooler.disk_added?(host, "8", 1)).to be(true) 93 | end 94 | end 95 | 96 | describe "#provision" do 97 | 98 | it 'provisions hosts from the pool' do 99 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 100 | allow( vmpooler ).to receive( :require ).and_return( true ) 101 | allow( vmpooler ).to receive( :sleep ).and_return( true ) 102 | vmpooler.provision 103 | 104 | hosts = vmpooler.instance_variable_get( :@hosts ) 105 | hosts.each do | host | 106 | expect( host['vmhostname'] ).to be === 'pool' 107 | end 108 | end 109 | 110 | it 'raises an error when a host template is not found in returned json' do 111 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 112 | 113 | allow( vmpooler ).to receive( :require ).and_return( true ) 114 | allow( vmpooler ).to receive( :sleep ).and_return( true ) 115 | allow( vmpooler ).to receive( :get_host_info ).and_return( nil ) 116 | 117 | expect { 118 | vmpooler.provision 119 | }.to raise_error( RuntimeError, 120 | /Vmpooler\.provision - requested VM templates \[.*\,.*\,.*\] not available/ 121 | ) 122 | end 123 | 124 | it 'repeats asking only for failed hosts' do 125 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 126 | 127 | allow( vmpooler ).to receive( :require ).and_return( true ) 128 | allow( vmpooler ).to receive( :sleep ).and_return( true ) 129 | allow( vmpooler ).to receive( :get_host_info ).with( 130 | anything, "vm1_has_a_template" ).and_return( nil ) 131 | allow( vmpooler ).to receive( :get_host_info ).with( 132 | anything, "vm2_has_a_template" ).and_return( 'y' ) 133 | allow( vmpooler ).to receive( :get_host_info ).with( 134 | anything, "vm3_has_a_template" ).and_return( 'y' ) 135 | 136 | expect { 137 | vmpooler.provision 138 | }.to raise_error( RuntimeError, 139 | /Vmpooler\.provision - requested VM templates \[[^\,]*\] not available/ 140 | ) # should be only one item in the list, no commas 141 | end 142 | end 143 | 144 | describe "#cleanup" do 145 | 146 | it "cleans up hosts in the pool" do 147 | mock_http = MockNet::HTTP.new( "host", "port" ) 148 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 149 | vmpooler.provision 150 | vm_count = vmpooler.instance_variable_get( :@hosts ).count 151 | 152 | expect( Net::HTTP ).to receive( :new ).exactly( vm_count ).times.and_return( mock_http ) 153 | expect( mock_http ).to receive( :request ).exactly( vm_count ).times 154 | expect( Net::HTTP::Delete ).to receive( :new ).exactly( vm_count ).times 155 | expect{ vmpooler.cleanup }.to_not raise_error 156 | end 157 | end 158 | end 159 | 160 | describe Vmpooler do 161 | 162 | before :each do 163 | stub_const( "Net", MockNet ) 164 | allow( JSON ).to receive( :parse ) do |arg| 165 | arg 166 | end 167 | allow( Socket ).to receive( :getaddrinfo ).and_return( true ) 168 | end 169 | 170 | describe "#load_credentials" do 171 | 172 | it 'loads credentials from a fog file' do 173 | credentials = { :vmpooler_token => "example_token" } 174 | make_opts = { :dot_fog => '.fog' } 175 | 176 | expect_any_instance_of( Beaker::Vmpooler ).to receive( :get_fog_credentials ).and_return(credentials) 177 | 178 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 179 | expect( vmpooler.credentials ).to be == credentials 180 | end 181 | 182 | it 'continues without credentials when there are problems loading the fog file' do 183 | logger = double( 'logger' ) 184 | make_opts = { :logger => logger, :dot_fog => '.fog' } 185 | 186 | expect_any_instance_of( Beaker::Vmpooler ).to receive( :get_fog_credentials ).and_raise( ArgumentError, 'something went wrong' ) 187 | expect( logger ).to receive( :warn ).with( /Invalid credentials file.*something went wrong.*Proceeding without authentication/m ) 188 | 189 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 190 | 191 | expect( vmpooler.credentials ).to be == {} 192 | end 193 | 194 | it 'continues without credentials when fog file has no vmpooler_token' do 195 | logger = double( 'logger' ) 196 | make_opts = { :logger => logger, :dot_fog => '.fog' } 197 | 198 | expect_any_instance_of( Beaker::Vmpooler ).to receive( :get_fog_credentials ).and_return( {} ) 199 | expect( logger ).to receive( :warn ).with( /vmpooler_token not found in credentials file.*Proceeding without authentication/m ) 200 | 201 | vmpooler = Beaker::Vmpooler.new( make_hosts, make_opts ) 202 | 203 | expect( vmpooler.credentials ).to be == {} 204 | end 205 | end 206 | end 207 | end 208 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /lib/beaker/hypervisor/vmpooler.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' unless defined?(YAML) 2 | require 'json' 3 | require 'net/http' 4 | 5 | module Beaker 6 | class Vmpooler < Beaker::Hypervisor 7 | 8 | SSH_EXCEPTIONS = [ 9 | SocketError, 10 | Timeout::Error, 11 | Errno::ETIMEDOUT, 12 | Errno::EHOSTDOWN, 13 | Errno::EHOSTUNREACH, 14 | Errno::ECONNREFUSED, 15 | Errno::ECONNRESET, 16 | Errno::ENETUNREACH, 17 | ] 18 | 19 | attr_reader :options, :logger, :hosts, :credentials 20 | 21 | def initialize(vmpooler_hosts, options) 22 | @options = options 23 | @logger = options[:logger] 24 | @hosts = vmpooler_hosts 25 | @credentials = load_credentials(@options[:dot_fog]) 26 | end 27 | 28 | def load_credentials(dot_fog = '.fog') 29 | creds = {} 30 | begin 31 | fog = get_fog_credentials(dot_fog) 32 | if fog[:vmpooler_token] 33 | creds[:vmpooler_token] = fog[:vmpooler_token] 34 | else 35 | @logger.warn "vmpooler_token not found in credentials file (#{dot_fog})\nProceeding without authentication" 36 | end 37 | rescue ArgumentError => e 38 | @logger.warn "Invalid credentials file:\n(#{e.class}) #{e.message}\nProceeding without authentication" 39 | end 40 | creds 41 | end 42 | 43 | def connection_preference(host) 44 | [:vmhostname, :ip, :hostname] 45 | end 46 | 47 | def check_url url 48 | begin 49 | URI.parse(url) 50 | rescue 51 | return false 52 | end 53 | true 54 | end 55 | 56 | def get_template_url pooling_api, template 57 | if not check_url(pooling_api) 58 | raise ArgumentError, "Invalid pooling_api URL: #{pooling_api}" 59 | end 60 | scheme = '' 61 | if not URI.parse(pooling_api).scheme 62 | scheme = 'http://' 63 | end 64 | #check that you have a valid uri 65 | template_url = scheme + pooling_api + '/vm/' + template 66 | if not check_url(template_url) 67 | raise ArgumentError, "Invalid full template URL: #{template_url}" 68 | end 69 | template_url 70 | end 71 | 72 | # Override host tags with presets 73 | # @param [Beaker::Host] host Beaker host 74 | # @return [Hash] Tag hash 75 | def add_tags(host) 76 | host[:host_tags].merge( 77 | 'beaker_version' => Beaker::Version::STRING, 78 | 'jenkins_build_url' => @options[:jenkins_build_url], 79 | 'department' => @options[:department], 80 | 'project' => @options[:project], 81 | 'created_by' => @options[:created_by], 82 | 'name' => host.name, 83 | 'roles' => host.host_hash[:roles].join(', ') 84 | ) 85 | end 86 | 87 | # Get host info hash from parsed json response 88 | # @param [Hash] parsed_response hash 89 | # @param [String] template string 90 | # @return [Hash] Host info hash 91 | def get_host_info(parsed_response, template) 92 | parsed_response[template] 93 | end 94 | 95 | def provision 96 | request_payload = {} 97 | start = Time.now 98 | 99 | @hosts.each_with_index do |h, i| 100 | if not h['template'] 101 | raise ArgumentError, "You must specify a template name for #{h}" 102 | end 103 | if h['template'] =~ /\// 104 | templatefolders = h['template'].split('/') 105 | h['template'] = templatefolders.pop 106 | end 107 | 108 | request_payload[h['template']] = (request_payload[h['template']].to_i + 1).to_s 109 | end 110 | 111 | last_wait, wait = 0, 1 112 | waited = 0 #the amount of time we've spent waiting for this host to provision 113 | begin 114 | uri = URI.parse(@options['pooling_api'] + '/vm/') 115 | 116 | http = Net::HTTP.new(uri.host, uri.port) 117 | http.use_ssl = uri.instance_of?(URI::HTTPS) 118 | request = Net::HTTP::Post.new(uri.request_uri) 119 | 120 | if @credentials[:vmpooler_token] 121 | request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token] 122 | @logger.notify "Requesting VM set from vmpooler (with authentication token)" 123 | else 124 | @logger.notify "Requesting VM set from vmpooler" 125 | end 126 | 127 | request_payload_json = request_payload.to_json 128 | @logger.trace( "Request payload json: #{request_payload_json}" ) 129 | request.body = request_payload_json 130 | 131 | response = http.request(request) 132 | parsed_response = JSON.parse(response.body) 133 | @logger.trace( "Response parsed json: #{parsed_response}" ) 134 | 135 | if parsed_response['ok'] 136 | domain = parsed_response['domain'] 137 | request_payload = {} 138 | 139 | @hosts.each_with_index do |h, i| 140 | # If the requested host template is not available on vmpooler 141 | host_template = h['template'] 142 | if get_host_info(parsed_response, host_template).nil? 143 | request_payload[host_template] ||= 0 144 | request_payload[host_template] += 1 145 | next 146 | end 147 | if parsed_response[h['template']]['hostname'].is_a?(Array) 148 | hostname = parsed_response[host_template]['hostname'].shift 149 | else 150 | hostname = parsed_response[host_template]['hostname'] 151 | end 152 | 153 | h['vmhostname'] = domain ? "#{hostname}.#{domain}" : hostname 154 | 155 | @logger.notify "Using available host '#{h['vmhostname']}' (#{h.name})" 156 | end 157 | unless request_payload.empty? 158 | raise "Vmpooler.provision - requested VM templates #{request_payload.keys} not available" 159 | end 160 | else 161 | if response.code == '401' 162 | raise "Vmpooler.provision - response from pooler not ok. Vmpooler token not authorized to make request.\n#{parsed_response}" 163 | else 164 | raise "Vmpooler.provision - response from pooler not ok. Requested host set #{request_payload.keys} not available in pooler.\n#{parsed_response}" 165 | end 166 | end 167 | rescue JSON::ParserError, RuntimeError, *SSH_EXCEPTIONS => e 168 | @logger.debug "Failed vmpooler provision: #{e.class} : #{e.message}" 169 | if waited <= @options[:timeout].to_i 170 | @logger.debug("Retrying provision for vmpooler host after waiting #{wait} second(s)") 171 | sleep wait 172 | waited += wait 173 | last_wait, wait = wait, [last_wait + wait, 15].min + rand(5) 174 | retry 175 | end 176 | report_and_raise(@logger, e, 'Vmpooler.provision') 177 | end 178 | 179 | @logger.notify 'Spent %.2f seconds grabbing VMs' % (Time.now - start) 180 | 181 | start = Time.now 182 | @logger.notify 'Tagging vmpooler VMs' 183 | 184 | @hosts.each_with_index do |h, i| 185 | begin 186 | uri = URI.parse(@options[:pooling_api] + '/vm/' + h['vmhostname'].split('.')[0]) 187 | 188 | http = Net::HTTP.new(uri.host, uri.port) 189 | http.use_ssl = uri.instance_of?(URI::HTTPS) 190 | request = Net::HTTP::Put.new(uri.request_uri) 191 | 192 | # merge pre-defined tags with host tags 193 | request.body = { 'tags' => add_tags(h) }.to_json 194 | 195 | response = http.request(request) 196 | rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError, 197 | Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e 198 | @logger.notify "Failed to connect to vmpooler for tagging!" 199 | end 200 | 201 | begin 202 | parsed_response = JSON.parse(response.body) 203 | 204 | unless parsed_response['ok'] 205 | @logger.notify "Failed to tag host '#{h['vmhostname']}'!" 206 | end 207 | rescue JSON::ParserError => e 208 | @logger.notify "Failed to tag host '#{h['vmhostname']}'! (failed with #{e.class})" 209 | end 210 | end 211 | 212 | @logger.notify 'Spent %.2f seconds tagging VMs' % (Time.now - start) 213 | 214 | # add additional disks to vm 215 | @logger.debug 'Looking for disks to add...' 216 | 217 | @hosts.each do |h| 218 | hostname = h['vmhostname'].split(".")[0] 219 | 220 | if h['disks'] 221 | @logger.debug "Found disks for #{hostname}!" 222 | disks = h['disks'] 223 | 224 | disks.each_with_index do |disk_size, index| 225 | start = Time.now 226 | 227 | add_disk(hostname, disk_size) 228 | 229 | done = wait_for_disk(hostname, disk_size, index) 230 | if done 231 | @logger.notify "Spent %.2f seconds adding disk #{index}. " % (Time.now - start) 232 | else 233 | raise "Could not verify disk was added after %.2f seconds" % (Time.now - start) 234 | end 235 | end 236 | else 237 | @logger.debug "No disks to add for #{hostname}" 238 | end 239 | end 240 | end 241 | 242 | def cleanup 243 | vm_names = @hosts.map {|h| h['vmhostname'] }.compact 244 | if @hosts.length != vm_names.length 245 | @logger.warn "Some hosts did not have vmhostname set correctly! This likely means VM provisioning was not successful" 246 | end 247 | 248 | start = Time.now 249 | vm_names.each do |name| 250 | @logger.notify "Handing '#{name}' back to vmpooler for VM destruction" 251 | 252 | uri = URI.parse(get_template_url(@options['pooling_api'], name)) 253 | 254 | http = Net::HTTP.new( uri.host, uri.port ) 255 | http.use_ssl = uri.instance_of?(URI::HTTPS) 256 | request = Net::HTTP::Delete.new(uri.request_uri) 257 | 258 | if @credentials[:vmpooler_token] 259 | request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token] 260 | end 261 | 262 | begin 263 | response = http.request(request) 264 | rescue *SSH_EXCEPTIONS => e 265 | report_and_raise(@logger, e, 'Vmpooler.cleanup (http.request)') 266 | end 267 | end 268 | 269 | @logger.notify "Spent %.2f seconds cleaning up" % (Time.now - start) 270 | end 271 | 272 | def add_disk(hostname, disk_size) 273 | @logger.notify "Requesting an additional disk of size #{disk_size}GB for #{hostname}" 274 | 275 | if !disk_size.to_s.match /[0123456789]/ || size <= '0' 276 | raise NameError.new "Disk size must be an integer greater than zero!" 277 | end 278 | 279 | begin 280 | uri = URI.parse(@options[:pooling_api] + '/api/v1/vm/' + hostname + '/disk/' + disk_size.to_s) 281 | 282 | http = Net::HTTP.new(uri.host, uri.port) 283 | http.use_ssl = uri.instance_of?(URI::HTTPS) 284 | request = Net::HTTP::Post.new(uri.request_uri) 285 | request['X-AUTH-TOKEN'] = @credentials[:vmpooler_token] 286 | 287 | response = http.request(request) 288 | 289 | parsed = parse_response(response) 290 | 291 | raise "Response from #{hostname} indicates disk was not added" if !parsed['ok'] 292 | 293 | rescue NameError, RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError, 294 | Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e 295 | report_and_raise(@logger, e, 'Vmpooler.add_disk') 296 | end 297 | end 298 | 299 | def parse_response(response) 300 | parsed_response = JSON.parse(response.body) 301 | end 302 | 303 | def disk_added?(host, disk_size, index) 304 | if host['disk'].nil? 305 | false 306 | else 307 | host['disk'][index] == "+#{disk_size}gb" 308 | end 309 | end 310 | 311 | def get_vm(hostname) 312 | begin 313 | uri = URI.parse(@options[:pooling_api] + '/vm/' + hostname) 314 | 315 | http = Net::HTTP.new(uri.host, uri.port) 316 | http.use_ssl = uri.instance_of?(URI::HTTPS) 317 | request = Net::HTTP::Get.new(uri.request_uri) 318 | 319 | response = http.request(request) 320 | rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError, 321 | Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e 322 | @logger.notify "Failed to connect to vmpooler while getting VM information!" 323 | end 324 | end 325 | 326 | def wait_for_disk(hostname, disk_size, index) 327 | response = get_vm(hostname) 328 | parsed = parse_response(response) 329 | 330 | @logger.notify "Waiting for disk" 331 | 332 | attempts = 0 333 | 334 | while (!disk_added?(parsed[hostname], disk_size, index) && attempts < 20) 335 | sleep 10 336 | begin 337 | response = get_vm(hostname) 338 | parsed = parse_response(response) 339 | rescue RuntimeError, Errno::EINVAL, Errno::ECONNRESET, EOFError, 340 | Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, *SSH_EXCEPTIONS => e 341 | report_and_raise(@logger, e, "Vmpooler.wait_for_disk") 342 | end 343 | print "." 344 | attempts += 1 345 | end 346 | 347 | puts " " 348 | 349 | disk_added?(parsed[hostname], disk_size, index) 350 | end 351 | end 352 | end 353 | --------------------------------------------------------------------------------