Module: Kontena::Plugin::DigitalOcean::Prompts

Included in:
Master::CreateCommand, Nodes::CreateCommand, Nodes::RestartCommand, Nodes::TerminateCommand
Defined in:
lib/kontena/plugin/digital_ocean/prompts.rb

Instance Method Summary collapse

Instance Method Details

#ask_do_tokenObject



2
3
4
5
6
7
8
# File 'lib/kontena/plugin/digital_ocean/prompts.rb', line 2

def ask_do_token
  if self.token.nil?
    prompt.ask('DigitalOcean API token: ', echo: false)
  else
    self.token
  end
end

#ask_droplet_region(do_token) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kontena/plugin/digital_ocean/prompts.rb', line 10

def ask_droplet_region(do_token)
  if self.region.nil?
    prompt.select("Choose a datacenter region: ") do |menu|
      do_client = DropletKit::Client.new(access_token: do_token)
      do_client.regions.all.sort_by{|r| r.slug }.each{ |region|
        menu.choice region.name, region.slug
      }
    end
  else
    self.region
  end
end

#ask_droplet_size(do_token, do_region) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kontena/plugin/digital_ocean/prompts.rb', line 23

def ask_droplet_size(do_token, do_region)
  if self.size.nil?
    prompt.select("Choose droplet size: ") do |menu|
      do_client = DropletKit::Client.new(access_token: do_token)
      do_client.sizes.all.to_a.select{ |s| s.memory > 1000 }.sort_by{|s| s.memory }.each{ |size|
        if size.regions.include?(do_region)
          memory = size.memory.to_i / 1024
          menu.choice "#{memory}GB/#{size.vcpus}CPU/#{size.disk}GB ($#{size.price_monthly.to_i}/mo)", size.slug
        end
      }
    end
  else
    self.size
  end
end

#ask_node(token) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kontena/plugin/digital_ocean/prompts.rb', line 39

def ask_node(token)
  if self.name.nil?
    nodes = client(token).get("grids/#{current_grid}/nodes")
    nodes = nodes['nodes'].select{ |n|
      n['labels'] && n['labels'].include?('provider=digitalocean'.freeze)
    }
    raise "Did not find any nodes with label provider=digitalocean" if nodes.size == 0
    prompt.select("Select node: ") do |menu|
      nodes.sort_by{|n| n['node_number'] }.reverse.each do |node|
        initial = node['initial_member'] ? '(initial) ' : ''
        menu.choice "#{node['name']} #{initial}", node['name']
      end
    end
  else
    self.name
  end
end