Example #1
0
def _setup_logs_folder(cloudwatch_log_source):
    if cloudwatch_log_source == logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE:
        logs_folder_name = _timestamped_directory_name()
    else:
        if not os.path.exists(
                fileoperations.get_logs_location(logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE)
        ):
            os.mkdir(
                fileoperations.get_logs_location(logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE)
            )

        logs_folder_name = os.path.join(
            logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE,
            _timestamped_directory_name()
        )

    os.mkdir(fileoperations.get_logs_location(logs_folder_name))

    return fileoperations.get_logs_location(logs_folder_name)
Example #2
0
def _setup_logs_folder(cloudwatch_log_source):
    if cloudwatch_log_source == logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE:
        logs_folder_name = _timestamped_directory_name()
    else:
        if not os.path.exists(
                fileoperations.get_logs_location(logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE)
        ):
            os.mkdir(
                fileoperations.get_logs_location(logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE)
            )

        logs_folder_name = os.path.join(
            logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE,
            _timestamped_directory_name()
        )

    os.mkdir(fileoperations.get_logs_location(logs_folder_name))

    return fileoperations.get_logs_location(logs_folder_name)
Example #3
0
def _handle_bundle_logs(instance_id_list, do_zip):
    logs_folder_name = _timestamped_directory_name()
    logs_location = fileoperations.get_logs_location(logs_folder_name)
    _download_logs_for_all_instances(instance_id_list, logs_location)
    fileoperations.set_user_only_permissions(logs_location)

    if do_zip:
        _handle_log_zipping(logs_location)
    else:
        io.echo(strings['logs.location'].replace('{location}', logs_location))
        _attempt_update_symlink_to_latest_logs_retrieved(logs_location)
Example #4
0
def _handle_bundle_logs(instance_id_list, do_zip):
    logs_folder_name = _timestamped_directory_name()
    logs_location = fileoperations.get_logs_location(logs_folder_name)
    _download_logs_for_all_instances(instance_id_list, logs_location)
    fileoperations.set_user_only_permissions(logs_location)

    if do_zip:
        _handle_log_zipping(logs_location)
    else:
        io.echo(strings['logs.location'].replace('{location}',
                                                 logs_location))
        _attempt_update_symlink_to_latest_logs_retrieved(logs_location)
Example #5
0
def print_logs():
    """
    Print the path to root local logs directory, the most recently written
    local logs directory and the path to 'latest' symlink.
    :return: None
    """

    root_log = fileoperations.get_logs_location(HOST_LOGS_DIRNAME)
    last_local_logs = _get_last_local_logs(root_log)
    timestamp = os.path.getmtime(last_local_logs) if last_local_logs else None

    _print_logs(root_log, last_local_logs, timestamp)
Example #6
0
 def logdir_path(cls):
     return fileoperations.get_logs_location(cls.ROOT_LOCAL_LOGS_DIRNAME)
Example #7
0
 def test_get_logs_location(self):
     self.assertEqual(
         os.path.join(os.path.abspath('.'), '.elasticbeanstalk', 'logs',
                      'some-folder'),
         fileoperations.get_logs_location('some-folder'))
Example #8
0
def get_logs(env_name, info_type, do_zip=False, instance_id=None):
    # Get logs
    result = elasticbeanstalk.retrieve_environment_info(env_name, info_type)
    """
        Results are ordered with latest last, we just want the latest
    """
    log_list = {}
    for log in result['EnvironmentInfo']:
        i_id = log['Ec2InstanceId']
        url = log['Message']
        log_list[i_id] = url

    if instance_id:
        try:
            log_list = {instance_id: log_list[instance_id]}
        except KeyError:
            raise NotFoundError(strings['beanstalk-logs.badinstance'].replace(
                '{instance_id}', instance_id))

    if info_type == 'bundle':
        # save file, unzip, place in logs directory
        logs_folder_name = datetime.now().strftime("%y%m%d_%H%M%S")
        logs_location = fileoperations.get_logs_location(logs_folder_name)
        #get logs for each instance
        for i_id, url in iteritems(log_list):
            zip_location = utils.save_file_from_url(url, logs_location,
                                                    i_id + '.zip')
            instance_folder = os.path.join(logs_location, i_id)
            fileoperations.unzip_folder(zip_location, instance_folder)
            fileoperations.delete_file(zip_location)

        fileoperations.set_user_only_permissions(logs_location)
        if do_zip:
            fileoperations.zip_up_folder(logs_location, logs_location + '.zip')
            fileoperations.delete_directory(logs_location)

            logs_location += '.zip'
            fileoperations.set_user_only_permissions(logs_location)
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
        else:
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
            # create symlink to logs/latest
            latest_location = fileoperations.get_logs_location('latest')
            try:
                os.unlink(latest_location)
            except OSError:
                # doesn't exist. Ignore
                pass
            try:
                os.symlink(logs_location, latest_location)
                io.echo('Updated symlink at', latest_location)
            except OSError:
                #Oh well.. we tried.
                ## Probably on windows, or logs/latest is not a symlink
                pass

    else:
        # print logs
        data = []
        for i_id, url in iteritems(log_list):
            data.append('============= ' + str(i_id) + ' ==============')
            log_result = utils.get_data_from_url(url)
            data.append(utils.decode_bytes(log_result))
        io.echo_with_pager(os.linesep.join(data))
Example #9
0
def retrieve_cloudwatch_logs(log_group,
                             info_type,
                             do_zip=False,
                             instance_id=None):
    # Get the log streams, a.k.a. the instance ids in the log group
    """
        Retrieves cloudwatch logs for every stream under the log group unless the instance_id is specified. If tail
         logs is enabled we will only get the last 100 lines and return the result to a pager for the user to use. If
         bundle info type is chosen we will get all of the logs and save them to a dir under .elasticbeanstalk/logs/
        and if zip is enabled we will zip those logs for the user.
        :param log_group: cloudwatch log group
        :param info_type: can be 'tail' or 'bundle'
        :param do_zip: boolean to determine if we should zip the logs we retrieve
        :param instance_id: if we only want a single instance we can specify it here
    """
    log_streams = cloudwatch.describe_log_streams(
        log_group, log_stream_name_prefix=instance_id)
    instance_ids = []

    if len(log_streams['logStreams']) == 0:
        io.log_error(strings['logs.nostreams'])

    for stream in log_streams['logStreams']:
        instance_ids.append(stream['logStreamName'])

    # This is analogous to getting the full logs
    if info_type == 'bundle':
        # Create directory to store logs
        logs_folder_name = datetime.now().strftime("%y%m%d_%H%M%S")
        logs_location = fileoperations.get_logs_location(logs_folder_name)
        os.makedirs(logs_location)
        # Get logs for each instance
        for instance_id in instance_ids:
            full_logs = get_cloudwatch_stream_logs(log_group, instance_id)
            full_filepath = '{0}/{1}.log'.format(logs_location, instance_id)
            log_file = open(full_filepath, 'w+')
            log_file.write(full_logs)
            log_file.close()
            fileoperations.set_user_only_permissions(full_filepath)

        if do_zip:
            fileoperations.zip_up_folder(logs_location, logs_location + '.zip')
            fileoperations.delete_directory(logs_location)

            logs_location += '.zip'
            fileoperations.set_user_only_permissions(logs_location)
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
        else:
            io.echo(strings['logs.location'].replace('{location}',
                                                     logs_location))
            # create symlink to logs/latest
            latest_location = fileoperations.get_logs_location('latest')
            try:
                os.unlink(latest_location)
            except OSError:
                # doesn't exist. Ignore
                pass
            try:
                os.symlink(logs_location, latest_location)
                io.echo('Updated symlink at', latest_location)
            except OSError:
                # Oh well.. we tried.
                ## Probably on windows, or logs/latest is not a symlink
                pass

    else:
        # print logs
        all_logs = ""
        for instance_id in instance_ids:
            tail_logs = get_cloudwatch_stream_logs(
                log_group, instance_id, num_log_events=TAIL_LOG_SIZE)
            all_logs += '\n\n============= {0} - {1} ==============\n\n'.format(
                str(instance_id), get_log_name(log_group))
            all_logs += tail_logs
        io.echo_with_pager(all_logs)