Skip to content

Instantly share code, notes, and snippets.

@shtirlic
Created April 15, 2013 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shtirlic/5390788 to your computer and use it in GitHub Desktop.
Save shtirlic/5390788 to your computer and use it in GitHub Desktop.
Digital Ocean CLI on ruby Place it in ~/bin/dio
#!/usr/bin/env ruby
require 'httparty'
require 'ostruct'
require "thor"
CLIENT_ID = ENV['DIGITAL_OCEAN_CLIENT_ID']
API_ID = ENV['DIGITAL_OCEAN_API_ID']
class DigitalOceanApi
include HTTParty
base_uri 'https://api.digitalocean.com'
def initialize(client_id, api_key)
self.class.default_params :client_id => client_id, :api_key => api_key
end
def images(opts)
options = { query: opts }
self.class.get('/images/', options).parsed_response
end
def sizes
self.class.get('/sizes/').parsed_response
end
def droplets
self.class.get('/droplets/').parsed_response
end
def ssh_keys
self.class.get('/ssh_keys/').parsed_response
end
def regions
self.class.get('/regions/').parsed_response
end
def new_droplet(opts)
options = { query: opts }
resp = self.class.get('/droplets/new/', options).parsed_response
end
end
class DigitalOceanClient < Thor
include Thor::Actions
@@client = DigitalOceanApi.new(CLIENT_ID, API_ID)
desc "images", "Show images"
method_option :image, :type => :string, :aliases => ['-i'],
:desc => "image id for detais"
method_option :all, :type => :boolean, :aliases => ['-a'],
:desc => "all images", :default => false
def images
filter = options[:all] ? 'global': 'my_images'
opts = { Distribution: :distribution }
print_result @@client.images({ filter:filter })['images'], opts
end
desc 'droplets','Show droplets'
def droplets
opts ={ "Backups Active" => :backups_active, "Image" => :image_id,
"Region" => :region_id, "Size" => :size_id, "Status" => :status }
print_result @@client.droplets['droplets'], opts
end
desc 'sizes','Show sizes'
def sizes
print_result @@client.sizes['sizes']
end
desc 'regions','Show regions'
def regions
print_result @@client.regions['regions']
end
desc 'keys','Show ssh keys'
def keys
print_result @@client.ssh_keys['ssh_keys']
end
desc "spin [NAME]", "Spin new droplet"
method_option :image, :type => :string, :aliases => ['-i'],
:desc => "image id", :required => true
method_option :size, :type => :string, :aliases => ['-s'],
:desc => "size id", :required => true
method_option :region, :type => :string, :aliases => ['-r'],
:desc => "region id", :required => true
def spin(name)
image = options[:image].to_i
size = options[:size].to_i
region = options[:region].to_i
p @@client.new_droplet({ name:name, image_id:image, size_id:size, region_id:region })
end
private
def print_result(obj, opts = {})
options = { ID: :id, Name: :name }.merge(opts)
puts options.keys.inject(''){ |a,v| a + "#{v}\t" }
obj.each do |item|
item = OpenStruct.new(item)
puts options.values.inject(''){|a,v| a + item.send(v.to_sym).to_s + "\t" }
end
end
end
DigitalOceanClient.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment