def _zip_logs_location(logs_location): 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))
def _handle_log_zipping(logs_location): logs_zip = logs_location + '.zip' fileoperations.zip_up_folder(logs_location, logs_zip) fileoperations.delete_directory(logs_location) fileoperations.set_user_only_permissions(logs_zip) io.echo(strings['logs.location'].replace('{location}', logs_zip))
def _write_full_logs_to_file(full_logs, logs_location, instance_id): full_filepath = __get_full_path_for_instance_logs(logs_location, instance_id) with open(full_filepath, 'w+') as log_file: log_file.write(full_logs) fileoperations.set_user_only_permissions(full_filepath)
def _zip_logs_location(logs_location): 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))
def download_config_from_s3(app_name, cfg_name): bucket = elasticbeanstalk.get_storage_location() body = s3.get_object(bucket, _get_s3_keyname_for_template(app_name, cfg_name)) location = write_to_local_config(cfg_name, body) fileoperations.set_user_only_permissions(location) io.echo() io.echo('Configuration saved at: ' + location)
def download_config_from_s3(app_name, cfg_name): bucket = elasticbeanstalk.get_storage_location() body = s3.get_object(bucket, _get_s3_keyname_for_template(app_name, cfg_name)) location = write_to_local_config(cfg_name, body) fileoperations.set_user_only_permissions(location) io.echo() io.echo('Configuration saved at: ' + location)
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)
def _handle_log_zipping(logs_location): logs_zip = logs_location + '.zip' fileoperations.zip_up_folder(logs_location, logs_zip) fileoperations.delete_directory(logs_location) fileoperations.set_user_only_permissions(logs_zip) io.echo( strings['logs.location'].replace( '{location}', logs_zip ) )
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)
def test_set_user_only_permissions(self): dir_1 = 'dir_1' dir_2 = os.path.join('dir_1', 'dir_2') dir_3 = os.path.join('dir_1', 'dir_2', 'dir_3') file_1 = os.path.join('dir_1', 'dir_2', 'dir_3', 'file_1') os.mkdir(dir_1) os.mkdir(dir_2) os.mkdir(dir_3) open(file_1, 'w').close() fileoperations.set_user_only_permissions('dir_1') if sys.version_info < (3, 0): self.assertEqual('040755', oct(os.stat(dir_1)[stat.ST_MODE])) self.assertEqual('040700', oct(os.stat(dir_2)[stat.ST_MODE])) self.assertEqual('040700', oct(os.stat(dir_3)[stat.ST_MODE])) self.assertEqual('0100600', oct(os.stat(file_1)[stat.ST_MODE])) else: self.assertEqual('0o40755', oct(os.stat(dir_1)[stat.ST_MODE])) self.assertEqual('0o40700', oct(os.stat(dir_2)[stat.ST_MODE])) self.assertEqual('0o40700', oct(os.stat(dir_3)[stat.ST_MODE])) self.assertEqual('0o100600', oct(os.stat(file_1)[stat.ST_MODE]))
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))
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)
def _write_full_logs_to_file(full_logs, logs_location, instance_id): full_filepath = __get_full_path_for_instance_logs(logs_location, instance_id) with open(full_filepath, 'w+') as log_file: log_file.write(full_logs) fileoperations.set_user_only_permissions(full_filepath)