<commit_before>/*
 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
 *
 * Functions for interacting with fwadm
 */

var fw = require('fw');



// --- Internal



function logPayload(opts, action) {
    var log = opts.log;
    var payload = opts.payload;
    var oldVMs;

    if (payload.hasOwnProperty('vms')) {
        oldVMs = payload.vms;
        delete payload.vms;
    }
    delete opts.log;

    log.debug(opts, 'fw: %s', action);

    if (oldVMs) {
        payload.vms = oldVMs;
    }

    opts.log = log;

    if (opts.req_id && !payload.req_id) {
        payload.req_id = opts.req_id;
    }
}



// --- Exports



/**
 * Add firewall data
 */
function add(opts, callback) {
    opts.payload.createdBy = 'fwapi';

    logPayload(opts, 'add');
    fw.add(opts.payload, callback);
}


/**
 * Delete firewall data
 */
function del(opts, callback) {
    logPayload(opts, 'del');
    fw.del(opts.payload, callback);
}


/**
 * Get remote VM
 */
function getRVM(opts, callback) {
    logPayload(opts, 'getRVM');
    fw.getRVM(opts.payload, callback);
}


/**
 * List rules
 */
function list(opts, callback) {
    logPayload(opts, 'list');
    fw.list(opts.payload, callback);
}


/**
 * Get rules that apply to a remote VM
 */
function rvmRules(opts, callback) {
    logPayload(opts, 'rvmRules');
    fw.rvmRules(opts.payload, callback);
}


/**
 * Update firewall data
 */
function update(opts, callback) {
    opts.payload.createdBy = 'fwapi';
    // Allow "updating" rules that don't exist:
    opts.payload.allowAdds = true;

    logPayload(opts, 'update');
    fw.update(opts.payload, callback);
}


/**
 * Return remote (non-local) targets for a set of rules
 */
function remoteTargets(opts, callback) {
    logPayload(opts, 'remoteTargets');
    fw.remoteTargets(opts.payload, callback);
}


/**
 * Get VMs that are affected by a rule
 */
function vms(opts, callback) {
    logPayload(opts, 'vms');
    fw.vms(opts.payload, callback);
}



module.exports = {
    _setOldIPF: fw._setOldIPF,
    add: add,
    del: del,
    getRVM: getRVM,
    rvmRules: rvmRules,
    list: list,
    update: update,
    remoteTargets: remoteTargets,
    vms: vms,
    VM_FIELDS: fw.VM_FIELDS
};
<commit_msg>OS-2790: vmadm dies on EMFILE<commit_after>/*
 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
 *
 * Functions for interacting with fwadm
 */

var fw = require('fw');
var fwLog = require('fw/lib/util/log');



// --- Internal



function logStart(opts, action, readOnly) {
    var log = opts.log;
    var payload = opts.payload;
    var oldVMs;

    if (payload.hasOwnProperty('vms')) {
        oldVMs = payload.vms;
        delete payload.vms;
    }
    delete opts.log;
    delete payload.log;

    log.debug(opts, 'fw: %s', action);

    if (oldVMs) {
        payload.vms = oldVMs;
    }

    opts.log = log;

    if (opts.req_id && !payload.req_id) {
        payload.req_id = opts.req_id;
    }

    payload.log = fwLog.create({ action: action }, readOnly);
}


function run(opts, fn, readOnly, callback) {
    logStart(opts, fn, readOnly);

    fw[fn](opts.payload, function (err, res) {
        opts.log.debug({ result: res }, 'fw: %s: finish', fn);

        var loggingToFile = false;
        var streams = opts.payload.log.streams;
        streams.forEach(function (str) {
            if (str.type === 'file') {
                loggingToFile = true;
            }
        });

        if (!loggingToFile) {
            // Not logging to a file, so no need to flush the log
            return callback(err, res);
        }

        fwLog.flush(opts.payload.log, function () {
            opts.payload.log.streams.forEach(function (s) {
                s.stream.end();
            });

            return callback(err, res);
        });
    });
}



// --- Exports



/**
 * Add firewall data
 */
function add(opts, callback) {
    opts.payload.createdBy = 'fwapi';
    run(opts, 'add', false, callback);
}


/**
 * Delete firewall data
 */
function del(opts, callback) {
    run(opts, 'del', false, callback);
}


/**
 * Get rule
 */
function get(opts, callback) {
    run(opts, 'get', true, callback);
}


/**
 * Get remote VM
 */
function getRVM(opts, callback) {
    run(opts, 'getRVM', true, callback);
}


/**
 * List rules
 */
function list(opts, callback) {
    run(opts, 'list', true, callback);
}


/**
 * Get rules that apply to a remote VM
 */
function rvmRules(opts, callback) {
    run(opts, 'rvmRules', true, callback);
}


/**
 * Update firewall data
 */
function update(opts, callback) {
    opts.payload.createdBy = 'fwapi';
    // Allow "updating" rules that don't exist:
    opts.payload.allowAdds = true;
    run(opts, 'update', false, callback);
}


/**
 * Return remote (non-local) targets for a set of rules
 */
function remoteTargets(opts, callback) {
    run(opts, 'remoteTargets', false, callback);
}


/**
 * Get VMs that are affected by a rule
 */
function vms(opts, callback) {
    run(opts, 'vms', true, callback);
}



module.exports = {
    _setOldIPF: fw._setOldIPF,
    add: add,
    del: del,
    get: get,
    getRVM: getRVM,
    rvmRules: rvmRules,
    list: list,
    update: update,
    remoteTargets: remoteTargets,
    vms: vms,
    VM_FIELDS: fw.VM_FIELDS
};

{'fraction_non_alphanumeric': 0.14901359892740854, 'fraction_numerical': 0.0022984102662325224, 'mean_word_length': 2.530764029749831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2577063', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1769, 'n_words': 445}