<commit_before>import json
import socket

from libcloud.compute.base import (Node, NodeDriver, NodeState,
                                   KeyPair, NodeLocation, NodeImage)
from libcloud.common.solusvm import SolusVMConnection
from libcloud.utils.networking import is_public_subnet
from libcloud.utils.publickey import get_pubkey_ssh2_fingerprint
from libcloud.compute.providers import Provider

__all__ = [
    "SolusVMNodeDriver"
]


class SolusVMNodeDriver(NodeDriver):
    """
    Base SolusVM node driver.
    """

    connectionCls = SolusVMConnection
    type = Provider.SOLUSVM
    name = 'SolusVM'
    website = 'http://solusvm.com/'

    def __init__(self, key=None, secret=None,
                 host='solusvm.com', port=443,
                 verify=True
                 ):
        """
        :param key: username
        :param secret: password
        :param host: solusvm host
        :param port: solusvm host port
        """
        if not key or not secret:
            raise Exception("Key and secret not specified")

        secure = False if host.startswith('http://') else True
        port = 80 if host.startswith('http://') else 443

        # strip the prefix
        prefixes = ['http://', 'https://']
        for prefix in prefixes:
            if host.startswith(prefix):
                host = host.replace(prefix, '')
        host = host.split('/')[0]

        super(SolusVMNodeDriver, self).__init__(key=key,
                                                secret=secret,
                                                host=host,
                                                port=port,
                                                secure=secure)

        self.connection.secure = secure

        self.connection.host = host
        self.connection.port = port

        try:
            socket.setdefaulttimeout(15)
            so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            so.connect((host, int(port)))
            so.close()
        except:
            raise Exception("Make sure solusvm host is accessible and port "
                            "%s is open" % port)
        # do not verify SSL certificate
        if not verify:
            self.connection.connection.ca_cert = False

    def create_node(self):
        """
        Add a VS

        """
        pass

    def ex_start_node(self, node):
        """
        Start a node
        """
        action = "/api/virtual_machines/{identifier}/boot".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def ex_stop_node(self, node):
        """
        Stop a node
        """
        action = "/api/virtual_machines/{identifier}/shutdown".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def reboot_node(self, node):
        """
        Reboot a node
        """
        action = "/api/virtual_machines/{identifier}/reboot".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def destroy_node(self, node):
        """
        Delete a VS
        """
        action = "/api/virtual_machines/{identifier}".format(
            identifier=node.id)

        self.connection.request(action, method="DELETE")
        return True

    def list_nodes(self):
        """
        List all VS

        :rtype: ``list`` of :class:`SolusVMNode`
        """
        response = self.connection.request("/api/virtual_machines")
        nodes = [self._to_node(vm["virtual_machine"])
                 for vm in response.object]
        return nodes

    def list_locations(self):
        """
        List locations
        """
        pass

    def list_sizes(self):
        """
        List sizes
        """
        pass

    def list_images(self):
        """
        List images
        """
        pass

    def _to_node(self, data):
        identifier = data['id']
        name = data['friendlyname']
        private_ips = []
        public_ips = []
        mainipaddress = data.get('mainipaddress')
        ipaddresses = data.get('ipaddresses')
        all_ips = [mainipaddress]
        if ipaddresses and isinstance(basestring, ipaddresses):
            all_ips.append(all_ips)
        if ipaddresses and isinstance(list, ipaddresses):
            all_ips.extend(all_ips)

        for ip in all_ips:
            try:
                if is_public_subnet(address):
                    public_ips.append(address)
                else:
                    private_ips.append(address)
            except:
                # IPV6 not supported
                pass
        extra = {}
        extra['creationdate'] = data.get('creationdate')
        extra['clientid'] = data.get('clientid')
        extra['consoleusername'] = data.get('consoleusername')
        extra['hostname'] = data.get('hostname')
        extra['template'] = data.get('template')
        extra['type'] = data.get('type')

        status = data.get('status')
        if status == 'online':
            state = NodeState.RUNNING<commit_msg>solusvm metadata<commit_after>import json
import socket

from libcloud.compute.base import (Node, NodeDriver, NodeState,
                                   KeyPair, NodeLocation, NodeImage)
from libcloud.common.solusvm import SolusVMConnection
from libcloud.utils.networking import is_public_subnet
from libcloud.utils.publickey import get_pubkey_ssh2_fingerprint
from libcloud.compute.providers import Provider

__all__ = [
    "SolusVMNodeDriver"
]


class SolusVMNodeDriver(NodeDriver):
    """
    Base SolusVM node driver.
    """

    connectionCls = SolusVMConnection
    type = Provider.SOLUSVM
    name = 'SolusVM'
    website = 'http://solusvm.com/'

    def __init__(self, key=None, secret=None,
                 host='solusvm.com', port=None,
                 verify=True
                 ):
        """
        :param key: username
        :param secret: password
        :param host: solusvm host
        :param port: solusvm host port
        """
        if not key or not secret:
            raise Exception("Key and secret not specified")

        secure = True if host.startswith('https://') else False

        if not port:
            port = 443 if host.startswith('https://') else 80

        # strip the prefix
        prefixes = ['http://', 'https://']
        for prefix in prefixes:
            if host.startswith(prefix):
                host = host.replace(prefix, '')
        host = host.split('/')[0]

        super(SolusVMNodeDriver, self).__init__(key=key,
                                                secret=secret,
                                                host=host,
                                                port=port,
                                                secure=secure)

        self.connection.secure = secure

        self.connection.host = host
        self.connection.port = port

        try:
            socket.setdefaulttimeout(15)
            so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            so.connect((host, int(port)))
            so.close()
        except:
            raise Exception("Make sure solusvm host is accessible and port "
                            "%s is open" % port)
        # do not verify SSL certificate
        if not verify:
            self.connection.connection.ca_cert = False

    def create_node(self):
        """
        Add a VS

        """
        pass

    def ex_start_node(self, node):
        """
        Start a node
        """
        action = "/api/virtual_machines/{identifier}/boot".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def ex_stop_node(self, node):
        """
        Stop a node
        """
        action = "/api/virtual_machines/{identifier}/shutdown".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def reboot_node(self, node):
        """
        Reboot a node
        """
        action = "/api/virtual_machines/{identifier}/reboot".format(
            identifier=node.id)

        result = self.connection.request(action, method="POST")
        return True if result.status == 201 else False

    def destroy_node(self, node):
        """
        Delete a VS
        """
        action = "/api/virtual_machines/{identifier}".format(
            identifier=node.id)

        self.connection.request(action, method="DELETE")
        return True

    def list_nodes(self):
        """
        List all VS

        :rtype: ``list`` of :class:`SolusVMNode`
        """
        response = self.connection.request("/api/virtual_machines")
        nodes = [self._to_node(vm["virtual_machine"])
                 for vm in response.object]
        return nodes

    def list_locations(self):
        """
        List locations
        """
        pass

    def list_sizes(self):
        """
        List sizes
        """
        pass

    def list_images(self):
        """
        List images
        """
        pass

    def _to_node(self, data):
        identifier = data['id']
        name = data['hostname']
        private_ips = []
        public_ips = []
        mainipaddress = data.get('mainipaddress')
        ipaddresses = data.get('ipaddresses')
        all_ips = [mainipaddress]
        if ipaddresses and isinstance(ipaddresses, basestring):
            all_ips.append(ipaddresses)
        if ipaddresses and isinstance(ipaddresses, list):
            all_ips.extend(ipaddresses)
        for ip in all_ips:
            try:
                if is_public_subnet(ip):
                    if ip not in public_ips:
                        public_ips.append(ip)
                else:
                    if ip not in private_ips:
                        private_ips.append(ip)
            except:
                # IPV6 not supported
                pass
        extra = {}
        extra['creationdate'] = data.get('creationdate')
        extra['clientid'] = data.get('clientid')
        extra['consoleusername'] = data.get('consoleusername')
        extra['hostname'] = data.get('hostname')
        extra['os'] = data.get('templatename')
        extra['type'] = data.get('type')
        disk = int(data.get('disk')) / 1024 / 1024
        extra['disk'] = str(disk) + 'MB'
        ram = int(data.get('ram')) / 1024 / 1024
        extra['memory'] = str(ram) + 'MB'
        bandwidth = int(data.get('freebandwidth')) / 1024 / 1024
        extra['bandwidth'] = str(bandwidth) + 'MB'

        status = data.get('status')
        if status == 'online':
            state = NodeState.RUNNING
{'fraction_non_alphanumeric': 0.10680944535969247, 'fraction_numerical': 0.005949112209408749, 'mean_word_length': 1.686086529006883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1539869', 'n_tokens_mistral': 3014, 'n_tokens_neox': 2850, 'n_words': 680}