Sha256: 4db50a3d0cc8a4946054b2208d80848f01762d349e10b9eb4c5a3c3dbfe1651c

Contents?: true

Size: 1.53 KB

Versions: 20

Compression:

Stored size: 1.53 KB

Contents

require 'droplet_kit'

class DigitalOcean < Provider

  def create(config)
    droplet = DropletKit::Droplet.new config
    resp = @connection.droplets.create droplet
    if resp == droplet
      ip = @connection.find_droplet_by_name(config["name"]).networks["v4"].first.ip_address
      say "Droplet #{config["name"]} (IP: #{ip}) successfully created!"
    else
      raise resp
    end
  end

  def delete(host)
    begin
      id = @connection.find_droplet_by_name(host).id
      resp = @connection.droplets.delete id: id
    rescue NoMethodError
      raise "#{host} does not exist"
    rescue
      raise resp
    end

    if resp.is_a?(TrueClass)
      say "Droplet #{host} successfully deleted!"
    else
      raise resp
    end
  end

  def list_params
    say "\nSizes:"
    @connection.sizes.all.each do |i|
      say "   - #{i.slug.ljust(6)} =>   $#{i.price_monthly}/mo"
    end

    say "\nRegions:"
    @connection.regions.all.each do |i|
      say "   - #{i.slug.ljust(6)} =>   #{i.name}"
    end

    say "\nImages:"
    @connection.images.all.each do |i|
      say "   - #{i.slug.ljust(20)} =>   #{i.distribution} #{i.name}" unless i.slug.nil?
    end

    say "\nSSH Keys:"
    @connection.ssh_keys.all.each do |i|
      say "   - #{i.fingerprint} =>   #{i.name}"
    end
  end

  private
  def create_connection(config)
    @connection = DropletKit::Client.new(access_token: config["token"])
  end
end

module DropletKit
  class Client
    def find_droplet_by_name(host_name)
      self.droplets.all.find{|d| d.name == host_name}
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
open-dock-0.1.18 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.17 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.16 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.15 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.14 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.13 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.11 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.10 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.9 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.8 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.7 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.6 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.5 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.3 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.2 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.1 lib/open-dock/providers/digital_ocean.rb
open-dock-0.1.0 lib/open-dock/providers/digital_ocean.rb
open-dock-0.0.15 lib/open-dock/providers/digital_ocean.rb
open-dock-0.0.14 lib/open-dock/providers/digital_ocean.rb
open-dock-0.0.13 lib/open-dock/providers/digital_ocean.rb