UNPKG

deep-package-manager

Version:
279 lines (230 loc) 7.02 kB
/** * Created by AlexanderC on 5/27/15. */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElasticacheService = undefined; var _AbstractService = require('./AbstractService'); var _deepCore = require('deep-core'); var _deepCore2 = _interopRequireDefault(_deepCore); var _AwsRequestSyncStack = require('../../Helpers/AwsRequestSyncStack'); var _FailedToCreateElasticacheClusterException = require('./Exception/FailedToCreateElasticacheClusterException'); var _FailedToRetrieveDefaultSecurityGroupException = require('./Exception/FailedToRetrieveDefaultSecurityGroupException'); var _FailedToRetrieveLambdaSubnetGroupException = require('./Exception/FailedToRetrieveLambdaSubnetGroupException'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Elasticache service */ class ElasticacheService extends _AbstractService.AbstractService { /** * @param {Array} args */ constructor(...args) { super(...args); } /** * @returns {String} */ name() { return _deepCore2.default.AWS.Service.ELASTIC_CACHE; } /** * @returns {String[]} */ static get AVAILABLE_REGIONS() { return _deepCore2.default.AWS.Region.all(); } /** * @param {Core.Generic.ObjectStorage} services * @returns {ElasticacheService} */ _setup(services) { // @todo: implement! if (this._isUpdate) { this._ready = true; return this; } // @todo: uncomment when VPC issues fixed // issues: accessing resources outside the VPC this._ready = true; //this._getDefaultSecurityGroupId((securityGroupId) => { // this._getLambdaSubnetGroups((subnetIds) => { // this._createCluster( // this.awsAccountId, // this.appIdentifier, // securityGroupId // )((clusterId, dsn) => { // this._config.clusterId = clusterId; // this._config.dsn = dsn; // this._config.securityGroupId = securityGroupId; // this._config.subnetIds = subnetIds; // // this._ready = true; // }); // }); //}); return this; } /** * @param {Core.Generic.ObjectStorage} services * @returns {ElasticacheService} */ _postProvision(services) { // @todo: implement! if (this._isUpdate) { this._readyTeardown = true; return this; } this._readyTeardown = true; return this; } /** * @param {Core.Generic.ObjectStorage} services * @returns {ElasticacheService} */ _postDeployProvision(services) { // @todo: implement! if (this._isUpdate) { this._ready = true; return this; } this._ready = true; return this; } /** * @param {Function} cb * @private */ _getLambdaSubnetGroups(cb) { let payload = { CacheSubnetGroupName: 'lambda' }; this._provisioning.elasticCache.describeCacheSubnetGroups(payload, (error, data) => { if (error) { throw new _FailedToRetrieveLambdaSubnetGroupException.FailedToRetrieveLambdaSubnetGroupException(error); } if (data.CacheSubnetGroups.length <= 0) { throw new _FailedToRetrieveLambdaSubnetGroupException.FailedToRetrieveLambdaSubnetGroupException('No Lambda subnet group assigned'); } let subnets = data.CacheSubnetGroups[0].Subnets; if (subnets.length <= 0) { throw new _FailedToRetrieveLambdaSubnetGroupException.FailedToRetrieveLambdaSubnetGroupException('No Lambda subnets available'); } let identifiers = []; subnets.forEach(subnetData => { identifiers.push(subnetData.SubnetIdentifier); }); cb(identifiers); }); } /** * @param {Function} cb * @private */ _getDefaultSecurityGroupId(cb) { let payload = { GroupNames: ['default'] }; this._provisioning.ec2.describeSecurityGroups(payload, (error, data) => { if (error) { throw new _FailedToRetrieveDefaultSecurityGroupException.FailedToRetrieveDefaultSecurityGroupException(error); } if (data.SecurityGroups.length <= 0) { throw new _FailedToRetrieveDefaultSecurityGroupException.FailedToRetrieveDefaultSecurityGroupException('No default security group assigned'); } cb(data.SecurityGroups[0].GroupId); }); } /** * @param {String} awsAccountId * @param {String} appIdentifier * @param {String} securityGroupId * @returns {Function} * @private */ _createCluster(awsAccountId, appIdentifier, securityGroupId) { let syncStack = new _AwsRequestSyncStack.AwsRequestSyncStack(); let ec = this.provisioning.elasticCache; let clusterId = this.generateAwsResourceName('ec', _deepCore2.default.AWS.Service.ELASTIC_CACHE, '', _AbstractService.AbstractService.DELIMITER_HYPHEN_LOWER_CASE); let parameters = { CacheClusterId: clusterId, AutoMinorVersionUpgrade: true, Engine: ElasticacheService.ENGINE, CacheNodeType: ElasticacheService.INSTANCE, NumCacheNodes: ElasticacheService.CACHE_NODES, SecurityGroupIds: [securityGroupId] }; syncStack.push(ec.createCacheCluster(parameters), (error, data) => { if (error) { throw new _FailedToCreateElasticacheClusterException.FailedToCreateElasticacheClusterException(clusterId, error); } }); return callback => { return syncStack.join().ready(() => { this._acquireEndpoint(clusterId, callback); }); }; } /** * @param {String} clusterId * @param {Function} callback * @private */ _acquireEndpoint(clusterId, callback) { let dsn = ''; let succeed = false; let ec = this.provisioning.elasticCache; let innerSyncStack = new _AwsRequestSyncStack.AwsRequestSyncStack(); let describeParameters = { CacheClusterId: clusterId, ShowCacheNodeInfo: true }; innerSyncStack.push(ec.describeCacheClusters(describeParameters), (error, data) => { if (error) { throw new _FailedToCreateElasticacheClusterException.FailedToCreateElasticacheClusterException(clusterId, error); } let nodes = data.CacheClusters[0].CacheNodes; if (nodes.length > 0) { let endpoint = nodes[0].Endpoint; dsn = `${endpoint.Address}:${endpoint.Port}`; succeed = true; } }); innerSyncStack.join().ready(() => { if (succeed) { callback(clusterId, dsn); } else { setTimeout(() => { this._acquireEndpoint(clusterId, callback); }, ElasticacheService.WAIT_TIME); } }); } /** * @returns {Number} */ static get WAIT_TIME() { return 2000; } /** * @returns {Number} */ static get CACHE_NODES() { return 1; } /** * @returns {String} */ static get INSTANCE() { return 'cache.t2.micro'; } /** * @returns {String} */ static get ENGINE() { return 'redis'; } } exports.ElasticacheService = ElasticacheService;