# Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Rackspace provisioner.
"""

from ._libcloud import monkeypatch, LibcloudProvisioner
from ._install import (
    provision, run,
    task_disable_firewall, task_open_control_firewall,
    task_upgrade_kernel_centos,
)


def provision_rackspace(node, package_source, distribution):
    """
    Provision flocker on this node.
    """
    if distribution in ('centos-7',):
        run(
            username='root',
            address=node.address,
            commands=task_upgrade_kernel_centos(),
        )
        node.reboot()

    commands = (
        provision(
            package_source=package_source,
            distribution=node.distribution,
        )
        + task_disable_firewall()
        # This should be part of ._install.configure_cluster
        + task_open_control_firewall()
    )
    run(
        username='root',
        address=node.address,
        commands=commands,
    )
    return node.address

    @property
    def name(self):
        return self._node.name


IMAGE_NAMES = {
    'fedora-20': u'Fedora 20 (Heisenbug) (PVHVM)',
    'centos-7': u'CentOS 7 (PVHVM)',
}


def rackspace_provisioner(username, key, region, keyname):
    """
    Create a LibCloudProvisioner for provisioning nodes on rackspace.

    :param bytes username: The user to connect to rackspace with.
    :param bytes key: The API key associated with the user.
    :param bytes region: The rackspace region in which to launch the instance.
    :param bytes keyname: The name of an existing ssh public key configured in
       rackspace. The provision step assumes the corresponding private key is
       available from an agent.
    """
    # Import these here, so that this can be imported without
    # installng libcloud.
    from libcloud.compute.providers import get_driver, Provider
    monkeypatch()
    driver = get_driver(Provider.RACKSPACE)(
        key=username,
        secret=key,
        region=region)

    provisioner = LibcloudProvisioner(
        driver=driver,
        keyname=keyname,
        image_names=IMAGE_NAMES,
        create_node_arguments=lambda **kwargs: {
            "ex_config_drive": "true",
        },
        provision=provision_rackspace,
        default_size="performance1-8",
    )

    return provisioner

# Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Rackspace provisioner.
"""

from ._libcloud import monkeypatch, LibcloudProvisioner
from ._install import (
    provision, run,
    task_disable_firewall, task_open_control_firewall,
    task_upgrade_kernel_centos,
)


def provision_rackspace(node, package_source, distribution):
    """
    Provision flocker on this node.
    """
    if distribution in ('centos-7',):
        run(
            username='root',
            address=node.address,
            commands=task_upgrade_kernel_centos(),
        )
        node.reboot()

    commands = (
        provision(
            package_source=package_source,
            distribution=node.distribution,
        )
        + task_disable_firewall()
        # https://clusterhq.atlassian.net/browse/FLOC-1550
        # This should be part of ._install.configure_cluster
        + task_open_control_firewall()
    )
    run(
        username='root',
        address=node.address,
        commands=commands,
    )
    return node.address

    @property
    def name(self):
        return self._node.name


IMAGE_NAMES = {
    'fedora-20': u'Fedora 20 (Heisenbug) (PVHVM)',
    'centos-7': u'CentOS 7 (PVHVM)',
}


def rackspace_provisioner(username, key, region, keyname):
    """
    Create a LibCloudProvisioner for provisioning nodes on rackspace.

    :param bytes username: The user to connect to rackspace with.
    :param bytes key: The API key associated with the user.
    :param bytes region: The rackspace region in which to launch the instance.
    :param bytes keyname: The name of an existing ssh public key configured in
       rackspace. The provision step assumes the corresponding private key is
       available from an agent.
    """
    # Import these here, so that this can be imported without
    # installng libcloud.
    from libcloud.compute.providers import get_driver, Provider
    monkeypatch()
    driver = get_driver(Provider.RACKSPACE)(
        key=username,
        secret=key,
        region=region)

    provisioner = LibcloudProvisioner(
        driver=driver,
        keyname=keyname,
        image_names=IMAGE_NAMES,
        create_node_arguments=lambda **kwargs: {
            "ex_config_drive": "true",
        },
        provision=provision_rackspace,
        default_size="performance1-8",
    )

    return provisioner

Add link to FLOC-1550.