Class: FauxhaiGenerator::Runner
- Inherits:
-
Object
- Object
- FauxhaiGenerator::Runner
- Defined in:
- lib/fauxhai_generator/runner.rb
Instance Method Summary collapse
-
#ami(platform, release) ⇒ Object
return the AMI for a platform/release.
-
#client ⇒ Object
ec2 client object.
- #config ⇒ Object
-
#create_instance(ami, platform, release) ⇒ Object
sping up an instance given an AMI.
-
#create_security_group ⇒ Object
create a new fauxhai_generator security group.
-
#find_existing_security_group ⇒ Object
find any existing security groups named fauxhai_generator to prevent failures if we fail before we cleanup the group.
- #gather_fauxhai_data(ip, plat) ⇒ Object
- #instance_dns_name(id) ⇒ Object
-
#json_sort(data) ⇒ Object
sort everything that comes back to make future diffs easier uses deepsort to make sorting the json easy.
-
#platforms ⇒ Object
list all platforms in the config.
-
#releases(platform) ⇒ Object
Return an array of releases for a given platform.
-
#resource ⇒ Object
ec2 resource object.
- #run ⇒ Object
-
#security_group_id ⇒ Object
return the security group ID find an existing group or create a new one.
- #terminate_instance(id) ⇒ Object
-
#user_name(platform) ⇒ Object
return the username to use for a given platform.
-
#wait_until_ready(instance) ⇒ Object
wait until the instance is ready and print out messagin while we wait.
- #write_data(platform, release, data) ⇒ Object
Instance Method Details
#ami(platform, release) ⇒ Object
return the AMI for a platform/release
121 122 123 |
# File 'lib/fauxhai_generator/runner.rb', line 121 def ami(platform, release) config["platforms"][platform][release]["ami"] end |
#client ⇒ Object
ec2 client object
13 14 15 |
# File 'lib/fauxhai_generator/runner.rb', line 13 def client @client ||= ::Aws::EC2::Client.new(region: config["aws"]["region"]) end |
#config ⇒ Object
8 9 10 |
# File 'lib/fauxhai_generator/runner.rb', line 8 def config @config ||= FauxhaiGenerator::Config.new.config end |
#create_instance(ami, platform, release) ⇒ Object
sping up an instance given an AMI
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fauxhai_generator/runner.rb', line 62 def create_instance(ami, platform, release) puts "Spinning up #{platform} #{release} AMI #{ami}" resource.create_instances( image_id: ami, min_count: 1, max_count: 1, key_name: config["aws"]["key_name"], instance_type: config["aws"]["instance_type"], security_group_ids: [security_group_id], tag_specifications: [{ resource_type: "instance", tags: [{ key: "creator", value: "fauxhai_generator" }, { key: "Name", value: "fauxhai_generator #{platform} #{release}" } ] }] ) end |
#create_security_group ⇒ Object
create a new fauxhai_generator security group
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fauxhai_generator/runner.rb', line 37 def create_security_group create_security_group_result = client.create_security_group( group_name: "fauxhai_generator", description: "A wide open security group for the Fauxhai Generator." ) client.( group_id: create_security_group_result.group_id, ip_permissions: [ { ip_protocol: "tcp", from_port: 22, to_port: 22, ip_ranges: [ { cidr_ip: "0.0.0.0/0" } ] } ] ) create_security_group_result.group_id end |
#find_existing_security_group ⇒ Object
find any existing security groups named fauxhai_generator to prevent failures if we fail before we cleanup the group
30 31 32 33 34 |
# File 'lib/fauxhai_generator/runner.rb', line 30 def find_existing_security_group client.describe_security_groups(group_names: ["fauxhai_generator"]).security_groups[0].group_id rescue Aws::EC2::Errors::InvalidGroupNotFound # we want a nil to be returned end |
#gather_fauxhai_data(ip, plat) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fauxhai_generator/runner.rb', line 125 def gather_fauxhai_data(ip, plat) puts " Installing Chef/Fauxhai and gathering data" 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) conn = train.connection conn.run_command("curl -k https://www.chef.io/chef/install.sh | sudo bash --") conn.run_command("/opt/chef/embedded/bin/gem install fauxhai --no-ri --no-rdoc") dump = conn.run_command("/opt/chef/embedded/bin/fauxhai").stdout conn.close dump end |
#instance_dns_name(id) ⇒ Object
137 138 139 |
# File 'lib/fauxhai_generator/runner.rb', line 137 def instance_dns_name(id) Aws::EC2::Instance.new(id).public_dns_name end |
#json_sort(data) ⇒ Object
sort everything that comes back to make future diffs easier uses deepsort to make sorting the json easy
143 144 145 |
# File 'lib/fauxhai_generator/runner.rb', line 143 def json_sort(data) JSON.pretty_generate(JSON.parse(data).deep_sort) end |
#platforms ⇒ Object
list all platforms in the config
101 102 103 |
# File 'lib/fauxhai_generator/runner.rb', line 101 def platforms config["platforms"].keys end |
#releases(platform) ⇒ Object
Return an array of releases for a given platform
106 107 108 |
# File 'lib/fauxhai_generator/runner.rb', line 106 def releases(platform) config["platforms"][platform].keys end |
#resource ⇒ Object
ec2 resource object
18 19 20 |
# File 'lib/fauxhai_generator/runner.rb', line 18 def resource @resource ||= ::Aws::EC2::Resource.new(region: config["aws"]["region"]) end |
#run ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/fauxhai_generator/runner.rb', line 156 def run # Spin up each platform release listed in the config and save the fauxhai output platforms.each do |plat| releases(plat).each do |rel| instance = create_instance(ami(plat, rel), plat, rel) wait_until_ready(instance) dump = gather_fauxhai_data(instance_dns_name(instance.first.id), plat) write_data(plat, rel, dump) terminate_instance(instance.first.id) end end end |
#security_group_id ⇒ Object
return the security group ID find an existing group or create a new one
24 25 26 |
# File 'lib/fauxhai_generator/runner.rb', line 24 def security_group_id @id ||= (find_existing_security_group || create_security_group) end |
#terminate_instance(id) ⇒ Object
87 88 89 90 91 |
# File 'lib/fauxhai_generator/runner.rb', line 87 def terminate_instance(id) puts "Terminating instance #{id}" resource.instance(id).terminate end |
#user_name(platform) ⇒ Object
return the username to use for a given platform
111 112 113 114 115 116 117 118 |
# File 'lib/fauxhai_generator/runner.rb', line 111 def user_name(platform) case platform when "ubuntu" "ubuntu" else "ec2-user" end end |
#wait_until_ready(instance) ⇒ Object
wait until the instance is ready and print out messagin while we wait
94 95 96 97 98 |
# File 'lib/fauxhai_generator/runner.rb', line 94 def wait_until_ready(instance) client.wait_until(:instance_status_ok, instance_ids: [instance.first.id]) do |w| w.before_wait { puts " Waiting for instance #{instance.first.id} to be ready" } end end |
#write_data(platform, release, data) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/fauxhai_generator/runner.rb', line 147 def write_data(platform, release, data) raise "No data to write for #{platform} #{release}!" if data.empty? puts "Writing data file to lib/fauxhai/platforms/#{platform}/#{release}.json" out = File.open("lib/fauxhai/platforms/#{platform}/#{release}.json", "w") out << json_sort(data) out.close end |