Class: Kontena::Plugin::Cloud::Platform::WizardCommand

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

Constant Summary collapse

INFRASTRUCTURE =
{
  'cloud' => 'Kontena Cloud',
  'aws' => 'Amazon Web Services (EC2)',
  'digitalocean' => 'DigitalOcean',
  'vagrant' => 'Vagrant (with VirtualBox)'
}

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_awsObject



131
132
133
134
135
136
137
138
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 131

def create_aws
  Kontena.run!([
    'aws', 'node', 'create',
    '--version', self.version,
    '--count', self.initial_size,
    '--region', self.region
  ])
end

#create_cloudObject



119
120
121
122
123
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 119

def create_cloud
  Kontena.run!([
    'cloud', 'node', 'create', '--count', self.initial_size
  ])
end

#create_digitaloceanObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 140

def create_digitalocean
  do_region = case self.region
  when 'eu-west-1'
    'lon1'
  when 'us-east-1'
    'nyc1'
  end

  Kontena.run!([
    'digitalocean', 'node', 'create',
    '--version', self.version,
    '--count', self.initial_size,
    '--channel', 'stable',
    '--region', do_region
  ])
end

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



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 105

def create_platform(name, organization, initial_size, region, type)
  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

#create_vagrantObject



125
126
127
128
129
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 125

def create_vagrant
  Kontena.run!([
    'vagrant', 'node', 'create', '--version', self.version, '--instances', self.initial_size
  ])
end

#executeObject



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

def execute
  exit_with_error "You don't have any supported plugins installed (#{INFRASTRUCTURE.keys.join(', ')})" if infrastucture_providers.empty?

  self.name = prompt.ask("Name:") unless self.name
  self.organization = prompt_organization unless self.organization
  self.type = prompt_type unless self.type
  self.region = prompt_region if self.region.nil? && self.type != 'mini'
  unless self.initial_size
    if self.type == 'mini'
      self.initial_size = 1
    else
      self.initial_size = 3
    end
  end

  platform = spinner "Creating platform master #{pastel.cyan(self.name)} to region #{pastel.cyan(self.region)}" do
    create_platform(self.name, self.organization, self.initial_size, self.region, self.type)
  end
  spinner "Waiting for platform master #{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)

  infra = prompt.select("Choose infrastructure provider for platform nodes") do |menu|
    INFRASTRUCTURE.each do |id, name|
      menu.choice name, id
    end
  end

  case infra
  when 'cloud'
    create_cloud
  when 'aws'
    create_aws
  when 'digitalocean'
    create_digitalocean
  when 'upcloud'
    create_upcloud
  when 'vagrant'
    create_vagrant
  end

  spinner "Platform #{pastel.cyan(platform.name)} is now ready!"
end

#infrastucture_providersObject



157
158
159
160
161
162
163
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 157

def infrastucture_providers
  if @infrastucture_providers.nil?
    main_commands = Kontena::MainCommand.recognised_subcommands.flat_map(&:names)
    @infrastucture_providers = INFRASTRUCTURE.dup.delete_if { |k, v| !main_commands.include?(k) }
  end
  @infrastucture_providers
end

#prompt_organizationObject



87
88
89
90
91
92
93
94
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 87

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



96
97
98
99
100
101
102
103
# File 'lib/kontena/plugin/cloud/platform/wizard_command.rb', line 96

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



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

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:



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

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