Class: Sunzi::Vps::Compute::DigitalOcean

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

Instance Attribute Summary

Attributes inherited from Base

#api

Instance Method Summary collapse

Methods inherited from Base

#ask, #down, #initialize, #proceed?, #up

Constructor Details

This class inherits a constructor from Sunzi::Vps::Compute::Base

Instance Method Details

#choose(key, result) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sunzi/vps/compute/digital_ocean.rb', line 59

def choose(key, result)
  abort "no #{key} found!" if result.first.nil?

  rows = result.map(&:attributes).map do |h|
    h.values.map do |v|
      next v unless v.is_a?(Array)
      if v.size > 5
        v.first(5).join(', ') + ', ...'
      else
        v.join(', ')
      end
    end
  end

  table = Terminal::Table.new headings: result.first.attributes.keys, rows: rows
  say table

  @attributes[key] = ask("which #{key}?: ", default: result.first.slug, limited_to: result.map(&:slug))
end

#do_downObject


79
80
81
82
# File 'lib/sunzi/vps/compute/digital_ocean.rb', line 79

def do_down
  say 'deleting droplet...'
  client.droplets.delete(id: @instance[:droplet_id])
end

#do_upObject


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
56
57
# File 'lib/sunzi/vps/compute/digital_ocean.rb', line 6

def do_up
  choose(:size, client.sizes.all.to_a)
  choose(:region, client.regions.all.to_a)

  # Choose an image
  result = client.images.all
  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_keys = client.ssh_keys.all.map(&:fingerprint)

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

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

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

  public_ip = droplet.networks.v4.first.ip_address
  say "Done. ip address = #{public_ip}"

  @instance = {
    droplet_id: @droplet_id,
    env:  @env,
    host: @host,
    fqdn: @fqdn,
    name: @name,
    public_ip:  public_ip,
    size:       @attributes[:size],
    region:     @attributes[:region],
    image:      @attributes[:image],
  }
end