class Niso::Cloud::DropletKit

Public Instance Methods

choose(key, result) click to toggle source
# File lib/niso/cloud/droplet_kit.rb, line 89
def choose(key, result)
  abort "no #{key} found!" if result.first.nil?
  # result.each{|i| say "#{i.slug}" }
  choices = result.map(&:slug).compact
  @attributes[:"#{key}"] = ask_menu("which #{key}?: ", choices)
end
do_setup() click to toggle source
# File lib/niso/cloud/droplet_kit.rb, line 6
def do_setup

  if @config["size"]
    # Choose size
    @attributes[:size] = @config["size"]
    say "using config size"
  else
    choose(:size, @client.sizes.all())
  end

  if @config["region"]
    # Choose region
    @attributes[:region] = @config["region"]
    say "using config region"
  else
    choose(:region, @client.regions.all())
  end

  # Choose an image
  if @config["image"]
    @attributes[:image] = @config["image"]
    say "using config image"
  else
    if @config['images_filter']
      result = @client.images.all(type: @config['images_filter'])
      result = result.select{|i| i.distribution.match Regexp.new(@config['images_filter'], Regexp::IGNORECASE) }
    end
    choose(:image, result)
  end

  # Go ahead?
  say " - name => #{@ui.color(@name, :blue, :bold)}"
  @attributes.each do |k,v|
    say " - #{k} => #{@ui.color(v, :blue, :bold)}"
  end
  if @config["private_networking"]
    say " - private_networking => #{@ui.color(@config["private_networking"].to_s, :blue, :bold)}"
  end
  say " - account => #{@ui.color(@client_info.email, :blue, :bold)}"
  moveon = ask("Are you ready to go ahead? (y/n) ", ['y','n'])
  exit unless moveon == 'y'

  ssh_keys = @client.ssh_keys.all.collect { |key| key.fingerprint }

  # Create
  say "creating a new droplet..."
  droplet = ::DropletKit::Droplet.new(
    name: @name,
    region: @attributes[:region],
    image: @attributes[:image],
    size: @attributes[:size],
    private_networking: @config["private_networking"],
    ssh_keys: ssh_keys
  )

  begin
    result = @client.droplets.create(droplet)
  rescue ::DropletKit::FailedCreate => e
    abort_with e.message
  end

  @droplet_id = result.id
  say "Created a new droplet (id: #{@droplet_id}). Booting..."

  # Boot - we need this before getting public IP
  while @client.droplets.find(id: @droplet_id).status.downcase != 'active'
    sleep 3
  end

  @networks = @client.droplets.find(id: @droplet_id).networks.to_h.to_json
  say "Done."

  @instance = {
    droplet_id: @droplet_id,
    env: @env,
    name: @name,
    networks: JSON.parse(@networks),
    size: @attributes[:size],
    region: @attributes[:region],
    image: @attributes[:image],
  }
end
do_teardown() click to toggle source
# File lib/niso/cloud/droplet_kit.rb, line 96
def do_teardown
  say 'deleting droplet...'
  @client.droplets.delete(id: @instance[:droplet_id])
end
setup_client() click to toggle source

Set the client for DropletKit

DropletKit::DropletResource

# File lib/niso/cloud/droplet_kit.rb, line 104
def setup_client
  begin
    @client = ::DropletKit::Client.new(access_token: @config['access_token'])
    @client_info = @client.account.info()
  rescue ::DropletKit::Error => e
    abort_with e.message
  end
end