UNPKG

sardines-shoal

Version:

A built-in service provider for sardines.js

346 lines (345 loc) 18.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; 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 }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RepositoryConnect = void 0; var repo_heart_1 = require("./repo_heart"); var sardines_core_1 = require("sardines-core"); var RepositoryConnect = (function (_super) { __extends(RepositoryConnect, _super); function RepositoryConnect() { return _super.call(this) || this; } RepositoryConnect.prototype.invokeHostAgent = function (target, service, data) { return __awaiter(this, void 0, void 0, function () { var hostId, rtInst, _a, provider_info, settings_for_provider, entry_type, runtime, agentResp, e_1; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 6, , 7]); if (!target || (!target.name && !target.id && !target.account) || (target.name && !target.account) || (!target.name && target.account)) { throw sardines_core_1.utils.unifyErrMesg("unsupported target data", 'repository', 'invoke host agent'); } if (!service) { throw sardines_core_1.utils.unifyErrMesg("unsupported agent service [" + service + "]", 'repository', 'invoke host agent'); } hostId = ''; if (!target.id) return [3, 1]; hostId = target.id; return [3, 3]; case 1: return [4, this.db.get('resource', { name: target.name, account: target.account, type: sardines_core_1.Sardines.Runtime.ResourceType.host }, null, 1, 0, ['id'])]; case 2: hostId = _b.sent(); _b.label = 3; case 3: if (!hostId) { throw sardines_core_1.utils.unifyErrMesg({ message: "can not find target host [name: " + target.name + ", account: " + target.account + "] in repository", tag: 'target_host_not_found' }, 'repository', 'invoke host agent'); } return [4, this.db.get('service_runtime', { resource_id: hostId, application: 'sardines', module: '/agent', name: service, status: sardines_core_1.Sardines.Runtime.RuntimeStatus.ready }, null, 1, 0, ['provider_info', 'settings_for_provider', 'entry_type'])]; case 4: rtInst = _b.sent(); _a = rtInst || {}, provider_info = _a.provider_info, settings_for_provider = _a.settings_for_provider, entry_type = _a.entry_type; if (!provider_info || !entry_type) { throw sardines_core_1.utils.unifyErrMesg("can not find alive agent on target host [name: " + target.name + ", account: " + target.account + ", id: " + hostId + "]", 'repository', 'invoke host agent'); } runtime = { identity: { application: 'sardines', module: '/agent', name: service }, entries: [{ providerInfo: provider_info, settingsForProvider: settings_for_provider, type: entry_type }], arguments: [{ name: 'data', type: 'any' }] }; return [4, sardines_core_1.Core.invoke(runtime, data)]; case 5: agentResp = _b.sent(); return [2, { res: agentResp }]; case 6: e_1 = _b.sent(); return [2, { error: e_1 }]; case 7: return [2]; } }); }); }; RepositoryConnect.prototype.registerAccessPoint = function (type, address, preference, token) { return __awaiter(this, void 0, void 0, function () { var tokenObj, typeInDb, addressInDb, preferenceInDb, instanceInDb, e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.validateToken(token, true)]; case 1: tokenObj = _a.sent(); if (!tokenObj || !tokenObj.account_id) throw 'invalid token'; if (!type || !address || !preference) { throw 'Invalid parameters for registering access point'; } _a.label = 2; case 2: _a.trys.push([2, 8, , 9]); typeInDb = type.substr(0, 50); addressInDb = address.substr(0, 200); preferenceInDb = preference.substr(0, 200); return [4, this.db.get('access_point', { type: typeInDb, address: addressInDb })]; case 3: instanceInDb = _a.sent(); if (!(instanceInDb && instanceInDb.preference !== preferenceInDb)) return [3, 5]; return [4, this.db.set('access_point', { type: typeInDb, address: addressInDb, preference: preferenceInDb }, { type: typeInDb, address: addressInDb })]; case 4: _a.sent(); return [3, 7]; case 5: if (!!instanceInDb) return [3, 7]; return [4, this.db.set('access_point', { type: typeInDb, address: addressInDb, preference: preferenceInDb })]; case 6: instanceInDb = _a.sent(); _a.label = 7; case 7: return [2, instanceInDb.id]; case 8: e_2 = _a.sent(); throw "ERROR while registering access point: " + e_2; case 9: return [2]; } }); }); }; RepositoryConnect.prototype.removeAccessPoint = function (type, address, token) { return __awaiter(this, void 0, void 0, function () { var tokenObj, typeInDb, addressInDb, instanceInDb, e_3; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.validateToken(token, true)]; case 1: tokenObj = _a.sent(); if (!tokenObj || !tokenObj.account_id) throw 'invalid token'; if (!type || !address) { throw 'Invalid parameters for removing access point'; } _a.label = 2; case 2: _a.trys.push([2, 7, , 8]); typeInDb = type.substr(0, 50); addressInDb = address.substr(0, 200); return [4, this.db.get('access_point', { type: typeInDb, address: addressInDb })]; case 3: instanceInDb = _a.sent(); if (!!instanceInDb) return [3, 4]; throw 'ERROR while removing access point: the target access point does not exist'; case 4: return [4, this.db.set('access_point', null, { type: typeInDb, address: addressInDb })]; case 5: _a.sent(); return [2, instanceInDb.id]; case 6: return [3, 8]; case 7: e_3 = _a.sent(); throw "ERROR while removing access point: " + e_3; case 8: return [2]; } }); }); }; RepositoryConnect.prototype.operateServiceRuntimeInAccessPoint = function (option, arrayOfServiceRuntimeIds, accessPoint, token) { return __awaiter(this, void 0, void 0, function () { var tokenObj, apId, e_4, operatedServiceRuntimeIdList, _i, arrayOfServiceRuntimeIds_1, srId, serviceRuntimeInDb, instInDb, e_5; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, this.validateToken(token, true)]; case 1: tokenObj = _a.sent(); if (!tokenObj || !tokenObj.account_id) throw 'invalid token'; if (!arrayOfServiceRuntimeIds || !Array.isArray(arrayOfServiceRuntimeIds) || !arrayOfServiceRuntimeIds.length) { throw 'ERROR while operating service runtime in access point: Invalid array of service runtime IDs'; } if (!accessPoint || (!accessPoint.id && (!accessPoint.address || !accessPoint.type))) { throw 'ERROR while operating service runtime in access point: invalid access point identity'; } if (!option || (typeof option.add == 'undefined' && typeof option.remove == 'undefined')) { throw 'ERROR while operating service runtime in access point: invalid operation options'; } if (!option.add && !option.remove) { throw 'ERROR while operating service runtime in access point: meaningless operation options'; } apId = ''; _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4, this.db.get('access_point', accessPoint, null, 1, 0, ['id'])]; case 3: apId = _a.sent(); return [3, 5]; case 4: e_4 = _a.sent(); throw ("ERROR while operating service runtime in access point: error while quring the target access point: " + e_4); case 5: if (!apId) { throw ('ERROR while operating service runtime in access point: target access point does not exist'); } operatedServiceRuntimeIdList = []; _a.label = 6; case 6: _a.trys.push([6, 20, , 21]); if (!option.add) return [3, 16]; _i = 0, arrayOfServiceRuntimeIds_1 = arrayOfServiceRuntimeIds; _a.label = 7; case 7: if (!(_i < arrayOfServiceRuntimeIds_1.length)) return [3, 15]; srId = arrayOfServiceRuntimeIds_1[_i]; return [4, this.db.get('service_runtime', { id: srId })]; case 8: serviceRuntimeInDb = _a.sent(); if (!serviceRuntimeInDb) return [3, 14]; return [4, this.db.get('service_runtime_in_access_point', { service_runtime_id: srId, access_point_id: apId })]; case 9: instInDb = _a.sent(); if (!!instInDb) return [3, 11]; return [4, this.db.set('service_runtime_in_access_point', { service_runtime_id: srId, access_point_id: apId, priority: option.priority || 100 })]; case 10: _a.sent(); return [3, 13]; case 11: if (!(instInDb && instInDb.priority !== option.priority || 100)) return [3, 13]; return [4, this.db.set('service_runtime_in_access_point', { service_runtime_id: srId, access_point_id: apId, priority: option.priority || 100 }, { service_runtime_id: srId, access_point_id: apId })]; case 12: _a.sent(); _a.label = 13; case 13: operatedServiceRuntimeIdList.push(srId); _a.label = 14; case 14: _i++; return [3, 7]; case 15: return [3, 19]; case 16: if (!option.remove) return [3, 19]; return [4, this.db.get('service_runtime_in_access_point', { service_runtime_id: arrayOfServiceRuntimeIds, access_point_id: apId, }, null, -1, 0, ['service_runtime_id'])]; case 17: operatedServiceRuntimeIdList = _a.sent(); return [4, this.db.set('service_runtime_in_access_point', null, { service_runtime_id: arrayOfServiceRuntimeIds, access_point_id: apId, })]; case 18: _a.sent(); _a.label = 19; case 19: return [2, operatedServiceRuntimeIdList]; case 20: e_5 = _a.sent(); throw ("ERROR while operating service runtimes in access point: " + e_5); case 21: return [2]; } }); }); }; return RepositoryConnect; }(repo_heart_1.RepositoryHeart)); exports.RepositoryConnect = RepositoryConnect;