Class: Kontena::Cli::Services::ExecCommand

Inherits:
Kontena::Command show all
Includes:
Common, GridOptions, Helpers::ExecHelper, ServicesHelper
Defined in:
lib/kontena/cli/services/exec_command.rb

Defined Under Namespace

Classes: ExecExit

Constant Summary

Constants included from Helpers::ExecHelper

Helpers::ExecHelper::WEBSOCKET_CLIENT_OPTIONS

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #health_status, #health_status_icon, #int_to_filesize, #parse_build_args, #parse_container_name, #parse_deploy_opts, #parse_health_check, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #render_service_deploy_instances, #restart_service, #scale_service, #show_service, #show_service_containers, #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=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #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

Methods included from Helpers::ExecHelper

#container_exec, #read_stdin, #websocket_exec, #websocket_exec_read, #websocket_exec_write, #websocket_exec_write_thread, #websocket_url

Methods included from GridOptions

included

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#executeObject



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
# File 'lib/kontena/cli/services/exec_command.rb', line 39

def execute
  exit_with_error "the input device is not a TTY" if tty? && !STDIN.tty?
  exit_with_error "--interactive cannot be used with --all" if all? && interactive?

  service_containers = client.get("services/#{parse_service_id(name)}/containers")['containers']
  service_containers.sort_by! { |container| container['instance_number'] }
  running_containers = service_containers.select{|container| container['status'] == 'running' }

  exit_with_error "Service #{name} does not have any running containers" if running_containers.empty?

  if all?
    ret = true
    service_containers.each do |container|
      if container['status'] == 'running'
        if !execute_container(container)
          ret = false
        end
      else
        warning "Service #{name} container #{container['name']} is #{container['status']}, skipping"
      end
    end
    return ret
  elsif instance
    if !(container = service_containers.find{|c| c['instance_number'] == instance})
      exit_with_error "Service #{name} does not have container instance #{instance}"
    elsif container['status'] != 'running'
      exit_with_error "Service #{name} container #{container['name']} is not running, it is #{container['status']}"
    else
      execute_container(container)
    end
  else
    execute_container(running_containers.first)
  end
end

#execute_container(container) ⇒ true, false

Parameters:

  • container (Hash)

Returns:

  • (true)

    exit exit status zero

  • (false)

    exit exit status non-zero and –skip

Raises:

  • (SystemExit)

    if exec exits with non-zero status, and not –skip



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kontena/cli/services/exec_command.rb', line 88

def execute_container(container)
  maybe_spinner "Executing command on #{container['name']}" do
    exit_status = container_exec(container['id'], self.cmd_list,
      interactive: interactive?,
      shell: shell?,
      tty: tty?,
    )
    raise ExecExit.new(exit_status) unless exit_status.zero?
  end
rescue ExecExit => exc
  if skip?
    return false
  else
    exit exc.exit_status
  end
else
  return true
end

#maybe_spinner(msg, &block) ⇒ Object

Run block with spinner by default if –all, or when using –verbose. Do not use spinner if –silent.



76
77
78
79
80
81
82
# File 'lib/kontena/cli/services/exec_command.rb', line 76

def maybe_spinner(msg, &block)
  if (all? || verbose?) && !silent?
    spinner(msg, &block)
  else
    yield
  end
end