UNPKG

@nordicsemiconductor/asset-tracker-cloud-aws

Version:

A reference implementation of a serverless backend for an IoT product developed using AWS CDK in TypeScript.

342 lines (341 loc) • 17.9 kB
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _async_to_generator(fn) { return function() { var self = this, args = arguments; return new Promise(function(resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _ts_generator(thisArg, body) { var f, y, t, g, _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function(v) { return step([ n, v ]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while(_)try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [ op[0] & 2, t.value ]; switch(op[0]){ case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [ 0 ]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [ 6, e ]; y = 0; } finally{ f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } import { CloudFormationClient, DescribeStackResourcesCommand } from '@aws-sdk/client-cloudformation'; import { CloudWatchLogsClient, DeleteLogGroupCommand, DescribeLogStreamsCommand, GetLogEventsCommand } from '@aws-sdk/client-cloudwatch-logs'; import chalk from 'chalk'; import { CORE_STACK_NAME } from '../../cdk/stacks/stackName.js'; export var logsCommand = function() { return { command: 'logs', options: [ { flags: '-n, --numLogGroups <numLogGroups>', description: 'Number of logGroups to consider, default: 1' }, { flags: '-s, --numLogStreams <numLogStreams>', description: 'Number of logStreams to consider, default: 100' }, { flags: '-X, --deleteLogGroups', description: 'delete log groups afterwards' } ], action: function() { var _ref = _async_to_generator(function(param) { var numLogGroups, numLogStreams, deleteLogGroups, _StackResources_filter, _StackResources, cf, logs, _StackResources_filter_map, logGroups, streams; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: numLogGroups = param.numLogGroups, numLogStreams = param.numLogStreams, deleteLogGroups = param.deleteLogGroups; cf = new CloudFormationClient({}); logs = new CloudWatchLogsClient({}); return [ 4, cf.send(new DescribeStackResourcesCommand({ StackName: CORE_STACK_NAME })) ]; case 1: logGroups = (_StackResources_filter_map = (_StackResources = _state.sent().StackResources) === null || _StackResources === void 0 ? void 0 : (_StackResources_filter = _StackResources.filter(function(param) { var ResourceType = param.ResourceType; return [ 'AWS::Logs::LogGroup', 'Custom::LogRetention' ].includes(ResourceType !== null && ResourceType !== void 0 ? ResourceType : ''); })) === null || _StackResources_filter === void 0 ? void 0 : _StackResources_filter.map(function(param) { var PhysicalResourceId = param.PhysicalResourceId; return PhysicalResourceId; })) !== null && _StackResources_filter_map !== void 0 ? _StackResources_filter_map : []; if (!(deleteLogGroups === true)) return [ 3, 3 ]; return [ 4, Promise.all(logGroups.map(function() { var _ref = _async_to_generator(function(logGroupName) { var e; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: console.log(chalk.gray("Deleting log group"), chalk.yellow(logGroupName)); _state.label = 1; case 1: _state.trys.push([ 1, 3, , 4 ]); return [ 4, logs.send(new DeleteLogGroupCommand({ logGroupName: logGroupName })) ]; case 2: _state.sent(); console.log(chalk.green("Deleting log group"), chalk.yellow(logGroupName)); return [ 3, 4 ]; case 3: e = _state.sent(); console.debug(chalk.redBright("Deleting log group failed."), chalk.yellow(logGroupName)); return [ 3, 4 ]; case 4: return [ 2 ]; } }); }); return function(logGroupName) { return _ref.apply(this, arguments); }; }())) ]; case 2: _state.sent(); return [ 2 ]; case 3: return [ 4, Promise.all(logGroups.map(function() { var _ref = _async_to_generator(function(logGroupName) { var logStreams, _logStreams_map; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: return [ 4, logs.send(new DescribeLogStreamsCommand({ logGroupName: logGroupName, orderBy: 'LastEventTime', descending: true, limit: numLogGroups !== undefined ? parseInt(numLogGroups, 10) : 1 })) ]; case 1: logStreams = _state.sent().logStreams; return [ 2, { logGroupName: logGroupName, logStreams: (_logStreams_map = logStreams === null || logStreams === void 0 ? void 0 : logStreams.map(function(param) { var logStreamName = param.logStreamName; return logStreamName; })) !== null && _logStreams_map !== void 0 ? _logStreams_map : [] } ]; } }); }); return function(logGroupName) { return _ref.apply(this, arguments); }; }())) ]; case 4: streams = _state.sent(); return [ 4, Promise.all(streams.map(function() { var _ref = _async_to_generator(function(param) { var logGroupName, logStreams, l; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: logGroupName = param.logGroupName, logStreams = param.logStreams; return [ 4, Promise.all(logStreams.map(function() { var _ref = _async_to_generator(function(logStreamName) { return _ts_generator(this, function(_state) { return [ 2, logs.send(new GetLogEventsCommand({ logGroupName: logGroupName, logStreamName: logStreamName, startFromHead: false, limit: numLogStreams !== undefined ? parseInt(numLogStreams, 10) : 100 })) ]; }); }); return function(logStreamName) { return _ref.apply(this, arguments); }; }())) ]; case 1: l = _state.sent(); console.log(chalk.yellow(logGroupName)); l.forEach(function(x) { var _x_events_filter_filter, _x_events_filter, _x_events; (_x_events = x.events) === null || _x_events === void 0 ? void 0 : (_x_events_filter = _x_events.filter(function(param) { var message = param.message; return !/^(START|END|REPORT) RequestId:/.test(message !== null && message !== void 0 ? message : ''); })) === null || _x_events_filter === void 0 ? void 0 : (_x_events_filter_filter = _x_events_filter.filter(function(param) { var message = param.message; return message === null || message === void 0 ? void 0 : message.includes('\tERROR\t'); })) === null || _x_events_filter_filter === void 0 ? void 0 : _x_events_filter_filter.forEach(function(e) { var _e_message; return console.log((_e_message = e.message) === null || _e_message === void 0 ? void 0 : _e_message.trim()); }); }); return [ 2 ]; } }); }); return function(_) { return _ref.apply(this, arguments); }; }())) ]; case 5: _state.sent(); return [ 2 ]; } }); }); return function(_) { return _ref.apply(this, arguments); }; }(), help: 'Query log files' }; };