Module: DTK::Common::AuxMixin
- Included in:
- Aux
- Defined in:
- lib/auxiliary.rb
Instance Method Summary collapse
- #convert_keys_to_symbols(hash) ⇒ Object
- #dtk_instance_repo_username ⇒ Object
- #get_ec2_instance_id ⇒ Object
- #get_ec2_public_dns ⇒ Object
- #get_macaddress ⇒ Object
- #get_ssh_rsa_pub_key ⇒ Object
- #hash_subset(hash, keys_subset, opts = {}) ⇒ Object
- #platform_is_linux? ⇒ Boolean
- #platform_is_windows? ⇒ Boolean
- #running_process_home_dir ⇒ Object
- #running_process_user ⇒ Object
- #snake_to_camel_case(snake_case) ⇒ Object
- #update_ssh_known_hosts(remote_host) ⇒ Object
Instance Method Details
#convert_keys_to_symbols(hash) ⇒ Object
58 59 60 |
# File 'lib/auxiliary.rb', line 58 def convert_keys_to_symbols(hash) hash.keys.inject(Hash.new){|h,k|h.merge(k.to_sym => hash[k])} end |
#dtk_instance_repo_username ⇒ Object
62 63 64 65 66 |
# File 'lib/auxiliary.rb', line 62 def dtk_instance_repo_username() #on ec2 changing mac addresses; so selectively pick instance id on ec2 unique_id = get_ec2_instance_id() || get_macaddress().gsub(/:/,'-') "dtk-#{unique_id}" end |
#get_ec2_instance_id ⇒ Object
89 90 91 92 93 94 |
# File 'lib/auxiliary.rb', line 89 def get_ec2_instance_id() # @ec2_instance_id_cached used because it could have tried to get this info and result was null return @ec2_instance_id if @ec2_instance_id_cached @ec2_instance_id_cached = true @ec2_instance_id = ('instance-id') end |
#get_ec2_public_dns ⇒ Object
85 86 87 |
# File 'lib/auxiliary.rb', line 85 def get_ec2_public_dns() ('public-hostname') end |
#get_macaddress ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/auxiliary.rb', line 77 def get_macaddress() return @macaddress if @macaddress #TODO: may just use underlying routines for facter - macaddress require 'facter' collection = ::Facter.collection @macaddress = collection.fact('macaddress').value end |
#get_ssh_rsa_pub_key ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/auxiliary.rb', line 34 def get_ssh_rsa_pub_key() path = "#{running_process_home_dir()}/.ssh/id_rsa.pub" begin File.open(path){|f|f.read}.chomp rescue Errno::ENOENT raise Error.new("user (#{ENV['USER']}) does not have a public key under #{path}") rescue => e raise e end end |
#hash_subset(hash, keys_subset, opts = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/auxiliary.rb', line 45 def hash_subset(hash,keys_subset,opts={}) keys_subset.inject(Hash.new) do |h,k| index = k.kind_of?(Hash) ? k.keys.first : k if opts[:no_non_nil] and hash[index].nil? then h elsif not hash.has_key?(index) then h else key = k.kind_of?(Hash) ? k.values.first : k val = hash[index] h.merge(key => val) end end end |
#platform_is_linux? ⇒ Boolean
100 101 102 |
# File 'lib/auxiliary.rb', line 100 def platform_is_linux?() RUBY_PLATFORM.downcase.include?("linux") end |
#platform_is_windows? ⇒ Boolean
104 105 106 |
# File 'lib/auxiliary.rb', line 104 def platform_is_windows?() RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw") end |
#running_process_home_dir ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/auxiliary.rb', line 116 def running_process_home_dir() if platform_is_windows?() File.('~') else Etc.getpwuid(Process.uid).dir end end |
#running_process_user ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/auxiliary.rb', line 108 def running_process_user() if platform_is_windows?() Etc.getlogin else Etc.getpwuid(Process.uid).name end end |
#snake_to_camel_case(snake_case) ⇒ Object
96 97 98 |
# File 'lib/auxiliary.rb', line 96 def snake_to_camel_case(snake_case) snake_case.gsub(/(^|_)(.)/) { $2.upcase } end |
#update_ssh_known_hosts(remote_host) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/auxiliary.rb', line 68 def update_ssh_known_hosts(remote_host) fingerprint = `ssh-keyscan -H -t rsa #{remote_host}` ssh_known_hosts = "#{running_process_home_dir()}/.ssh/known_hosts" if File.file?(ssh_known_hosts) `ssh-keygen -f "#{ssh_known_hosts}" -R #{remote_host}` end File.open(ssh_known_hosts,"a"){|f| f << "#{fingerprint}\n"} end |