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

    parameter "NODE_ID", "Node id"
    parameter "[COMMANDS] ...", "Run command on host"

    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
      node = client.get("nodes/#{current_grid}/#{node_id}")

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

      commands_list.insert('--') unless commands_list.empty?

      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']<commit_msg>kontena node ssh --any: connect to first connected node (#1359)<commit_after>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("grids/#{current_grid}/nodes/#{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

      commands_list.insert('--') unless commands_list.empty?

      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']
{'fraction_non_alphanumeric': 0.14194669756662803, 'fraction_numerical': 0.0011587485515643105, 'mean_word_length': 3.0815602836879434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280949', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1026, 'n_words': 309}