<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var Task = require('../task_agent/task');
var VM  = require('/usr/vm/node_modules/VM');
var async = require('async');
var common = require('../common');
var fs = require('fs');
var imgadm = require('../imgadm');
var kvmdebug = require('./kvmdebug');
var path = require('path');
var spawn = require('child_process').spawn;
var util = require('util');
var zfs = require('zfs').zfs;

var kvm_debugging = false;

var MachineCreateTask = module.exports = function (req) {
    Task.call(this);
    this.req = req;
    this.zpool = req.params.zfs_storage_pool_name || 'zones';
};

Task.createTask(MachineCreateTask);

function start(callback) {
    var self = this;

    VM.logger = common.makeVmadmLogger(self);
    VM.logname = 'machine_create';
    var creationGuardFilename;

    self.pre_check(function (error) {
        if (error) {
            self.fatal(error.message);
            return;
        }

        async.waterfall([
            function (cb) {
                common.provisionInProgressFile(
                    self.req.params.uuid,
                    function (err, filename) {
                        creationGuardFilename = filename;
                        cb();
                        return;
                    });
            },
            self.ensure_dataset_present.bind(self),
            function (found, cb) {
                // The previous step (ensure..) returns a boolean indicating
                // whether the dataset was found. If that flag is set, we'll
                // run this (fetch) step and skip it if not.
                if (!found) {
                    return self.fetch_dataset(cb);
                } else {
                    return cb();
                }
            },
            self.create_machine.bind(self)
        ],
        function (err0) {
            if (kvm_debugging) {
                kvmdebug.stopKVMDebugging(self.log);
                kvm_debugging = false;
            }

            VM.load(self.req.params.uuid, function (err1, machine) {
                fs.unlink(creationGuardFilename, function (err2) {
                    if (err2) {
                        self.log.error(err2.message);
                        return;
                    }

                    if (err0) {
                        if (machine) {
                            self.fatal(err0.message, null, { vm: machine });
                            return;
                        } else {
                            self.fatal(err0.message);
                            return;
                        }
                    } else {
                        if (err1) {
                            self.log.error(err1.message);
                            return;
                        }

                        self.finish({ vm: machine });
                    }
                });

            });
        });
    });
}


function pre_check(callback) {
    var self = this;
    var zoneDataset = path.join(self.zpool, self.req.params.uuid);

    async.waterfall([
        function (cb) {
            // fail if zone with uuid exists
            common.zoneList(self.req.params.uuid, function (error, zones) {
                if (zones[self.req.params.uuid]) {
                    cb(new Error(
                        'Machine ' + self.req.params.uuid + ' exists.'));
                    return;
                }
                cb();
            });
        },<commit_msg>AGENT-846: error was not being returned under some circumstances<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var Task = require('../task_agent/task');
var VM  = require('/usr/vm/node_modules/VM');
var async = require('async');
var common = require('../common');
var fs = require('fs');
var imgadm = require('../imgadm');
var kvmdebug = require('./kvmdebug');
var path = require('path');
var spawn = require('child_process').spawn;
var util = require('util');
var zfs = require('zfs').zfs;

var kvm_debugging = false;

var MachineCreateTask = module.exports = function (req) {
    Task.call(this);
    this.req = req;
    this.zpool = req.params.zfs_storage_pool_name || 'zones';
};

Task.createTask(MachineCreateTask);

function start(callback) {
    var self = this;

    VM.logger = common.makeVmadmLogger(self);
    VM.logname = 'machine_create';
    var creationGuardFilename;

    async.waterfall([
        function (cb) {
            self.pre_check(function (error) {
                if (error) {
                    self.fatal(error.message);
                    return;
                }
                cb();
            });
        },
        function (cb) {
            common.provisionInProgressFile(
                self.req.params.uuid,
                function (err, filename) {
                    creationGuardFilename = filename;
                    cb();
                    return;
                });
        },
        self.ensure_dataset_present.bind(self),
        function (found, cb) {
            // The previous step (ensure..) returns a boolean indicating
            // whether the dataset was found. If that flag is set, we'll
            // run this (fetch) step and skip it if not.
            if (!found) {
                return self.fetch_dataset(cb);
            } else {
                return cb();
            }
        },
        self.create_machine.bind(self),
        function (cb) {
            fs.unlink(creationGuardFilename, function (unlinkError) {
                if (unlinkError) {
                    self.log.error(unlinkError.message);
                }
                cb();
            });
        },
        function (cb) {
            if (kvm_debugging) {
                kvmdebug.stopKVMDebugging(self.log);
                kvm_debugging = false;
            }
            cb();
        }
    ],
    function (waterfallError) {
        VM.load(self.req.params.uuid, function (loadError, machine) {
            if (waterfallError) {
                if (machine) {
                    self.fatal(waterfallError.message, null, { vm: machine });
                    return;
                } else {
                    self.fatal(waterfallError.message);
                    return;
                }
            } else {
                if (loadError) {
                    self.log.error(loadError.message);
                    self.finish();
                    return;
                }

                self.finish({ vm: machine });
            }
        });
    });
}


function pre_check(callback) {
    var self = this;
    var zoneDataset = path.join(self.zpool, self.req.params.uuid);

    async.waterfall([
        function (cb) {
            // fail if zone with uuid exists
            common.zoneList(self.req.params.uuid, function (error, zones) {
                if (zones[self.req.params.uuid]) {
                    cb(new Error(
                        'Machine ' + self.req.params.uuid + ' exists.'));
                    return;
                }
                cb();
            });
        },
{'fraction_non_alphanumeric': 0.11624182570399039, 'fraction_numerical': 0.0038702789269985318, 'mean_word_length': 1.3316739265712507, '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': '4010198', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1949, 'n_words': 469}