|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const constants = require('./constants'); |
| 5 | + |
| 6 | +module.exports = { |
| 7 | + createContainers() { |
| 8 | + return BbPromise.bind(this) |
| 9 | + .then(this.getContainers) |
| 10 | + .then(this.createOrUpdateContainers); |
| 11 | + }, |
| 12 | + |
| 13 | + getContainers() { |
| 14 | + const containersUrl = `namespaces/${this.namespace.id}/containers`; |
| 15 | + return this.provider.apiManager.get(containersUrl) |
| 16 | + .then(response => response.data.containers) |
| 17 | + .catch((err) => { |
| 18 | + throw new Error(err.response.data.message) |
| 19 | + }) |
| 20 | + }, |
| 21 | + |
| 22 | + createOrUpdateContainers(foundContainers) { |
| 23 | + const containers = this.provider.serverless.service.custom.containers; |
| 24 | + const containerNames = Object.keys(containers); |
| 25 | + const promises = containerNames.map((containerName) => { |
| 26 | + const container = Object.assign(containers[containerName], { name: containerName }); |
| 27 | + const foundContainer = foundContainers.find(c => c.name === container.name); |
| 28 | + return foundContainer ? this.updateContainer(container, foundContainer) : this.createSingleContainer(container); |
| 29 | + }); |
| 30 | + |
| 31 | + return Promise.all(promises) |
| 32 | + .then((updatedContainers) => { |
| 33 | + this.containers = updatedContainers; |
| 34 | + }); |
| 35 | + }, |
| 36 | + |
| 37 | + createSingleContainer(container) { |
| 38 | + const params = { |
| 39 | + name: container.name, |
| 40 | + environment_variables: container.env, |
| 41 | + namespace_id: this.namespace.id, |
| 42 | + memory_limit: container.memoryLimit, |
| 43 | + min_scale: container.minScale, |
| 44 | + max_scale: container.maxScale, |
| 45 | + timeout: container.timeout |
| 46 | + }; |
| 47 | + |
| 48 | + this.serverless.cli.log(`Creating container ${container.name}...`); |
| 49 | + |
| 50 | + return this.provider.apiManager.post('containers', params) |
| 51 | + .then(response => Object.assign(response.data, { directory: container.directory })) |
| 52 | + .catch((err) => { |
| 53 | + throw new Error(err.response.data.message) |
| 54 | + }) |
| 55 | + }, |
| 56 | + |
| 57 | + updateContainer(container, foundContainer) { |
| 58 | + const params = { |
| 59 | + redeploy: false, |
| 60 | + environment_variables: container.env, |
| 61 | + memory_limit: container.memoryLimit, |
| 62 | + min_scale: container.minScale, |
| 63 | + max_scale: container.maxScale, |
| 64 | + timeout: container.timeout |
| 65 | + } |
| 66 | + |
| 67 | + const updateUrl = `containers/${foundContainer.id}`; |
| 68 | + this.serverless.cli.log(`Updating container ${container.name}...`); |
| 69 | + return this.provider.apiManager.patch(updateUrl, params) |
| 70 | + .then(response => Object.assign(response.data, { directory: container.directory })) |
| 71 | + .catch((err) => { |
| 72 | + throw new Error(err.response.data.message) |
| 73 | + }) |
| 74 | + }, |
| 75 | +}; |
0 commit comments