#! /usr/bin/env node /** * Gaia autoland task definitions and outputs content to /autolander.json. */ var decorate = require('../lib/decorate_task'); var yaml = require('js-yaml'); var fs = require('fs'); var fsPath = require('path'); var util = require('util'); var template = require('json-templater/object'); // Cache for task definitions... var TASK_DEFINITIONS = {}; function loadTask(name, parameters) { if (!TASK_DEFINITIONS[name]) { var path = fsPath.resolve(__dirname + '/../tasks/' + name + '.yml'); TASK_DEFINITIONS[name] = yaml.safeLoad(fs.readFileSync(path)); } // decorate_task hook. parameters.externalHook = true; return decorate(TASK_DEFINITIONS[name], parameters); } function params() { return Array.prototype.slice.call(arguments).reduce(function(target, props) { for (var key in props) target[key] = props[key]; return target; }, {}); } // List of all the possible definitions... var config = yaml.safeLoad(fs.readFileSync(__dirname + '/../autolander.yml')); // Final task graph definition place holder... var graph = { tasks: [] }; // Expand all tasks based on the `tasks.yml` file... Object.keys(config.definitions).forEach(function(name) { var definition = config.definitions[name]; // If we are not using chunks don't loop. if (!definition || !definition.chunks) { var task = loadTask(name, params(config.params, { // Useful for environment variables. process: process })); graph.tasks.push(task); return; } // Handle all chunking options... for (var chunk = 1; chunk <= definition.chunks; chunk++) { // In practice relying on chunks in the graph is probably a bad idea unless // doing it explicitly with some sort of specific logic but here for // consistency. var task = loadTask(name, params(config.params, { // Allow process to be used in variables useful for process.env. process: process, // Chunk parameters chunk: chunk, totalChunks: definition.chunks, // whether this is a private image privateImage: definition.privateImage, })); graph.tasks.push(task); } }); graph.tasks = graph.tasks.map(function(task) { return template(task); }); // Write the content to autolander.json var jsonPath = fsPath.resolve(__dirname + '/../../../autolander.json'); fs.writeFileSync(jsonPath, JSON.stringify(graph, null, 2));