UNPKG

@apillon/sdk

Version:

▶◀ Apillon SDK for NodeJS ▶◀

96 lines 4.14 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Indexer = void 0; /* eslint-disable security/detect-non-literal-fs-filename */ const axios_1 = __importDefault(require("axios")); const fs = __importStar(require("fs")); const apillon_1 = require("../../lib/apillon"); const apillon_api_1 = require("../../lib/apillon-api"); const apillon_logger_1 = require("../../lib/apillon-logger"); const indexer_utils_1 = require("../../util/indexer-utils"); const docs_index_1 = require("../../docs-index"); class Indexer extends apillon_1.ApillonModel { /** * Constructor which should only be called via Indexing class. * @param uuid Unique identifier of the indexer. * @param data Data to populate the indexer with. */ constructor(uuid, data) { super(uuid); /** * User assigned name of the indexer. */ this.name = null; /** * User assigned description of the indexer. */ this.description = null; this.API_PREFIX = `/indexing/indexers/${uuid}`; this.populate(data); } async get() { throw new Error('Method not supported.'); } /** * Prepare indexer source code, upload it to s3 and deploy the indexer. * @param path Path to the indexer source code directory. */ async deployIndexer(path) { //Check directory and if squid.yaml exists in it if (!fs.existsSync(path)) { throw new Error('Error deploying indexer: Invalid path'); } if (!fs.existsSync(`${path}/squid.yaml`)) { throw new Error('Error deploying indexer: squid.yaml not found in directory'); } //Create tar.gz file const numOfFiles = await (0, indexer_utils_1.compressIndexerSourceCode)(path, `${path}/builds/${this.uuid}.tar.gz`); if (numOfFiles === 0) { throw new Error('Error deploying indexer: Source directory is empty'); } apillon_logger_1.ApillonLogger.log(`Compressed ${numOfFiles} files. Uploading to s3...`); //Get s3 URL for upload const url = await apillon_api_1.ApillonApi.get(`${this.API_PREFIX}/upload-url`); //Upload tar.gz to s3 const content = fs.readFileSync(`${path}/builds/${this.uuid}.tar.gz`); await axios_1.default.put(url, content, { headers: { 'Content-Type': 'application/gzip' }, }); apillon_logger_1.ApillonLogger.log(`'Upload complete. Deploying indexer...'`); //Call deploy API const deployResponse = await apillon_api_1.ApillonApi.post(`${this.API_PREFIX}/deploy`); if (deployResponse.deployment.failed != 'NO') { apillon_logger_1.ApillonLogger.log(deployResponse.deployment, docs_index_1.LogLevel.ERROR); return console.error('Indexer deployment failed!'); } return deployResponse; } } exports.Indexer = Indexer; //# sourceMappingURL=indexer.js.map