<commit_before>/*
 * grunt-ssh-deploy
 * https://github.com/dcarlson/grunt-ssh-deploy
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var getScpOptions = function(options) {
    var scpOptions = {
        port: options.port,
        host: options.host,
        username: options.username
    };

    if (options.privateKey) {
        scpOptions.privateKey = options.privateKey;
    }
    else if (options.password) {
        scpOptions.password = options.password;
    }
    else if (options.agent) {
        scpOptions.agent = options.agent;
    } else {
        throw new Error('Agent, Password or private key required for secure copy.');
    }

    return scpOptions;
};

module.exports = function(grunt) {

    grunt.registerTask('ssh_deploy', 'Begin Deployment', function() {
        var done = this.async();
        var Connection = require('ssh2');
        var client = require('scp2');
        var moment = require('moment');
        var timestamp = moment().format('YYYYMMDDHHmmssSSS');
        var async = require('async');
        var extend = require('extend');

        var defaults = {
            current_symlink: 'current',
            port: 22,
            zip_deploy: false,
            max_buffer: 200 * 1024
        };

        var options = extend({}, defaults, grunt.config.get('environments').options,
            grunt.config.get('environments')[this.args]['options']);

        // scp defaults
        client.defaults(getScpOptions(options));

        var c = new Connection();
        c.on('connect', function() {
            grunt.log.subhead('Connecting :: ' + options.host);
        });
        c.on('ready', function() {
            grunt.log.subhead('Connected :: ' + options.host);
            // execution of tasks
            execCommands(options,c);
        });
        c.on('error', function(err) {
            grunt.log.subhead("Error :: " + options.host);
            grunt.log.errorlns(err);
            if (err) {throw err;}
        });
        c.on('close', function(had_error) {
            grunt.log.subhead("Closed :: " + options.host);

            return true;
        });
        c.connect(options);

        var execCommands = function(options, connection){
            var childProcessExec = require('child_process').exec;

            var execLocal = function(cmd, next) {
            	var execOptions = {
            		maxBuffer: options.max_buffer	
            	};
            	
                childProcessExec(cmd, execOptions, function(err, stdout, stderr){
                    grunt.log.debug(cmd);
                    grunt.log.debug('stdout: ' + stdout);
                    grunt.log.debug('stderr: ' + stderr);
                    if (err !== null) {
                        grunt.log.errorlns('exec error: ' + err);
                        grunt.log.subhead('Error deploying. Closing connection.');

                        deleteRelease(closeConnection);
                    } else {
                        next();
                    }
                });
            };

            // executes a remote command via ssh
            var execRemote = function(cmd, showLog, next){
                connection.exec(cmd, function(err, stream) {
                    if (err) {
                        grunt.log.errorlns(err);
                        grunt.log.subhead('ERROR DEPLOYING. CLOSING CONNECTION AND DELETING RELEASE.');

                        deleteRelease(closeConnection);
                    }
                    stream.on('data', function(data, extended) {
                        grunt.log.debug((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ') + data);
                    });
                    stream.on('end', function() {
                        grunt.log.debug('REMOTE: ' + cmd);
                        if(!err) {
                            next();
                        }
                    });
                });
            };

            var zipForDeploy = function(callback) {
                if (!options.zip_deploy) return callback();
                var command = "tar -czvf ./deploy.tgz --directory=" + options.local_path + " . --exclude=deploy.tgz";
                grunt.log.subhead('--------------- ZIPPING FOLDER');
                grunt.log.subhead('--- ' + command);
                execLocal(command, callback);
            };

            var onBeforeDeploy = function(callback){
                if (typeof options.before_deploy === "undefined") return callback();
                var command = options.before_deploy;
                grunt.log.subhead("--------------- RUNNING PRE-DEPLOY COMMANDS");
                if (command instanceof Array) {
                    async.eachSeries(command, function(command, callback) {
                        grunt.log.subhead('--- ' + command);
                        execRemote(command, options.debug, callback);
                    }, callback);
                } else {
                    grunt.log.subhead('--- ' + command);
                    execRemote(command, options.debug, callback);
                }
            };

            var createReleases = function(callback) {
                var command = 'cd ' + options.deploy_path + ' && mkdir -p releases/' + timestamp;
                grunt.log.subhead('--------------- CREATING NEW RELEASE');<commit_msg>Fix for error message "file changed as we read it"<commit_after>/*
 * grunt-ssh-deploy
 * https://github.com/dcarlson/grunt-ssh-deploy
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var getScpOptions = function(options) {
    var scpOptions = {
        port: options.port,
        host: options.host,
        username: options.username
    };

    if (options.privateKey) {
        scpOptions.privateKey = options.privateKey;
    }
    else if (options.password) {
        scpOptions.password = <PASSWORD>;
    }
    else if (options.agent) {
        scpOptions.agent = options.agent;
    } else {
        throw new Error('Agent, Password or private key required for secure copy.');
    }

    return scpOptions;
};

module.exports = function(grunt) {

    grunt.registerTask('ssh_deploy', 'Begin Deployment', function() {
        var done = this.async();
        var Connection = require('ssh2');
        var client = require('scp2');
        var moment = require('moment');
        var timestamp = moment().format('YYYYMMDDHHmmssSSS');
        var async = require('async');
        var extend = require('extend');

        var defaults = {
            current_symlink: 'current',
            port: 22,
            zip_deploy: false,
            max_buffer: 200 * 1024
        };

        var options = extend({}, defaults, grunt.config.get('environments').options,
            grunt.config.get('environments')[this.args]['options']);

        // scp defaults
        client.defaults(getScpOptions(options));

        var c = new Connection();
        c.on('connect', function() {
            grunt.log.subhead('Connecting :: ' + options.host);
        });
        c.on('ready', function() {
            grunt.log.subhead('Connected :: ' + options.host);
            // execution of tasks
            execCommands(options,c);
        });
        c.on('error', function(err) {
            grunt.log.subhead("Error :: " + options.host);
            grunt.log.errorlns(err);
            if (err) {throw err;}
        });
        c.on('close', function(had_error) {
            grunt.log.subhead("Closed :: " + options.host);

            return true;
        });
        c.connect(options);

        var execCommands = function(options, connection){
            var childProcessExec = require('child_process').exec;

            var execLocal = function(cmd, next) {
            	var execOptions = {
            		maxBuffer: options.max_buffer	
            	};
            	
                childProcessExec(cmd, execOptions, function(err, stdout, stderr){
                    grunt.log.debug(cmd);
                    grunt.log.debug('stdout: ' + stdout);
                    grunt.log.debug('stderr: ' + stderr);
                    if (err !== null) {
                        grunt.log.errorlns('exec error: ' + err);
                        grunt.log.subhead('Error deploying. Closing connection.');

                        deleteRelease(closeConnection);
                    } else {
                        next();
                    }
                });
            };

            // executes a remote command via ssh
            var execRemote = function(cmd, showLog, next){
                connection.exec(cmd, function(err, stream) {
                    if (err) {
                        grunt.log.errorlns(err);
                        grunt.log.subhead('ERROR DEPLOYING. CLOSING CONNECTION AND DELETING RELEASE.');

                        deleteRelease(closeConnection);
                    }
                    stream.on('data', function(data, extended) {
                        grunt.log.debug((extended === 'stderr' ? 'STDERR: ' : 'STDOUT: ') + data);
                    });
                    stream.on('end', function() {
                        grunt.log.debug('REMOTE: ' + cmd);
                        if(!err) {
                            next();
                        }
                    });
                });
            };

            var zipForDeploy = function(callback) {
                if (!options.zip_deploy) return callback();
                var command = "tar -czvf ./deploy.tgz --ignore-failed-read --directory=" + options.local_path + " . --exclude=deploy.tgz";
                grunt.log.subhead('--------------- ZIPPING FOLDER');
                grunt.log.subhead('--- ' + command);
                execLocal(command, callback);
            };

            var onBeforeDeploy = function(callback){
                if (typeof options.before_deploy === "undefined") return callback();
                var command = options.before_deploy;
                grunt.log.subhead("--------------- RUNNING PRE-DEPLOY COMMANDS");
                if (command instanceof Array) {
                    async.eachSeries(command, function(command, callback) {
                        grunt.log.subhead('--- ' + command);
                        execRemote(command, options.debug, callback);
                    }, callback);
                } else {
                    grunt.log.subhead('--- ' + command);
                    execRemote(command, options.debug, callback);
                }
            };

            var createReleases = function(callback) {
                var command = 'cd ' + options.deploy_path + ' && mkdir -p releases/' + timestamp;
                grunt.log.subhead('--------------- CREATING NEW RELEASE');
{'fraction_non_alphanumeric': 0.13217536689578302, 'fraction_numerical': 0.0027865502507895224, 'mean_word_length': 1.4548563611491108, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '1314438', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2707, 'n_words': 571}