Module: Kontena::Plugin::Cloud::Platform::Common

Instance Method Summary collapse

Instance Method Details

#cached_platformsObject



7
8
9
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 7

def cached_platforms
  @cached_platforms ||= []
end

#current_organizationString, NilClass

Returns:

  • (String, NilClass)


41
42
43
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 41

def current_organization
  @current_organization || ENV['KONTENA_ORGANIZATION']
end

#current_platformObject



45
46
47
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 45

def current_platform
  self.current_grid
end

#fetch_platformsObject



11
12
13
14
15
16
17
18
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 11

def fetch_platforms
  all = []
  organizations = cloud_client.get('/organizations')['data']
  organizations.each do |org|
    all = all + fetch_platforms_for_org(org['id'])
  end
  all
end

#fetch_platforms_for_org(org_id) ⇒ Array<Kontena::Cli::Models::Platform>

Parameters:

  • org_id (String)

Returns:



22
23
24
25
26
27
28
29
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 22

def fetch_platforms_for_org(org_id)
  platforms = cloud_client.get("/organizations/#{org_id}/platforms")['data']
  platforms.map do |p|
    platform = Kontena::Cli::Models::Platform.new(p)
    cached_platforms << platform if cached_platforms.none?{|cached| platform.id == cached.id }
    platform
  end
end

#find_platform_by_name(name, org, cache = true) ⇒ Kontena::Cli::Models::Platform, NilClass

Parameters:

  • name (String)
  • org (String)
  • cache (Boolean) (defaults to: true)

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 84

def find_platform_by_name(name, org, cache = true)
  if cache && platform = cached_platforms.find{|p| p.name == name && p.organization == org }
    platform
  else
    data = cloud_client.get("/organizations/#{org}/platforms/#{name}")['data']
    if data
      platform = Kontena::Cli::Models::Platform.new(data)
      cached_platforms << platform
      platform
    end
  end
end

#login_to_platform(name, url) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 97

def (name, url)
  organization, platform = name.split('/')
  platform = find_platform_by_name(platform, organization)
  authorization = cloud_client.post("/organizations/#{organization}/masters/#{platform.master_id}/authorize", {})
  exchanger = Kontena::Cli::MasterCodeExchanger.new(platform.url)
  code = exchanger.exchange_code(authorization['code'])

   = Kontena::Cli::Master::LoginCommand.new('kontena')
  cmd = [
    '--silent', '--no-login-info', '--skip-grid-auto-select',
    '--name', name, '--code', code, url
  ]
  .run(cmd)
rescue => e
  error e.message
end

#parse_platform_name(name) ⇒ Array<String>

Returns organization, platform.

Parameters:

  • name (String)

Returns:

  • (Array<String>)

    organization, platform

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
73
74
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 65

def parse_platform_name(name)
  unless name.include?('/')
    name = "#{current_organization}/#{name}"
  end
  org, platform = name.split('/')

  raise ArgumentError, "Organization missing" unless org

  [org, platform]
end

#platform_config_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 76

def platform_config_exists?(name)
  !self.config.find_server_by(name: name).nil?
end

#prompt_platformObject



31
32
33
34
35
36
37
38
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 31

def prompt_platform
  platforms = fetch_platforms_for_org(current_organization)
  prompt.select("Choose platform") do |menu|
    platforms.each do |p|
      menu.choice p.name, p
    end
  end
end

#require_platform(name) ⇒ Object

Parameters:

  • name (String)


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kontena/plugin/cloud/platform/common.rb', line 50

def require_platform(name)
  org, platform = parse_platform_name(name)
  @current_organization = org
  p = find_platform_by_name(platform, org)
  exit_with_error("Platform not found") unless p
  unless platform_config_exists?(p.to_path)
    ("#{current_organization}/#{platform}", p.url)
  end
  self.current_master = "#{current_organization}/#{platform}"
  self.current_grid = p.grid_id
  p
end