All files / lib services.js

0% Statements 0/39
0% Branches 0/24
0% Functions 0/8
0% Lines 0/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                                                                                                                                                             
'use strict';
 
// Modules
const _ = require('lodash');
 
/*
 * Helper to map lagoon type data to a lando service
 */
const getLandoServiceType = type => {
  switch (type) {
    case 'elasticsearch': return 'platformsh-elasticsearch';
    case 'influxdb': return 'platformsh-influxdb';
    case 'kafka': return 'platformsh-kafka';
    case 'varnish': return 'platformsh-varnish';
    case 'chrome-headless': return 'platformsh-chrome-headless';
    case 'mariadb': return 'platformsh-mariadb';
    case 'memcached': return 'platformsh-memcached';
    case 'mongodb': return 'platformsh-mongodb';
    case 'mysql': return 'platformsh-mysql';
    case 'php': return 'platformsh-php';
    case 'postgresql': return 'platformsh-postgresql';
    case 'rabbitmq': return 'platformsh-rabbitmq';
    case 'redis': return 'platformsh-redis';
    case 'redis-persistent': return 'platformsh-redis';
    case 'solr': return 'platformsh-solr';
    default: return false;
  }
};
 
/*
 * Helper to return a type and version from platform data
 */
const getPlatformServiceType = ({name = 'app', type} = {}) => ({
  name,
  type: _.first(type.split(':')),
  version: _.last(type.split(':')),
});
 
/*
 * Helper to map into a lando service
 */
const getLandoService = platform => {
  // Start with defaults
  const lando = {
    name: platform.name,
    type: getLandoServiceType(platform.type),
    version: platform.version,
    platformsh: platform,
  };
 
  // If this is an application then we need some more juice
  if (platform.application) {
    // Add some magic to reset the web/app user
    lando.build_as_root_internal = ['/helpers/psh-recreate-users.sh'];
    // We need to reeload our keys because in some situations they cannot be
    // set until now
    lando.build_as_root_internal.push('/helpers/load-keys.sh --silent');
    // Add in the build wrapper
    lando.build_internal = ['/helpers/psh-build.sh'];
    // Generate certs for proxy purposes but dont expose things
    lando.ssl = true;
    lando.sslExpose = false;
    lando.proxyPort = 80;
  }
 
  // Set the varnish port if applicable
  if (lando.type === 'platformsh-varnish') lando.proxyPort = 8080;
 
  // Return
  return lando;
};
 
/*
 * Maps parsed platform config into related Lando things
 */
exports.getLandoServices = (services = [], runConfig = []) => _(services)
    // Break apart platform type into typ and version
    .map(service => _.merge({}, service, getPlatformServiceType(service)))
    // Filter out services that are not supported yet
    .filter(service => getLandoServiceType(service.type) !== false)
    // Merge in other needed lando things
    .map(service => _.merge({}, getLandoService(service), {runConfig: _.find(runConfig, {service: service.name})}))
    // Finally map to an object
    .map(service => ([service.name, service]))
    .fromPairs()
    .value();