Class: Sunzi::Cloud::DigitalOcean

Inherits:
Base
  • Object
show all
Defined in:
lib/sunzi/cloud/digital_ocean.rb

Instance Method Summary collapse

Methods inherited from Base

#ask, #assign_config_and_dns, #initialize, #instance_config_path, #proceed?, #provider_config_path, #setup, #teardown

Methods included from Utility

#abort_with, #exit_with, #say

Constructor Details

This class inherits a constructor from Sunzi::Cloud::Base

Instance Method Details

#assign_apiObject



69
70
71
# File 'lib/sunzi/cloud/digital_ocean.rb', line 69

def assign_api
  @api = ::DigitalOcean::API.new :api_key => @config['api_key'], :client_id => @config['client_id']
end

#choose(key, result) ⇒ Object



57
58
59
60
61
62
# File 'lib/sunzi/cloud/digital_ocean.rb', line 57

def choose(key, result)
  abort "no #{key} found!" if result.first.nil?
  result.each{|i| say "#{i.id}: #{i.name}" }
  @attributes[:"#{key}_id"] = ask("which #{key}?: ", Integer) {|q| q.in = result.map(&:id); q.default = result.first.id }
  @attributes[:"#{key}_name"] = result.find{|i| i.id == @attributes[:"#{key}_id"] }.name
end

#do_setupObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sunzi/cloud/digital_ocean.rb', line 6

def do_setup
  choose(:size, @api.sizes.list.sizes)
  choose(:region, @api.regions.list.regions)

  # Choose an image
  result = @api.images.list({'filter' => 'global'}).images
  if @config['distributions_filter']
    result = result.select{|i| i.distribution.match Regexp.new(@config['distributions_filter'], Regexp::IGNORECASE) }
  end
  choose(:image, result)

  # Go ahead?
  proceed?

  ssh_key_ids = @api.ssh_keys.list.ssh_keys.map(&:id).join(',')

  # Create
  say "creating a new droplets..."
  result = @api.droplets.create(:name => @name,
    :size_id    => @attributes[:size_id],
    :image_id   => @attributes[:image_id],
    :region_id  => @attributes[:region_id],
    :ssh_key_ids => ssh_key_ids)

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

  # Boot - we need this before getting public IP
  while @api.droplets.show(@droplet_id).droplet.status.downcase != 'active'
    sleep 3
  end

  @public_ip = @api.droplets.show(@droplet_id).droplet.ip_address
  say "Done. ip address = #{@public_ip}"

  @instance = {
    :droplet_id => @droplet_id,
    :env  => @env,
    :host => @host,
    :fqdn => @fqdn,
    :name => @name,
    :ip_address => @public_ip,
    :size_id      => @attributes[:size_id],
    :size_name    => @attributes[:size_name],
    :region_id    => @attributes[:region_id],
    :region_name  => @attributes[:region_name],
    :image_id     => @attributes[:image_id],
    :image_name   => @attributes[:image_name],
  }
end

#do_teardownObject



64
65
66
67
# File 'lib/sunzi/cloud/digital_ocean.rb', line 64

def do_teardown
  say 'deleting droplet...'
  @api.droplets.delete(@instance[:droplet_id])
end

#ip_keyObject



73
74
75
# File 'lib/sunzi/cloud/digital_ocean.rb', line 73

def ip_key
  :ip_address
end