Sha256: 6888799953ad9cc4de3534894cb9a8299613a98d77a4da10d4ae3ff9013d71ce

Contents?: true

Size: 1.97 KB

Versions: 19

Compression:

Stored size: 1.97 KB

Contents

module Kontena::Cli::Nodes
  class SshCommand < Kontena::Command
    include Kontena::Cli::Common
    include Kontena::Cli::GridOptions

    parameter "[NODE_ID]", "SSH to Grid node. Use --any to connect to the first available node"
    parameter "[COMMANDS] ...", "Run command on host"
    option ["-a", "--any"], :flag, "Connect to first available node"
    option ["-i", "--identity-file"], "IDENTITY_FILE", "Path to ssh private key"
    option ["-u", "--user"], "USER", "Login as a user", default: "core"
    option "--private-ip", :flag, "Connect to node's private IP address"
    option "--internal-ip", :flag, "Connect to node's internal IP address (requires VPN connection)"

    requires_current_master
    requires_current_grid

    def execute
      exit_with_error "Cannot combine --any with a node name" if node_id && any?

      if node_id
        node = client.get("nodes/#{current_grid}/#{node_id}")
      elsif any?
        nodes = client.get("grids/#{current_grid}/nodes")['nodes']
        node = nodes.select{ |node| node['connected'] }.first
      else
        exit_with_error "No node name given. Use --any to connect to the first available node"
      end

      provider = Array(node["labels"]).find{ |l| l.start_with?('provider=')}.to_s.split('=').last

      if provider == 'vagrant'
        unless Kontena::PluginManager.instance.plugins.find { |plugin| plugin.name == 'kontena-plugin-vagrant' }
          exit_with_error 'You need to install vagrant plugin to ssh into this node. Use kontena plugin install vagrant'
        end
        cmd = ['vagrant', 'node', 'ssh', node['name']] + commands_list
        Kontena.run!(cmd)
      else
        cmd = ['ssh']
        cmd += ["-i", identity_file] if identity_file
        if internal_ip?
          ip = node['overlay_ip']
        elsif private_ip?
          ip = node['private_ip']
        else
          ip = node['public_ip']
        end
        cmd << "#{user}@#{ip}"
        cmd += commands_list
        exec(*cmd)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
kontena-cli-1.3.5 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.5.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.4 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.4.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.3 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.3.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.2 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.2.rc2 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.2.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.1.rc2 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.1.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.rc4 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.rc3 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.rc2 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.rc1 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.pre2 lib/kontena/cli/nodes/ssh_command.rb
kontena-cli-1.3.0.pre1 lib/kontena/cli/nodes/ssh_command.rb