Class: Kontena::Plugin::Cloud::Node::CreateCommand

Inherits:
Command
  • Object
show all
Includes:
Cli::Common, Machine::RandomName, Common, PlatformOption, Organization::Common, Platform::Common
Defined in:
lib/kontena/plugin/cloud/node/create_command.rb

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from Platform::Common

#cached_platforms, #current_organization, #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 Organization::Common

#fetch_organizations, #prompt_organization

Methods included from PlatformOption

included

Methods included from Common

#cached_platforms_by_id, #compute_client, #compute_url, #config, #get_platform

Methods included from CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#create_node(platform, token) ⇒ Object



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

def create_node(platform, token)
  name = "#{generate_name}-#{rand(1..999)}"
  node_token = SecureRandom.hex(32)
  target_region = self.region || platform.region
  ssh_keys = []
  if ssh_key
    begin
      key_path = File.realpath(ssh_key)
      ssh_keys << File.read(key_path)
    rescue Errno::ENOENT
      exit_with_error "ssh key #{ssh_key} does not exist"
    end
  end

  spinner "Provisioning a node #{pastel.cyan(name)} to platform #{pastel.cyan(platform.to_path)}, region #{pastel.cyan(target_region)}" do
    client.post("grids/#{current_grid}/nodes", {
      name: name,
      token: node_token
    })

    data = {
      type: 'nodes',
      attributes: {
        name: name,
        type: self.type,
        region: target_region,
        tokens: {
          grid: token,
          node: node_token
        },
        'ssh-keys' => ssh_keys
      },
      relationships: {
        platform: {
          data: {
            type: 'platforms',
            id: platform.id
          }
        }
      }
    }
    compute_client.post("/organizations/#{current_organization}/nodes", { data: data })
  end
end

#current_platformObject



81
82
83
# File 'lib/kontena/plugin/cloud/node/create_command.rb', line 81

def current_platform
  ENV['KONTENA_PLATFORM'] || current_master.name
end

#default_countObject



77
78
79
# File 'lib/kontena/plugin/cloud/node/create_command.rb', line 77

def default_count
  prompt.ask("How many nodes?", default: 1).to_i
end

#default_typeObject



85
86
87
88
89
90
91
92
93
# File 'lib/kontena/plugin/cloud/node/create_command.rb', line 85

def default_type
  node_types = compute_client.get("/node_types")['data']
  prompt.select("Choose node type:") do |menu|
    menu.default 3
    node_types.each do |t|
      menu.choice "#{t['id']} (#{t.dig('attributes', 'cpus')}xCPU, #{t.dig('attributes', 'memory')}GB, #{t.dig('attributes', 'disk')}GB SSD)", t['id']
    end
  end
end

#executeObject



23
24
25
26
27
28
29
30
# File 'lib/kontena/plugin/cloud/node/create_command.rb', line 23

def execute
  org, platform = parse_platform_name(current_platform)
  platform = require_platform("#{org}/#{platform}")
  grid = client.get("grids/#{current_grid}")
  self.count.times do
    create_node(platform, grid['token'])
  end
end