Ejemplo n.º 1
0
    def start(self):
        log = logging.getLogger(__name__)

        if self.enabled and self.image_name is not None:
            if self.docker_present:
                log.info(h1('Starting OCI-in-Docker support'))
                docker_image = terminal.docker_images(self.image_name)

                if not docker_image:
                    log.info('Pulling docker image {}'.format(self.image_name))
                    terminal.docker_pull(self.image_name)

                log.info('OCI-in-Docker provided by image {}'.format(
                    self.image_name))

                return FeatureStartResult(True, [])
            else:
                log.info(h1('OCI-in-Docker support unavailable.'))
                log.info(
                    h1('To provide support ensure Docker is running and restart the sandbox'
                       ))

                return FeatureStartResult(False, [])
        else:
            return FeatureStartResult(False, [])
Ejemplo n.º 2
0
def start_docker_instance(proxy_bind_addr, bundle_http_port, proxy_ports,
                          all_feature_ports):
    log = logging.getLogger(__name__)
    log.info(h1('Starting HAProxy'))

    docker_image = terminal.docker_images(HAPROXY_DOCKER_IMAGE)
    if not docker_image:
        log.info('Pulling docker image {}'.format(HAPROXY_DOCKER_IMAGE))
        terminal.docker_pull(HAPROXY_DOCKER_IMAGE)

    all_proxy_ports = [bundle_http_port]
    all_proxy_ports.extend(DEFAULT_PROXY_PORTS)
    all_proxy_ports.extend(all_feature_ports)
    all_proxy_ports.extend(proxy_ports)
    all_proxy_ports = sorted(set(all_proxy_ports))

    port_args = []
    for port in all_proxy_ports:
        port_args.append('-p')
        port_args.append('{}:{}:{}'.format(proxy_bind_addr.exploded, port,
                                           port))

    docker_args = ['-d', '--name', DEFAULT_SANDBOX_PROXY_CONTAINER_NAME] + port_args + \
                  ['-v', '{}:/usr/local/etc/haproxy:ro'.format(HAPROXY_CFG_DIR)]

    log.info('Exposing the following ports {}'.format(all_proxy_ports))
    terminal.docker_run(docker_args, HAPROXY_DOCKER_IMAGE, positional_args=[])
Ejemplo n.º 3
0
    def test_hung_processes(self):
        ps_output = [{
            'id': 58002,
            'type': 'core'
        }, {
            'id': 58003,
            'type': 'agent'
        }]

        stdout = MagicMock()
        stderr = MagicMock()
        mock_os_kill = MagicMock()
        mock_time_sleep = MagicMock()
        mock_find_pids = MagicMock(return_value=ps_output)

        with patch('os.kill', mock_os_kill), \
                patch('time.sleep', mock_time_sleep), \
                patch('conductr_cli.sandbox_common.find_pids', mock_find_pids):
            logging_setup.configure_logging(MagicMock(**self.default_args),
                                            stdout, stderr)
            sandbox_stop_jvm.stop(MagicMock(**self.default_args))

        self.assertEqual(h1('Stopping ConductR') + '\n', self.output(stdout))
        self.assertEqual(
            strip_margin(
                as_error(
                    """|Error: ConductR core pid 58002 could not be stopped
                                                  |Error: ConductR agent pid 58003 could not be stopped
                                                  |Error: Please stop the processes manually
                                                  |""")), self.output(stderr))
        mock_os_kill.assert_has_calls(
            [call(58002, signal.SIGTERM),
             call(58003, signal.SIGTERM)])
Ejemplo n.º 4
0
def stop(args):
    """
    Stops the existing ConductR core and agent processes.
    This is done by interrogating the output of the ps, looking for java process which is running of the sandbox image.
    directory.

    :param args: args parsed from the input arguments
    """
    log = logging.getLogger(__name__)
    core_info, agent_info = sandbox_common.resolve_conductr_info(
        args.image_dir)

    pids_info = sandbox_common.find_pids(core_info['extraction_dir'],
                                         agent_info['extraction_dir'])
    if pids_info:
        log.info(h1('Stopping ConductR'))
        killed_pids_info, hung_pids_info = kill_processes(
            core_info, agent_info, pids_info)
        if hung_pids_info:
            for hung_pid_info in hung_pids_info:
                log.error('ConductR {} pid {} could not be stopped'.format(
                    hung_pid_info['type'], hung_pid_info['id']))
            log.error('Please stop the processes manually')
            return False
        else:
            log.info('ConductR has been successfully stopped')
            return True
    else:
        return True
Ejemplo n.º 5
0
 def test_h1_wrapping(self):
     self.assertEqual(
         strip_margin("""||------------------------------------------------|
                         || Starting logging feature based on              |
                         || elasticsearch and kibana                       |
                         ||------------------------------------------------|"""
                      ),
         screen_utils.h1(
             'Starting logging feature based on elasticsearch and kibana'))
Ejemplo n.º 6
0
def log_run_attempt(args, run_result, feature_results, feature_provided):
    log = logging.getLogger(__name__)
    container_names = run_result.container_names
    log.info(h1('Summary'))
    log.info('ConductR has been started')
    plural_string = 's' if len(container_names) > 1 else ''
    log.info(
        'Check resource consumption of Docker container{} that run the ConductR node{} with:'
        .format(plural_string, plural_string))
    log.info('  docker stats {}'.format(' '.join(container_names)))
    log.info('Check current bundle status with:')
    log.info('  conduct info')
Ejemplo n.º 7
0
 def start(self):
     if is_conductr_supportive_of_features(self.image_version):
         log = logging.getLogger(__name__)
         log.info(h1('Starting logging feature based on eslite'))
         eslite = select_bintray_uri('eslite', self.version_args)
         log.info('Deploying bundle %s..' % eslite['bundle'])
         load_command = ['load', eslite['bundle'], '--disable-instructions'] + \
             parse_offline_mode_arg(self.offline_mode)
         conductr_cli.conduct_main.run(load_command,
                                       configure_logging=False)
         conductr_cli.conduct_main.run(
             ['run', eslite['name'], '--disable-instructions'],
             configure_logging=False)
         return FeatureStartResult(True, [])
     else:
         return FeatureStartResult(False, [])
Ejemplo n.º 8
0
def log_run_attempt(args, run_result, feature_results, feature_provided):
    """
    Logs the run attempt. This method will be called after the completion of run method and when all the features has
    been started.

    :param args: args parsed from the input arguments
    :param run_result: the result from calling sandbox_run_jvm.run() - instance of sandbox_run_jvm.SandboxRunResult
    :param feature_results: the feature result
    :param feature_provided: the values provided by all started features
    :return:
    """
    log = logging.getLogger(__name__)
    log.info(h1('Summary'))
    log.info(h2('ConductR'))
    log.info('ConductR has been started:')
    plural_core = 's' if len(run_result.core_addrs) > 1 else ''
    log.info('  core instance{} on {}'.format(plural_core, ', '.join(str(i) for i in run_result.core_addrs)))
    plural_agent = 's' if len(run_result.agent_addrs) > 1 else ''
    log.info('  agent instance{} on {}'.format(plural_agent, ', '.join(str(i) for i in run_result.agent_addrs)))
    log.info('ConductR service locator has been started on:')
    log.info('  {}:{}'.format(run_result.host, DEFAULT_SERVICE_LOCATOR_PORT))

    log.info(h2('Proxy'))
    if FEATURE_PROVIDE_PROXYING in feature_provided:
        log.info('HAProxy has been started')
        log.info('By default, your bundles are accessible on:')
        log.info('  {}:{}'.format(run_result.host, args.bundle_http_port))
    else:
        log.info('HAProxy has not been started')
        log.info('To enable proxying ensure Docker is running and restart the sandbox')

    if feature_results:
        log.info(h2('Features'))
        log.info('The following feature related bundles have been started:')
        for feature_result in feature_results:
            if FEATURE_PROVIDE_PROXYING in feature_provided:
                uri = '{}:{}'.format(run_result.host, feature_result.port)
            else:
                uri = '{}:{}/services/{}'.format(run_result.host, DEFAULT_SERVICE_LOCATOR_PORT,
                                                 feature_result.name)
            log.info('  {} on {}'.format(feature_result.name, uri))

    log.info(h2('Bundles'))
    log.info('Check latest bundle status with:')
    log.info('  conduct info')
    log.info('Current bundle status:')
    conduct_main.run(['info', '--host', run_result.host], configure_logging=False)
Ejemplo n.º 9
0
def stop_proxy():
    log = logging.getLogger(__name__)

    if docker.is_docker_present():
        try:
            running_container = get_running_haproxy()
            if running_container:
                log.info(h1('Stopping HAProxy'))
                terminal.docker_rm([DEFAULT_SANDBOX_PROXY_CONTAINER_NAME])
                log.info('HAProxy has been successfully stopped')

            return True
        except (AttributeError, CalledProcessError, NOT_FOUND_ERROR):
            return False
    else:
        # Docker is not present, so the proxy feature won't be running in the first place.
        return True
Ejemplo n.º 10
0
def stop(args):
    log = logging.getLogger(__name__)
    try:
        running_containers = sandbox_common.resolve_running_docker_containers()
    except (AttributeError, CalledProcessError, NOT_FOUND_ERROR):
        # return True because Docker is not installed and therefore no containers need to be stopped.
        return True

    if running_containers:
        log.info(h1('Stopping ConductR'))
        try:
            terminal.docker_rm(running_containers)
            log.info('ConductR has been successfully stopped')
            return True
        except (AttributeError, CalledProcessError, NOT_FOUND_ERROR):
            log.error('ConductR containers could not be stopped')
            log.error('Please stop the Docker containers manually')
            return False
    else:
        return True
Ejemplo n.º 11
0
 def start(self):
     log = logging.getLogger(__name__)
     log.info(h1('Starting monitoring feature'))
     bundle_repo = 'lightbend/commercial-monitoring/' if self.version_args and self.version_args[0] == 'snapshot' \
         else ''
     if is_cinnamon_grafana_docker_based(self.image_version):
         bundle_name = 'cinnamon-grafana-docker'
     else:
         bundle_name = 'cinnamon-grafana'
     grafana = select_bintray_uri(bundle_name, self.version_args,
                                  bundle_repo)
     log.info('Deploying bundle %s..' % grafana['bundle'])
     load_command = ['load', grafana['bundle'], '--disable-instructions'] + \
         parse_offline_mode_arg(self.offline_mode)
     conductr_cli.conduct_main.run(load_command, configure_logging=False)
     conductr_cli.conduct_main.run([
         'run', grafana['name'], '--disable-instructions', '--wait-timeout',
         '600'
     ],
                                   configure_logging=False)
     return FeatureStartResult(
         True, [BundleStartResult(grafana['name'], self.ports[0])])
Ejemplo n.º 12
0
 def start(self):
     if is_conductr_supportive_of_features(self.image_version):
         log = logging.getLogger(__name__)
         log.info(
             h1('Starting logging feature based on elasticsearch and kibana'
                ))
         log.info(
             'conductr-kibana bundle is packaged as a Docker image. Checking Docker requirements..'
         )
         docker.validate_docker_vm(docker.vm_type())
         log.info('Docker is installed and configured correctly.')
         elasticsearch = select_bintray_uri('conductr-elasticsearch',
                                            self.version_args)
         log.info('Deploying bundle %s..' % elasticsearch['bundle'])
         elasticsearch_load_command = ['load', elasticsearch['bundle'], '--disable-instructions'] + \
             parse_offline_mode_arg(self.offline_mode)
         conductr_cli.conduct_main.run(elasticsearch_load_command,
                                       configure_logging=False)
         conductr_cli.conduct_main.run(
             ['run', elasticsearch['name'], '--disable-instructions'],
             configure_logging=False)
         kibana = select_bintray_uri('conductr-kibana', self.version_args)
         log.info('Deploying bundle %s..' % kibana['bundle'])
         kibana_load_command = ['load', kibana['bundle'], '--disable-instructions'] + \
             parse_offline_mode_arg(self.offline_mode)
         conductr_cli.conduct_main.run(kibana_load_command,
                                       configure_logging=False)
         conductr_cli.conduct_main.run([
             'run', kibana['name'], '--disable-instructions',
             '--wait-timeout', '600'
         ],
                                       configure_logging=False)
         return FeatureStartResult(True, [
             BundleStartResult('conductr-kibana', self.ports[0]),
             BundleStartResult('conductr-elasticsearch', self.ports[1])
         ])
     else:
         return FeatureStartResult(False, [])
Ejemplo n.º 13
0
def start_nodes(args, nr_of_containers, features):
    container_names = []
    log = logging.getLogger(__name__)
    log.info(h1('Starting ConductR'))
    ports = collect_ports(args, features)
    conductr_args = flatten([feature.conductr_args() for feature in features])
    conductr_features = flatten(
        [feature.conductr_feature_envs() for feature in features])
    feature_conductr_roles = flatten(
        [feature.conductr_roles() for feature in features])
    for i in range(nr_of_containers):
        container_name = '{prefix}{nr}'.format(prefix=CONDUCTR_NAME_PREFIX,
                                               nr=i)
        container_names.append(container_name)
        # Display the ports on the command line. Only if the user specifies a certain feature, then
        # the corresponding port will be displayed when running 'sandbox run' or 'sandbox debug'
        if ports:
            host_ip = host.DOCKER_IP
            ports_desc = ' exposing ' + ', '.join([
                '{}:{}'.format(host_ip, map_port(i, port))
                for port in sorted(ports)
            ])
        else:
            ports_desc = ''
        log.info('Starting container {container}{port_desc}..'.format(
            container=container_name, port_desc=ports_desc))
        cond0_ip = inspect_cond0_ip() if i > 0 else None
        conductr_container_roles = sandbox_common.resolve_conductr_roles_by_instance(
            args.conductr_roles, feature_conductr_roles, i)
        run_conductr_cmd(
            i, nr_of_containers, container_name, cond0_ip, args.envs,
            '{image}:{version}'.format(image=args.image,
                                       version=args.image_version),
            args.log_level, ports, args.bundle_http_port, conductr_features,
            conductr_container_roles, conductr_args)
    return container_names
Ejemplo n.º 14
0
 def test_h1(self):
     self.assertEqual(
         strip_margin("""||------------------------------------------------|
                         || Summary                                        |
                         ||------------------------------------------------|"""
                      ), screen_utils.h1('Summary'))
Ejemplo n.º 15
0
def run(args, features):
    """
    Starts the ConductR core and agent.

    :param args: args parsed from the input arguments
    :param features: list of features which are specified via -f switch.
                     This is only relevant for Docker based sandbox since the features decides what port to expose
    :return: SandboxRunResult
    """
    nr_of_core_instances, nr_of_agent_instances = instance_count(args.image_version, args.nr_of_instances)

    validate_jvm_support()
    validate_hostname_lookup()
    validate_64bit_support()
    validate_bintray_credentials(args.image_version, args.offline_mode)

    sandbox_stop.stop(args)

    log = logging.getLogger(__name__)
    log.info(h1('Starting ConductR'))

    cleanup_tmp_dir(args.tmp_dir)

    bind_addrs = find_bind_addrs(max(nr_of_core_instances, nr_of_agent_instances), args.addr_range)

    core_extracted_dir, agent_extracted_dir = obtain_sandbox_image(args.image_dir, args.image_version,
                                                                   args.offline_mode)

    core_addrs = bind_addrs[0:nr_of_core_instances]
    core_pids = start_core_instances(core_extracted_dir,
                                     args.tmp_dir,
                                     args.envs,
                                     args.envs_core,
                                     args.args,
                                     args.args_core,
                                     core_addrs,
                                     args.conductr_roles,
                                     features,
                                     args.log_level)

    sandbox_common.wait_for_start(WaitForConductrArgs(core_addrs[0]))

    try:
        license_validation.validate_license(args.image_version,
                                            core_addrs[0],
                                            nr_of_agent_instances,
                                            DEFAULT_LICENSE_FILE)
    except LicenseValidationError as e:
        sandbox_stop.stop(args)
        raise e

    agent_addrs = bind_addrs[0:nr_of_agent_instances]
    agent_pids = start_agent_instances(agent_extracted_dir,
                                       args.tmp_dir,
                                       args.envs,
                                       args.envs_agent,
                                       args.args,
                                       args.args_agent,
                                       bind_addrs[0:nr_of_agent_instances],
                                       core_addrs,
                                       args.conductr_roles,
                                       features,
                                       args.log_level)
    return SandboxRunResult(core_pids, core_addrs, agent_pids, agent_addrs, wait_for_conductr=False)