def self.add_ssh_config_host(rhc_domain, ssh_key_file_path, ssh_config, ssh_config_d)
puts "Checking ~/.ssh/config"
ssh_key_file_name = File.basename(ssh_key_file_path)
if ssh_key_file_path =~ /^#{ENV['HOME']}/
ssh_key_file_path = ssh_key_file_path[ENV['HOME'].length..-1]
if ssh_key_file_path =~ /^\// || ssh_key_file_path =~ /^\\/
ssh_key_file_path = '~' + ssh_key_file_path
else
ssh_key_file_path = '~/' + ssh_key_file_path
end
end
if (ssh_key_file_name != 'id_rsa')
found = false
begin
File.open(ssh_config, "r") do |sline|
while(line = sline.gets)
if line.to_s.index("Host *.#{rhc_domain}") == 0
found = true
break
end
end
end
rescue Errno::EACCES
puts "Could not read from #{ssh_config}"
puts "Reason: " + $!
puts
puts "Please correct this first. Then run rerun."
puts
exit 213
rescue Errno::ENOENT
puts "Could not find #{ssh_config}. This is ok, continuing"
end
if found
puts "Found #{rhc_domain} in ~/.ssh/config... No need to adjust"
else
puts " Adding #{rhc_domain} to ~/.ssh/config"
begin
f = File.open(ssh_config, "a")
f.puts "\n# Added by 'rhc domain create' on \#{`date`}\nHost *.\#{rhc_domain}\n IdentityFile \#{ssh_key_file_path}\n VerifyHostKeyDNS yes\n StrictHostKeyChecking no\n UserKnownHostsFile ~/.ssh/libra_known_hosts\n\n"
f.close
rescue Errno::EACCES
puts "Could not write to #{ssh_config}"
puts "Reason: " + $!
puts
puts "Please correct this first. Then run rerun."
puts
exit 214
rescue Errno::ENOENT
puts "Could not find directory: " + $!
puts "creating"
FileUtils.mkdir_p ssh_config_d
file = File.open(ssh_config, 'w')
file.close
retry
end
end
File.chmod(0700, ssh_config_d)
File.chmod(0600, ssh_config)
end
end