kitchen-vmpool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47a09fa8754874d175c4f371a901d36e6ee860c5
4
+ data.tar.gz: f5c5345cd9933b589f26c06d03954709e40a1ab3
5
+ SHA512:
6
+ metadata.gz: 9f8c6d51d87c3cb4e9f1eb06c894c0632e24804c4dbf8b634a3c74929434a49c84ee3b21f749e255244a0c68ecda083c79f1590e3a77d20d149e5f09674e0b3a
7
+ data.tar.gz: bc99c8c6fd5ceddb516c587ecc16da6384179f0c1997cdbcb7be08b43512e5923e57eeaafc1f72911b2ac1b37e459406e24f90d3dfb10f07c0803eac7cbd09fa
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at corey@nwops.io. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem 'gitlab'
4
+
5
+ # Specify your gem's dependencies in kitchen-vmpool.gemspec
6
+ group :dev do
7
+ gem 'test-kitchen'
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ gem 'kitchen-vmpool', path: './'
11
+ gem 'pry'
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Corey Osman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Kitchen::Vmpool
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kitchen/vmpool`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+
10
+ ```ruby
11
+ gem 'kitchen-vmpool'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install kitchen-vmpool
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/logicminds/kitchen-vmpool. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
40
+ ## Code of Conduct
41
+
42
+ Everyone interacting in the Kitchen::Vmpool project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/logicminds/kitchen-vmpool/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kitchen/vmpool"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kitchen-vmpool"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["Corey Osman"]
9
+ spec.email = ["corey@nwops.io"]
10
+
11
+ spec.summary = %q{Test Kitchen driver for virtual machine pools}
12
+ spec.description = %q{When you need to create pools of vms and manage them with test kitchen}
13
+ spec.homepage = "https://gitlab.com/nwops/kitchen-vmpool"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "gitlab", "~> 4.2"
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,119 @@
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Author:: Corey Osman <corey@nwops.io>
4
+ #
5
+ # Copyright:: Copyright (c) 2017 NWOPS, LLC.
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+ require 'kitchen'
21
+ require "kitchen/version"
22
+ require 'kitchen/driver/base'
23
+ module Kitchen
24
+ module Driver
25
+ class Vmpool < Kitchen::Driver::Base
26
+ plugin_version "0.1.0"
27
+
28
+ default_config :pool_name, 'pool1'
29
+ default_config :pool_file, 'vmpool.yaml'
30
+ default_config :state_store, 'file'
31
+ default_config :store_options, {}
32
+
33
+ required_config :create_command
34
+ default_config :destroy_command, nil
35
+
36
+ no_parallel_for :create, :destroy
37
+
38
+ # (see Base#create)
39
+ def create(state)
40
+ state[:hostname] = pool_member
41
+ end
42
+
43
+ # (see Base#destroy)
44
+ def destroy(state)
45
+ return if state[:hostname].nil?
46
+ mark_unused(state[:hostname])
47
+ state.delete(:hostname)
48
+ end
49
+
50
+ private
51
+
52
+ # @return [String] - a random host from the list of systems
53
+ def pool_member
54
+ sample = pool_hosts.sample
55
+ member = pool_hosts.delete(sample)
56
+ mark_used(member)
57
+ return member
58
+ end
59
+
60
+ # @return Array[String] - a list of pool names
61
+ def pool_names
62
+ store.pool_data.keys
63
+ end
64
+
65
+ # @return [Hash] - a pool hash by the given pool_name from the config
66
+ def pool
67
+ name = config[:pool_name]
68
+ raise ArgumentError.new("Pool #{name} does not exist") unless pool_exists?(name)
69
+ store.pool_data[name]
70
+ end
71
+
72
+ # @return [Boolean] - true if the pool exists
73
+ def pool_exists?(name)
74
+ pool_names.include?(name)
75
+ end
76
+
77
+ # @return Array[String] - a list of host names in the pool
78
+ def pool_hosts
79
+ pool['pool_instances']
80
+ end
81
+
82
+ # @param name [String] - the hostname to mark not used
83
+ # @return Array[String] - list of unused instances
84
+ def mark_unused(name)
85
+ pool['pool_instances'] = [] unless pool['pool_instances']
86
+ pool['pool_instances'] << name
87
+ store.save
88
+ pool['pool_instances']
89
+ end
90
+
91
+ # @param name [String] - the hostname to mark used
92
+ # @return Array[String] - list of used instances
93
+ def mark_used(name)
94
+ pool['used_instances'] = [] unless pool['used_instances']
95
+ pool['used_instances'] << name
96
+ store.save
97
+ pool['used_instances']
98
+ end
99
+
100
+ # @return [Hash] - a store hash that contains one or more pools
101
+ def store
102
+ @store ||= begin
103
+ # load the store adapter and create a new instance of the store
104
+ store = sprintf("%s%s", config[:state_store].capitalize, 'Store')
105
+ require "kitchen/driver/vmpool_stores/#{config[:state_store]}_store"
106
+ klass = Object.const_get("Kitchen::Driver::VmpoolStores::#{store}")
107
+ # create a new instance of the store with the provided options
108
+ store_opts = config[:store_options]
109
+ # convert everything key to strings
110
+ store_opts.tap do |h|
111
+ h.keys.each { |k| h[k.to_s] = h.delete(k) }
112
+ end
113
+ klass.send(:new, store_opts)
114
+ end
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,68 @@
1
+ require 'yaml'
2
+
3
+ module Kitchen
4
+ module Driver
5
+ module VmpoolStores
6
+ class FileStore
7
+
8
+ attr_reader :pool_file
9
+
10
+ # @option pool_file [String] - the file path that holds the pool information
11
+ def initialize(options = nil)
12
+ raise ArgumentError unless options['pool_file']
13
+ options ||= { 'pool_file' => 'vmpool.yaml' }
14
+ @pool_file = options['pool_file']
15
+ end
16
+
17
+ def update(content)
18
+ write_content(content)
19
+ read
20
+ end
21
+
22
+ def create
23
+ write_content(base_content)
24
+ read
25
+ end
26
+
27
+ def read
28
+ puts "Reading snippet"
29
+ read_content
30
+ end
31
+
32
+ def save
33
+ write_content
34
+ read
35
+ end
36
+
37
+ def pool_data
38
+ @pool_data ||= YAML.load(read_content)
39
+ end
40
+
41
+ private
42
+
43
+ def base_content
44
+ {
45
+ pool1: {
46
+ pool_name: pool1,
47
+ payload_file: pool1_payload.yaml,
48
+ instances: 1,
49
+ pool_instances: [],
50
+ requests: []
51
+ }
52
+ }
53
+ end
54
+
55
+ def read_content
56
+ data = File.read(pool_file)
57
+ raise ArgumentError unless data
58
+ data
59
+ end
60
+
61
+ def write_content(content = pool_data)
62
+ File.open(pool_file, 'w') { |f| f.write(content.to_yaml) }
63
+ end
64
+
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,93 @@
1
+ require 'gitlab'
2
+ module Kitchen
3
+ module Driver
4
+ module VmpoolStores
5
+ class GitlabStore
6
+
7
+ attr_accessor :project_id, :snippet_id
8
+ attr_reader :pool_file
9
+
10
+ # @option project_id [Integer] - the project id in gitlab
11
+ # @option snippet_id [Integer] - the snipppet id in the gitlab project
12
+ # @option pool_file [String] - the snipppet file name
13
+ def initialize(options = nil)
14
+ options ||= { project_id: nil, snippet_id: nil, pool_file: 'vmpool.yaml'}
15
+ raise ArgumentError.new("You must pass the project_id option") unless options['project_id'].to_i > 0
16
+ raise ArgumentError.new("You must pass the snippet_id option") unless options['snippet_id'].to_i > 0
17
+ @snippet_id = options['snippet_id'] #ie. 630
18
+ @project_id = options['project_id'] #ie. 89
19
+ @pool_file = options['pool_file']
20
+ end
21
+
22
+ def update
23
+ update_snippet
24
+ read
25
+ end
26
+
27
+ def create
28
+ create_snippet
29
+ read
30
+ end
31
+
32
+ def read
33
+ puts "Reading snippet"
34
+ pool_content
35
+ end
36
+
37
+ def pool_data
38
+ @pool_data ||= YAML.load(pool_content)
39
+ end
40
+
41
+ def save
42
+ update_snippet
43
+ read
44
+ end
45
+
46
+ def pool_content
47
+ read_snippet
48
+ end
49
+
50
+ private
51
+
52
+ def client
53
+ @client ||= Gitlab.client
54
+ end
55
+
56
+ def snippet_exists?(project = project_id)
57
+ client.snippets(project, {
58
+ title: 'Virtual Machine Pools',
59
+ visibility: 'public',
60
+ file_name: pool_file,
61
+ code: pool_content})
62
+ end
63
+
64
+ def create_snippet(project = project_id)
65
+ client.create_snippet(project, {
66
+ title: 'Virtual Machine Pools',
67
+ visibility: 'public',
68
+ file_name: pool_file,
69
+ code: pool_content
70
+ })
71
+ end
72
+
73
+ def update_snippet(project = project_id)
74
+ client.edit_snippet(project, snippet_id, {
75
+ title: 'Virtual Machine Pools',
76
+ visibility: 'public',
77
+ file_name: pool_file,
78
+ code: pool_data.to_yaml
79
+ })
80
+ end
81
+
82
+ def project_snippets(project = project_id)
83
+ client.snippets(project).map {|s| s.id }
84
+ end
85
+
86
+ def read_snippet(project = project_id, id = snippet_id)
87
+ client.snippet_content(project, id)
88
+ end
89
+
90
+ end
91
+ end
92
+ end
93
+ end
data/vmpool.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ base_rhel6_pool:
3
+ pool_name: base_rhel6
4
+ payload_file: base_rhel6_payload.yaml
5
+ instances: 1
6
+ pool_instances: []
7
+ requests: []
data/vra_create.rb ADDED
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env ruby
2
+ require 'vra'
3
+ require 'erb'
4
+ require 'highline/import'
5
+ require 'openssl'
6
+ require 'json'
7
+ require 'yaml'
8
+
9
+ # Purpose: Submits a single request to VRA for vm creation
10
+ module Vra
11
+ class Client
12
+ # monkey patch the init method to accept token
13
+ def initialize(opts)
14
+ @base_url = opts[:base_url]
15
+ @username = opts[:username]
16
+ @password = PasswordMasker.new(opts[:password])
17
+ @tenant = opts[:tenant]
18
+ @verify_ssl = opts.fetch(:verify_ssl, true)
19
+ @bearer_token = PasswordMasker.new(nil)
20
+ @page_size = opts.fetch(:page_size, 20)
21
+
22
+ validate_client_options!
23
+ end
24
+ end
25
+
26
+ # monkey patch the init method to accept additional params
27
+ class CatalogRequest
28
+ def initialize(client, catalog_id, opts = {})
29
+ @client = client
30
+ @catalog_id = catalog_id
31
+ @cpus = opts[:cpus]
32
+ @memory = opts[:memory]
33
+ @requested_for = opts[:requested_for]
34
+ @lease_days = opts[:lease_days]
35
+ @notes = opts[:notes]
36
+ @subtenant_id = opts[:subtenant_id]
37
+ @additional_params = opts[:additional_params] || Vra::RequestParameters.new
38
+ @catalog_item = Vra::CatalogItem.new(client, id: catalog_id)
39
+ end
40
+ end
41
+ end
42
+
43
+ module VraUtilities
44
+ def classification
45
+ ENV['VRA_CLASSIFY']
46
+ end
47
+
48
+ def branch
49
+ `git rev-parse --abbrev-ref HEAD`.chomp if ENV['USE_BRANCH']
50
+ end
51
+
52
+ def sandbox
53
+ branch || 'dev'
54
+ end
55
+
56
+ def datacenter
57
+ ENV['DATACENTER'] || 'Eroc'
58
+ end
59
+
60
+ def vra_email
61
+ @vra_email ||= ENV['VRA_EMAIL'] || ask('What is your frit email for VRA notifications')
62
+ end
63
+
64
+ def subtenant_id
65
+ ENV['VRA_SUB_TENANT_ID']
66
+ end
67
+
68
+ def vra_user
69
+ @vra_user ||= ENV['VRA_USER'] || ask('Enter User: ') {|q| q.echo = true}
70
+ end
71
+
72
+ def vra_pass
73
+ @vra_pass ||= ENV['VRA_PASS'] || ask('Enter VRA Password: ') {|q| q.echo = 'x'}
74
+ end
75
+
76
+ def base_url
77
+ @server ||= ENV['VRA_URL']
78
+ end
79
+
80
+
81
+ # @return [VRA::Client] - creates a new client object and returns it
82
+ def client
83
+ @client ||= Vra::Client.new(
84
+ username: vra_user,
85
+ password: vra_pass,
86
+ tenant: 'vsphere.local',
87
+ base_url: base_url,
88
+ verify_ssl: false,
89
+ )
90
+ end
91
+
92
+ # @return Array[String] - returns an array of catalog items
93
+ def catalog_items
94
+ client.catalog.all_items.map {|i| {name: i.name, id: i.id}}
95
+ end
96
+
97
+ def request_options
98
+ {
99
+ cpus: 1,
100
+ memory: 4096,
101
+ requested_for: 'someone@localhost',
102
+ lease_days: 2,
103
+ additional_params: request_params,
104
+ notes: 'Corey Test',
105
+ subtenant_id: request_data['organization']['subtenantRef']
106
+ }
107
+ end
108
+
109
+ def request_data
110
+ @request_data ||= YAML.load_file(@payload_file)
111
+ end
112
+
113
+ def parameters
114
+ request_data['requestData']['entries'].map {|item| [item['key'], item['value'].values].flatten }
115
+ end
116
+
117
+ def request_params
118
+ unless @request_params
119
+ @request_params = Vra::RequestParameters.new
120
+ parameters.each { |p| @request_params.set(*p)}
121
+ end
122
+ @request_params
123
+ end
124
+
125
+ def request_item
126
+ blueprint = request_data['catalogItemRef']['id']
127
+ client.catalog.request(blueprint, request_options)
128
+ end
129
+
130
+ # @return [Vra::Request] - returns a request item
131
+ def submit_new_request(payload_file)
132
+ @payload_file = payload_file
133
+ request_item.submit
134
+ end
135
+
136
+ require 'optparse'
137
+
138
+ def run
139
+ options = {}
140
+ OptionParser.new do |opts|
141
+ opts.program_name = 'vra-pool'
142
+ opts.on_head(<<-EOF
143
+
144
+ Summary: A tool used to provision systems in VRA
145
+ EOF
146
+ )
147
+ opts.on('-n', '--node-file FILE', "Load the request data from this file and create it") do |c|
148
+ options[:node_file] = c
149
+ @payload_file = c
150
+ submit_new_request if File.exist?(@payload_file) # create the request
151
+ end
152
+ end.parse!
153
+ end
154
+ end
155
+
156
+ include VraUtilities
data/vra_create_pool ADDED
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'resolv'
5
+ require 'optparse'
6
+ require 'yaml'
7
+ require_relative 'vra_create'
8
+ require 'kitchen/driver/vmpool_stores/gitlab_store'
9
+ require 'socket'
10
+ require 'timeout'
11
+
12
+ include VraUtilities
13
+
14
+ # @return [Hash] - hash of config options
15
+ # reads from vmpool_config.yaml file if exists
16
+ def options
17
+ @options ||= begin
18
+ opts = File.exist?('vmpool_config.yaml') ? YAML.load_file('vmpool_config.yaml') : {}
19
+ end
20
+ end
21
+
22
+ def create_pool(pool_data)
23
+ (1..pool_data['instances']).map {|num| submit_new_request(pool_data['payload_file']).id }
24
+ end
25
+
26
+ # return hostnames or false
27
+ def resolve_vm_name(request)
28
+ r = client.requests.by_id(request)
29
+ return r.resources.map(&:name) if r.successful? and r.completed?
30
+ return false
31
+ end
32
+
33
+ # @return [Hash] - a store hash that contains one or more pools
34
+ # @option project_id [Integer] - the project id in gitlab
35
+ # @option snippet_id [Integer] - the snipppet id in the gitlab project
36
+ # @option pool_file [String] - the snipppet file name
37
+ def store(store_options = options)
38
+ # create a new instance of the store with the provided options
39
+ @store ||= Kitchen::Driver::VmpoolStores::GitlabStore.new(store_options)
40
+ end
41
+
42
+ # creates the number of instances defined in the pool data
43
+ def create_pools
44
+ pools.map do |key, value|
45
+ # convert the requests to vm names
46
+ pools[key]['requests'] = value['requests'].find_all do |req|
47
+ if hostnames = resolve_vm_name(req)
48
+ pools[key]['pool_instances'] = value['pool_instances'] + hostnames
49
+ false
50
+ else
51
+ req
52
+ end
53
+ end
54
+
55
+ # return the alive instances and save to the pool
56
+ pools[key]['pool_instances'] = pools[key]['pool_instances'].find_all {|h| is_alive?(h) }
57
+
58
+ # create the pool, and save the request in the requests
59
+ # do not create if the number of systems and requests are more than the requested amount
60
+ current_total = value['pool_instances'].count + pools[key]['requests'].count
61
+ unless current_total >= value['instances']
62
+ reqs = create_pool(value)
63
+ pools[key]['requests'] = reqs
64
+ end
65
+ end
66
+ store.save
67
+ end
68
+
69
+ # @return [Boolean] - true if the host is alive and listening
70
+ def is_alive?(node)
71
+ has_dns_record?(node) && ssh_is_alive?(node)
72
+ end
73
+
74
+ # @return [Boolean] - true if the host is listening on 22, false otherwise
75
+ def ssh_is_alive?(node)
76
+ puts "Checking if #{node} has ssh alive"
77
+ begin
78
+ Timeout::timeout(3) { TCPSocket.new(node, 22) }
79
+ puts "Responded!!"
80
+ return true
81
+ rescue Errno::ECONNREFUSED
82
+ puts "Connection refused"
83
+ return false
84
+ rescue SocketError
85
+ puts "No connection!!"
86
+ return false
87
+ rescue Timeout::Error
88
+ puts "No connection, timed out!!"
89
+ return false
90
+ end
91
+ end
92
+
93
+ # @return [Boolean] - true if the host is still in dns, false otherwise
94
+ def has_dns_record?(node)
95
+ begin
96
+ result = Resolv.getaddress(node)
97
+ rescue Resolv::ResolvError
98
+ result = false
99
+ end
100
+ result
101
+ end
102
+
103
+ # @return [Hash] - returns a hash of all the pools
104
+ def pools
105
+ @pools ||= store.pool_data
106
+ end
107
+
108
+ # @return [Boolean] - true if options are valid
109
+ def valid_options?
110
+ options['pool_file'] &&
111
+ File.exist?(options['pool_file']) &&
112
+ options['project_id'].to_i > 0 &&
113
+ options['snippet_id'].to_i > 0
114
+ end
115
+
116
+ ## main entry point
117
+
118
+ OptionParser.new do |opts|
119
+ opts.program_name = 'create-pool'
120
+ opts.on_head(<<-EOF
121
+
122
+ Summary: A tool used to create a pool of vra systems
123
+
124
+ Example:
125
+ #{__FILE__} -f pool_file.yaml -p 33 -s 103
126
+
127
+ If you wish to store the config in a file, this script will read vmpool_config.yaml
128
+ for the same configs.
129
+
130
+ EOF
131
+ )
132
+ opts.on('-f', '--pool-file FILE', "Create the pools found in the given pool file") do |c|
133
+ options['pool_file'] = c
134
+ end
135
+ opts.on('-p', '--project-id ID', "The gitlab project id") do |c|
136
+ options['project_id'] = c.to_i
137
+ end
138
+ opts.on('-s', '--snippet-id ID', "The gitlab project snippet id") do |c|
139
+ options['snippet_id'] = c.to_i
140
+ end
141
+
142
+ end.parse!
143
+
144
+
145
+ if valid_options?
146
+ create_pools
147
+ else
148
+ puts "Invalid options: #{options.inspect}"
149
+ exit 1
150
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kitchen-vmpool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Corey Osman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: When you need to create pools of vms and manage them with test kitchen
70
+ email:
71
+ - corey@nwops.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - kitchen-vmpool.gemspec
87
+ - lib/kitchen/driver/vmpool.rb
88
+ - lib/kitchen/driver/vmpool_stores/file_store.rb
89
+ - lib/kitchen/driver/vmpool_stores/gitlab_store.rb
90
+ - vmpool.yaml
91
+ - vra_create.rb
92
+ - vra_create_pool
93
+ homepage: https://gitlab.com/nwops/kitchen-vmpool
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.11
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Test Kitchen driver for virtual machine pools
117
+ test_files: []