Class: Tugboat::Middleware::AddKey

Inherits:
Base
  • Object
show all
Defined in:
lib/tugboat/middleware/add_key.rb

Constant Summary

Constants inherited from Base

Base::CLEAR, Base::GREEN, Base::RED, Base::YELLOW

Instance Method Summary collapse

Methods inherited from Base

#check_response_success, #get_droplet_list, #initialize, #print_droplet_info, #print_droplet_info_full, #response_stringify, #restart_droplet, #verify_credentials, #wait_for_state

Constructor Details

This class inherits a constructor from Tugboat::Middleware::Base

Instance Method Details

#call(env) ⇒ Object



4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tugboat/middleware/add_key.rb', line 4

def call(env)
  ocean = env['barge']

  if env['add_key_pub_key']
    pub_key_string = env['add_key_pub_key']
  else
    if env['add_key_file_path']
      pub_key_string = File.read(env['add_key_file_path'])
    else
      possible_keys = Dir.glob("#{ENV['HOME']}/.ssh/*.pub")

      # Only show hinted keys if the user has any
      unless possible_keys.empty?
        say "Possible public key paths from #{ENV['HOME']}/.ssh:"
        say
        possible_keys.each do |key_file|
          say key_file.to_s
        end
        say
      end

      ssh_key_file = ask 'Enter the path to your SSH key:'
      pub_key_string = File.read(ssh_key_file.to_s)
    end
  end

  say "Queueing upload of SSH key '#{env['add_key_name']}'...", nil, false

  response = ocean.key.create name: env['add_key_name'],
                              public_key: pub_key_string

  unless response.success?
    say "Failed to create key: #{response.message}", :red
    exit 1
  end

  say 'SSH Key uploaded', :green
  say
  say "Name: #{response.ssh_key.name}"
  say "ID: #{response.ssh_key.id}"

  @app.call(env)
end