<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
  Provides info about a particular server.
"""

import restclient
import simplejson
import subprocess

import string
import random
import util_cli as util

class Info:
    def __init__(self):
        self.debug = False

    def _remoteShellName(self):
        tmp = ''.join(random.choice(string.ascii_letters) for i in xrange(20))
        return 'ctl-%s@127.0.0.1' % tmp

    def runCmd(self, cmd, server, port,
               user, password, ssl, opts):
        vm = 'ns_server'
        for (o, a) in opts:
            if o == '-d' or o == '--debug':
                self.debug = True

            if o == '--vm':
                vm = a

        rest = util.restclient_factory(server, port, {'debug':self.debug}, ssl)
        opts = {'error_msg': 'server-info error'}

        data = rest.restCmd('GET', '/nodes/self',
                            user, password, opts)

        json = rest.getJson(data)

        for x in ['license', 'licenseValid', 'licenseValidUntil']:
            if x in json:
                del(json[x])

        if cmd == 'server-eshell':
            node = json['otpNode']
            cookie = json['otpCookie']

            if vm != 'ns_server':
                rest = util.restclient_factory(server, port, {'debug':self.debug}, ssl)
                rest.setPayload('ns_server:get_babysitter_cookie().')
                cookie = rest.sendCmd('POST', '/diag/eval', user, password).read()

                [short, _] = node.split('@')

                if vm == 'babysitter':
                    node = 'babysitter_of_%s@127.0.0.1' % short
                elif vm == 'couchdb':
                    node = 'couchdb_%s@127.0.0.1' % short
                else:
                    raise ValueError("unknown vm type \'%s\'" % vm)

            name = self._remoteShellName()
            p = subprocess.call(['erl', '-name', name, '-setcookie',
                                 cookie, '-hidden', '-remsh', node])
        elif cmd == 'get-server-info':
            return json
        else:
            print simplejson.dumps(json, sort_keys=True, indent=2)

    def getCommandSummary(self, cmd):
        """Return one-line summary info for each supported command"""
        command_summary = {
            "server-info" : "show details on one server"}
        if cmd in command_summary:
            return command_summary[cmd]
        else:
            return None

    def getCommandExampleHelp(self, cmd):
        """ Obtain detailed example help for command
        Returns a list of command examples to illustrate how to use command
        or None if there's no example help or cmd is unknown.
        """

        if cmd == "server-info":
            return [("Server information",
"""
    couchbase-cli server-info -c 192.168.0.1:8091""")]
        else:
            return None<commit_msg>MB-18681: Try to use the Couchbase installed erlang for server-eshell<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
  Provides info about a particular server.
"""

import restclient
import simplejson
import subprocess

import os
import sys
import string
import random
import util_cli as util

class Info:
    def __init__(self):
        self.debug = False

    def _remoteShellName(self):
        tmp = ''.join(random.choice(string.ascii_letters) for i in xrange(20))
        return 'ctl-%s@127.0.0.1' % tmp

    def runCmd(self, cmd, server, port,
               user, password, ssl, opts):
        vm = 'ns_server'
        for (o, a) in opts:
            if o == '-d' or o == '--debug':
                self.debug = True

            if o == '--vm':
                vm = a

        rest = util.restclient_factory(server, port, {'debug':self.debug}, ssl)
        opts = {'error_msg': 'server-info error'}

        data = rest.restCmd('GET', '/nodes/self',
                            user, password, opts)

        json = rest.getJson(data)

        for x in ['license', 'licenseValid', 'licenseValidUntil']:
            if x in json:
                del(json[x])

        if cmd == 'server-eshell':
            node = json['otpNode']
            cookie = json['otpCookie']

            if vm != 'ns_server':
                rest = util.restclient_factory(server, port, {'debug':self.debug}, ssl)
                rest.setPayload('ns_server:get_babysitter_cookie().')
                cookie = rest.sendCmd('POST', '/diag/eval', user, password).read()

                [short, _] = node.split('@')

                if vm == 'babysitter':
                    node = 'babysitter_of_%s@127.0.0.1' % short
                elif vm == 'couchdb':
                    node = 'couchdb_%s@127.0.0.1' % short
                else:
                    raise ValueError("unknown vm type \'%s\'" % vm)

            name = self._remoteShellName()
            p = subprocess.call([self.getErlPath(), '-name', name, '-setcookie',
                                 cookie, '-hidden', '-remsh', node])
        elif cmd == 'get-server-info':
            return json
        else:
            print simplejson.dumps(json, sort_keys=True, indent=2)

    def getErlPath(self):
        bin = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '..', '..', 'bin')
        cb_erl = os.path.join(bin, 'erl')
        if os.path.isfile(cb_erl):
            return cb_erl
        else:
            print "WARNING: Cannot locate Couchbase erlang. Attempting to use non-Couchbase erlang."
            return 'erl'

    def getCommandSummary(self, cmd):
        """Return one-line summary info for each supported command"""
        command_summary = {
            "server-info" : "show details on one server"}
        if cmd in command_summary:
            return command_summary[cmd]
        else:
            return None

    def getCommandExampleHelp(self, cmd):
        """ Obtain detailed example help for command
        Returns a list of command examples to illustrate how to use command
        or None if there's no example help or cmd is unknown.
        """

        if cmd == "server-info":
            return [("Server information",
"""
    couchbase-cli server-info -c 192.168.0.1:8091""")]
        else:
            return None
{'fraction_non_alphanumeric': 0.12713228194399742, 'fraction_numerical': 0.011908593498551657, 'mean_word_length': 1.7844982078853047, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963950', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1765, 'n_words': 505}