Class: Niso::Cloud::DropletKit

Inherits:
Base
  • Object
show all
Defined in:
lib/niso/cloud/droplet_kit.rb

Instance Method Summary collapse

Methods inherited from Base

#ask, #ask_menu, #assign_config, #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 Niso::Cloud::Base

Instance Method Details

#choose(key, result) ⇒ Object



89
90
91
92
93
94
# 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_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
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
82
83
84
85
86
87
# 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_teardownObject



96
97
98
99
# File 'lib/niso/cloud/droplet_kit.rb', line 96

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

#setup_clientObject

Set the client for DropletKit

DropletKit::DropletResource



104
105
106
107
108
109
110
111
# 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..info()
  rescue ::DropletKit::Error => e
    abort_with e.message
  end
end