def install_deps(cluster_config):

    log_info("Installing dependencies for cluster_config: {}".format(cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)
    status = ansible_runner.run_ansible_playbook("os-level-modifications.yml")
    if status != 0:
        raise ProvisioningError("Failed to make os modifications")

    status = ansible_runner.run_ansible_playbook("install-common-tools.yml")
    if status != 0:
        raise ProvisioningError("Failed to install dependencies")
def install_deps(cluster_config):

    log_info("Installing dependencies for cluster_config: {}".format(
        cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)
    status = ansible_runner.run_ansible_playbook("os-level-modifications.yml")
    if status != 0:
        raise ProvisioningError("Failed to make os modifications")

    status = ansible_runner.run_ansible_playbook("install-common-tools.yml")
    if status != 0:
        raise ProvisioningError("Failed to install dependencies")
def clean_cluster(cluster_config):

    log_info("Cleaning cluster: {}".format(cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)
    status = ansible_runner.run_ansible_playbook("remove-previous-installs.yml")
    if status != 0:
        raise ProvisioningError("Failed to removed previous installs")

    # Clear firewall rules
    status = ansible_runner.run_ansible_playbook("flush-firewall.yml")
    if status != 0:
        raise ProvisioningError("Failed to flush firewall")
def clean_cluster(cluster_config):

    log_info("Cleaning cluster: {}".format(cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)
    status = ansible_runner.run_ansible_playbook(
        "remove-previous-installs.yml")
    if status != 0:
        raise ProvisioningError("Failed to removed previous installs")

    # Clear firewall rules
    status = ansible_runner.run_ansible_playbook("flush-firewall.yml")
    if status != 0:
        raise ProvisioningError("Failed to flush firewall")
def install_couchbase_server(cluster_config, couchbase_server_config):

    log_info(cluster_config)
    log_info(couchbase_server_config)

    ansible_runner = AnsibleRunner(cluster_config)

    log_info(">>> Installing Couchbase Server")
    # Install Server
    server_baseurl, server_package_name = couchbase_server_config.get_baseurl_package()
    status = ansible_runner.run_ansible_playbook(
        "install-couchbase-server-package.yml",
        extra_vars={
            "couchbase_server_package_base_url": server_baseurl,
            "couchbase_server_package_name": server_package_name
        }
    )
    if status != 0:
        raise ProvisioningError("Failed to install Couchbase Server")

    # Wait for server to be in 'healthy state'
    print(">>> Waiting for server to be in 'healthy' state")
    cluster_keywords = ClusterKeywords()
    cluster_topology = cluster_keywords.get_cluster_topology(cluster_config)
    server_url = cluster_topology["couchbase_servers"][0]
    cb_server = CouchbaseServer(server_url)
    cb_server.wait_for_ready_state()
def provision_cluster(couchbase_server_config, sync_gateway_config, install_deps):

    print "\n>>> Host info:\n"

    with open(os.environ["CLUSTER_CONFIG"], "r") as ansible_hosts:
        print(ansible_hosts.read())

    print(couchbase_server_config)
    print(sync_gateway_config)

    if not sync_gateway_config.is_valid():
        print("Invalid sync_gateway provisioning configuration. Exiting ...")
        sys.exit(1)

    print(">>> Provisioning cluster...")

    # Get server base url and package name
    server_baseurl, server_package_name = couchbase_server_config.get_baseurl_package()

    print(">>> Server package: {0}/{1}".format(server_baseurl, server_package_name))
    print(">>> Using sync_gateway config: {}".format(sync_gateway_config.config_path))

    ansible_runner = AnsibleRunner()

    # Reset previous installs
    status = ansible_runner.run_ansible_playbook("remove-previous-installs.yml", stop_on_fail=False)
    assert(status == 0)

    if install_deps:
        # OS-level modifications
        status = ansible_runner.run_ansible_playbook("os-level-modifications.yml", stop_on_fail=False)
        assert(status == 0)

        # Install dependencies
        status = ansible_runner.run_ansible_playbook("install-common-tools.yml", stop_on_fail=False)
        assert(status == 0)

    # Clear firewall rules
    status = ansible_runner.run_ansible_playbook("flush-firewall.yml", stop_on_fail=False)
    assert(status == 0)

    # Install server package
    install_couchbase_server.install_couchbase_server(couchbase_server_config)

    # Install sync_gateway
    install_sync_gateway.install_sync_gateway(sync_gateway_config)
def install_sync_gateway(cluster_config, sync_gateway_config):
    log_info(sync_gateway_config)

    if not sync_gateway_config.is_valid():
        raise ProvisioningError("Invalid sync_gateway provisioning configuration. Exiting ...")

    if sync_gateway_config.build_flags != "":
        log_warn("\n\n!!! WARNING: You are building with flags: {} !!!\n\n".format(sync_gateway_config.build_flags))

    ansible_runner = AnsibleRunner(cluster_config)
    config_path = os.path.abspath(sync_gateway_config.config_path)

    # Create buckets unless the user explicitly asked to skip this step
    if not sync_gateway_config.skip_bucketcreation:
        create_server_buckets(cluster_config, sync_gateway_config)

    # Install Sync Gateway via Source or Package
    if sync_gateway_config.commit is not None:
        # Install from source
        status = ansible_runner.run_ansible_playbook(
            "install-sync-gateway-source.yml",
            extra_vars={
                "sync_gateway_config_filepath": config_path,
                "commit": sync_gateway_config.commit,
                "build_flags": sync_gateway_config.build_flags
            }
        )
        if status != 0:
            raise ProvisioningError("Failed to install sync_gateway source")

    else:
        # Install from Package
        sync_gateway_base_url, sync_gateway_package_name, sg_accel_package_name = sync_gateway_config.sync_gateway_base_url_and_package()
        status = ansible_runner.run_ansible_playbook(
            "install-sync-gateway-package.yml",
            extra_vars={
                "couchbase_sync_gateway_package_base_url": sync_gateway_base_url,
                "couchbase_sync_gateway_package": sync_gateway_package_name,
                "couchbase_sg_accel_package": sg_accel_package_name,
                "sync_gateway_config_filepath": config_path
            }
        )
        if status != 0:
            raise ProvisioningError("Failed to install sync_gateway package")
def install_sync_gateway(sync_gateway_config):
    print(sync_gateway_config)

    if not sync_gateway_config.is_valid():
        print "Invalid sync_gateway provisioning configuration. Exiting ..."
        sys.exit(1)

    if sync_gateway_config.build_flags != "":
        print("\n\n!!! WARNING: You are building with flags: {} !!!\n\n".format(sync_gateway_config.build_flags))

    ansible_runner = AnsibleRunner()
    config_path = os.path.abspath(sync_gateway_config.config_path)

    if sync_gateway_config.commit is not None:
        # Install source
        status = ansible_runner.run_ansible_playbook(
            "install-sync-gateway-source.yml",
            "sync_gateway_config_filepath={0} commit={1} build_flags={2} skip_bucketflush={3}".format(
                config_path,
                sync_gateway_config.commit,
                sync_gateway_config.build_flags,
                sync_gateway_config.skip_bucketflush
            ),
            stop_on_fail=False
        )
        assert(status == 0)

    else:
        # Install build
        sync_gateway_base_url, sync_gateway_package_name, sg_accel_package_name = sync_gateway_config.sync_gateway_base_url_and_package()
        status = ansible_runner.run_ansible_playbook(
            "install-sync-gateway-package.yml",
            "couchbase_sync_gateway_package_base_url={0} couchbase_sync_gateway_package={1} couchbase_sg_accel_package={2} sync_gateway_config_filepath={3} skip_bucketflush={4}".format(
                sync_gateway_base_url,
                sync_gateway_package_name,
                sg_accel_package_name,
                config_path,
                sync_gateway_config.skip_bucketflush
            ),
            stop_on_fail=False
        )
        assert(status == 0)
def clean_cluster():
    try:
        cluster_config = os.environ["CLUSTER_CONFIG"]
    except KeyError as ke:
        print ("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to provision")
        raise KeyError("CLUSTER_CONFIG not defined. Unable to provision cluster.")

    print("Cleaning cluster: {}".format(cluster_config))

    ansible_runner = AnsibleRunner()
    status = ansible_runner.run_ansible_playbook("remove-previous-installs.yml", stop_on_fail=False)
    assert(status == 0)
def install_aws_credentials(cluster_config, aws_access_key_id, aws_secret_access_key):

    log_info("Installing aws credentials for cluster_config: {}".format(cluster_config))

    ansible_runner = AnsibleRunner(config=cluster_config)

    status = ansible_runner.run_ansible_playbook(
        "install-aws-credentials.yml",
        extra_vars={
            "aws_access_key_id": aws_access_key_id,
            "aws_secret_access_key": aws_secret_access_key,
        },
    )
    if status != 0:
        raise ProvisioningError("Failed to aws credentials")
def install_couchbase_server(couchbase_server_config):

    print(couchbase_server_config)

    ansible_runner = AnsibleRunner()

    server_baseurl, server_package_name = couchbase_server_config.get_baseurl_package()
    status = ansible_runner.run_ansible_playbook(
        "install-couchbase-server-package.yml",
        "couchbase_server_package_base_url={0} couchbase_server_package_name={1}".format(
            server_baseurl,
            server_package_name
        ),
        stop_on_fail=False
    )
    assert(status == 0)
def install_nginx(cluster_config):
    """
    Deploys nginx to nodes with the load_balancer tag

    1. Get the sync_gateway endpoints from the cluster configuration
    2. Use the endpoints to render the nginx config (resources/nginx_configs/nginx.conf)
      to distribute load across the running sync_gateways.
      i.e. If your 'cluster_config' has 2 sync_gateways, nginx will be setup to forward
        requests to both of the sync_gateways using a weighted round robin distribution.
        If you have 3, it will split the load between 3, etc ...
    3. Deploy the config and install nginx on load_balancer nodes
    4. Start the nginx service
    """

    cluster = ClusterKeywords()
    # Set lb_enable to False to get the actual SG IPs for nginx.conf
    topology = cluster.get_cluster_topology(cluster_config, lb_enable=False)

    # Get sync_gateway enpoints from cluster_config
    #  and build a string of upstream server definitions
    # upstream sync_gateway {
    #   server 192.168.33.11:4984;
    #   server 192.168.33.12:4984;
    #  }
    upstream_definition = ""
    upstream_definition_admin = ""

    for sg in topology["sync_gateways"]:
        # string http:// to adhere to expected format for nginx.conf
        ip_port = sg["public"].replace("http://", "")
        ip_port_admin = sg["admin"].replace("http://", "")
        upstream_definition += "server {};\n".format(ip_port)
        upstream_definition_admin += "server {};\n".format(ip_port_admin)

    log_info("Upstream definition: {}".format(upstream_definition))
    log_info("Upstream definition admin: {}".format(upstream_definition_admin))

    ansible_runner = AnsibleRunner(cluster_config)
    status = ansible_runner.run_ansible_playbook(
        "install-nginx.yml",
        extra_vars={
            "upstream_sync_gatways": upstream_definition,
            "upstream_sync_gatways_admin": upstream_definition_admin
        })

    assert status == 0, "Failed to install nginx!"
def install_nginx(cluster_config):
    """
    Deploys nginx to nodes with the load_balancer tag

    1. Get the sync_gateway endpoints from the cluster configuration
    2. Use the endpoints to render the nginx config (resources/nginx_configs/nginx.conf)
      to distribute load across the running sync_gateways.
      i.e. If your 'cluster_config' has 2 sync_gateways, nginx will be setup to forward
        requests to both of the sync_gateways using a weighted round robin distribution.
        If you have 3, it will split the load between 3, etc ...
    3. Deploy the config and install nginx on load_balancer nodes
    4. Start the nginx service
    """

    cluster = ClusterKeywords()
    topology = cluster.get_cluster_topology(cluster_config)

    # Get sync_gateway enpoints from cluster_config
    #  and build a string of upstream server definitions
    # upstream sync_gateway {
    #   server 192.168.33.11:4984;
    #   server 192.168.33.12:4984;
    #  }
    upstream_definition = ""
    for sg in topology["sync_gateways"]:
        # string http:// to adhere to expected format for nginx.conf
        ip_port = sg["public"].replace("http://", "")
        upstream_definition += "server {};\n".format(ip_port)

    log_info("Upstream definition: ")
    log_info(upstream_definition)

    ansible_runner = AnsibleRunner(cluster_config)
    status = ansible_runner.run_ansible_playbook(
        "install-nginx.yml",
        extra_vars={
            "upstream_sync_gatways": upstream_definition
        }
    )

    assert status == 0, "Failed to install nginx!"
import os

from ansible_runner import AnsibleRunner


if __name__ == "__main__":
    usage = "usage: python stop_telegraf.py"

    try:
        cluster_config = os.environ["CLUSTER_CONFIG"]
    except KeyError as ke:
        print ("Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to run against")
        raise KeyError("CLUSTER_CONFIG not defined. Unable to stop telegraf collectors.")

    ansible_runner = AnsibleRunner(cluster_config)
    status = ansible_runner.run_ansible_playbook("stop-telegraf.yml")
    assert status == 0, "Failed to stop telegraf collectors"
Example #15
0
import os

from ansible_runner import AnsibleRunner

if __name__ == "__main__":
    usage = "usage: python stop_telegraf.py"

    try:
        cluster_config = os.environ["CLUSTER_CONFIG"]
    except KeyError as ke:
        print(
            "Make sure CLUSTER_CONFIG is defined and pointing to the configuration you would like to run against"
        )
        raise KeyError(
            "CLUSTER_CONFIG not defined. Unable to stop telegraf collectors.")

    ansible_runner = AnsibleRunner(cluster_config)
    status = ansible_runner.run_ansible_playbook("stop-telegraf.yml")
    assert status == 0, "Failed to stop telegraf collectors"