Skip to content

Commit

Permalink
Merge pull request #1 from marcqualie/feature/sshkeys
Browse files Browse the repository at this point in the history
Add ssh key on droplet creation
  • Loading branch information
manishval committed Apr 24, 2013
2 parents 32a6367 + b25ff8c commit 7840990
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ To view a droplet

To create a droplet

$ pearl droplet add <name> --size <size id> --image <image id> --region <region id>
$ pearl droplet add <name> --size <size id> --image <image id> --region <region id> --ssh_key_ids <ssh key ids>

or

$ pearl droplet add <name> -s <size id> -i <image id> -r <region id>
$ pearl droplet add <name> -s <size id> -i <image id> -r <region id> -k <ssh key ids>

Reboot a droplet

Expand Down
29 changes: 27 additions & 2 deletions lib/pearl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def self.execute(*args)
image = nil
region = nil
size = nil
ssh_key_ids = nil

opts = OptionParser.new do |opts|
opts.banner = "Usage: pearl [resource] [options]"
Expand All @@ -35,6 +36,10 @@ def self.execute(*args)
size = s
end

opts.on('-k', '--ssh_key_ids [KEY ID]', 'SSH Keys to add to the machine') do |k|
ssh_key_ids = k
end

opts.on('-v', '--version', 'Display version') do
puts Pearl::VERSION
exit
Expand Down Expand Up @@ -77,7 +82,13 @@ def self.execute(*args)
name = command.split(' ', 3)[2]
raise 'Error: Invalid droplet name.' if name.nil? || name.length <= 0

options = { name: name, image_id: image_id, region_id: region_id, size_id: size_id }
options = {
name: name,
image_id: image_id,
region_id: region_id,
size_id: size_id,
ssh_key_ids: ssh_key_ids
}

Pearl.create_droplet(options)
exit
Expand Down Expand Up @@ -195,9 +206,23 @@ def self.execute(*args)
when /\Aregions\z/i
Pearl.regions
exit

# SSH Keys
when /\Asshkeys\z/i
Pearl.sshkeys
exit
when /\Asshkey\z/i
ssh_key_ids = ssh_key_ids.to_i
raise 'Error: Invalid SSH Key ID.' if ssh_key_ids == 0 || !ssh_key_ids.is_a?(Fixnum)
Pearl.view_sshkey(ssh_key_ids)
exit

# Sizes
when /\Asizes\z/i
Pearl.sizes
exit

# Any other command
else
puts "Error: '#{command}' is an invalid command."
exit
Expand All @@ -212,4 +237,4 @@ def self.execute(*args)
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/pearl/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Client
include Images
include Regions
include Sizes
include Sshkeys

include PrettyTable

Expand All @@ -37,4 +38,4 @@ def authenticated?
@client_id != nil && @api_key != nil
end
end
end
end
5 changes: 3 additions & 2 deletions lib/pearl/client/droplets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def create_droplet(options = {})
params = { name: options[:name],
size_id: options[:size_id],
image_id: options[:image_id],
region_id: options[:region_id]
region_id: options[:region_id],
ssh_key_ids: options[:ssh_key_ids]
}
response = request("droplets/new", params).body
pretty_table("Droplet", response)
Expand Down Expand Up @@ -93,4 +94,4 @@ def destroy_droplet(id)
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/pearl/client/sshkeys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Pearl
class Client

module Sshkeys

def sshkeys
response = request("ssh_keys").body
pretty_table("SSH Keys", response)
end

def view_sshkey(id)
response = request("ssh_keys/#{id}").body
data = MultiJson.load(response, symbolize_keys: true)
ssh_key = data[:ssh_key][:ssh_pub_key]
data[:ssh_key].delete :ssh_pub_key
pretty_table('ssh key', MultiJson.dump(data))
print "\n"
print ssh_key
print "\n\n"
end

end
end
end

0 comments on commit 7840990

Please sign in to comment.