<commit_before>/*global require, exports*/
var util = require('util'),
    _ = require('underscore'),
    extendWithGettersAndSetters = require('../util/extendWithGettersAndSetters'),
    Relation = require('./Relation'),
    HtmlRelation = require('./HtmlRelation');

function HtmlScript(config) {
    HtmlRelation.call(this, config);
}

util.inherits(HtmlScript, HtmlRelation);

extendWithGettersAndSetters(HtmlScript.prototype, {
    get href() {
        return this.node.getAttribute('src') || undefined;
    },

    set href(href) {
        this.node.setAttribute('src', href);
        // Clear any inline script
        while (this.node.firstChild) {
            this.node.removeChild(this.node.firstChild);
        }
    },

    inline: function () {
        Relation.prototype.inline.call(this);
        if (this.node.hasAttribute('src')) {
            this.node.removeAttribute('src');
        }
        while (this.node.firstChild) {
            this.node.removeChild(this.node.firstChild);
        }
        this.node.appendChild(this.from.parseTree.createTextNode(this.to.text.replace(/<\/(?=(\s*)script[\/ >])/gi, '<\\/'))); // Safety hack for UglifyJS: https://github.com/mishoo/UglifyJS/issues/164
        this.from.markDirty();
        return this;
    },

    attach: function (asset, position, adjacentRelation) {
        this.node = asset.parseTree.createElement('script');
        if (position === 'first') {
            var existingScriptNodes = asset.parseTree.getElementsByTagName('script');
            if (existingScriptNodes.length > 0) {
                existingScriptNodes[0].parentNode.insertBefore(this.node, existingScriptNodes[0]);
            } else if (asset.parseTree.body) {
                asset.parseTree.body.insertBefore(this.node, asset.parseTree.body.firstChild);
            } else {
                asset.parseTree.head.appendChild(this.node);
            }
        } else {
            this.attachNodeBeforeOrAfter(position, adjacentRelation);
        }
        return HtmlRelation.prototype.attach.call(this, asset, position, adjacentRelation);
    }
});

module.exports = HtmlScript;
<commit_msg>HtmlScript<commit_after>var util = require('util'),
    extendWithGettersAndSetters = require('../util/extendWithGettersAndSetters'),
    Relation = require('./Relation'),
    HtmlRelation = require('./HtmlRelation');

function HtmlScript(config) {
    HtmlRelation.call(this, config);
}

util.inherits(HtmlScript, HtmlRelation);

extendWithGettersAndSetters(HtmlScript.prototype, {
    get href() {
        return this.node.getAttribute('src') || undefined;
    },

    set href(href) {
        this.node.setAttribute('src', href);
        // Clear any inline script
        while (this.node.firstChild) {
            this.node.removeChild(this.node.firstChild);
        }
    },

    inline: function () {
        Relation.prototype.inline.call(this);
        if (this.node.hasAttribute('src')) {
            this.node.removeAttribute('src');
        }
        while (this.node.firstChild) {
            this.node.removeChild(this.node.firstChild);
        }
        this.node.appendChild(this.from.parseTree.createTextNode(this.to.text.replace(/<\/(?=(\s*)script[\/ >])/gi, '<\\/'))); // Safety hack for UglifyJS: https://github.com/mishoo/UglifyJS/issues/164
        this.from.markDirty();
        return this;
    },

    attach: function (asset, position, adjacentRelation) {
        this.node = asset.parseTree.createElement('script');
        if (position === 'first') {
            var existingScriptNodes = asset.parseTree.getElementsByTagName('script');
            if (existingScriptNodes.length > 0) {
                existingScriptNodes[0].parentNode.insertBefore(this.node, existingScriptNodes[0]);
            } else if (asset.parseTree.body) {
                asset.parseTree.body.insertBefore(this.node, asset.parseTree.body.firstChild);
            } else {
                asset.parseTree.head.appendChild(this.node);
            }
        } else {
            this.attachNodeBeforeOrAfter(position, adjacentRelation);
        }
        return HtmlRelation.prototype.attach.call(this, asset, position, adjacentRelation);
    }
});

module.exports = HtmlScript;
<|endoftext|>


<commit_before>import { getAsset,
         getAssets,
         updateteAssets,
         uploadAsset,
         removeAsset,
         editAsset } from './asset';
import _ from 'lodash';
import Promise from 'promise';

export function setQueueAsset(_owner, _content, _path, _task) {
    const _item = {};
    _item.path = _path;
    _item.task = _task;
    _item.content = _content;
    _owner.queueList.push(_item);
    queueStart(_owner, _path);
};

function queueStart(_owner, _path) {
  if (!_owner.inWork && _owner.queueList[0]) {
    recursionQueue(_owner).then(function() {
      _owner.inWork = false;
    })
  }
}

function assetSort (_owner, _path) {
  var _taskIndex = _.findLastIndex(_owner.queueList, function(o) { return o.path == _path; });
  if (_taskIndex > -1) {
    for (var i = 0; i < _owner.queueList.length; i++) {
      if (_owner.queueList[i] && _owner.queueList[i].path === _path && i != _taskIndex) {
        _owner.queueList.splice(i, 1);
      }
    }
  }
}

function recursionQueue(_owner) {
  return new Promise(function (resolve, reject) {
    if (_owner.queueList[0]){
      _owner.inWork = true;
      _iterationQueue(_owner);
    }else{
      _owner.inWork = false;
      resolve();
    }

    function _iterationQueue(_owner) {
      if (!_owner.queueList[0]) {
        _owner.inWork = false;
        resolve();
      }

      if (_owner.queueList[0]) {
        var _queue = _owner.queueList[0];
        updateteAssets(_owner).then(function () {

          if (!_queue.task.unlink) {
            if (_owner.assets[_queue.path]) {
              editAsset (_owner, _queue.content.asset, _owner.assets[_queue.path].id, _queue.path).then(function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              })
            }else{
              uploadAsset (_owner, _queue.content.upload, _queue.path).then(function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              })
            }
          }

          if (_queue.task.unlink && _owner.assets[_queue.path]) {
            removeAsset(_owner, _owner.assets[_queue.path].id, _queue.path, _queue.task.name).then(function() {
              _owner.queueList.splice(0, 1)
              if (_owner.queueList[0]) {
                return _iterationQueue(_owner);
              }else{
                resolve();
              }
            })
          }else{
            if (_queue.task.unlink) {
              _owner.queueList.splice(0, 1)
              if (_owner.queueList[0]) {
                return _iterationQueue(_owner);
              }else{
                resolve();
              }
            }
          }
        })
      }else{
        resolve();
      }
}
});
}
<commit_msg>Update assetManager.js<commit_after>import { getAsset,
         getAssets,
         updateteAssets,
         uploadAsset,
         removeAsset,
         editAsset } from './asset';
import _ from 'lodash';
import Promise from 'promise';

export function setQueueAsset(_owner, _content, _path, _task) {
    const _item = {};
    _item.path = _path;
    _item.task = _task;
    _item.content = _content;
    _owner.queueList.push(_item);
    queueStart(_owner, _path);
};

function queueStart(_owner, _path) {
  if (!_owner.inWork && _owner.queueList[0]) {
    recursionQueue(_owner).then(function() {
      _owner.inWork = false;
    },
    function() {
      _owner.inWork = false;
    })
  }
}

function assetSort (_owner, _path) {
  var _taskIndex = _.findLastIndex(_owner.queueList, function(o) { return o.path == _path; });
  if (_taskIndex > -1) {
    for (var i = 0; i < _owner.queueList.length; i++) {
      if (_owner.queueList[i] && _owner.queueList[i].path === _path && i != _taskIndex) {
        _owner.queueList.splice(i, 1);
      }
    }
  }
}

function recursionQueue(_owner) {
  return new Promise(function (resolve, reject) {
    if (_owner.queueList[0]){
      _owner.inWork = true;
      _iterationQueue(_owner);
    }else{
      _owner.inWork = false;
      resolve();
    }

    function _iterationQueue(_owner) {
      if (!_owner.queueList[0]) {
        _owner.inWork = false;
        resolve();
      }

      if (_owner.queueList[0]) {
        var _queue = _owner.queueList[0];
        updateteAssets(_owner).then(function () {

          if (!_queue.task.unlink) {
            if (_owner.assets[_queue.path]) {
              editAsset (_owner, _queue.content.asset, _owner.assets[_queue.path].id, _queue.path).then(function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              },
              function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              })
            }else{
              uploadAsset (_owner, _queue.content.upload, _queue.path).then(function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              },
              function() {
                _owner.queueList.splice(0, 1)
                if (_owner.queueList[0]) {
                  return _iterationQueue(_owner);
                }else{
                  resolve();
                }
              })
            }
          }

          if (_queue.task.unlink && _owner.assets[_queue.path]) {
            removeAsset(_owner, _owner.assets[_queue.path].id, _queue.path, _queue.task.name).then(function() {
              _owner.queueList.splice(0, 1)
              if (_owner.queueList[0]) {
                return _iterationQueue(_owner);
              }else{
                resolve();
              }
            },
            function() {
              _owner.queueList.splice(0, 1)
              if (_owner.queueList[0]) {
                return _iterationQueue(_owner);
              }else{
                resolve();
              }
            })
          }else{
            if (_queue.task.unlink) {
              _owner.queueList.splice(0, 1)
              if (_owner.queueList[0]) {
                return _iterationQueue(_owner);
              }else{
                resolve();
              }
            }
          }
        })
      }else{
        resolve();
      }
}
});
}
<|endoftext|>


<commit_before>
var ddb = require('../../config/dynamo_database').ddb;
var ratingModel = require('../model/ratingModel');

exports.update = function(req,res){
	var ratings = ratingModel.newRating(req);

	ddb.putItem('blindRatings', ratings, {}, function (err) {
		if (!err) {
			return ddb.scan('blindRatings',{filter : { username: { eq: req._passport.session.user }}}, function (err, blindRatings) {
				if (!err) {
					return res.send(blindRatings);
				} else {
					return res.send(200,"Informação salva, mas erro ao contar quantos votos já foram feitos");
				}
			});
		} else {
			res.send(400,err);
			return console.log(err);
		}
	});
}<commit_msg>Retornando a quantidade de votos já realizados<commit_after>
var ddb = require('../../config/dynamo_database').ddb;
var ratingModel = require('../model/ratingModel');

exports.update = function(req,res){
	var ratings = ratingModel.newRating(req);

	ddb.putItem('blindRatings', ratings, {}, function (err) {
		if (!err) {
			return ddb.scan('blindRatings',{filter : { username: { eq: req._passport.session.user }}}, function (err, blindRatings) {
				if (!err) {
					return res.send(blindRatings.count);
				} else {
					return res.send(200,"Informação salva, mas erro ao contar quantos votos já foram feitos");
				}
			});
		} else {
			res.send(400,err);
			return console.log(err);
		}
	});
}<|endoftext|>


<commit_before>'use strict';

var _ = require('underscore');
var ops = require('./ops');
var values = require('./values');
var errors = require('../errors');
var JuttleMoment = require('../runtime/types/juttle-moment');

function makeDate(time) {
    if (time instanceof Date) { return time; }

    var number = Number(time);
    if (number === number) { // check for NaN
        time = number * 1000; // convert seconds to milliseconds for Date constructor
    }

    return new Date(time);
}

function toNative(records, epsilon) {
    _.each(records, function(record) {
        // check if we already called toNative on these points
        // that may happen if we have multiple live programs on the same points
        if (record.time !== undefined && !(record.time instanceof JuttleMoment)) {
            var date = makeDate(record.time);
            if (date.getTime() === date.getTime()) { // speedy check for invalid date, to make sure its getTime() isn't NaN
                record.time = new JuttleMoment({rawDate: date});
            } else {
                record.time = new JuttleMoment(record.time);
            }

            if (epsilon) {
                record.time.epsilon = epsilon;
            }
        }
    });

    return records;
}

// Assigns the time from timeField to 'time' and attempts to convert it
// into JuttleMoment
//
// If not specified, timeField defaults to 'time'. If specified and point
// is missing that field, emits a warning using the supplied context.
function parseTime(points, timeField, context) {
    _.each(points, function(pt) {
        if (timeField && !pt[timeField]) {
            context.trigger('warning', new errors.runtimeError('RT-POINT-MISSING-TIME', {field: timeField}));
        }

        var time = pt[timeField || 'time'];
        if (time) { pt.time = time; }
    });

    toNative(points);

    return points;
}

function filterPoints(points, from, to) {
    return _.filter(points, function(point) {
        if (point.time && point.time.moment) {
            return point.time.gte(from) && point.time.lt(to);
        } else {
            return true;
        }
    });
}

function fromNative(records) {
    return _.map(records, values.toJSONCompatible, values);
}

function isInteger(x) {
    return (_.isFinite(x)) && (x % 1 === 0);
}

function pointSortFunc(field, direction) {
    // given a field name, return a function that can be passed to
    // Array.sort to order points by the field.
    // if optional direction === 'desc', sort in reverse order.
    return function(first, second) {
        var a = first[field];
        var b = second[field];
        var fieldCompare;

        if (a === undefined && b === undefined) {
            fieldCompare = 0;
        } else if (a === undefined && b !== undefined) {
            fieldCompare = 1;
        } else if (a !== undefined && b === undefined) {
            fieldCompare = -1;
        } else if (_.isString(a) && _.isString(b)) {
            fieldCompare = a.toLowerCase().localeCompare(b.toLowerCase());
        } else if (a instanceof JuttleMoment) {
            fieldCompare = JuttleMoment.subtract(a, b).milliseconds();
        } else if (values.isArray(a)) {
            fieldCompare = ops.gt(a, b) ? 1 : -1;
        } else {
            fieldCompare = a - b;
        }
        if (direction === 'desc') {
            fieldCompare *= -1;
        }
        return fieldCompare;
    };
}

module.exports = {
    isInteger: isInteger,
    toNative: toNative,
    fromNative: fromNative,
    filterPoints: filterPoints,
    parseTime: parseTime,
    pointSortFunc: pointSortFunc
};
<commit_msg>just remove the field to begin with<commit_after>'use strict';

var _ = require('underscore');
var ops = require('./ops');
var values = require('./values');
var errors = require('../errors');
var JuttleMoment = require('../runtime/types/juttle-moment');

function makeDate(time) {
    if (time instanceof Date) { return time; }

    var number = Number(time);
    if (number === number) { // check for NaN
        time = number * 1000; // convert seconds to milliseconds for Date constructor
    }

    return new Date(time);
}

function toNative(records, epsilon) {
    _.each(records, function(record) {
        // check if we already called toNative on these points
        // that may happen if we have multiple live programs on the same points
        if (record.time !== undefined && !(record.time instanceof JuttleMoment)) {
            var date = makeDate(record.time);
            if (date.getTime() === date.getTime()) { // speedy check for invalid date, to make sure its getTime() isn't NaN
                record.time = new JuttleMoment({rawDate: date});
            } else {
                record.time = new JuttleMoment(record.time);
            }

            if (epsilon) {
                record.time.epsilon = epsilon;
            }
        }
    });

    return records;
}

// Assigns the time from timeField to 'time' and attempts to convert it
// into JuttleMoment
//
// If not specified, timeField defaults to 'time'. If specified and point
// is missing that field, emits a warning using the supplied context.
function parseTime(points, timeField, context) {
    _.each(points, function(pt) {
        if (timeField && !pt[timeField]) {
            context.trigger('warning', new errors.runtimeError('RT-POINT-MISSING-TIME', {field: timeField}));
        }

        var time = pt[timeField || 'time'];
        if (time) {
            pt.time = time;
            if (timeField !== 'time') {
                delete pt[timeField];
            }
        }
    });

    toNative(points);

    return points;
}

function filterPoints(points, from, to) {
    return _.filter(points, function(point) {
        if (point.time && point.time.moment) {
            return point.time.gte(from) && point.time.lt(to);
        } else {
            return true;
        }
    });
}

function fromNative(records) {
    return _.map(records, values.toJSONCompatible, values);
}

function isInteger(x) {
    return (_.isFinite(x)) && (x % 1 === 0);
}

function pointSortFunc(field, direction) {
    // given a field name, return a function that can be passed to
    // Array.sort to order points by the field.
    // if optional direction === 'desc', sort in reverse order.
    return function(first, second) {
        var a = first[field];
        var b = second[field];
        var fieldCompare;

        if (a === undefined && b === undefined) {
            fieldCompare = 0;
        } else if (a === undefined && b !== undefined) {
            fieldCompare = 1;
        } else if (a !== undefined && b === undefined) {
            fieldCompare = -1;
        } else if (_.isString(a) && _.isString(b)) {
            fieldCompare = a.toLowerCase().localeCompare(b.toLowerCase());
        } else if (a instanceof JuttleMoment) {
            fieldCompare = JuttleMoment.subtract(a, b).milliseconds();
        } else if (values.isArray(a)) {
            fieldCompare = ops.gt(a, b) ? 1 : -1;
        } else {
            fieldCompare = a - b;
        }
        if (direction === 'desc') {
            fieldCompare *= -1;
        }
        return fieldCompare;
    };
}

module.exports = {
    isInteger: isInteger,
    toNative: toNative,
    fromNative: fromNative,
    filterPoints: filterPoints,
    parseTime: parseTime,
    pointSortFunc: pointSortFunc
};
<|endoftext|>


<commit_before>(function() {
    var domStatus = document.getElementById("status");
    var domSectPkgs = document.getElementById("sectPkgs");
    var domListPkgs = document.getElementById("listPkgs");
    var domSectTypes = document.getElementById("sectTypes");
    var domListTypes = document.getElementById("listTypes");

    var curNav = {
        // each element is a package name, e.g. @import("a") then within there @import("b")
        // starting implicitly from root package
        pkgNames: [],
        // same as above except actual packages, not names
        pkgObjs: [],
        // Each element is a decl name, `a.b.c`, a is 0, b is 1, c is 2, etc.
        // empty array means refers to the package itself
        declNames: [],
        // these will be all types, except the last one may be a type or a decl
        declObjs: [],
    };

    var rootIsStd = detectRootIsStd();
    var typeKindTypeId = findTypeKindType();
    var typeTypeId = findTypeTypeId();
    window.addEventListener('hashchange', onHashChange, false);
    onHashChange();

    function render() {
        domStatus.classList.add("hidden");

        var pkg = zigAnalysis.packages[zigAnalysis.rootPkg];
        curNav.pkgObjs = [pkg];
        for (var i = 0; i < curNav.pkgNames.length; i += 1) {
            var childPkg = zigAnalysis.packages[pkg.table[curNav.pkgNames[i]]];
            if (childPkg == null) {
                return render404();
            }
            pkg = childPkg;
            curNav.pkgObjs.push(pkg);
        }

        var decl = zigAnalysis.types[pkg.main];
        curNav.declObjs = [decl];
        for (var i = 0; i < curNav.declNames.length; i += 1) {
            var childDecl = findSubDecl(decl, curNav.declNames[i]);
            if (childDecl == null) {
                return render404();
            }
            var container = getDeclContainerType(childDecl);
            if (container == null) {
                if (i + 1 === curNav.declNames.length) {
                    curNav.declObjs.push(childDecl);
                    break;
                } else {
                    return render404();
                }
            }
            decl = container;
            curNav.declObjs.push(decl);
        }

        var lastPkg = curNav.pkgObjs[curNav.pkgObjs.length - 1];
        renderPkgList(lastPkg);

        var lastDecl = curNav.declObjs[curNav.declObjs.length - 1];
        if (lastDecl.decls != null) {
            return renderContainer(lastDecl);
        } else {
            throw new Error("docs for this decl which is not a container");
        }
    }

    function render404() {
        domStatus.textContent = "404 Not Found";
        domStatus.classList.remove("hidden");
        domSectPkgs.classList.add("hidden");
        domListPkgs.classList.add("hidden");
    }

    function renderPkgList(pkg) {
        var list = [];
        for (var key in pkg.table) {
            if (key === "root" && rootIsStd) continue;
            list.push({
                name: key,
                pkg: pkg.table[key],
            });
        }
        list.sort(function(a, b) {
            return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
        });

        if (list.length === 0) {
            domSectPkgs.classList.remove("hidden");
        } else {
            resizeDomList(domListPkgs, list.length, '

');
            for (var i = 0; i < list.length; i += 1) {
                var liDom = domListPkgs.children[i];
                var aDom = liDom.children[0];
                aDom.textContent = list[i].name;
                aDom.setAttribute('href', "#" + list[i].name);
            }

            domSectPkgs.classList.remove("hidden");
        }
    }

    function resizeDomList(listDom, desiredLen, templateHtml) {
        // add the missing dom entries
        var i, ev;
        for (i = listDom.childElementCount; i < desiredLen; i += 1) {
            listDom.insertAdjacentHTML('beforeend', templateHtml);
        }
        // remove extra dom entries
        while (desiredLen < listDom.childElementCount) {
            listDom.removeChild(listDom.lastChild);
        }
    }

    function renderContainer(container) {
        // Find only the types of this package
        var list = [];
        for (var i = 0; i < container.decls.length; i += 1) {
            var decl = zigAnalysis.decls[container.decls[i]];
            if (decl.type == typeTypeId) {
                list.push(decl);
            }
        }
        list.sort(function(a, b) {
            return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
        });

        resizeDomList(domListTypes, list.length, '

');
        for (var i = 0; i < list.length; i += 1) {
            var domItem = domListTypes.children[i];
            var decl = list[i];
            domItem.textContent = decl.name;
        }

        domSectTypes.classList.remove("hidden");
    }

    function operatorCompare(a, b) {
        if (a === b) {
            return 0;
        } else if (a < b) {
            return -1;
        } else {
            return 1;
        }
    }

    function detectRootIsStd() {
        var rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
        if (rootPkg.table["std"] == null) {
            // no std mapped into the root package
            return false;
        }
        var stdPkg = zigAnalysis.packages[rootPkg.table["std"]];
        return rootPkg.file === stdPkg.file;
    }

    function findTypeKindType() {
        for (var i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
            if (zigAnalysis.typeKinds[i] === "Type") {
                return i;
            }
        }
        throw new Error("No type kind 'Type' found");
    }

    function findTypeTypeId() {
        for (var i = 0; i < zigAnalysis.types.length; i += 1) {
            if (zigAnalysis.types[i].kind == typeKindTypeId) {
                return i;
            }
        }
        throw new Error("No type 'type' found");
    }

    function onHashChange() {
        curNav = {
            pkgNames: [],
            pkgObjs: [],
            declNames: [],
            declObjs: [],
        };
        if (location.hash[0] === '#') {
            var parts = location.hash.substring(1).split(";");
            curNav.pkgNames = parts[0].split(".");
            if (parts[1] != null) {
                curNav.declNames = parts[1] ? parts[1].split(".") : [];
            }
        }
        render();
    }

    function findSubDecl(parentType, childName) {
        if (parentType.decls == null) throw new Error("parent object has no decls");
        for (var i = 0; i < parentType.decls.length; i += 1) {
            var declIndex = parentType.decls[i];
            var childDecl = zigAnalysis.decls[declIndex];
            if (childDecl.name === childName) {
                return childDecl;
            }
        }
        return null;
    }

    function getDeclContainerType(decl) {
        if (decl.type === typeTypeId) {
            return zigAnalysis.types[decl.value];
        }
        return null;
    }
})();
<commit_msg>generated docs: navigable types<commit_after>(function() {
    var domStatus = document.getElementById("status");
    var domSectPkgs = document.getElementById("sectPkgs");
    var domListPkgs = document.getElementById("listPkgs");
    var domSectTypes = document.getElementById("sectTypes");
    var domListTypes = document.getElementById("listTypes");

    var curNav = {
        // each element is a package name, e.g. @import("a") then within there @import("b")
        // starting implicitly from root package
        pkgNames: [],
        // same as above except actual packages, not names
        pkgObjs: [],
        // Each element is a decl name, `a.b.c`, a is 0, b is 1, c is 2, etc.
        // empty array means refers to the package itself
        declNames: [],
        // these will be all types, except the last one may be a type or a decl
        declObjs: [],
    };

    var rootIsStd = detectRootIsStd();
    var typeKindTypeId = findTypeKindType();
    var typeTypeId = findTypeTypeId();
    window.addEventListener('hashchange', onHashChange, false);
    onHashChange();

    function render() {
        domStatus.classList.add("hidden");

        var pkg = zigAnalysis.packages[zigAnalysis.rootPkg];
        curNav.pkgObjs = [pkg];
        for (var i = 0; i < curNav.pkgNames.length; i += 1) {
            var childPkg = zigAnalysis.packages[pkg.table[curNav.pkgNames[i]]];
            if (childPkg == null) {
                return render404();
            }
            pkg = childPkg;
            curNav.pkgObjs.push(pkg);
        }

        var decl = zigAnalysis.types[pkg.main];
        curNav.declObjs = [decl];
        for (var i = 0; i < curNav.declNames.length; i += 1) {
            var childDecl = findSubDecl(decl, curNav.declNames[i]);
            if (childDecl == null) {
                return render404();
            }
            var container = getDeclContainerType(childDecl);
            if (container == null) {
                if (i + 1 === curNav.declNames.length) {
                    curNav.declObjs.push(childDecl);
                    break;
                } else {
                    return render404();
                }
            }
            decl = container;
            curNav.declObjs.push(decl);
        }

        var lastPkg = curNav.pkgObjs[curNav.pkgObjs.length - 1];
        renderPkgList(lastPkg);

        var lastDecl = curNav.declObjs[curNav.declObjs.length - 1];
        if (lastDecl.decls != null) {
            return renderContainer(lastDecl);
        } else {
            throw new Error("docs for this decl which is not a container");
        }
    }

    function render404() {
        domStatus.textContent = "404 Not Found";
        domStatus.classList.remove("hidden");
        domSectPkgs.classList.add("hidden");
        domSectTypes.classList.add("hidden");
    }

    function renderPkgList(pkg) {
        var list = [];
        for (var key in pkg.table) {
            if (key === "root" && rootIsStd) continue;
            list.push({
                name: key,
                pkg: pkg.table[key],
            });
        }
        list.sort(function(a, b) {
            return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
        });

        if (list.length === 0) {
            domSectPkgs.classList.add("hidden");
        } else {
            resizeDomList(domListPkgs, list.length, '

');
            for (var i = 0; i < list.length; i += 1) {
                var liDom = domListPkgs.children[i];
                var aDom = liDom.children[0];
                aDom.textContent = list[i].name;
                aDom.setAttribute('href', navLinkPkg(list[i].name));
            }

            domSectPkgs.classList.remove("hidden");
        }
    }

    function navLinkPkg(childName) {
        return '#' + (curNav.pkgNames.concat([childName])).join(',');
    }

    function navLinkDecl(childName) {
        return '#' + curNav.pkgNames.join(",") + ';' + (curNav.declNames.concat([childName])).join(",");
    }

    function resizeDomList(listDom, desiredLen, templateHtml) {
        // add the missing dom entries
        var i, ev;
        for (i = listDom.childElementCount; i < desiredLen; i += 1) {
            listDom.insertAdjacentHTML('beforeend', templateHtml);
        }
        // remove extra dom entries
        while (desiredLen < listDom.childElementCount) {
            listDom.removeChild(listDom.lastChild);
        }
    }

    function renderContainer(container) {
        // Find only the types of this package
        var list = [];
        for (var i = 0; i < container.decls.length; i += 1) {
            var decl = zigAnalysis.decls[container.decls[i]];
            if (decl.type == typeTypeId) {
                list.push(decl);
            }
        }
        list.sort(function(a, b) {
            return operatorCompare(a.name.toLowerCase(), b.name.toLowerCase());
        });

        resizeDomList(domListTypes, list.length, '

');
        for (var i = 0; i < list.length; i += 1) {
            var liDom = domListTypes.children[i];
            var aDom = liDom.children[0];
            var decl = list[i];
            aDom.textContent = decl.name;
            aDom.setAttribute('href', navLinkDecl(decl.name));
        }

        domSectTypes.classList.remove("hidden");
    }

    function operatorCompare(a, b) {
        if (a === b) {
            return 0;
        } else if (a < b) {
            return -1;
        } else {
            return 1;
        }
    }

    function detectRootIsStd() {
        var rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
        if (rootPkg.table["std"] == null) {
            // no std mapped into the root package
            return false;
        }
        var stdPkg = zigAnalysis.packages[rootPkg.table["std"]];
        return rootPkg.file === stdPkg.file;
    }

    function findTypeKindType() {
        for (var i = 0; i < zigAnalysis.typeKinds.length; i += 1) {
            if (zigAnalysis.typeKinds[i] === "Type") {
                return i;
            }
        }
        throw new Error("No type kind 'Type' found");
    }

    function findTypeTypeId() {
        for (var i = 0; i < zigAnalysis.types.length; i += 1) {
            if (zigAnalysis.types[i].kind == typeKindTypeId) {
                return i;
            }
        }
        throw new Error("No type 'type' found");
    }

    function onHashChange() {
        curNav = {
            pkgNames: [],
            pkgObjs: [],
            declNames: [],
            declObjs: [],
        };
        if (location.hash[0] === '#') {
            var parts = location.hash.substring(1).split(";");
            curNav.pkgNames = parts[0].split(".");
            if (parts[1] != null) {
                curNav.declNames = parts[1] ? parts[1].split(".") : [];
            }
        }
        render();
    }

    function findSubDecl(parentType, childName) {
        if (parentType.decls == null) throw new Error("parent object has no decls");
        for (var i = 0; i < parentType.decls.length; i += 1) {
            var declIndex = parentType.decls[i];
            var childDecl = zigAnalysis.decls[declIndex];
            if (childDecl.name === childName) {
                return childDecl;
            }
        }
        return null;
    }

    function getDeclContainerType(decl) {
        if (decl.type === typeTypeId) {
            return zigAnalysis.types[decl.value];
        }
        return null;
    }
})();
<|endoftext|>


<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) 2015, 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 restify = require('restify');
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.add_firewall_rules.bind(self),
        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();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the dataset
            // exists.
            zfs.list(
            zoneDataset, { type: 'all' }, function (error, fields, list) {
                if (list && list.length) {
                    cb(new Error('Dataset ' + zoneDataset + ' exists.'));
                    return;
                }
                cb();
            });
        }
    ],
    function (error) {
        if (error) {
            callback(error);
            return;
        }
        callback();
    });
}

function ensure_dataset_present(callback) {
    var self = this;

    var fullDataset;
    var params = self.req.params;

    // TODO Enable provisioner to be able to check a list of image_uuids and
    // fetch any that are not installed
    self.toImport = null;

    if (params.image_uuid) {
        self.toImport = params.image_uuid;
    } else if (self.req.params.disks && self.req.params.disks.length) {
        self.toImport = self.req.params.disks[0].image_uuid;
    }

    fullDataset = this.zpool + '/' + self.toImport;

    self.log.info(
        'Checking whether zone template dataset '
        + fullDataset + ' exists on the system.');

    zfs.list(
        fullDataset,
        { type: 'all' },
        function (error, fields, list) {
            if (!error && list.length) {
                self.log.info('Dataset ' + fullDataset + ' exists.');
                callback(null, true);
                return;
            } else if (error && error.toString().match(/does not exist/)) {
                self.log.info('Dataset template didn\'t appear to exist.');
                callback(null, false);
                return;
            }
        });
}

function fetch_dataset(callback) {
    var self = this;

    var options = {
        uuid: self.toImport,
        zpool: self.zpool,
        log: self.log
    };
    imgadm.importImage(options, function (err) {
        if (err) {
            self.log.error(err);
            callback(err);
            return;
        }
        callback();
    });
}

function add_firewall_rules(callback) {
    var self = this;

    if (!self.req.params.firewall_rules) {
        self.log.info('No firewall_rules property in params: not adding');
        callback();
        return;
    }

    if (self.req.params.firewall_rules.length === 0) {
        self.log.info('No firewall rules to add in params');
        callback();
        return;
    }

    var fwClient = restify.createJsonClient({
        url: 'http://localhost:2021',
        headers: {
            'x-request-id': self.req.req_id
        }
    });

    self.log.debug('Adding firewall rules');

    async.each(self.req.params.firewall_rules, function (rule, cb) {
        var putOpts = {
            path: '/rules/' + rule.uuid
        };

        fwClient.put(putOpts, rule, function (err, res) {
            if (err) {
                self.log.error({ err: err, rule: rule },
                    'Error adding firewall rule');
            } else {
                self.log.info('Added firewall rule');
            }

            cb(err);
            return;
        });
    }, function (doneErr) {
        fwClient.close();

        self.log.debug('Added firewall rules');
        callback(doneErr);
        return;
    });
}

function normalizeError(error) {
    if (error instanceof String || typeof (error === 'string')) {
        return new Error(error);
    }
    return error;
}

function create_machine(callback) {
    var self = this;
    var req = self.req;

    if (req.params.brand === 'kvm') {
        kvmdebug.startKVMDebugging(self.log, function (cmd, args, output) {
            var cmdline = cmd + ' ' + args.join(' ');
            self.log.debug({cmdline: cmdline, output: output});
        });
        kvm_debugging = true;
    }

    VM.create(req.params, function (error, info) {
        if (error) {
            var msg = error instanceof Error ? error.message : error;
            return callback(new Error('VM.create error: ' + msg));
        }
        return callback();
    });
}

MachineCreateTask.setStart(start);

MachineCreateTask.createSteps({
    pre_check: {
        fn: pre_check,
        progress: 20,
        description: 'Pre-flight sanity check'
    },
    ensure_dataset_present: {
        fn: ensure_dataset_present,
        progress: 30,
        description: 'Checking for zone template dataset'
    },
    fetch_dataset: {
        fn: fetch_dataset,
        progress: 50,
        description: 'Fetching zone template dataset'
    },
    add_firewall_rules: {
        fn: add_firewall_rules,
        progress: 60,
        description: 'Adding firewall rules'
    },
    create_machine: {
        fn: create_machine,
        progress: 100,
        description: 'Creating machine'
    }
});
<commit_msg>AGENT-896: add_firewall_rules should use admin ip<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) 2015, 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 restify = require('restify');
var smartdcconfig = require('../smartdc-config');
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) {
            smartdcconfig.sysinfo(function (err, sysinfo) {
                if (err) {
                    cb(err);
                    return;
                }

                self.adminip = firstAdminIp(sysinfo);
                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.add_firewall_rules.bind(self),
        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();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the dataset
            // exists.
            zfs.list(
            zoneDataset, { type: 'all' }, function (error, fields, list) {
                if (list && list.length) {
                    cb(new Error('Dataset ' + zoneDataset + ' exists.'));
                    return;
                }
                cb();
            });
        }
    ],
    function (error) {
        if (error) {
            callback(error);
            return;
        }
        callback();
    });
}

function ensure_dataset_present(callback) {
    var self = this;

    var fullDataset;
    var params = self.req.params;

    // TODO Enable provisioner to be able to check a list of image_uuids and
    // fetch any that are not installed
    self.toImport = null;

    if (params.image_uuid) {
        self.toImport = params.image_uuid;
    } else if (self.req.params.disks && self.req.params.disks.length) {
        self.toImport = self.req.params.disks[0].image_uuid;
    }

    fullDataset = this.zpool + '/' + self.toImport;

    self.log.info(
        'Checking whether zone template dataset '
        + fullDataset + ' exists on the system.');

    zfs.list(
        fullDataset,
        { type: 'all' },
        function (error, fields, list) {
            if (!error && list.length) {
                self.log.info('Dataset ' + fullDataset + ' exists.');
                callback(null, true);
                return;
            } else if (error && error.toString().match(/does not exist/)) {
                self.log.info('Dataset template didn\'t appear to exist.');
                callback(null, false);
                return;
            }
        });
}

function fetch_dataset(callback) {
    var self = this;

    var options = {
        uuid: self.toImport,
        zpool: self.zpool,
        log: self.log
    };
    imgadm.importImage(options, function (err) {
        if (err) {
            self.log.error(err);
            callback(err);
            return;
        }
        callback();
    });
}

function add_firewall_rules(callback) {
    var self = this;

    if (!self.req.params.firewall_rules) {
        self.log.info('No firewall_rules property in params: not adding');
        callback();
        return;
    }

    if (self.req.params.firewall_rules.length === 0) {
        self.log.info('No firewall rules to add in params');
        callback();
        return;
    }

    var fwClient = restify.createJsonClient({
        url: 'http://' + self.adminip + ':2021',
        headers: {
            'x-request-id': self.req.req_id
        }
    });

    self.log.debug('Adding firewall rules');

    async.each(self.req.params.firewall_rules, function (rule, cb) {
        var putOpts = {
            path: '/rules/' + rule.uuid
        };

        fwClient.put(putOpts, rule, function (err, res) {
            if (err) {
                self.log.error({ err: err, rule: rule },
                    'Error adding firewall rule');
            } else {
                self.log.info('Added firewall rule');
            }

            cb(err);
            return;
        });
    }, function (doneErr) {
        fwClient.close();

        self.log.debug('Added firewall rules');
        callback(doneErr);
        return;
    });
}

function normalizeError(error) {
    if (error instanceof String || typeof (error === 'string')) {
        return new Error(error);
    }
    return error;
}

function create_machine(callback) {
    var self = this;
    var req = self.req;

    if (req.params.brand === 'kvm') {
        kvmdebug.startKVMDebugging(self.log, function (cmd, args, output) {
            var cmdline = cmd + ' ' + args.join(' ');
            self.log.debug({cmdline: cmdline, output: output});
        });
        kvm_debugging = true;
    }

    VM.create(req.params, function (error, info) {
        if (error) {
            var msg = error instanceof Error ? error.message : error;
            return callback(new Error('VM.create error: ' + msg));
        }
        return callback();
    });
}

function firstAdminIp(sysinfo) {
    var interfaces;

    interfaces = sysinfo['Network Interfaces'];

    for (var iface in interfaces) {
        if (!interfaces.hasOwnProperty(iface)) {
            continue;
        }

        var nic = interfaces[iface]['NIC Names'];
        var isAdmin = nic.indexOf('admin') !== -1;
        if (isAdmin) {
            var ip = interfaces[iface].ip4addr;
            return ip;
        }
    }

    throw new Error('No NICs with name "admin" detected.');
}

MachineCreateTask.setStart(start);

MachineCreateTask.createSteps({
    pre_check: {
        fn: pre_check,
        progress: 20,
        description: 'Pre-flight sanity check'
    },
    ensure_dataset_present: {
        fn: ensure_dataset_present,
        progress: 30,
        description: 'Checking for zone template dataset'
    },
    fetch_dataset: {
        fn: fetch_dataset,
        progress: 50,
        description: 'Fetching zone template dataset'
    },
    add_firewall_rules: {
        fn: add_firewall_rules,
        progress: 60,
        description: 'Adding firewall rules'
    },
    create_machine: {
        fn: create_machine,
        progress: 100,
        description: 'Creating machine'
    }
});
<|endoftext|>


<commit_before>var Task = require('task_agent/lib/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 path = require('path');
var spawn = require('child_process').spawn;
var system = require('/usr/node/node_modules/system');
var util = require('util');
var zfs = require('zfs').zfs;

var MachineCreateTask = module.exports = function (req) {
    Task.call(this);
    req.params.zpool_path = path.join('/', this.zpool);
    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.req.params.image_uuid
        = self.req.params.image_uuid || self.req.params.dataset_uuid;

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

        async.waterfall([
            function (cb) {
                common.createMachineInProgressFile(
                    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 (err) {
            fs.unlink(creationGuardFilename, function () {
                if (err) {
                    self.fatal(err.message);
                    return;
                }
                self.finish();
            });
        });
    });
}

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

    dataset = self.req.params.image_uuid;

    var zoneSnapshot
    = path.join(self.zpool, dataset) + '@' + self.req.params.uuid;

    async.waterfall([
        function (cb) {
            // Ensure we have enough memory if machine is a VM
            if (self.req.params.brand !== 'kvm') {
                cb();
                return;
            }

            system.getProvisionableMemory(
                function (error, provisionableMemory) {
                    if (error) {
                        self.fatal(
                            'Error getting system provisionable memory: '
                            + error.message);
                        return;
                    }

                    self.log.info('Requesting vm with ' + self.req.params.ram
                        + ' MiB of ram, compute node has '
                        + provisionableMemory
                        + ' MiB available for provisioning.');

                    if (self.req.params.ram > provisionableMemory) {
                        self.fatal(
                        'Requested machine ram exceeds provisionable memory');
                        return;
                    }
                    cb();
            });
        },
        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();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the dataset
            // exists.
            zfs.list(
            zoneDataset, { type: 'all' }, function (error, fields, list) {
                if (list && list.length) {
                    cb(new Error('Dataset ' + zoneDataset + ' exists.'));
                    return;
                }
                cb();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the snapshot for
            // the zone template exists.
            zfs.list(zoneSnapshot, { type: 'all' },  function (error) {
                if (!error) {
                    cb(new Error('Snapshot ' + zoneSnapshot + ' exists.'));
                    return;
                }
                cb();
            });
        }
    ],
    function (error) {
        if (error) {
            callback(error);
            return;
        }
        callback();
    });
}

function ensure_dataset_present(callback) {
    var dataset;
    var fullDataset;
    var self = this;
    var params = self.req.params;

    dataset = params.image_uuid;
    if (!dataset) {
        callback(new Error('payload is missing image_uuid'));
        return;
    }
    fullDataset = this.zpool + '/' + dataset;

    self.log.info(
        'Checking whether zone template dataset '
        + fullDataset + ' exists on the system.');

    zfs.list(
        fullDataset,
        { type: 'all' },
        function (error, fields, list) {
            if (!error && list.length) {
                self.log.info('Dataset ' + fullDataset + ' exists.');
                callback(null, true);
                return;
            } else if (error && error.toString().match(/does not exist/)) {
                self.log.info('Dataset template didn\'t appear to exist.');
                callback(null, false);
                return;
            }
        });
}

function fetch_dataset(callback) {
    var self = this;
    var options = {
        uuid: self.req.params.image_uuid,
        zpool: self.zpool,
        log: self.log
    };
    imgadm.importImage(options, function (err) {
        if (err) {
            self.log.error(err);
            callback(err);
            return;
        }
        callback();
    });
}

function normalizeError(error) {
    if (error instanceof String || typeof (error === 'string')) {
        return new Error(error);
    }
    return error;
}

function create_machine(callback) {
    var self = this;
    var req = self.req;

    VM.create(req.params, function (error, info) {
        if (error) {
            var msg = error instanceof Error ? error.message : error;
            return callback(new Error('VM.create error: ' + msg));
        }
        return callback();
    });
}

MachineCreateTask.setStart(start);

MachineCreateTask.createSteps({
    pre_check: {
        fn: pre_check,
        progress: 20,
        description: 'Pre-flight sanity check'
    },
    ensure_dataset_present: {
        fn: ensure_dataset_present,
        progress: 30,
        description: 'Checking for zone template dataset'
    },
    fetch_dataset: {
        fn: fetch_dataset,
        progress: 50,
        description: 'Fetching zone template dataset'
    },
    create_machine: {
        fn: create_machine,
        progress: 100,
        description: 'Creating machine'
    }
});
<commit_msg>AGENT-572: Do not abort if image_uuid is unspecified<commit_after>var Task = require('task_agent/lib/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 path = require('path');
var spawn = require('child_process').spawn;
var system = require('/usr/node/node_modules/system');
var util = require('util');
var zfs = require('zfs').zfs;

var MachineCreateTask = module.exports = function (req) {
    Task.call(this);
    req.params.zpool_path = path.join('/', this.zpool);
    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.createMachineInProgressFile(
                    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 (err) {
            fs.unlink(creationGuardFilename, function () {
                if (err) {
                    self.fatal(err.message);
                    return;
                }
                self.finish();
            });
        });
    });
}

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

    dataset = self.req.params.image_uuid;

    var zoneSnapshot
    = path.join(self.zpool, dataset) + '@' + self.req.params.uuid;

    async.waterfall([
        function (cb) {
            // Ensure we have enough memory if machine is a VM
            if (self.req.params.brand !== 'kvm') {
                cb();
                return;
            }

            system.getProvisionableMemory(
                function (error, provisionableMemory) {
                    if (error) {
                        self.fatal(
                            'Error getting system provisionable memory: '
                            + error.message);
                        return;
                    }

                    self.log.info('Requesting vm with ' + self.req.params.ram
                        + ' MiB of ram, compute node has '
                        + provisionableMemory
                        + ' MiB available for provisioning.');

                    if (self.req.params.ram > provisionableMemory) {
                        self.fatal(
                        'Requested machine ram exceeds provisionable memory');
                        return;
                    }
                    cb();
            });
        },
        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();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the dataset
            // exists.
            zfs.list(
            zoneDataset, { type: 'all' }, function (error, fields, list) {
                if (list && list.length) {
                    cb(new Error('Dataset ' + zoneDataset + ' exists.'));
                    return;
                }
                cb();
            });
        },
        function (cb) {
            // If we don't get an error on this `list` it means the snapshot for
            // the zone template exists.
            zfs.list(zoneSnapshot, { type: 'all' },  function (error) {
                if (!error) {
                    cb(new Error('Snapshot ' + zoneSnapshot + ' exists.'));
                    return;
                }
                cb();
            });
        }
    ],
    function (error) {
        if (error) {
            callback(error);
            return;
        }
        callback();
    });
}

function ensure_dataset_present(callback) {
    var self = this;

    var fullDataset;
    var params = self.req.params;

    // TODO Enable provisioner to be able to check a list of image_uuids and
    // fetch any that are not installed
    self.toImport = null;

    if (params.image_uuid) {
        self.toImport = params.image_uuid;
    } else if (self.req.params.disks && self.req.params.disks.length) {
        self.toImport = self.req.params.disks[0].image_uuid;
    }

    fullDataset = this.zpool + '/' + self.toImport;

    self.log.info(
        'Checking whether zone template dataset '
        + fullDataset + ' exists on the system.');

    zfs.list(
        fullDataset,
        { type: 'all' },
        function (error, fields, list) {
            if (!error && list.length) {
                self.log.info('Dataset ' + fullDataset + ' exists.');
                callback(null, true);
                return;
            } else if (error && error.toString().match(/does not exist/)) {
                self.log.info('Dataset template didn\'t appear to exist.');
                callback(null, false);
                return;
            }
        });
}

function fetch_dataset(callback) {
    var self = this;

    var options = {
        uuid: self.toImport,
        zpool: self.zpool,
        log: self.log
    };
    imgadm.importImage(options, function (err) {
        if (err) {
            self.log.error(err);
            callback(err);
            return;
        }
        callback();
    });
}

function normalizeError(error) {
    if (error instanceof String || typeof (error === 'string')) {
        return new Error(error);
    }
    return error;
}

function create_machine(callback) {
    var self = this;
    var req = self.req;

    VM.create(req.params, function (error, info) {
        if (error) {
            var msg = error instanceof Error ? error.message : error;
            return callback(new Error('VM.create error: ' + msg));
        }
        return callback();
    });
}

MachineCreateTask.setStart(start);

MachineCreateTask.createSteps({
    pre_check: {
        fn: pre_check,
        progress: 20,
        description: 'Pre-flight sanity check'
    },
    ensure_dataset_present: {
        fn: ensure_dataset_present,
        progress: 30,
        description: 'Checking for zone template dataset'
    },
    fetch_dataset: {
        fn: fetch_dataset,
        progress: 50,
        description: 'Fetching zone template dataset'
    },
    create_machine: {
        fn: create_machine,
        progress: 100,
        description: 'Creating machine'
    }
});
<|endoftext|>


<commit_before>var chalk = require('chalk'),
    _ = require('lodash'),
    Path = require('path'),
    urlTools = require('urltools'),
    AssetGraph = require('../index');

function indentSubsequentLines(str, level) {
    return str.replace(/\n/g, '\n' + new Array(level + 1).join(' '));
}

function escapeRegExpMetaChars(str) {
    return str.replace(/[$\.^\(\)\[\]\{\}]/g, '\\$&');
}

module.exports = function (options) {
    var startReplRegExp;

    options = options || {};

    if (options.repl) {
        startReplRegExp = new RegExp(_.flatten(_.flatten([options.repl]).map(function (transformName) {
            return transformName.split(',');
        })).map(function (transformName) {
            return transformName.replace(/[\.\+\{\}\[\]\(\)\?\^\$]/g, '\\$&');
        }).join('|'));
    }

    return function logEvents(assetGraph) {
        var assetGraphRootRelativeToCwd = Path.relative(process.cwd(), urlTools.fileUrlToFsPath(assetGraph.root)),
            assetGraphRootRelativeToCwdRegExp = new RegExp('\\b' + escapeRegExpMetaChars(assetGraphRootRelativeToCwd) + '/', 'g'),
            cwdRegExp = new RegExp('(?:file://)?' + escapeRegExpMetaChars(process.cwd() + '/'), 'g'),
            colorBySeverity = {info: 'cyan', warn: 'yellow', error: 'red'},
            symbolBySeverity = {info: 'ℹ', warn: '⚠', error: '✘'};

        function outputMessage(messageOrError, severity) {
            severity = severity || 'info';
            var message;
            if (Object.prototype.toString.call(messageOrError) === '[object Error]') {
                if (severity === 'error') {
                    message = messageOrError.stack;
                } else {
                    message = messageOrError.message || messageOrError.name || messageOrError.code;
                }
                if (messageOrError.asset) {
                    message = messageOrError.asset.urlOrDescription + ': ' + message;
                }
            } else {
                if (typeof messageOrError === 'string') {
                    message = messageOrError;
                } else if (typeof messageOrError.message === 'string') {
                    message = messageOrError.message;
                } else {
                    // Give up guessing. This is probably an error on the next lines...
                    message = messageOrError;
                }
            }
            var caption = ' ' + symbolBySeverity[severity] + ' ' + severity.toUpperCase() + ': ';

            message = message
                .replace(cwdRegExp, '')
                .replace(assetGraphRootRelativeToCwdRegExp, chalk.gray(assetGraphRootRelativeToCwd + '/'));

            console[severity](chalk[colorBySeverity[severity]](caption) + indentSubsequentLines(message, caption.length));
        }

        var firstWarningSeenDuringTransform = null,
            transformRunning = false;
        assetGraph
            .on('beforeTransform', function () {
                firstWarningSeenDuringTransform = null;
                transformRunning = true;
            })
            .on('afterTransform', function (transform, elapsedTime) {
                if (firstWarningSeenDuringTransform && options.stopOnWarning) {
                    console.error(chalk.red(' ✘ ERROR: ') + (transform.name ? transform.name + ': ' : '') + 'A warning was encountered while stopOnWarning is on, exiting with a non-zero exit code');
                    process.exit(1);
                }
                transformRunning = false;

                if (options.afterTransform !== false) {
                    console.log(chalk.green(' ✔ ') + (elapsedTime / 1000).toFixed(3) + ' secs: ' + transform.name);
                }
                if (startReplRegExp && startReplRegExp.test(transform.name)) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                }
            })
            .on('info', function (info) {
                outputMessage(info, 'info');
            })
            .on('warn', function (err) {
                // These are way too noisy
                if (options.suppressJavaScriptCommonJsRequireWarnings && err.relationType === 'JavaScriptCommonJsRequire') {
                    return;
                }
                outputMessage(err, 'warn');
                if (startReplRegExp && startReplRegExp.test('warn')) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                }
                if (transformRunning) {
                    firstWarningSeenDuringTransform = firstWarningSeenDuringTransform || err;
                } else {
                    console.error(chalk.red(' ✘ ERROR: ') + 'A warning was encountered while stopOnWarning is on, exiting with a non-zero exit code');
                    process.exit(1);
                }
            })
            .on('error', function (err) {
                outputMessage(err, 'error');
                if (startReplRegExp && startReplRegExp.test('error')) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                } else {
                    process.exit(1);
                }
            });
    };
};
<commit_msg>logEvents: Unbreak the active transform housekeeping when a transform calls other transforms<commit_after>var chalk = require('chalk'),
    _ = require('lodash'),
    Path = require('path'),
    urlTools = require('urltools'),
    AssetGraph = require('../index');

function indentSubsequentLines(str, level) {
    return str.replace(/\n/g, '\n' + new Array(level + 1).join(' '));
}

function escapeRegExpMetaChars(str) {
    return str.replace(/[$\.^\(\)\[\]\{\}]/g, '\\$&');
}

module.exports = function (options) {
    var startReplRegExp;

    options = options || {};

    if (options.repl) {
        startReplRegExp = new RegExp(_.flatten(_.flatten([options.repl]).map(function (transformName) {
            return transformName.split(',');
        })).map(function (transformName) {
            return transformName.replace(/[\.\+\{\}\[\]\(\)\?\^\$]/g, '\\$&');
        }).join('|'));
    }

    return function logEvents(assetGraph) {
        var assetGraphRootRelativeToCwd = Path.relative(process.cwd(), urlTools.fileUrlToFsPath(assetGraph.root)),
            assetGraphRootRelativeToCwdRegExp = new RegExp('\\b' + escapeRegExpMetaChars(assetGraphRootRelativeToCwd) + '/', 'g'),
            cwdRegExp = new RegExp('(?:file://)?' + escapeRegExpMetaChars(process.cwd() + '/'), 'g'),
            colorBySeverity = {info: 'cyan', warn: 'yellow', error: 'red'},
            symbolBySeverity = {info: 'ℹ', warn: '⚠', error: '✘'};

        function outputMessage(messageOrError, severity) {
            severity = severity || 'info';
            var message;
            if (Object.prototype.toString.call(messageOrError) === '[object Error]') {
                if (severity === 'error') {
                    message = messageOrError.stack;
                } else {
                    message = messageOrError.message || messageOrError.name || messageOrError.code;
                }
                if (messageOrError.asset) {
                    message = messageOrError.asset.urlOrDescription + ': ' + message;
                }
            } else {
                if (typeof messageOrError === 'string') {
                    message = messageOrError;
                } else if (typeof messageOrError.message === 'string') {
                    message = messageOrError.message;
                } else {
                    // Give up guessing. This is probably an error on the next lines...
                    message = messageOrError;
                }
            }
            var caption = ' ' + symbolBySeverity[severity] + ' ' + severity.toUpperCase() + ': ';

            message = message
                .replace(cwdRegExp, '')
                .replace(assetGraphRootRelativeToCwdRegExp, chalk.gray(assetGraphRootRelativeToCwd + '/'));

            console[severity](chalk[colorBySeverity[severity]](caption) + indentSubsequentLines(message, caption.length));
        }

        var firstWarningSeenDuringTransform = null,
            transformStack = [logEvents];
        assetGraph
            .on('beforeTransform', function (transform) {
                firstWarningSeenDuringTransform = null;
                transformStack.push(transform);
            })
            .on('afterTransform', function (transform, elapsedTime) {
                if (firstWarningSeenDuringTransform && options.stopOnWarning) {
                    console.error(chalk.red(' ✘ ERROR: ') + (transform.name ? transform.name + ': ' : '') + 'A warning was encountered while stopOnWarning is on, exiting with a non-zero exit code');
                    process.exit(1);
                }
                if (transform !== transformStack.pop()) {
                    throw new Error('logEvents: Internal error in the transformStack housekeeping');
                }

                if (options.afterTransform !== false) {
                    console.log(chalk.green(' ✔ ') + (elapsedTime / 1000).toFixed(3) + ' secs: ' + transform.name);
                }
                if (startReplRegExp && startReplRegExp.test(transform.name)) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                }
            })
            .on('info', function (info) {
                outputMessage(info, 'info');
            })
            .on('warn', function (err) {
                // These are way too noisy
                if (options.suppressJavaScriptCommonJsRequireWarnings && err.relationType === 'JavaScriptCommonJsRequire') {
                    return;
                }
                outputMessage(err, 'warn');
                if (startReplRegExp && startReplRegExp.test('warn')) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                }
                if (transformStack.length > 0) {
                    firstWarningSeenDuringTransform = firstWarningSeenDuringTransform || err;
                } else {
                    console.error(chalk.red(' ✘ ERROR: ') + 'A warning was encountered while stopOnWarning is on, exiting with a non-zero exit code');
                    process.exit(1);
                }
            })
            .on('error', function (err) {
                outputMessage(err, 'error');
                if (startReplRegExp && startReplRegExp.test('error')) {
                    this.transformQueue.transforms.unshift(AssetGraph.transforms.startRepl());
                } else {
                    process.exit(1);
                }
            });
    };
};
<|endoftext|>


<commit_before>import { logger } from '@storybook/client-logger';
import { fetch } from 'global';
import semver from 'semver';

import { version as currentVersion } from '../../package.json';

const checkInterval = 24 * 60 * 60 * 1000;

const versionsUrl = 'https://storybook.js.org/versions.json';
async function fetchLatestVersion() {
  const fromFetch = await fetch(`${versionsUrl}?current=${currentVersion}`);
  return fromFetch.json();
}

export default function({ store }) {
  const {
    versions: persistedVersions = {},
    lastVersionCheck,
    dismissedVersionNotification,
  } = store.getState();

  // Check to see if we have info about the current version persisted
  const persistedCurrentVersion = Object.values(persistedVersions).find(
    v => v.version === currentVersion
  );
  const state = {
    versions: {
      ...persistedVersions,
      current: {
        version: currentVersion,
        ...(persistedCurrentVersion && { info: persistedCurrentVersion.info }),
      },
    },
    lastVersionCheck,
    dismissedVersionNotification,
  };

  const api = {
    getCurrentVersion: () => {
      const {
        versions: { current },
      } = store.getState();
      return current;
    },
    getLatestVersion: () => {
      const {
        versions: { latest, next, current },
      } = store.getState();
      if (current && semver.prerelease(current.version) && next) {
        return latest && semver.gt(latest.version, next.version) ? latest : next;
      }
      return latest;
    },
    versionUpdateAvailable: () => {
      const latest = api.getLatestVersion();
      const current = api.getCurrentVersion();

      return latest && semver.gt(latest.version, current.version);
    },
  };

  // Grab versions from the server/local storage right away
  async function init({ api: { versionUpdateAvailable, getLatestVersion, addNotification } }) {
    const { versions = {} } = store.getState();

    const now = Date.now();
    if (!lastVersionCheck || now - lastVersionCheck > checkInterval) {
      try {
        const { latest, next } = await fetchLatestVersion(currentVersion);

        await store.setState(
          { versions: { ...versions, latest, next }, lastVersionCheck: now },
          { persistence: 'permanent' }
        );
      } catch (error) {
        logger.warn(`Failed to fetch latest version from server: ${error}`);
      }
    }

    if (versionUpdateAvailable()) {
      const latestVersion = getLatestVersion().version;

      if (latestVersion !== dismissedVersionNotification) {
        addNotification({
          id: 'update',
          link: '/settings/about',
          content: `🎉 Storybook ${latestVersion} is available`,
          onClear() {
            store.setState(
              { dismissedVersionNotification: latestVersion },
              { persistence: 'permanent' }
            );
          },
        });
      }
    }
  }

  return { init, state, api };
}
<commit_msg>A little more enthusiasm in the update notification phrasing<commit_after>import { logger } from '@storybook/client-logger';
import { fetch } from 'global';
import semver from 'semver';

import { version as currentVersion } from '../../package.json';

const checkInterval = 24 * 60 * 60 * 1000;

const versionsUrl = 'https://storybook.js.org/versions.json';
async function fetchLatestVersion() {
  const fromFetch = await fetch(`${versionsUrl}?current=${currentVersion}`);
  return fromFetch.json();
}

export default function({ store }) {
  const {
    versions: persistedVersions = {},
    lastVersionCheck,
    dismissedVersionNotification,
  } = store.getState();

  // Check to see if we have info about the current version persisted
  const persistedCurrentVersion = Object.values(persistedVersions).find(
    v => v.version === currentVersion
  );
  const state = {
    versions: {
      ...persistedVersions,
      current: {
        version: currentVersion,
        ...(persistedCurrentVersion && { info: persistedCurrentVersion.info }),
      },
    },
    lastVersionCheck,
    dismissedVersionNotification,
  };

  const api = {
    getCurrentVersion: () => {
      const {
        versions: { current },
      } = store.getState();
      return current;
    },
    getLatestVersion: () => {
      const {
        versions: { latest, next, current },
      } = store.getState();
      if (current && semver.prerelease(current.version) && next) {
        return latest && semver.gt(latest.version, next.version) ? latest : next;
      }
      return latest;
    },
    versionUpdateAvailable: () => {
      const latest = api.getLatestVersion();
      const current = api.getCurrentVersion();

      return latest && semver.gt(latest.version, current.version);
    },
  };

  // Grab versions from the server/local storage right away
  async function init({ api: { versionUpdateAvailable, getLatestVersion, addNotification } }) {
    const { versions = {} } = store.getState();

    const now = Date.now();
    if (!lastVersionCheck || now - lastVersionCheck > checkInterval) {
      try {
        const { latest, next } = await fetchLatestVersion(currentVersion);

        await store.setState(
          { versions: { ...versions, latest, next }, lastVersionCheck: now },
          { persistence: 'permanent' }
        );
      } catch (error) {
        logger.warn(`Failed to fetch latest version from server: ${error}`);
      }
    }

    if (versionUpdateAvailable()) {
      const latestVersion = getLatestVersion().version;

      if (latestVersion !== dismissedVersionNotification) {
        addNotification({
          id: 'update',
          link: '/settings/about',
          content: `🎉 Storybook ${latestVersion} is available!`,
          onClear() {
            store.setState(
              { dismissedVersionNotification: latestVersion },
              { persistence: 'permanent' }
            );
          },
        });
      }
    }
  }

  return { init, state, api };
}
<|endoftext|>