Class: Railshoster::Utilities
- Inherits:
-
Object
- Object
- Railshoster::Utilities
- Defined in:
- lib/railshoster/utilities.rb
Class Method Summary collapse
-
.backup_file(path) ⇒ Object
Creates a backup of a file.
- .find_public_ssh_keys(ssh_dir = File.join(get_user_home, ".ssh"), options = {:verbose => true}) ⇒ Object
- .get_user_home ⇒ Object
- .parse_public_ssh_key(key) ⇒ Object
- .parse_public_ssh_key_file(path_to_key) ⇒ Object
- .select_public_ssh_key(ssh_dir = File.join(get_user_home, ".ssh"), options = {:verbose => true}) ⇒ Object
Class Method Details
.backup_file(path) ⇒ Object
Creates a backup of a file. Since this is a recursive method you’ll never loose a file. it also creates backups of backups of …
Examples
myfile -> myfile.bak if there is already a myfile.bak: myfile.bak -> myfile.bak.bak myfile -> myfile.bak …
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/railshoster/utilities.rb', line 16 def self.backup_file(path) backup_path = path + ".bak" if File.exists?(backup_path) then # There is already a backup, so we need to backup the backup first. backup_file(backup_path) end FileUtils.cp(path, backup_path) end |
.find_public_ssh_keys(ssh_dir = File.join(get_user_home, ".ssh"), options = {:verbose => true}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/railshoster/utilities.rb', line 58 def self.find_public_ssh_keys(ssh_dir = File.join(get_user_home, ".ssh"), = {:verbose => true}) files_in_ssh_dir = Dir.glob(File.join(ssh_dir, "*.pub")) ssh_keys = [] files_in_ssh_dir.each do |filepath| filepathname = Pathname.new(filepath) begin ssh_keys << parse_public_ssh_key_file(filepathname) rescue InvalidPublicSshKeyError => e puts "\tNotice: #{filepathname} is not a valid public ssh key: #{e.}" if ( && [:verbose]) end end ssh_keys end |
.get_user_home ⇒ Object
27 28 29 30 31 |
# File 'lib/railshoster/utilities.rb', line 27 def self.get_user_home homes = ["HOME", "HOMEPATH"] home_key = homes.detect { |h| ENV[h] != nil } ENV[home_key] end |
.parse_public_ssh_key(key) ⇒ Object
82 83 84 85 86 |
# File 'lib/railshoster/utilities.rb', line 82 def self.parse_public_ssh_key(key) format_identifier, key_data = key.split(" ") raise InvalidPublicSshKeyError.new("Couldn't recognize both format_identifier and/or key_data. One is missing") unless(format_identifier && key_data) {:format => format_identifier.chomp, :key_data => key_data.chomp, :key => key.chomp} end |
.parse_public_ssh_key_file(path_to_key) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/railshoster/utilities.rb', line 75 def self.parse_public_ssh_key_file(path_to_key) key = File.open(path_to_key).read key_hash = parse_public_ssh_key(key) key_hash[:path] = path_to_key key_hash end |
.select_public_ssh_key(ssh_dir = File.join(get_user_home, ".ssh"), options = {:verbose => true}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/railshoster/utilities.rb', line 33 def self.select_public_ssh_key(ssh_dir = File.join(get_user_home, ".ssh"), = {:verbose => true}) keys = Railshoster::Utilities.find_public_ssh_keys(ssh_dir, ) if keys.size == 0 raise NoSshKeyGivenError.new("No SSH key given. Use the ssh-keygen command to generate one.") end return keys.first if keys.size == 1 puts "\nThere are multiple public ssh keys. Please choose your deploy key:" keys.each_with_index do |key, i| puts "#{i+1}) #{Pathname.new(key[:path]).basename.to_s}" end selected_key = nil while selected_key.nil? do print "Your choice: " index = (STDIN.gets.chomp.to_i - 1) selected_key = keys[index] puts "Invalid choice!" unless selected_key end selected_key end |