mirror of
https://github.com/jambonz/sbc-inbound.git
synced 2026-10-04 18:44:43 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88b24e72b1 | ||
|
|
830e58feb5 | ||
|
|
2744634573 | ||
|
|
11677c8954 | ||
|
|
11f0a9fb99 | ||
|
|
80e16225d3 |
+33
-23
@@ -6,15 +6,20 @@ const {LifeCycleEvents} = require('./constants');
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const getString = bent('string');
|
const getString = bent('string');
|
||||||
const AWS = require('aws-sdk');
|
const {
|
||||||
const sns = new AWS.SNS({apiVersion: '2010-03-31'});
|
SNSClient,
|
||||||
const autoscaling = new AWS.AutoScaling({apiVersion: '2011-01-01'});
|
SubscribeCommand,
|
||||||
|
UnsubscribeCommand } = require('@aws-sdk/client-sns');
|
||||||
|
const snsClient = new SNSClient({ region: process.env.AWS_REGION, apiVersion: '2010-03-31' });
|
||||||
|
const {
|
||||||
|
AutoScalingClient,
|
||||||
|
DescribeAutoScalingGroupsCommand,
|
||||||
|
CompleteLifecycleActionCommand } = require('@aws-sdk/client-auto-scaling');
|
||||||
|
const autoScalingClient = new AutoScalingClient({ region: process.env.AWS_REGION, apiVersion: '2011-01-01' });
|
||||||
const {Parser} = require('xml2js');
|
const {Parser} = require('xml2js');
|
||||||
const parser = new Parser();
|
const parser = new Parser();
|
||||||
const {validatePayload} = require('verify-aws-sns-signature');
|
const {validatePayload} = require('verify-aws-sns-signature');
|
||||||
|
|
||||||
AWS.config.update({region: process.env.AWS_REGION});
|
|
||||||
|
|
||||||
class SnsNotifier extends Emitter {
|
class SnsNotifier extends Emitter {
|
||||||
constructor(logger) {
|
constructor(logger) {
|
||||||
super();
|
super();
|
||||||
@@ -64,7 +69,7 @@ class SnsNotifier extends Emitter {
|
|||||||
subscriptionRequestId: this.subscriptionRequestId
|
subscriptionRequestId: this.subscriptionRequestId
|
||||||
}, 'response from SNS SubscribeURL');
|
}, 'response from SNS SubscribeURL');
|
||||||
const data = await this.describeInstance();
|
const data = await this.describeInstance();
|
||||||
this.lifecycleState = data.AutoScalingInstances[0].LifecycleState;
|
this.lifecycleState = data.AutoScalingGroups[0].Instances[0].LifecycleState;
|
||||||
this.emit('SubscriptionConfirmation', {publicIp: this.publicIp});
|
this.emit('SubscriptionConfirmation', {publicIp: this.publicIp});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -130,11 +135,12 @@ class SnsNotifier extends Emitter {
|
|||||||
|
|
||||||
async subscribe() {
|
async subscribe() {
|
||||||
try {
|
try {
|
||||||
const response = await sns.subscribe({
|
const params = {
|
||||||
Protocol: 'http',
|
Protocol: 'http',
|
||||||
TopicArn: process.env.AWS_SNS_TOPIC_ARM,
|
TopicArn: process.env.AWS_SNS_TOPIC_ARM,
|
||||||
Endpoint: this.snsEndpoint
|
Endpoint: this.snsEndpoint
|
||||||
}).promise();
|
};
|
||||||
|
const response = await snsClient.send(new SubscribeCommand(params));
|
||||||
this.logger.info({response}, `response to SNS subscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
|
this.logger.info({response}, `response to SNS subscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({err}, `Error subscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
|
this.logger.error({err}, `Error subscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
|
||||||
@@ -144,9 +150,10 @@ class SnsNotifier extends Emitter {
|
|||||||
async unsubscribe() {
|
async unsubscribe() {
|
||||||
if (!this.subscriptionArn) throw new Error('SnsNotifier#unsubscribe called without an active subscription');
|
if (!this.subscriptionArn) throw new Error('SnsNotifier#unsubscribe called without an active subscription');
|
||||||
try {
|
try {
|
||||||
const response = await sns.unsubscribe({
|
const params = {
|
||||||
SubscriptionArn: this.subscriptionArn
|
SubscriptionArn: this.subscriptionArn
|
||||||
}).promise();
|
};
|
||||||
|
const response = await snsClient.send(new UnsubscribeCommand(params));
|
||||||
this.logger.info({response}, `response to SNS unsubscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
|
this.logger.info({response}, `response to SNS unsubscribe to ${process.env.AWS_SNS_TOPIC_ARM}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({err}, `Error unsubscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
|
this.logger.error({err}, `Error unsubscribing to SNS topic arn ${process.env.AWS_SNS_TOPIC_ARM}`);
|
||||||
@@ -155,26 +162,29 @@ class SnsNotifier extends Emitter {
|
|||||||
|
|
||||||
completeScaleIn() {
|
completeScaleIn() {
|
||||||
assert(this.scaleInParams);
|
assert(this.scaleInParams);
|
||||||
autoscaling.completeLifecycleAction(this.scaleInParams, (err, response) => {
|
autoScalingClient.send(new CompleteLifecycleActionCommand(this.scaleInParams))
|
||||||
if (err) return this.logger.error({err}, 'Error completing scale-in');
|
.then((data) => {
|
||||||
this.logger.info({response}, 'Successfully completed scale-in action');
|
return this.logger.info({data}, 'Successfully completed scale-in action');
|
||||||
});
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
this.logger.error({err}, 'Error completing scale-in');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
describeInstance() {
|
describeInstance() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!this.instanceId) return reject('instance-id unknown');
|
if (!this.instanceId) return reject('instance-id unknown');
|
||||||
autoscaling.describeAutoScalingInstances({
|
autoScalingClient.send(new DescribeAutoScalingGroupsCommand({
|
||||||
InstanceIds: [this.instanceId]
|
InstanceIds: [this.instanceId]
|
||||||
}, (err, data) => {
|
}))
|
||||||
if (err) {
|
.then((data) => {
|
||||||
|
this.logger.info({data}, 'SnsNotifier: describeInstance');
|
||||||
|
return resolve(data);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
this.logger.error({err}, 'Error describing instances');
|
this.logger.error({err}, 'Error describing instances');
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
});
|
||||||
this.logger.info({data}, 'SnsNotifier: describeInstance');
|
|
||||||
resolve(data);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +198,7 @@ module.exports = async function(logger) {
|
|||||||
process.on('SIGHUP', async() => {
|
process.on('SIGHUP', async() => {
|
||||||
try {
|
try {
|
||||||
const data = await notifier.describeInstance();
|
const data = await notifier.describeInstance();
|
||||||
const state = data.AutoScalingInstances[0].LifecycleState;
|
const state = data.AutoScalingGroups[0].Instances[0].LifecycleState;
|
||||||
if (state !== notifier.lifecycleState) {
|
if (state !== notifier.lifecycleState) {
|
||||||
notifier.lifecycleState = state;
|
notifier.lifecycleState = state;
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
|||||||
Generated
+2082
-256
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -33,7 +33,8 @@
|
|||||||
"@jambonz/stats-collector": "^0.1.8",
|
"@jambonz/stats-collector": "^0.1.8",
|
||||||
"@jambonz/time-series": "^0.2.5",
|
"@jambonz/time-series": "^0.2.5",
|
||||||
"@jambonz/digest-utils": "^0.0.3",
|
"@jambonz/digest-utils": "^0.0.3",
|
||||||
"aws-sdk": "^2.1354.0",
|
"@aws-sdk/client-sns": "^3.360.0",
|
||||||
|
"@aws-sdk/client-auto-scaling": "^3.360.0",
|
||||||
"bent": "^7.3.12",
|
"bent": "^7.3.12",
|
||||||
"cidr-matcher": "^2.1.1",
|
"cidr-matcher": "^2.1.1",
|
||||||
"debug": "^4.3.4",
|
"debug": "^4.3.4",
|
||||||
|
|||||||
Reference in New Issue
Block a user