Module: Kontena::Cli::Stacks::Common

Defined Under Namespace

Modules: StackNameParam

Instance Method Summary collapse

Methods included from Kontena::Cli::Services::ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #restart_service, #scale_service, #show_service, #show_service_instances, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

Methods included from Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #api_url_version, #ask, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_account, #current_grid, #current_grid=, #current_master, #current_master=, #current_master_index, #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_current_account, #require_current_grid, #require_current_master, #require_token, #reset_client, #reset_cloud_client, #running_silent?, #running_verbose?, #settings, #settings_filename, #spinner, #sprint, #sputs, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning, #yes?

Instance Method Details

#abort_on_validation_errors(errors) ⇒ Object



106
107
108
109
110
# File 'lib/kontena/cli/stacks/common.rb', line 106

def abort_on_validation_errors(errors)
  STDERR.puts "YAML validation failed! Aborting.".colorize(:red)
  display_notifications(errors, :red)
  abort
end

#current_dirString

Returns:



77
78
79
# File 'lib/kontena/cli/stacks/common.rb', line 77

def current_dir
  File.basename(Dir.getwd)
end

#display_notifications(messages, color = :yellow) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kontena/cli/stacks/common.rb', line 81

def display_notifications(messages, color = :yellow)
  messages.each do |files|
    files.each do |file, services|
      STDERR.puts "#{file}:".colorize(color)
      services.each do |service|
        service.each do |name, errors|
          STDERR.puts "  #{name}:".colorize(color)
          if errors.is_a?(String)
            STDERR.puts "    - #{errors}".colorize(color)
          else
            errors.each do |key, error|
              STDERR.puts "    - #{key}: #{error.to_json}".colorize(color)
            end
          end
        end
      end
    end
  end
end

#generate_services(yaml_services, version) ⇒ Hash

Parameters:

  • yaml (Hash)
  • version (String)

Returns:

  • (Hash)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kontena/cli/stacks/common.rb', line 59

def generate_services(yaml_services, version)
  services = []
  generator_klass = ServiceGeneratorV2
  yaml_services.each do |service_name, config|
    exit_with_error("Image is missing for #{service_name}. Aborting.") unless config['image']
    service = generator_klass.new(config).generate
    service['name'] = service_name
    services << service
  end
  services
end

#hint_on_validation_notifications(errors) ⇒ Object



101
102
103
104
# File 'lib/kontena/cli/stacks/common.rb', line 101

def hint_on_validation_notifications(errors)
  STDERR.puts "YAML contains the following unsupported options and they were rejected:".colorize(:yellow)
  display_notifications(errors)
end

#require_config_file(filename) ⇒ Object



50
51
52
# File 'lib/kontena/cli/stacks/common.rb', line 50

def require_config_file(filename)
  exit_with_error("File #{filename} does not exist") unless File.exists?(filename)
end

#set_env_variables(stack, grid) ⇒ Object



71
72
73
74
# File 'lib/kontena/cli/stacks/common.rb', line 71

def set_env_variables(stack, grid)
  ENV['STACK'] = stack
  ENV['GRID'] = grid
end

#stack_from_yaml(filename, from_registry: false, name: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kontena/cli/stacks/common.rb', line 27

def stack_from_yaml(filename, from_registry: false, name: nil)
  reader = Kontena::Cli::Stacks::YAML::Reader.new(filename, from_registry: from_registry)
  if reader.stack_name.nil?
    exit_with_error "Stack MUST have stack name in YAML top level field 'stack'! Aborting."
  end
  set_env_variables(name || reader.stack_name, current_grid)
  outcome = reader.execute

  hint_on_validation_notifications(outcome[:notifications]) if outcome[:notifications].size > 0
  abort_on_validation_errors(outcome[:errors]) if outcome[:errors].size > 0
  kontena_services = generate_services(outcome[:services], outcome[:version])
  stack = {
    'name' => outcome[:name],
    'stack' => outcome[:stack],
    'expose' => outcome[:expose],
    'version' => outcome[:version],
    'source' => reader.raw_content,
    'registry' => 'file://',
    'services' => kontena_services
  }
  stack
end

#stack_nameObject



23
24
25
# File 'lib/kontena/cli/stacks/common.rb', line 23

def stack_name
  @stack_name ||= self.name || stack_name_from_yaml(filename)
end

#stacks_clientObject



112
113
114
115
116
117
# File 'lib/kontena/cli/stacks/common.rb', line 112

def stacks_client
  return @stacks_client if @stacks_client
  Kontena.run('cloud login') unless cloud_auth?
  config.reset_instance
  @stacks_client = Kontena::StacksClient.new(.stacks_url, .token)
end