Skip to content

Commit

Permalink
Add option to transfer an image, rename a droplet and to add an ssh key
Browse files Browse the repository at this point in the history
  • Loading branch information
manishval committed Jun 3, 2013
1 parent 0d027f8 commit 98f54b3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ Disable automatic backups

$ pearl droplet disable backups -d <droplet id>

Rename droplet

$ pearl droplet rename <name> -d <droplet id>

Destroy droplet

$ pearl droplet destroy -d <droplet id>
Expand Down Expand Up @@ -119,19 +123,27 @@ To destroy an image

$ pearl image destroy -i <image id>

To transfer an image

$ pearl image transfer -i <image id> -r <region id>

### SSH Keys

Display all ssh keys

$ pearl ssh_keys

Add a ssh key

$ pearl ssh_key add <name> -p <public ssh key>

To view a ssh key

$ pearl ssh_key -k <ssh_key_id>
$ pearl ssh_key -k <ssh key id>

To delete a ssh key

$ paerl ssh_key destroy -k <ssh_key_id>
$ pearl ssh_key destroy -k <ssh key id>

## TODO

Expand Down
33 changes: 31 additions & 2 deletions lib/pearl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.execute(*args)
region = nil
size = nil
ssh_key_ids = nil
ssh_key_pub = nil
public_ssh_key = nil

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

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

opts.on('-p', '--public_ssh_key [PUBLIC SSH KEY]', 'Public SSH Key to add to the droplet') do |k|
public_ssh_key = p
end

opts.on('-v', '--version', 'Display version') do
puts Pearl::VERSION
exit
Expand Down Expand Up @@ -177,6 +181,15 @@ def self.execute(*args)

Pearl.disable_backups(droplet_id)
exit
when /\Adroplet rename [\w\s]{3,}\z/i
droplet_id = droplet.to_i
raise 'Error: Invalid droplet id.' if droplet_id == 0 || !droplet_id.is_a?(Fixnum)

name = command.split(' ', 3)[2]
raise 'Error: Invalid droplet name.' if name.nil? || name.length <= 0

Pearl.rename(droplet_id, name)
exit
when /\Adroplet (destroy|delete)\z/i
droplet_id = droplet.to_i
raise 'Error: Invalid droplet id.' if droplet_id == 0 || !droplet_id.is_a?(Fixnum)
Expand Down Expand Up @@ -204,6 +217,15 @@ def self.execute(*args)

Pearl.destroy_image(image_id)
exit
when /\Aimage (transfer)\z/i
image_id = image.to_i
raise 'Error: Invalid image id.' if image_id == 0 || !image_id.is_a?(Fixnum)

region_id = region.to_i
raise 'Error: Invalid region id.' if region_id == 0 || !region_id.is_a?(Fixnum)

Pearl.transfer(image_id, region_id)
exit
when /\Aregions\z/i
Pearl.regions
exit
Expand All @@ -217,7 +239,14 @@ def self.execute(*args)
raise 'Error: Invalid SSH Key ID.' if ssh_key_ids == 0 || !ssh_key_ids.is_a?(Fixnum)
Pearl.ssh_key(ssh_key_ids)
exit
when /\sssh_key add [\w\-\.]{3,}\z/i
name = command.split(' ', 3)[2]
raise 'Error: Invalid ssh key name.' if name.nil? || name.length <= 0

raise 'Error: Invalid public ssh key id.' if public_ssh_key.nil? || public_ssh_key.nil?

Pearl.add_ssh_key(name, public_ssh_key)
exit
when /\Assh_key (destroy|delete)\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)
Expand Down
7 changes: 6 additions & 1 deletion lib/pearl/client/droplets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def resize(id, size_id)
end

def snapshot(id, name)
puts name
params = { name: name }
response = request("droplets/#{id}/snapshot", params).body
pretty_event("Creating snapshot '#{name}' of droplet #{id}", response)
Expand Down Expand Up @@ -88,6 +87,12 @@ def disable_backups(id)
pretty_event("Disabling auto backups for droplet #{id}", response)
end

def rename(id, name)
params = { name: name }
response = request("droplets/#{id}/rename", params).body
pretty_event("Renaming droplet '#{id}' to #{name}", response)
end

def destroy_droplet(id)
response = request("droplets/#{id}/destroy").body
pretty_event("Destroying droplet #{id}", response)
Expand Down
6 changes: 6 additions & 0 deletions lib/pearl/client/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def destroy_image(id)
response = request("images/#{id}/destroy").body
pretty_event("Destroying image #{id}", response)
end

def transfer(id, region)
params = { region_id: region }
response = request("images/#{id}/transfer", params).body
pretty_event("Transferring image '#{id}' to region #{region}", response)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/pearl/client/sshkeys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def ssh_key(id)
pretty_table('SSH Key', response)
end

def add_ssh_key(name, public_ssh_key)
params = { name: name,
public_ssh_key: public_ssh_key
}
response = request("ssh_keys/new", params).body
pretty_table("SSH Key: #{name}", response)
end

def delete_ssh_key(id)
response = request("ssh_keys/#{id}/destroy").body
pretty_basic("Deleting ssh key #{id}", response)
Expand Down
2 changes: 1 addition & 1 deletion lib/pearl/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Pearl
VERSION = "0.0.6"
VERSION = "0.0.7"
end

0 comments on commit 98f54b3

Please sign in to comment.