diff options
author | Thomas Wiest <twiest@gmail.com> | 2014-10-23 15:35:16 -0400 |
---|---|---|
committer | Thomas Wiest <twiest@gmail.com> | 2014-10-23 15:35:16 -0400 |
commit | cde5cba28b7b0e9386f1c549ecf9141bbcadc64e (patch) | |
tree | 22a39c5589aa3f6a2a01f185041258e4fd69dea6 /lib/aws_helper.rb | |
parent | 1057c69acdaf47e2bcd4b395069e3bc1bd9eec88 (diff) | |
parent | 5f9c7eb2d2ad44776d33197857dcd0afe693b5f5 (diff) | |
download | openshift-cde5cba28b7b0e9386f1c549ecf9141bbcadc64e.tar.gz openshift-cde5cba28b7b0e9386f1c549ecf9141bbcadc64e.tar.bz2 openshift-cde5cba28b7b0e9386f1c549ecf9141bbcadc64e.tar.xz openshift-cde5cba28b7b0e9386f1c549ecf9141bbcadc64e.zip |
Merge pull request #11 from twiest/pull
Added atomic aws host to cloud.rb
Diffstat (limited to 'lib/aws_helper.rb')
-rw-r--r-- | lib/aws_helper.rb | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lib/aws_helper.rb b/lib/aws_helper.rb new file mode 100644 index 000000000..6d213107b --- /dev/null +++ b/lib/aws_helper.rb @@ -0,0 +1,82 @@ +require 'fileutils' + +module OpenShift + module Ops + class AwsHelper + MYDIR = File.expand_path(File.dirname(__FILE__)) + + def self.get_list() + cmd = "#{MYDIR}/../inventory/aws/ec2.py --list" + hosts = %x[#{cmd} 2>&1] + + raise "Error: failed to list hosts\n#{hosts}" unless $?.exitstatus == 0 + return JSON.parse(hosts) + end + + def self.get_hosts() + hosts = get_list() + + retval = [] + hosts['_meta']['hostvars'].each do |host, info| + retval << OpenStruct.new({ + :name => info['ec2_tag_Name'], + :env => info['ec2_tag_environment'] || 'UNSET', + :external_ip => info['ec2_ip_address'], + :public_dns => info['ec2_public_dns_name'] + }) + end + + retval.sort_by! { |h| [h.env, h.name] } + + return retval + end + + def self.get_host_details(host) + hosts = get_list() + dns_names = hosts["tag_Name_#{host}"] + + raise "Error: host not found [#{host}]" if dns_names.nil? + + return hosts['_meta']['hostvars'][dns_names.first] + end + + def self.check_creds() + raise "AWS_ACCESS_KEY_ID environment variable must be set" if ENV['AWS_ACCESS_KEY_ID'].nil? + raise "AWS_SECRET_ACCESS_KEY environment variable must be set" if ENV['AWS_SECRET_ACCESS_KEY'].nil? + end + + def self.clear_inventory_cache() + path = "#{ENV['HOME']}/.ansible/tmp" + cache_files = ["#{path}/ansible-ec2.cache", "#{path}/ansible-ec2.index"] + FileUtils.rm(cache_files) + end + + def self.generate_env_tag(env) + return { "environment" => env } + end + + def self.generate_env_tag_name(env) + h = generate_env_tag(env) + return "tag_#{h.keys.first}_#{h.values.first}" + end + + def self.generate_host_type_tag(host_type) + return { "host-type" => host_type } + end + + def self.generate_host_type_tag_name(host_type) + h = generate_host_type_tag(host_type) + return "tag_#{h.keys.first}_#{h.values.first}" + end + + def self.generate_env_host_type_tag(env, host_type) + return { "env-host-type" => "#{env}-#{host_type}" } + end + + def self.generate_env_host_type_tag_name(env, host_type) + h = generate_env_host_type_tag(env, host_type) + return "tag_#{h.keys.first}_#{h.values.first}" + end + end + end +end |