|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | + |
| 5 | +class ServerlessPlugin { |
| 6 | + constructor(serverless, options) { |
| 7 | + this.serverless = serverless |
| 8 | + this.options = options |
| 9 | + this.provider = this.serverless.getProvider('aws') |
| 10 | + |
| 11 | + this.commands = { |
| 12 | + deploy: { |
| 13 | + commands: { |
| 14 | + additionalstack: { |
| 15 | + usage: 'Deploy additional stacks', |
| 16 | + lifecycleEvents: [ |
| 17 | + 'deploy', |
| 18 | + ], |
| 19 | + options: { |
| 20 | + all: { |
| 21 | + usage: 'Deploy all additional stacks', |
| 22 | + shortcut: 'a', |
| 23 | + required: false, |
| 24 | + }, |
| 25 | + stack: { |
| 26 | + usage: 'Additional stack name to deploy', |
| 27 | + shortcut: 'k', |
| 28 | + required: false, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + this.hooks = { |
| 37 | + 'before:deploy:deploy': this.beforeDeployGlobal.bind(this), |
| 38 | + 'after:deploy:deploy': this.afterDeployGlobal.bind(this), |
| 39 | + 'deploy:additionalstack:deploy': this.deployAdditionalStackDeploy.bind(this), |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + getAdditionalStacks() { |
| 44 | + return this.serverless.service.custom && this.serverless.service.custom.additionalStacks || {} |
| 45 | + } |
| 46 | + |
| 47 | + getAdditionalBeforeStacks() { |
| 48 | + const beforeStacks = {} |
| 49 | + const stacks = this.getAdditionalStacks() |
| 50 | + Object.keys(stacks).map(stackName => { |
| 51 | + if (!stacks[stackName].Deploy || stacks[stackName].Deploy.toLowerCase() === 'before') { |
| 52 | + beforeStacks[stackName] = stacks[stackName] |
| 53 | + } |
| 54 | + }) |
| 55 | + return beforeStacks |
| 56 | + } |
| 57 | + |
| 58 | + getAdditionalAfterStacks() { |
| 59 | + const afterStacks = {} |
| 60 | + const stacks = this.getAdditionalStacks() |
| 61 | + Object.keys(stacks).map(stackName => { |
| 62 | + if (stacks[stackName].Deploy && stacks[stackName].Deploy.toLowerCase() === 'after') { |
| 63 | + afterStacks[stackName] = stacks[stackName] |
| 64 | + } |
| 65 | + }) |
| 66 | + return afterStacks |
| 67 | + } |
| 68 | + |
| 69 | + // Deploy additional stacks befpre deploying the main stack |
| 70 | + // These are stacks with Deploy: Before, which is the default |
| 71 | + beforeDeployGlobal() { |
| 72 | + const stacks = this.getAdditionalBeforeStacks() |
| 73 | + if (Object.keys(stacks).length > 0) { |
| 74 | + this.serverless.cli.log('Deploying additional stacks...') |
| 75 | + return this.deployStacks(stacks) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Deploy additional stacks after deploying the main stack |
| 80 | + // These are stacks with Deploy: After |
| 81 | + afterDeployGlobal() { |
| 82 | + const stacks = this.getAdditionalAfterStacks() |
| 83 | + if (Object.keys(stacks).length > 0) { |
| 84 | + this.serverless.cli.log('Deploying additional stacks...') |
| 85 | + return this.deployStacks(stacks) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Deploy additional stacks specified with sls deploy stack [name] |
| 90 | + deployAdditionalStackDeploy() { |
| 91 | + const stacks = this.getAdditionalStacks() |
| 92 | + |
| 93 | + if (this.options.all) { |
| 94 | + // Deploy all additional stacks |
| 95 | + if (Object.keys(stacks).length > 0) { |
| 96 | + this.serverless.cli.log('Deploying all additional stacks...') |
| 97 | + return this.deployStacks(stacks) |
| 98 | + } else { |
| 99 | + this.serverless.cli.log('No additional stacks defined. Add a custom.additionalStacks section to serverless.yml.') |
| 100 | + return Promise.resolve() |
| 101 | + } |
| 102 | + } else if (this.options.stack) { |
| 103 | + const stack = stacks[this.options.stack] |
| 104 | + if (stack) { |
| 105 | + this.serverless.cli.log('Deploying additional stack ' + this.options.stack + '...') |
| 106 | + return this.deployStack(this.options.stack, stack) |
| 107 | + } else { |
| 108 | + return Promise.reject(new Error('Additional stack not found: ' + this.options.stack)) |
| 109 | + } |
| 110 | + } else { |
| 111 | + return Promise.reject(new Error('Please specify either sls deploy additionalstack -a to deploy all additional stacks or -k [stackName] to deploy a single stack')) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // This deploys all the specified stacks |
| 116 | + deployStacks(stacks) { |
| 117 | + let promise = Promise.resolve() |
| 118 | + Object.keys(stacks).map(stackName => { |
| 119 | + promise = promise |
| 120 | + .then(() => { |
| 121 | + return this.deployStack(stackName, stacks[stackName]) |
| 122 | + }) |
| 123 | + }) |
| 124 | + return promise |
| 125 | + } |
| 126 | + |
| 127 | + // This is where we actually handle the deployment to AWS |
| 128 | + deployStack(stackName, stack) { |
| 129 | + this.serverless.cli.log('DEPLOYING NOW ' + stackName + ' ' + JSON.stringify(stack)) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +module.exports = ServerlessPlugin |
0 commit comments