fauxhai_generator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a23333b1030fe4aa71a54818861eeb7b86d4b2311df9e8465ebcc4174f39bdb9
4
+ data.tar.gz: 6f6a80090c2bb92abed2397bbb9932c941fee8df3e9d1495ea95c8888fd7abd8
5
+ SHA512:
6
+ metadata.gz: 411bde6f95a18152c96d5e3cc0d143d514083e36fad220ba332b82b51f7d6b712eb96f6473f0a868245762607f685e60788e68eabc23306893840d46e340c863
7
+ data.tar.gz: e69a9c34e944ce4d981c2b373fc6fa64982a059d16011316a4dc7167e225e6adcefb9eedc007639cb976ff5b0716368c1de2fd175f1a8c59d1a415a3bd3d746a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Tim Smith
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.
@@ -0,0 +1,15 @@
1
+ # fauxhai_generator
2
+
3
+ Spin up systems in AWS and generate new Fauxhai dumps
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install fauxhai_generator
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ - Run `aws configure` to make sure you have a properly formatted AWS config file
14
+ - Make sure config.yml is updated with all the AMIs you want to run
15
+ - From the fauxhai directory run fauxhai_generator KEY_PAIR_NAME PATH_TO_KEYPAIR
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
5
+ require "fauxhai_generator/version"
6
+ require "fauxhai_generator/runner"
7
+ require "fauxhai_generator/config"
8
+
9
+ FauxhaiGenerator::Runner.new.run
@@ -0,0 +1,52 @@
1
+ aws:
2
+ region: 'us-west-2'
3
+ instance_type: 't2.micro'
4
+
5
+ # make sure the platform versions are actually strings
6
+ platforms:
7
+ ubuntu:
8
+ '14.04':
9
+ ami: 'ami-a523b4dd'
10
+ '16.04':
11
+ ami: 'ami-78d3ba00'
12
+ '17.10':
13
+ ami: 'ami-2192f559'
14
+ '18.04':
15
+ ami: 'ami-22741f5a'
16
+ amazon:
17
+ '2':
18
+ ami: 'ami-07eb707f'
19
+ '2018.03':
20
+ ami: 'ami-6b8cef13'
21
+ '2017.03':
22
+ ami: 'ami-215dbe59'
23
+ '2017.09':
24
+ ami: 'ami-f64bdf8e'
25
+ '2016.09':
26
+ ami: 'ami-9e73ccfe'
27
+ '2016.03':
28
+ ami: 'ami-d8f704b8'
29
+ '2015.09':
30
+ ami: 'ami-42b15122'
31
+ '2015.03':
32
+ ami: 'ami-d1c5d1e1'
33
+ redhat:
34
+ '7.5':
35
+ ami: 'ami-28e07e50'
36
+ '7.4':
37
+ ami: 'ami-9fa343e7'
38
+ '7.3':
39
+ ami: 'ami-6f68cf0f'
40
+ '6.9':
41
+ ami: 'ami-d61a92b6'
42
+ '6.8':
43
+ ami: 'ami-6fb7450f'
44
+ suse:
45
+ '12.3':
46
+ ami: 'ami-6bc56f13'
47
+ '12.2':
48
+ ami: 'ami-974357ee'
49
+ '12.1':
50
+ ami: 'ami-61ad6501'
51
+ '11.4':
52
+ ami: 'ami-7eb31906'
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("lib", __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fauxhai_generator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fauxhai_generator"
8
+ spec.version = FauxhaiGenerator::VERSION
9
+ spec.authors = ["Tim Smith"]
10
+ spec.email = ["tsmith@chef.io"]
11
+
12
+ spec.summary = "Spin up systems in AWS and generate new Fauxhai dumps"
13
+ spec.description = "Spin up systems in AWS and generate new Fauxhai dumps"
14
+ spec.homepage = "http://www.github.com/chefspec/fauxhai_generator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "aws-sdk-ec2"
25
+ spec.add_dependency "deepsort"
26
+ spec.add_dependency "train-core"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
@@ -0,0 +1,58 @@
1
+ require "optparse"
2
+ require "yaml"
3
+
4
+ module FauxhaiGenerator
5
+ class Config
6
+ def initialize
7
+ readiness_check
8
+ @config = load_config
9
+ end
10
+
11
+ attr_reader :config
12
+
13
+ # parse the command line options
14
+ def options
15
+ # since optparse doesn't have a "required" flag we have to hack one on
16
+ ARGV << "-h" if ARGV.count < 6
17
+
18
+ options = {}
19
+ OptionParser.new do |opts|
20
+ opts.banner = "Usage: fauxhai_generator [options]"
21
+
22
+ opts.on("-c", "--config FILE_PATH ", "fauxhai_generator config.yml file path. (required)") do |n|
23
+ raise "The passed config file at #{n} does not exist!" unless File.exist?(n)
24
+ options["config_file"] = n
25
+ end
26
+
27
+ opts.on("-f", "--key-file FILE_PATH ", "The path to the key used to login to AWS instances. (required)") do |n|
28
+ raise "The passed key file at #{n} does not exist!" unless File.exist?(n)
29
+ options["key_path"] = n
30
+ end
31
+
32
+ opts.on("-k", "--key_name KEYNAME ", "The name of the keypair to setup AWS instances with. (required)") do |n|
33
+ options["key_name"] = n
34
+ end
35
+
36
+ opts.on("-h", "--help", "Display fauxhai_generator options") do
37
+ puts opts
38
+ exit
39
+ end
40
+ end.parse!
41
+ options
42
+ end
43
+
44
+ # fail if things aren't in order
45
+ def readiness_check
46
+ raise "You must run fauxhai_generator from the root of the fauxhai repository!" unless Dir.exist?("lib/fauxhai/platforms")
47
+ end
48
+
49
+ # the config in config.yml mixed in with the key_name/key_path passed via CLI
50
+ def load_config
51
+ opts = options
52
+ yaml = YAML.safe_load(File.open(opts["config_file"]))
53
+ yaml["aws"]["key_name"] = opts["key_name"]
54
+ yaml["aws"]["key_path"] = opts["key_path"]
55
+ yaml
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,171 @@
1
+ require "train"
2
+ require "aws-sdk"
3
+ require "json"
4
+ require "deepsort"
5
+
6
+ module FauxhaiGenerator
7
+ class Runner
8
+ def config
9
+ @config ||= FauxhaiGenerator::Config.new.config
10
+ end
11
+
12
+ # ec2 client object
13
+ def client
14
+ @client ||= ::Aws::EC2::Client.new(region: config["aws"]["region"])
15
+ end
16
+
17
+ # ec2 resource object
18
+ def resource
19
+ @resource ||= ::Aws::EC2::Resource.new(region: config["aws"]["region"])
20
+ end
21
+
22
+ # return the security group ID
23
+ # find an existing group or create a new one
24
+ def security_group_id
25
+ @id ||= (find_existing_security_group || create_security_group)
26
+ end
27
+
28
+ # find any existing security groups named fauxhai_generator
29
+ # to prevent failures if we fail before we cleanup the group
30
+ def find_existing_security_group
31
+ client.describe_security_groups(group_names: ["fauxhai_generator"]).security_groups[0].group_id
32
+ rescue Aws::EC2::Errors::InvalidGroupNotFound
33
+ # we want a nil to be returned
34
+ end
35
+
36
+ # create a new fauxhai_generator security group
37
+ def create_security_group
38
+ create_security_group_result = client.create_security_group(
39
+ group_name: "fauxhai_generator",
40
+ description: "A wide open security group for the Fauxhai Generator."
41
+ )
42
+
43
+ client.authorize_security_group_ingress(
44
+ group_id: create_security_group_result.group_id,
45
+ ip_permissions: [
46
+ {
47
+ ip_protocol: "tcp",
48
+ from_port: 22,
49
+ to_port: 22,
50
+ ip_ranges: [
51
+ {
52
+ cidr_ip: "0.0.0.0/0"
53
+ }
54
+ ]
55
+ }
56
+ ]
57
+ )
58
+ create_security_group_result.group_id
59
+ end
60
+
61
+ # sping up an instance given an AMI
62
+ def create_instance(ami, platform, release)
63
+ puts "Spinning up #{platform} #{release} AMI #{ami}"
64
+
65
+ resource.create_instances(
66
+ image_id: ami,
67
+ min_count: 1,
68
+ max_count: 1,
69
+ key_name: config["aws"]["key_name"],
70
+ instance_type: config["aws"]["instance_type"],
71
+ security_group_ids: [security_group_id],
72
+ tag_specifications: [{
73
+ resource_type: "instance",
74
+ tags: [{
75
+ key: "creator",
76
+ value: "fauxhai_generator"
77
+ },
78
+ {
79
+ key: "Name",
80
+ value: "fauxhai_generator #{platform} #{release}"
81
+ }
82
+ ]
83
+ }]
84
+ )
85
+ end
86
+
87
+ def terminate_instance(id)
88
+ puts "Terminating instance #{id}"
89
+
90
+ resource.instance(id).terminate
91
+ end
92
+
93
+ # wait until the instance is ready and print out messagin while we wait
94
+ def wait_until_ready(instance)
95
+ client.wait_until(:instance_status_ok, instance_ids: [instance.first.id]) do |w|
96
+ w.before_wait { puts " Waiting for instance #{instance.first.id} to be ready" }
97
+ end
98
+ end
99
+
100
+ # list all platforms in the config
101
+ def platforms
102
+ config["platforms"].keys
103
+ end
104
+
105
+ # Return an array of releases for a given platform
106
+ def releases(platform)
107
+ config["platforms"][platform].keys
108
+ end
109
+
110
+ # return the username to use for a given platform
111
+ def user_name(platform)
112
+ case platform
113
+ when "ubuntu"
114
+ "ubuntu"
115
+ else
116
+ "ec2-user"
117
+ end
118
+ end
119
+
120
+ # return the AMI for a platform/release
121
+ def ami(platform, release)
122
+ config["platforms"][platform][release]["ami"]
123
+ end
124
+
125
+ def gather_fauxhai_data(ip, plat)
126
+ puts " Installing Chef/Fauxhai and gathering data"
127
+
128
+ train = Train.create("ssh", host: ip, port: 22, user: user_name(plat), key_files: ARGV[1], auth_methods: ["publickey"], connection_retries: 10, connection_retry_sleep: 5, sudo: true)
129
+ conn = train.connection
130
+ conn.run_command("curl -k https://www.chef.io/chef/install.sh | sudo bash --")
131
+ conn.run_command("/opt/chef/embedded/bin/gem install fauxhai --no-ri --no-rdoc")
132
+ dump = conn.run_command("/opt/chef/embedded/bin/fauxhai").stdout
133
+ conn.close
134
+ dump
135
+ end
136
+
137
+ def instance_dns_name(id)
138
+ Aws::EC2::Instance.new(id).public_dns_name
139
+ end
140
+
141
+ # sort everything that comes back to make future diffs easier
142
+ # uses deepsort to make sorting the json easy
143
+ def json_sort(data)
144
+ JSON.pretty_generate(JSON.parse(data).deep_sort)
145
+ end
146
+
147
+ def write_data(platform, release, data)
148
+ raise "No data to write for #{platform} #{release}!" if data.empty?
149
+ puts "Writing data file to lib/fauxhai/platforms/#{platform}/#{release}.json"
150
+
151
+ out = File.open("lib/fauxhai/platforms/#{platform}/#{release}.json", "w")
152
+ out << json_sort(data)
153
+ out.close
154
+ end
155
+
156
+ def run
157
+ # Spin up each platform release listed in the config and save the fauxhai output
158
+ platforms.each do |plat|
159
+ releases(plat).each do |rel|
160
+ instance = create_instance(ami(plat, rel), plat, rel)
161
+ wait_until_ready(instance)
162
+
163
+ dump = gather_fauxhai_data(instance_dns_name(instance.first.id), plat)
164
+ write_data(plat, rel, dump)
165
+
166
+ terminate_instance(instance.first.id)
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,3 @@
1
+ module FauxhaiGenerator
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fauxhai_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-ec2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: deepsort
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: train-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description: Spin up systems in AWS and generate new Fauxhai dumps
84
+ email:
85
+ - tsmith@chef.io
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/fauxhai_generator
96
+ - config.yml
97
+ - fauxhai_generator.gemspec
98
+ - lib/fauxhai_generator/config.rb
99
+ - lib/fauxhai_generator/runner.rb
100
+ - lib/fauxhai_generator/version.rb
101
+ homepage: http://www.github.com/chefspec/fauxhai_generator
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.7.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Spin up systems in AWS and generate new Fauxhai dumps
125
+ test_files: []