@pocinnovation/alumi
Version:
Dynamic pulumi provider for Aleph
173 lines (172 loc) • 7.99 kB
JavaScript
;
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 __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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Program = exports.getDefaultRuntime = void 0;
const pulumi = __importStar(require("@pulumi/pulumi"));
const client_1 = require("@aleph-sdk/client");
const fs_1 = require("fs");
const utils_1 = require("./utils");
const propChannel = 'channel';
const propPath = 'path';
const propEntryPoint = 'entryPoint';
// const propSubscriptions = 'subscriptions';
const propMemory = 'memory';
const propRuntime = 'runtime';
const propVolumes = 'volumes';
const propStorageEngine = 'storageEngine';
const propAccountEnvName = 'accountEnvName';
const getDefaultRuntime = () => {
return 'f873715dc2feec3833074bd4b8745363a0e0093746b987b4c8191268883b2463';
};
exports.getDefaultRuntime = getDefaultRuntime;
const ProgramProvider = {
diff(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
// TODO: allow changes
const replaces = [];
if (olds[propChannel] !== news[propChannel]) {
replaces.push(propChannel);
}
const outPathZip = yield (0, utils_1.zipPath)((0, utils_1.hashData)(news[propPath]), news[propPath]);
const zipBuffer = (0, fs_1.readFileSync)(outPathZip);
if (olds.zip_hash !== (0, utils_1.hashData)(zipBuffer)) {
replaces.push(propPath);
}
if (olds[propEntryPoint] !== news[propEntryPoint]) {
replaces.push(propEntryPoint);
}
// if (
// JSON.stringify(olds[propSubscriptions]) !==
// JSON.stringify(news[propSubscriptions])
// ) {
// replaces.push(propSubscriptions);
// }
if (olds[propMemory] !== news[propMemory]) {
replaces.push(propMemory);
}
if (olds[propRuntime] !== news[propRuntime]) {
replaces.push(propRuntime);
}
if (JSON.stringify(olds[propVolumes]) !== JSON.stringify(news[propVolumes])) {
replaces.push(propVolumes);
}
if (olds[propStorageEngine] !== news[propStorageEngine]) {
replaces.push(propStorageEngine);
}
if (replaces.length === 0) {
return { changes: false };
}
return { replaces: replaces };
});
},
update(id, olds, news) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error('Update not implemented; Only Delete and Created');
});
},
delete(id, props) {
return __awaiter(this, void 0, void 0, function* () {
const account = yield (0, utils_1.getAccount)(props[propAccountEnvName]);
const client = new client_1.AuthenticatedAlephHttpClient(account);
yield client.forget({
channel: props[propChannel],
hashes: [props.item_hash],
sync: true,
});
});
},
create(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const account = yield (0, utils_1.getAccount)(inputs[propAccountEnvName]);
const client = new client_1.AuthenticatedAlephHttpClient(account);
const outPathZip = yield (0, utils_1.zipPath)((0, utils_1.hashData)(inputs[propPath]), inputs[propPath]);
const zipBuffer = (0, fs_1.readFileSync)(outPathZip);
if (zipBuffer.length > 4 * 1024 * 1024 && inputs[propStorageEngine]) {
throw new Error(`storageEngine is ${inputs[propStorageEngine]} but size of the program is ${zipBuffer.length}\nThis can lead to truncated file being sent. Use ItemType.ipfs instead.`);
}
const zipHash = (0, utils_1.hashData)(zipBuffer);
const res = yield client.createProgram({
channel: inputs[propChannel],
file: zipBuffer,
entrypoint: inputs[propEntryPoint],
// subscriptions: inputs[propSubscriptions],
memory: inputs[propMemory],
runtime: inputs[propRuntime],
volumes: inputs[propVolumes].map((volume) => {
if (volume._type === 'immutable') {
return Object.assign(Object.assign({}, volume), { is_read_only: () => true });
}
else if (volume._type === 'ephemeral') {
return Object.assign(Object.assign({}, volume), { is_read_only: () => false });
}
else {
throw new Error('Invalid volume type: This should never happen.');
}
}),
storageEngine: inputs[propStorageEngine],
});
const out = {
// inputs
channel: inputs[propChannel],
path: inputs[propPath],
entryPoint: inputs[propEntryPoint],
// subscriptions: inputs[propSubscriptions],
memory: inputs[propMemory],
runtime: inputs[propRuntime],
volumes: inputs[propVolumes],
storageEngine: inputs[propStorageEngine],
accountEnvName: inputs[propAccountEnvName],
// outputs
chain: res.chain,
sender: res.sender,
type: `${res.type}`,
item_hash: res.item_hash,
// Created
aleph_explorer_url: (0, utils_1.getAlephExplorerUrl)(res.chain, res.sender, 'PROGRAM', res.item_hash),
aleph_vm_url: 'https://aleph.sh/vm/' + encodeURIComponent(res.item_hash),
zip_hash: zipHash,
};
return {
id: `${account.address}-${res.item_hash}`,
outs: out,
};
});
},
};
class Program extends pulumi.dynamic.Resource {
constructor(name, args, opts) {
super(ProgramProvider, name, Object.assign({ chain: undefined, sender: undefined, type: undefined, item_hash: undefined, aleph_explorer_url: undefined, aleph_vm_url: undefined, zip_hash: undefined }, args), opts);
}
}
exports.Program = Program;