Class: Sunzi::Vps::Compute::Vultr

Inherits:
Base
  • Object
show all
Defined in:
lib/sunzi/vps/compute/vultr.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, ids: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sunzi/vps/compute/vultr.rb', line 55

def choose(key, result, ids: nil)
  abort "API returned for #{key}!" if result[:status] != 200
  result = result[:result].map{|id,hash| hash.merge(id: id) }

  # Filter out items not included in ids
  if ids
    ids = ids.map(&:to_s)
    result.reject!{|h| !ids.include?(h[:id].to_s) }
  end

  rows = result.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.keys, rows: rows
  say table

  @attributes[key] = ask("which #{key}?: ", default: result.first[:id], limited_to: result.map{|i| i[:id] })
  result.find{|i| i[:id].to_s == @attributes[key] }
end

#do_downObject



83
84
85
86
87
# File 'lib/sunzi/vps/compute/vultr.rb', line 83

def do_down
  say 'deleting instance...'
  result = client::Server.destroy(SUBID: @instance[:subid])
  abort result[:result] if result[:status] != 200
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
# File 'lib/sunzi/vps/compute/vultr.rb', line 6

def do_up
  result = choose(:plan, client::Plans.list)
  choose(:region, client::Regions.list, ids: result['available_locations'])

  # Choose an image
  result = client::OS.list
  if config.distributions_filter
    result[:result].reject!{|id, hash| hash['name'] !~ Regexp.new(config.distributions_filter, Regexp::IGNORECASE) }
  end
  choose(:os, result)

  # Go ahead?
  proceed?

  # Create
  say 'creating a new instance...'
  result = client::Server.create(
    label: @name,
    DCID: @attributes[:region],
    VPSPLANID: @attributes[:plan],
    OSID: @attributes[:os]
  )

  subid = result[:result]['SUBID']

  say "Created a new instance (id: #{subid}). Booting..."

  begin
    sleep 3
    result = client::Server.list_ipv4(SUBID: subid)
    public_ip = result[:result][subid].first['ip']
  end while public_ip == '0.0.0.0'

  say "Done. ip address = #{public_ip}"

  @instance = {
    subid: subid,
    env:  @env,
    host: @host,
    fqdn: @fqdn,
    name: @name,
    public_ip:  public_ip,
    plan:       @attributes[:plan],
    region:     @attributes[:region],
    os:         @attributes[:os],
  }

end