#!/usr/bin/env python3

import logging
import os
import random
import string
import sys

import test_util.aws
import test_util.cluster


logging.basicConfig(format='[%(asctime)s|%(name)s|%(levelname)s]: %(message)s', level=logging.DEBUG)
log = logging.getLogger(__name__)


def main():
    num_masters = int(os.getenv('MASTERS', '3'))
    num_agents = int(os.getenv('AGENTS', '2'))
    num_public_agents = int(os.getenv('PUBLIC_AGENTS', '1'))
    stack_name = 'upgrade-test-' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))

    pytest_dir = os.getenv('DCOS_PYTEST_DIR', '/opt/mesosphere/active/dcos-integration-test')
    pytest_cmd = os.getenv('DCOS_PYTEST_CMD', 'py.test -rs -vv ' + os.getenv('CI_FLAGS', ''))

    stable_installer_url = os.environ['STABLE_INSTALLER_URL']
    installer_url = os.environ['INSTALLER_URL']

    vpc, ssh_info = test_util.aws.VpcCfStack.create(
        stack_name=stack_name,
        instance_type='m4.xlarge',
        instance_os='cent-os-7-dcos-prereqs',
        # An instance for each cluster node plus the bootstrap.
        instance_count=(num_masters + num_agents + num_public_agents + 1),
        admin_location='0.0.0.0/0',
        key_pair_name='default',
        boto_wrapper=test_util.aws.BotoWrapper(
            region=os.getenv('DEFAULT_AWS_REGION', 'eu-central-1'),
            aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
            aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
        ),
    )
    vpc.wait_for_stack_creation()
    cluster = test_util.cluster.Cluster.from_vpc(
        vpc,
        ssh_info,
        ssh_key_path=os.getenv('DCOS_SSH_KEY_PATH', 'default_ssh_key'),
        num_masters=num_masters,
        num_agents=num_agents,
        num_public_agents=num_public_agents,
    )

    # Use the CLI installer to set exhibitor_storage_backend = zookeeper.
    test_util.cluster.install_dcos(cluster, stable_installer_url, api=False)
    with cluster.ssher.tunnel(cluster.bootstrap_host) as bootstrap_host_tunnel:
        bootstrap_host_tunnel.remote_cmd(['sudo', 'rm', '-rf', cluster.ssher.home_dir + '/*'])
    test_util.cluster.upgrade_dcos(cluster, installer_url)

    result = test_util.cluster.run_integration_tests(cluster, pytest_dir=pytest_dir, pytest_cmd=pytest_cmd)
    if result == 0:
        log.info("Test successsful! Deleting VPC if provided in this run...")
        vpc.delete()
    else:
        log.info("Test failed! VPC will remain for debugging 1 hour from instantiation")
    sys.exit(result)

@@ -15,16 +15,1164 @@
  python3
+%0A%22%22%22Integration test for onprem DC/OS upgraded from latest stable.%0A%0AThe following environment variables control test procedure:%0A%0AMASTERS: integer (default=3)%0A    The number of masters to create from a newly created VPC.%0A%0AAGENTS: integer (default=2)%0A    The number of agents to create from a newly created VPC.%0A%0APUBLIC_AGENTS: integer (default=1)%0A    The number of public agents to create from a newly created VPC.%0A%0ADCOS_SSH_KEY_PATH: string (default='default_ssh_key')%0A    Use to set specific ssh key path. Otherwise, script will expect key at default_ssh_key%0A%0AINSTALLER_URL: Installer URL for the DC/OS release under test.%0A%0ASTABLE_INSTALLER_URL: Installer URL for the latest stable DC/OS release.%0A%0ACI_FLAGS: string (default=None)%0A    If provided, this string will be passed directly to py.test as in:%0A    py.test -vv CI_FLAGS integration_test.py%0A%0ADCOS_PYTEST_DIR: string(default='/opt/mesosphere/active/dcos-integration-test')%0A    The working dir for py.test.%0A%0ADCOS_PYTEST_CMD: string(default='py.test -rs -vv ' + os.getenv('CI_FLAGS', ''))%0A    The complete py.test command used to run integration tests. If provided,%0A    CI_FLAGS is ignored.%0A%0A%22%22%22
 %0A%0Aimport

Add test_util.test_upgrade_vpc module docstring