All files / lib run.js

0% Statements 0/51
0% Branches 0/11
0% Functions 0/18
0% Lines 0/43

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
'use strict';
 
// Modules
const _ = require('lodash');
const overrides = require('./overrides');
const path = require('path');
const utils = require('./utils');
 
// Constants
const applicationConfigDefaults = {
  app_dir: '/app',
  hooks: {
    _deploy: null,
    build: null,
    post_deploy: null,
  },
  timezone: null,
  disk: 2048,
  web: {
    move_to_root: false,
  },
  is_production: false,
  access: {},
  preflight: {
    enabled: true,
    ignored_rules: [],
  },
  resources: null,
  runtime: {},
  size: 'AUTO',
  variables: {},
};
 
/*
 * Helper to encode into a base64 string
 */
const encode = data => {
  if (_.isObject(data)) data = JSON.stringify(data);
  return Buffer.from(data).toString('base64');
};
 
/*
 * Helper to get the applications environment variables
 */
const getEnvironmentVariables = appConfig => _(_.get(appConfig, 'variables.env', {}))
  .map((value, key) => ([key, (_.isObject(value)) ? JSON.stringify(value) : value]))
  .fromPairs()
  .value();
 
/*
 * Helper to get the applications environment variables
 */
const getPlatformVariables = app => {
  const strippedVars = _.omit(_.get(app, 'variables', {}), ['env']);
 
  // Loop through and try to build things out
  const vars = {};
  _.forEach(strippedVars, (value, key) => {
    if (_.isPlainObject(value)) {
      // @NOTE sorry to my CS teacher for these names
      _.forEach(value, (value2, key2) => {
        vars[`${key}:${key2}`] = value2;
      });
    } else {
      vars[key] = value;
    }
  });
 
  // Merge in needed local overrides and encode
  return encode(_.merge({}, vars, overrides));
};
 
/*
 * Helper to build the application configuration
 */
const getApplicationConfig = (app, {id}) => ({
  configuration: _.merge({}, applicationConfigDefaults, app, {
    slug: `${id}-${app.name}`,
    tree_id: `${id}-${app.name}`,
    variables: {},
  }),
  crons: _.get(app, 'crons', {}),
  cron_minimum_interval: '1',
  enable_smtp: 'false',
  mounts: _.get(app, 'mounts', {}),
  slug: `${id}-${app.name}`,
});
 
/*
 * Helper to parse the platformsh config files
 */
const getApplicationsConfig = (apps, config) => _(apps)
  // Start by just getting the basic config
  .map(app => getApplicationConfig(app, config))
  // Then fix some things up we need for platform
  .map(app => {
    // Reset the variables
    app.configuration.variables = getApplicationEnvironment(app.configuration, config);
 
    // Find the web prefix
    const appName = _.get(app, 'configuration.name');
    const appConfig = _.find(config.applications, {name: appName});
    const webPrefix = appConfig.webPrefix;
 
    // Go through the web location and prefix the root if we need to
    // @NOTE: We do this because for multiapp we still mount the ENTIRE
    // application into /app, not just the source.root
    _.forEach(_.get(app, 'configuration.web.locations'), block => {
      if (_.has(block, 'root')) block.root = path.join(webPrefix, block.root);
    });
 
    // app.configuration.web.locations['/'].root = 'php/web';
    return app;
  })
  // and return
  .value();
 
/*
 * Helper to get the application environment
 *
 * Handle the variables with the exception of PLATFORM_RELATIONSHIPS
 * which is special and needs to be handled separately
 */
const getApplicationEnvironment = (appConfig, config) => _.merge({}, getEnvironmentVariables(appConfig), {
  PLATFORM_DOCUMENT_ROOT: utils.getDocRoot(appConfig),
  PLATFORM_APPLICATION: encode(appConfig),
  // @NOTE: PLATFORM_APP_DIR is normally set to /app but this is problematic locally
  // eg on Drupal this puts the /tmp and /private at /app/tmp and /app/private and
  // we probably dont want these things ending up in git
  //
  // That said changing this could def be problematic for other reasons
  // PLATFORM_APP_DIR: '/app',
  PLATFORM_ENVIRONMENT: 'lando',
  PLATFORM_APPLICATION_NAME: appConfig.name,
  PLATFORM_PROJECT: config.id,
  PLATFORM_DIR: '/app',
  PLATFORM_PROJECT_ENTROPY: encode(appConfig),
  PLATFORM_BRANCH: 'master',
  PLATFORM_TREE_ID: `${config.id}-${appConfig.name}`,
  PLATFORM_ROUTES: encode(config.routes),
  PLATFORM_VARIABLES: getPlatformVariables(appConfig),
});
 
/*
 * Helper to build the service configurations
 */
const getServiceConfig = (id, name) => ({
  access: {},
  project_info: {
    name: id,
    settings: {
      variables_prefix: 'PLATFORM_',
      crons_in_git: false,
      product_code: 'platformsh',
      product_name: 'Platform.sh',
      enforce_mfa: false,
      // @NOTE: we kept this because it was flagged by @ralt as SUPER IMPORTANT ;)
      bot_email: 'bot@platform.sh',
    },
  },
  environment_info: {
    is_production: false,
    machine_name: name,
    name: id,
    is_main: true,
  },
});
 
/*
 * Helper to generate our platform JSON
 */
const getPlatformConfig = ({id, name, platformsh, _config}, service = {}) => {
  const externalIP = _.get(_config, 'appEnv.LANDO_HOST_IP');
  const uid = _.toInteger(_.get(_config, 'uid', 1000));
  const gid = _.toInteger(_.get(_config, 'gid', 1000));
 
  // Start with all the application
  let applications = platformsh.config.applications;
  // But if its application service lets only use that one
  if (service.application) {
    applications = _(applications)
      .filter(application => application.name === service.name)
      .value();
  }
 
  return {
    primary_ip: '127.0.0.1',
    features: [],
    domainname: `${name}.${service.name}.service._.lndo.site`,
    host_ip: externalIP,
    applications: getApplicationsConfig(applications, platformsh),
    configuration: _.merge({application_size: 536}, getServiceConfig(id, name), service.configuration),
    info: {
      'mail_relay_host': null,
      'mail_relay_host_v2': null,
      'limits': {
        disk: _.get(service, 'disk', 2048),
        memory: 2048,
        cpu: 1,
      },
      'external ip': externalIP,
    },
    log_file: '/dev/stderr',
    log_gid: gid,
    log_uid: uid,
    name: name,
    service: service.name,
    cluster: 'bespin',
    region: 'lando',
    hostname: service.hostname,
    instance: _.get(_config, 'instance', 'lando'),
    nameserver: '127.0.0.11',
    web_gid: gid,
    web_uid: uid,
  };
};
 
/*
 * Helper to build the /run/config.json for each service
 */
exports.buildRunConfig = app => _(app.platformsh.applications)
  // Add some indicator that this is an app
  .map(app => _.merge({}, app, {application: true}))
  // Arrayify and merge in our services
  .concat(app.platformsh.services)
  // Map into the full blown config
  .map(service => ({
    service: service.name,
    application: service.application,
    file: path.join(app.configPath, `${service.name}.json`),
    data: getPlatformConfig(app, service),
  }))
  // Return
  .value();