Class: Kontena::Plugin::Cloud::Node::ShellCommand

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

Constant Summary

Constants included from CloudCommand

CloudCommand::PLATFORM_NOT_SELECTED_ERROR

Instance Method Summary collapse

Methods included from Common

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

Methods included from PlatformOption

included

Methods included from Platform::Common

#cached_platforms, #current_organization, #current_platform, #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 CloudCommand

#verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#create_service(service_name, node_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kontena/plugin/cloud/node/shell_command.rb', line 40

def create_service(service_name, node_name)
  data = {
    name: service_name,
    image: 'kontena/nsenter:latest',
    stateful: false,
    cmd: ['sleep', '60000'],
    pid: 'host',
    privileged: true,
    affinity: [
      "node==#{node_name}"
    ],
    env: [
      "TERM=xterm"
    ]
  }
  service = client.post("grids/#{current_grid}/services", data)
  deployment = client.post("services/#{service['id']}/deploy", {})
  until deployment['finished_at'] do
    sleep 1
    deployment = client.get("services/#{service['id']}/deploys/#{deployment['id']}", {})
  end

  service
end

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kontena/plugin/cloud/node/shell_command.rb', line 16

def execute
  service_name = "#{name}-shell-#{rand(1..10_000)}"

  service = nil
  spinner "Creating shell session to node #{pastel.cyan(name)}" do
    service = create_service(service_name, name)
  end

  exec_opts = ['-i']
  exec_opts << 't' if STDIN.tty?
  if cmd_list.empty?
    exec_command = ''
  else
    exec_command = "-c \"#{cmd_list.join(' ')}\""
  end

  Kontena.run!([
    'service', 'exec', exec_opts.join(''), '--shell', service_name,
    "nsenter --target 1 --mount --uts --net --pid -- su #{exec_command} - core"
  ])
ensure
  remove_service(service) if service
end

#remove_service(service) ⇒ Object



65
66
67
# File 'lib/kontena/plugin/cloud/node/shell_command.rb', line 65

def remove_service(service)
  client.delete("services/#{service['id']}", {})
end