Top Level Namespace

Defined Under Namespace

Classes: Pair

Constant Summary collapse

J =
MultiJson
SERVER_HOST =
"pairkit.com"
SERVER_WEB =
"http://#{SERVER_HOST}:80"
SERVER_SSH_USERNAME =
'pairkit'

Instance Method Summary collapse

Instance Method Details

#askObject



48
49
50
# File 'lib/ssh_keys.rb', line 48

def ask
  $stdin.gets.to_s.strip
end

#error(message) ⇒ Object



52
53
54
55
# File 'lib/ssh_keys.rb', line 52

def error(message)
  $stderr.puts(message)
  exit(1)
end

#generate_ssh_key(keyfile) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ssh_keys.rb', line 35

def generate_ssh_key(keyfile)
  ssh_dir = File.join(home_directory, ".ssh")
  unless File.exists?(ssh_dir)
    FileUtils.mkdir_p ssh_dir
    File.chmod(0700, ssh_dir)
  end
  `ssh-keygen -t rsa -N "" -f \"#{home_directory}}/.ssh/#{keyfile}\" 2>&1`
end

#get_key(key) ⇒ Object



44
45
46
# File 'lib/ssh_keys.rb', line 44

def get_key(key)
  File.read(key).chomp #strip trailing newline if present
end

#get_or_generate_ssh_keyObject


Thank you, Heroku gem, for nearly all of this!




7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ssh_keys.rb', line 7

def get_or_generate_ssh_key
  public_keys = Dir.glob("#{home_directory}/.ssh/*.pub").sort

  case public_keys.length
  when 0 then
    puts "Could not find an existing public key."
    puts "Let's generate one..."
    puts "Generating new SSH public key."
    generate_ssh_key("id_rsa")
    get_key("#{home_directory}/.ssh/id_rsa.pub")
  when 1 then
    puts "Using existing public key: #{public_keys.first}"
    get_key(public_keys.first)
  else
    puts "Found the following SSH public keys:"
    public_keys.each_with_index do |key, index|
      puts "#{index+1}) #{File.basename(key)}"
    end
    puts "Which would you like to use? (Enter number) "
    choice = ask.to_i - 1
    chosen = public_keys[choice]
    if choice == -1 || chosen.nil?
      error("Invalid choice.")
    end
    get_key(chosen)
  end
end

#home_directoryObject



57
58
59
# File 'lib/ssh_keys.rb', line 57

def home_directory
  running_on_windows? ? ENV['USERPROFILE'].gsub("\\","/") : ENV['HOME']
end

#running_on_windows?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ssh_keys.rb', line 61

def running_on_windows?
  RUBY_PLATFORM =~ /mswin32|mingw32/
end