Class: Helper

Inherits:
Object
  • Object
show all
Includes:
Kontena::Cli::Common
Defined in:
lib/kontena/scripts/completer.rb

Instance Method Summary collapse

Methods included from Kontena::Cli::Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Instance Method Details

#client_configObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kontena/scripts/completer.rb', line 7

def client_config
  require 'json'
  config_file = File.expand_path('~/.krates.json')
  if(File.exist?(config_file))
    JSON.parse(File.read(config_file))
  else
    {}
  end
rescue => ex
  logger.debug ex
  {}
end

#containersObject



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/kontena/scripts/completer.rb', line 103

def containers
  results = []
  client.get("grids/#{current_grid}/services")['services'].each do |service|
    containers = client.get("services/#{service['id']}/containers")['containers']
    results.push(containers.map{|c| c['name'] })
    results.push(containers.map{|c| c['id'] })
  end
  results
rescue => ex
  logger.debug ex
  []
end

#current_gridObject



20
21
22
23
24
25
# File 'lib/kontena/scripts/completer.rb', line 20

def current_grid
  client_config['servers'].find { |s| s['name'] == client_config['current_server']}['grid']
rescue => ex
  logger.debug ex
  nil
end

#current_master_nameObject



27
28
29
# File 'lib/kontena/scripts/completer.rb', line 27

def current_master_name
  client_config['current_server']
end

#directories(word) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/kontena/scripts/completer.rb', line 128

def directories(word)
  if word && File.directory?(word) && !word.end_with?('/')
    ['%s/' % word]
  else
    Dir[File.join('.', '%s*' % word)].select { |file| File.directory?(file) }.map { |file| '%s/' % file.sub('./', '') }
  end
end

#gridsObject



31
32
33
34
35
36
# File 'lib/kontena/scripts/completer.rb', line 31

def grids
  client.get("grids")['grids'].map{|grid| grid['id']}
rescue => ex
  logger.debug ex
  []
end

#master_namesObject



148
149
150
151
152
153
# File 'lib/kontena/scripts/completer.rb', line 148

def master_names
  client_config['servers'].map{|s| s['name']}
rescue => ex
  logger.debug ex
  []
end

#nodesObject



38
39
40
41
42
# File 'lib/kontena/scripts/completer.rb', line 38

def nodes
  client.get("grids/#{current_grid}/nodes")['nodes'].map{|node| node['name']}
rescue
  []
end

#registry_stack_versions(stackname) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/kontena/scripts/completer.rb', line 77

def registry_stack_versions(stackname)
  return [] unless stack_registry_usable?
  logger.debug stackname.inspect
  stacks_client.versions(stackname).map { |v| [stackname, v['version']].join(':') }
rescue => ex
  logger.debug ex
  []
end

#registry_stacks(query = '') ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kontena/scripts/completer.rb', line 65

def registry_stacks(query = '')
  return [] unless stack_registry_usable?
  results = stacks_client.search(query).map { |s| s['stack'] }
  if results.empty? && !query.empty? # this is here because old stack registry does not return anything for "org/"
    results = stacks_client.search('').map { |s| s['stack'] }.select { |s| s.start_with?(query) }
  end
  results
rescue => ex
  logger.debug ex
  []
end

#servicesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kontena/scripts/completer.rb', line 86

def services
  services = client.get("grids/#{current_grid}/services")['services']
  results = []
  results.push services.map{ |s|
    stack = s['stack']['id'].split('/').last
    if stack != 'null'
      "#{stack}/#{s['name']}"
    else
      s['name']
    end
  }
  results
rescue => ex
  logger.debug ex
  []
end

#stack_registry_usable?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/kontena/scripts/completer.rb', line 55

def stack_registry_usable?
  return false if .nil? || .stacks_url.nil?
  return false if .stacks_read_authentication && .token.nil? || .token.access_token.nil?
  true
end

#stacksObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/kontena/scripts/completer.rb', line 44

def stacks
  stacks = client.get("grids/#{current_grid}/stacks")['stacks']
  results = []
  results.push stacks.map{|s| s['name']}
  results.delete('null')
  results
rescue => ex
  logger.debug ex
  []
end

#stacks_clientObject



61
62
63
# File 'lib/kontena/scripts/completer.rb', line 61

def stacks_client
  Kontena::StacksClient.new(.stacks_url, .token, read_requires_token: .stacks_read_authentication)
end

#subcommand_tree(cmd = nil, base = nil) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/kontena/scripts/completer.rb', line 155

def subcommand_tree(cmd = nil, base = nil)
  puts "#{cmd} ".strip
  if base.has_subcommands?
    base.recognised_subcommands.each do |sc|
      subcommand_tree("#{cmd} #{sc.names.first}", sc.subcommand_class)
    end
  end
end

#yml_files(word) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/kontena/scripts/completer.rb', line 136

def yml_files(word)
  if word && File.directory?(word) && word.end_with?('/')
    glob = File.join(word, '*.{yml,yaml}')
  else
    glob = File.join('.', '%s*.{yml,yaml}' % word)
  end
  Dir[glob].map { |file| file.sub('./', '') } + directories(word)
rescue => ex
  logger.debug ex
  []
end

#yml_servicesObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kontena/scripts/completer.rb', line 116

def yml_services
  require 'yaml'
  if File.exist?('kontena.yml')
    yaml = YAML.safe_load(File.read('kontena.yml'), [], [], true, 'kontena.yml')
    services = yaml['services']
    services.keys
  end
rescue => ex
  logger.debug ex
  []
end