Class: Kontena::Plugin::Cloud::Platform::CreateCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Common
Defined in:
lib/kontena/plugin/cloud/platform/create_command.rb

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from Common

#cached_platforms, #current_organization, #current_platform, #fetch_platforms, #fetch_platforms_for_org, #find_platform_by_name, #login_to_platform, #parse_platform_name, #platform_config_exists?, #prompt_platform, #require_platform

Methods included from CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#create_platform(name, organization, type, initial_size, region) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 86

def create_platform(name, organization, type, initial_size, region)
  data = {
    attributes: {
      "name" => name,
      "initial-size" => initial_size,
      "hosted-type" => type
    },
    relationships: {
      region: {
        "data" => { "type" => "region", "id" => region }
      }
    }
  }
  data[:attributes]['version'] = self.version if self.version
  data = cloud_client.post("/organizations/#{organization}/platforms", { data: data })['data']
  Kontena::Cli::Models::Platform.new(data)
end

#executeObject



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
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 17

def execute
  self.name = prompt.ask("Name:") unless self.name
  self.organization = prompt_organization unless self.organization
  self.type = prompt_type unless self.type
  if self.type == 'mini' && self.region
    exit_with_error "mini does not support region selection"
  elsif self.type == 'mini'
    self.region = nil
    self.initial_size = 1 unless self.initial_size
  else
    self.region = prompt_region unless self.region
    self.initial_size = 3 unless self.initial_size
  end

  platform = nil
  spinner_text = "Creating platform #{pastel.cyan(name)}"
  spinner_text = spinner_text + " to region #{pastel.cyan(region)}" if region
  spinner spinner_text do
    platform = create_platform(name, organization, type, initial_size, region)
  end
  spinner "Waiting for platform #{pastel.cyan(name)} to come online" do
    while !platform.online? do
      sleep 5
      platform = find_platform_by_name(platform.id, organization)
    end
  end
  use_platform(platform) if use?

  puts ""
  puts "  Platform #{pastel.cyan(name)} needs at least #{pastel.cyan(self.initial_size)} node(s) to be functional."
  puts "  You can add nodes from Kontena Cloud ('kontena cloud node create --count #{self.initial_size}') or you can bring your own nodes (https://www.kontena.io/docs/using-kontena/install-nodes/)."
  puts ""
end

#prompt_organizationObject



61
62
63
64
65
66
67
68
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 61

def prompt_organization
  organizations = cloud_client.get('/organizations')['data']
  prompt.select("Choose organization:") do |menu|
    organizations.each do |o|
      menu.choice o.dig('attributes', 'name')
    end
  end
end

#prompt_regionObject



70
71
72
73
74
75
76
77
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 70

def prompt_region
  regions = cloud_client.get('/regions')['data']
  prompt.select("Choose region:") do |menu|
    regions.each do |d|
      menu.choice d.dig('attributes', 'name'), d['id']
    end
  end
end

#prompt_typeObject



79
80
81
82
83
84
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 79

def prompt_type
  prompt.select("Platform type:") do |menu|
    menu.choice "standard (high-availability, business critical services)", "standard"
    menu.choice "mini (non-business critical services)", "mini"
  end
end

#use_platform(platform) ⇒ Object

Parameters:



52
53
54
55
56
57
58
59
# File 'lib/kontena/plugin/cloud/platform/create_command.rb', line 52

def use_platform(platform)
  platform_name = "#{organization}/#{name}"
  (platform_name, platform.url)
  spinner "Switching to use platform #{pastel.cyan(platform_name)}" do
    config.current_grid = name
    config.write
  end
end