Module: DTK::Common::AuxMixin

Included in:
Aux
Defined in:
lib/auxiliary.rb

Instance Method Summary collapse

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_usernameObject



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_idObject



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_dnsObject



85
86
87
# File 'lib/auxiliary.rb', line 85

def get_ec2_public_dns()
  ('public-hostname')
end

#get_macaddressObject



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_keyObject



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

Returns:

  • (Boolean)


100
101
102
# File 'lib/auxiliary.rb', line 100

def platform_is_linux?()
  RUBY_PLATFORM.downcase.include?("linux")
end

#platform_is_windows?Boolean

Returns:

  • (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_dirObject



116
117
118
119
120
121
122
# File 'lib/auxiliary.rb', line 116

def running_process_home_dir()
  if platform_is_windows?()
    File.expand_path('~')
  else
    Etc.getpwuid(Process.uid).dir
  end
end

#running_process_userObject



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