import { formatProjectError } from "../error" ; import { adaptServer , AdaptServer } from "../server" ; import { createDeployment as createDeploymentObj , Deployment , destroyDeployment } from "../server/deployment" ; import { HistoryStatus } from "../server/history" ; import { buildAndDeploy } from "./buildAndDeploy" ; import { defaultDeployCommonOptions , DeployCommonOptions , DeployState , withOpsSetup , } from "./common" ; import { forkExports } from "./fork" ; export interface CreateOptions extends DeployCommonOptions { projectName : string ; stackName : string ; deployID ? : string ; initLocalServer ? : boolean ; initialStateJson ? : string ; initialObservationsJson ? : string ; } const defaultOptions = { initLocalServer : false , initialStateJson : "{}" , initialObservationsJson : "{}" } ; export async function createDeployment ( options ) { const finalOptions = { ... defaultDeployCommonOptions , ... defaultOptions , ... options } ; const { adaptUrl , client , deployID , initLocalServer , initialStateJson , initialObservationsJson , logger : _logger , loggerId , projectName , ... buildOpts } = finalOptions ; const setup = { name : "createDeployment" , description : "Creating deployment" , client , logger : _logger , loggerId , } ; return withOpsSetup ( setup , async ( info ) : Promise < DeployState > => { const { logger , taskObserver } = info ; let ds ; let server = null ; let deployment = null ; try { server = await adaptServer ( adaptUrl , { init : finalOptions . initLocalServer , } ) ; deployment = await createDeploymentObj ( server , projectName , finalOptions . stackName , { deployID , } ) ; ds = await buildAndDeploy ( { deployment , prevStateJson : initialStateJson , observationsJson : initialObservationsJson , taskObserver , ... buildOpts } ) ; } catch ( err ) { logger . error ( `Error creating deployment: ${ formatProjectError ( err ) }` ) ; ds = { type : "error" , messages : logger . messages , summary : logger . summary , domXml : err . domXml , } ; if ( deployment ) { const dep = deployment as Deployment ; if ( await dep . lastEntry ( HistoryStatus . complete ) ) { ds . deployID = dep . deployID ; } } } if ( server && deployment && ( finalOptions . dryRun || ! ds . deployID ) ) { try { await destroyDeployment ( server , deployment . deployID ) ; } catch ( err ) { logger . warning ( `Error destroying deployment: ${ err }` ) ; } } return ds ; } ) ; } forkExports ( module , "createDeployment" ) ;