<commit_before>const AWS = require('aws-sdk');
const werelogs = require('werelogs');

const config = require('../../conf/Config');
const management = require('../../lib/management/index');

const logger = new werelogs.Logger('mdManagement:lifecycle');

function getS3Client(endpoint) {
    const serviceCredentials =
          management.getLatestServiceAccountCredentials();
    // FIXME
    const keys = serviceCredentials.accounts[0].keys;
    const credentials = new AWS.Credentials(keys.access, keys.secret);
    const s3Client = new AWS.S3({
        endpoint,
        sslEnabled: false,
        credentials,
        s3ForcePathStyle: true,
        signatureVersion: 'v4',
        httpOptions: { timeout: 0 },
        maxRetries: 3,
    });
    return s3Client;
}

function putLifecycleConfiguration(bucketName, workflows, cb) {
    logger.debug('updating lifecycle configuration');
    const cfg = config.s3;
    const endpoint = `${cfg.host}:${cfg.port}`;

    const params = {
        Bucket: bucketName,
        LifecycleConfiguration: {
            // TODO this is where the translation from Orbit config
            // to lifecycle config shall occur
            Rules: workflows.map(wf => wf),
        },
    };
    getS3Client(endpoint).putBucketLifecycleConfiguration(params, err => {
        logger.debug('lifecycle configuration apply done', {
            bucket: bucketName, error: err });
        if (err && err.code === 'NoSuchBucket') {
            return cb();
        }
        return cb(err);
    });
}

function deleteLifecycleConfiguration(bucketName, cb) {
    logger.debug('deleting lifecycle configuration');
    const cfg = config.extensions.lifecycle.source.s3;
    const endpoint = `${cfg.host}:${cfg.port}`;

    const params = {
        Bucket: bucketName,
    };
    getS3Client(endpoint).deleteBucketLifecycle(params, err => {
        logger.debug('lifecycle configuration deleted', {
            bucket: bucketName, error: err });
        if (err && err.code === 'NoSuchBucket') {
            return cb();
        }
        return cb(err);
    });
}

function applyBucketLifecycleWorkflows(bucketName, bucketWorkflows,
                                       workflowUpdates, cb) {
    if (bucketWorkflows.length > 0) {
        putLifecycleConfiguration(bucketName, bucketWorkflows, cb);
    } else {
        deleteLifecycleConfiguration(bucketName, cb);
    }
}

module.exports = {
    applyBucketLifecycleWorkflows,
};
<commit_msg>ft: Apply lifecycle configuration in backbeat management<commit_after>const AWS = require('aws-sdk');
const werelogs = require('werelogs');

const config = require('../../conf/Config');
const management = require('../../lib/management/index');

const logger = new werelogs.Logger('mdManagement:lifecycle');

function getS3Client(endpoint) {
    const serviceCredentials =
          management.getLatestServiceAccountCredentials();
    // FIXME
    const keys = serviceCredentials.accounts[0].keys;
    const credentials = new AWS.Credentials(keys.access, keys.secret);
    const s3Client = new AWS.S3({
        endpoint,
        sslEnabled: false,
        credentials,
        s3ForcePathStyle: true,
        signatureVersion: 'v4',
        httpOptions: { timeout: 0 },
        maxRetries: 3,
    });
    return s3Client;
}

function putLifecycleConfiguration(bucketName, workflows, cb) {
    logger.debug('updating lifecycle configuration');
    const cfg = config.s3;
    const endpoint = `${cfg.host}:${cfg.port}`;
    const params = {
        Bucket: bucketName,
        LifecycleConfiguration: {
            Rules: workflows.map(wf => {
                const workflow = {
                    ID: wf.workflowId,
                    Status: wf.enabled ? 'Enabled' : 'Disabled',
                };
                if (wf.currentVersionTriggerDelayDays) {
                    workflow.Expiration = {
                        Days: wf.currentVersionTriggerDelayDays,
                    };
                }
                if (wf.filter && wf.filter.objectKeyPrefix) {
                    workflow.Filter = {
                        Prefix: wf.filter.objectKeyPrefix,
                    };
                }
                if (wf.previousVersionTriggerDelayDays) {
                    workflow.NoncurrentVersionExpiration = {
                        NoncurrentDays: wf.previousVersionTriggerDelayDays,
                    };
                }
                return workflow;
            }),
        },
    };
    getS3Client(endpoint).putBucketLifecycleConfiguration(params, err => {
        logger.debug('lifecycle configuration apply done', {
            bucket: bucketName, error: err });
        if (err && err.code === 'NoSuchBucket') {
            return cb();
        }
        return cb(err);
    });
}

function deleteLifecycleConfiguration(bucketName, cb) {
    logger.debug('deleting lifecycle configuration');
    const cfg = config.s3;
    const endpoint = `${cfg.host}:${cfg.port}`;

    const params = {
        Bucket: bucketName,
    };
    getS3Client(endpoint).deleteBucketLifecycle(params, err => {
        logger.debug('lifecycle configuration deleted', {
            bucket: bucketName, error: err });
        if (err && err.code === 'NoSuchBucket') {
            return cb();
        }
        return cb(err);
    });
}

function applyBucketLifecycleWorkflows(bucketName, bucketWorkflows,
                                       workflowUpdates, cb) {
    if (bucketWorkflows.length > 0) {
        putLifecycleConfiguration(bucketName, bucketWorkflows, cb);
    } else {
        deleteLifecycleConfiguration(bucketName, cb);
    }
}

module.exports = {
    applyBucketLifecycleWorkflows,
};

{'fraction_non_alphanumeric': 0.11488388458831808, 'fraction_numerical': 0.0049261083743842365, 'mean_word_length': 2.2393162393162394, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5376135', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1506, 'n_words': 319}