| 9 | import { logAndReturnError, MultiMessageError } from '../utils/infrastructure'; |
| 10 | |
| 11 | export async function deploy(configPath: string, secretsPath: string, receiptFile: string, autoRemove: boolean) { |
| 12 | const secrets = parseSecretsFile(secretsPath); |
| 13 | const config = loadConfig(configPath, secrets); |
| 14 | |
| 15 | if (config.nodeSettings.cloudProvider.type === 'local') { |
| 16 | throw logAndReturnError(`Deployer can't deploy to "local" cloud provider`); |
| 17 | } |
| 18 | |
| 19 | // Deployment is not an atomic operation. It is possible that some resources are deployed even when there is a |
| 20 | // deployment error. We want to write a receipt file, because the user might use the receipt to remove the deployed |
| 21 | // resources from the failed deployment. (The removal is not guaranteed, but it's better compared to asking user to |
| 22 | // remove the resources manually in the cloud provider dashboard). |
| 23 | |
| 24 | const time = new Date(); |
| 25 | const goDeployAirnode = await go(() => deployAirnode(config, configPath, secretsPath, time.getTime())); |
| 26 | writeReceiptFile(receiptFile, config, time.toISOString(), goDeployAirnode.success); |
| 27 | |
| 28 | if (!goDeployAirnode.success && !autoRemove) { |
| 29 | logger.fail( |
| 30 | `Airnode deployment failed due to unexpected errors.\n` + |
| 31 | ` It is possible that some resources have been deployed on cloud provider.\n` + |
| 32 | ` Please use the "remove" command from the deployer CLI to ensure all cloud resources are removed.`, |
| 33 | { bold: true } |
| 34 | ); |
| 35 | |
| 36 | throw goDeployAirnode.error; |
| 37 | } |
| 38 | |
| 39 | if (!goDeployAirnode.success) { |
| 40 | logger.fail( |
| 41 | `Airnode deployment failed due to unexpected errors.\n` + |
| 42 | ` It is possible that some resources have been deployed on cloud provider.\n` + |
| 43 | ` Attempting to remove them...\n`, |
| 44 | { bold: true } |
| 45 | ); |
| 46 | |
| 47 | // Try to remove deployed resources |
| 48 | const goRemoveAirnode = await go(() => removeWithReceipt(receiptFile)); |
| 49 | if (!goRemoveAirnode.success) { |
| 50 | logger.fail( |
| 51 | `Airnode removal failed due to unexpected errors.\n` + |
| 52 | ` It is possible that some resources have been deployed on cloud provider.\n` + |
| 53 | ` Please check the resources on the cloud provider dashboard and\n` + |
| 54 | ` use the "remove" command from the deployer CLI to remove them.\n` + |
| 55 | ` If the automatic removal via CLI fails, remove the resources manually.`, |
| 56 | { bold: true } |
| 57 | ); |
| 58 | |
| 59 | throw new MultiMessageError([ |
| 60 | 'Deployment error:\n' + goDeployAirnode.error.message, |
| 61 | 'Removal error:\n' + goRemoveAirnode.error.message, |
| 62 | ]); |
| 63 | } |
| 64 | |
| 65 | logger.succeed('Successfully removed the Airnode deployment'); |
| 66 | throw new Error('Deployment error:\n' + goDeployAirnode.error.message); |
| 67 | } |
| 68 | |