Source: deploy/platform-deployment.js

const { resolve } = require('path')
const { readFileSync } = require('fs')
const { PlatformTemplateService } = require('../template')
const { PlatformConfigService, SupportedDeploymentTypes } = require('../config')
const { DeploymentQueueService } = require('../deployment-queue')
const { logError } = require('../log')

// export interface DeploymentTemplate {
//   type: SupportedDeploymentTypes;
//   targetDeploymentPlatform: string;
//   subdomain?: string;
//   env?: {
//     shared?: { [name: string]: string | number };
//     prod?: { [name: string]: string | number };
//     test?: { [name: string]: string | number };
//     qa?: { [name: string]: string | number };
//   }
// }

// export interface ContainerDeploymentTemplate extends DeploymentTemplate {
//   type: SupportedDeploymentTypes.Container;
//   dockerFile: string;
//   dockerContext: string;
//   image: string;
//   hostPort: number;
//   containerPort: number;
//   registry: string;
// }

// export interface ContainerDeploymentRequest extends ContainerDeploymentTemplate {
//   authentication?: string;
//   username?: string;
// }

/**
  * @class
*/
class PlatformDeploymentService {

  static deploy({
    username,
    authentication,
    targetEnvironment,
    pathToPlatformTemplates,
    pathToOssyFile
  }) {

    const platformConfigRequest = PlatformTemplateService.readFromFile(pathToPlatformTemplates)
      .then(templates => templates.map(x => ({ ...x, activeEnvironment: targetEnvironment })))
      .then(templates => templates.map(x => PlatformConfigService.from(x)))

    const deploymentTemplatesRequest = PlatformDeploymentService.getDeploymentTemplates(pathToOssyFile)

    return Promise.all([platformConfigRequest, deploymentTemplatesRequest])
      .then(([platformConfigs, deploymentTemplates]) => {
        deploymentTemplates.map(deploymentTemplate => {

          const platformConfig = platformConfigs.find(config => config.platformName === deploymentTemplate.targetDeploymentPlatform)

          if (!platformConfig) {
            logError({ message: `[PlatformDeploymentService] Could not find a deployment platform with the name ${deploymentTemplate.targetDeploymentPlatform}` })
            return Promise.reject()
          }

          process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'

          if (deploymentTemplate.type !== SupportedDeploymentTypes.Container) {
            logError({ message: `[PlatformDeploymentService] Unsupported deployment type of ${deploymentTemplate.type}` })
            return Promise.reject()
          }

          const deploymentRequest = {
            ...deploymentTemplate,
            env: PlatformDeploymentService.getEnvironmentVariables(targetEnvironment, deploymentTemplate),
            username: username,
            authentication: authentication
          }

          return DeploymentQueueService.sendDeploymentRequest(platformConfig, deploymentRequest)

        })
      })
      .catch(error => logError({ message: '[PlatformDeploymentService] Could not send deployment request', error }))
  }

  static getDeploymentTemplates(pathToOssyFile) {
    if (!pathToOssyFile) return logError({ message: '[PlatformDeploymentService] No path to ossy.json provided' })
    const ossyfile = JSON.parse(readFileSync(resolve(pathToOssyFile), 'utf8'))
    return Promise.resolve(ossyfile.deployments || [])
  }

  static getEnvironmentVariables(targetEnvironment, deploymentRequest) {
    const envs = deploymentRequest.env || {}
    return {
      ...(envs.shared || {}),
      ...(envs[targetEnvironment] || {})
    }
  }

}

module.exports = {
  PlatformDeploymentService
}