@platforma-sdk/bootstrap 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/assets/compose-backend.yaml +6 -6
  2. package/dist/cmd-opts.d.ts +6 -3
  3. package/dist/cmd-opts.d.ts.map +1 -1
  4. package/dist/commands/start/docker/s3.d.ts +6 -3
  5. package/dist/commands/start/docker/s3.d.ts.map +1 -1
  6. package/dist/commands/start/docker.d.ts +6 -3
  7. package/dist/commands/start/docker.d.ts.map +1 -1
  8. package/dist/commands/start/local/s3.d.ts +6 -3
  9. package/dist/commands/start/local/s3.d.ts.map +1 -1
  10. package/dist/commands/start/local.d.ts +6 -3
  11. package/dist/commands/start/local.d.ts.map +1 -1
  12. package/dist/core.d.ts +15 -6
  13. package/dist/core.d.ts.map +1 -1
  14. package/dist/index.d.ts +10 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +47 -40
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +928 -469
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/package.d.ts +0 -1
  21. package/dist/package.d.ts.map +1 -1
  22. package/dist/platforma.d.ts.map +1 -1
  23. package/dist/state.d.ts +3 -0
  24. package/dist/state.d.ts.map +1 -1
  25. package/dist/templates/pl-config.d.ts.map +1 -1
  26. package/dist/templates/types.d.ts +6 -0
  27. package/dist/templates/types.d.ts.map +1 -1
  28. package/dist/util.d.ts +2 -0
  29. package/dist/util.d.ts.map +1 -1
  30. package/package.json +10 -7
  31. package/src/block.ts +0 -82
  32. package/src/cmd-opts.ts +0 -175
  33. package/src/commands/create-block.ts +0 -21
  34. package/src/commands/reset.ts +0 -23
  35. package/src/commands/start/docker/s3.ts +0 -59
  36. package/src/commands/start/docker.ts +0 -65
  37. package/src/commands/start/local/s3.ts +0 -96
  38. package/src/commands/start/local.ts +0 -89
  39. package/src/commands/start.ts +0 -23
  40. package/src/commands/stop.ts +0 -23
  41. package/src/core.ts +0 -696
  42. package/src/index.ts +0 -10
  43. package/src/package.ts +0 -54
  44. package/src/platforma.ts +0 -194
  45. package/src/run.ts +0 -120
  46. package/src/state.ts +0 -105
  47. package/src/templates/pl-config.ts +0 -280
  48. package/src/templates/types.ts +0 -172
  49. package/src/util.ts +0 -55
package/src/package.ts DELETED
@@ -1,54 +0,0 @@
1
- import { resolve, join } from 'path';
2
- import fs from 'fs';
3
- import os from 'os';
4
-
5
- export function path(...p: string[]): string {
6
- return resolve(__dirname, '..', ...p);
7
- }
8
-
9
- export function dist(...p: string[]): string {
10
- return path('dist', ...p);
11
- }
12
-
13
- export function assets(...p: string[]): string {
14
- return path('assets', ...p);
15
- }
16
-
17
- export function binaries(...p: string[]): string {
18
- return path('binaries', ...p);
19
- }
20
-
21
- export function composeFiles(): string[] {
22
- const dockerDirEntries = fs.readdirSync(assets());
23
- return dockerDirEntries
24
- .filter((entry) => {
25
- return entry.startsWith('compose-') && entry.endsWith('.yaml');
26
- })
27
- .map((value) => assets(value));
28
- }
29
-
30
- export function readFileSync(...p: string[]): Buffer {
31
- return fs.readFileSync(path(...p));
32
- }
33
-
34
- export type packageJson = {
35
- 'pl-version': string;
36
- };
37
-
38
- var _packageJson: packageJson;
39
-
40
- export function getPackageJson(): packageJson {
41
- if (!_packageJson) {
42
- _packageJson = JSON.parse(readFileSync('package.json').toString());
43
- }
44
-
45
- return _packageJson;
46
- }
47
-
48
- export function plImageTag(version?: string): string {
49
- if (!version) {
50
- version = getPackageJson()['pl-version'];
51
- }
52
-
53
- return `quay.io/milaboratories/platforma:${version}`;
54
- }
package/src/platforma.ts DELETED
@@ -1,194 +0,0 @@
1
- import os from 'os';
2
- import fs from 'fs';
3
- import https from 'https';
4
- import * as tar from 'tar';
5
- import winston from 'winston';
6
- import * as pkg from './package';
7
- import path from 'path';
8
-
9
- export const OSes = ['linux', 'macos', 'windows'] as const;
10
- export type OSType = (typeof OSes)[number];
11
-
12
- export function archiveOS(osName?: string): OSType {
13
- const platform = osName ?? os.platform();
14
-
15
- switch (platform) {
16
- case 'darwin':
17
- return 'macos';
18
- case 'linux':
19
- return 'linux';
20
- case 'win32':
21
- return 'windows';
22
- default:
23
- throw new Error(
24
- `operating system '${platform}' is not currently supported by Platforma ecosystem. The list of OSes supported: ` +
25
- JSON.stringify(OSes)
26
- );
27
- }
28
- }
29
-
30
- export const Arches = ['amd64', 'arm64'] as const;
31
- export type ArchType = (typeof Arches)[number];
32
-
33
- export function archiveArch(archName?: string): ArchType {
34
- const arch = archName ?? os.arch();
35
-
36
- switch (arch) {
37
- case 'arm64':
38
- return 'arm64';
39
- case 'x64':
40
- return 'amd64';
41
- default:
42
- throw new Error(
43
- `processor architecture '${arch}' is not currently supported by Platforma ecosystem. The list of architectures supported: ` +
44
- JSON.stringify(Arches)
45
- );
46
- }
47
- }
48
-
49
- export function downloadArchive(
50
- logger: winston.Logger,
51
- options?: {
52
- version?: string;
53
- showProgress?: boolean;
54
- downloadURL?: string;
55
- saveTo?: string;
56
- }
57
- ): Promise<string> {
58
- const version = options?.version ?? pkg.getPackageJson()['pl-version'];
59
- const showProgress = options?.showProgress ?? process.stdout.isTTY;
60
-
61
- const archiveName = `pl-${version}-${archiveArch()}.tgz`;
62
- const downloadURL = options?.downloadURL ?? `https://cdn.platforma.bio/software/pl/${archiveOS()}/${archiveName}`;
63
-
64
- const archiveFilePath = options?.saveTo ?? pkg.binaries(archiveName);
65
- if (fs.existsSync(archiveFilePath)) {
66
- logger.info(`Platforma Backend archive download skipped: '${archiveFilePath}' already exists`);
67
- return Promise.resolve(archiveFilePath);
68
- }
69
-
70
- fs.mkdirSync(path.dirname(archiveFilePath), { recursive: true });
71
-
72
- logger.info(`Downloading Platforma Backend archive:\n URL: ${downloadURL}\n Save to: ${archiveFilePath}`);
73
-
74
- const request = https.get(downloadURL);
75
-
76
- return new Promise((resolve, reject) => {
77
- request.on('response', (response) => {
78
- if (!response.statusCode) {
79
- const err = new Error('failed to download archive: no HTTP status code in response from server');
80
- request.destroy();
81
- reject(err);
82
- return;
83
- }
84
- if (response.statusCode !== 200) {
85
- const err = new Error(`failed to download archive: ${response.statusCode} ${response.statusMessage}`);
86
- request.destroy();
87
- reject(err);
88
- return;
89
- }
90
-
91
- const totalBytes = parseInt(response.headers['content-length'] || '0', 10);
92
- let downloadedBytes = 0;
93
-
94
- const archive = fs.createWriteStream(archiveFilePath);
95
-
96
- response.pipe(archive);
97
- response.on('data', (chunk) => {
98
- downloadedBytes += chunk.length;
99
- const progress = (downloadedBytes / totalBytes) * 100;
100
- if (showProgress) {
101
- process.stdout.write(` downloading: ${progress.toFixed(2)}%\r`);
102
- }
103
- });
104
-
105
- response.on('error', (err: Error) => {
106
- fs.unlinkSync(archiveFilePath);
107
- logger.error(`Failed to download Platforma Binary: ${err.message}`);
108
- request.destroy();
109
- reject(err);
110
- });
111
-
112
- archive.on('finish', () => {
113
- archive.close();
114
- logger.info(` ... download done.`);
115
- request.destroy();
116
- resolve(archiveFilePath);
117
- });
118
- });
119
- });
120
- }
121
-
122
- export function extractArchive(
123
- logger: winston.Logger,
124
- options?: {
125
- version?: string;
126
- archivePath?: string;
127
- extractTo?: string;
128
- }
129
- ): string {
130
- logger.debug('extracting archive...');
131
-
132
- const version = options?.version ?? pkg.getPackageJson()['pl-version'];
133
- logger.debug(` version: '${version}'`);
134
- const archiveName = `${binaryDirName({ version })}.tgz`;
135
-
136
- const archivePath = options?.archivePath ?? pkg.binaries(archiveName);
137
- logger.debug(` archive path: '${archivePath}'`);
138
-
139
- const targetDir = options?.extractTo ?? trimExtension(archivePath);
140
- logger.debug(` target dir: '${targetDir}'`);
141
-
142
- if (fs.existsSync(targetDir)) {
143
- logger.info(`Platforma Backend binaries unpack skipped: '${targetDir}' exists`);
144
- return targetDir;
145
- }
146
-
147
- if (!fs.existsSync(archivePath)) {
148
- const msg = `Platforma Backend binary archive not found at '${archivePath}'`;
149
- logger.error(msg);
150
- throw new Error(msg);
151
- }
152
-
153
- if (!fs.existsSync(targetDir)) {
154
- logger.debug(` creating target dir '${targetDir}'`);
155
- fs.mkdirSync(targetDir, { recursive: true });
156
- }
157
-
158
- logger.info(`Unpacking Platforma Backend archive:\n Archive: ${archivePath}\n Target dir: ${targetDir}`);
159
-
160
- tar.x({
161
- file: archivePath,
162
- cwd: targetDir,
163
- gzip: true,
164
- sync: true
165
- });
166
-
167
- logger.info(` ... unpack done.`);
168
-
169
- return targetDir;
170
- }
171
-
172
- export function getBinary(
173
- logger: winston.Logger,
174
- options?: { version?: string; showProgress?: boolean }
175
- ): Promise<string> {
176
- return downloadArchive(logger, options).then((archivePath) => extractArchive(logger, { archivePath }));
177
- }
178
-
179
- function binaryDirName(options?: { version?: string }): string {
180
- const version = options?.version ?? pkg.getPackageJson()['pl-version'];
181
- return `pl-${version}-${archiveArch()}`;
182
- }
183
-
184
- export function binaryPath(version?: string, ...p: string[]): string {
185
- return pkg.binaries(binaryDirName({ version }), ...p);
186
- }
187
-
188
- function trimExtension(filename: string): string {
189
- const lastDotIndex = filename.lastIndexOf('.');
190
- if (lastDotIndex === -1) {
191
- return filename;
192
- }
193
- return filename.slice(0, lastDotIndex);
194
- }
package/src/run.ts DELETED
@@ -1,120 +0,0 @@
1
- import fs from 'fs';
2
- import { spawnSync, SpawnOptions, SpawnSyncReturns, ChildProcess, spawn } from 'child_process';
3
- import state, { dockerRunInfo, processRunInfo } from './state';
4
- import winston from 'winston';
5
-
6
- export function runDocker(
7
- logger: winston.Logger,
8
- args: readonly string[],
9
- options: SpawnOptions,
10
- stateToSave?: dockerRunInfo
11
- ) {
12
- state.lastRun = {
13
- ...state.lastRun,
14
-
15
- mode: 'docker',
16
- cmd: 'docker',
17
- args: args,
18
- workdir: options.cwd as string,
19
- envs: options.env,
20
-
21
- docker: {
22
- ...state.lastRun?.docker,
23
- ...stateToSave
24
- }
25
- };
26
-
27
- return runSync(logger, 'docker', args, options);
28
- }
29
-
30
- export function runProcess(
31
- logger: winston.Logger,
32
- cmd: string,
33
- args: readonly string[],
34
- options: SpawnOptions,
35
- stateToSave?: processRunInfo
36
- ): ChildProcess {
37
- state.lastRun = {
38
- ...state.lastRun,
39
-
40
- mode: 'process',
41
- cmd: cmd,
42
- args: args,
43
- workdir: options.cwd as string,
44
- envs: options.env,
45
-
46
- process: {
47
- ...state.lastRun?.process,
48
- ...stateToSave
49
- }
50
- };
51
-
52
- const result = run(logger, cmd, args, options);
53
- state.lastRun.process = {
54
- ...state.lastRun.process,
55
- pid: result.pid
56
- };
57
- return result;
58
- }
59
-
60
- export function rerunLast(logger: winston.Logger, options: SpawnOptions): SpawnSyncReturns<Buffer> {
61
- if (!state.lastRun) {
62
- throw new Error('no previous run info found: this is the first run after package installation');
63
- }
64
-
65
- options = {
66
- cwd: state.lastRun.workdir,
67
- env: {
68
- ...state.lastRun.envs,
69
- ...options.env
70
- },
71
- ...options
72
- };
73
-
74
- return runSync(logger, state.lastRun.cmd, state.lastRun.args, options);
75
- }
76
-
77
- function run(logger: winston.Logger, cmd: string, args: readonly string[], options: SpawnOptions): ChildProcess {
78
- logger.debug(
79
- `Running:\n env: ${JSON.stringify(options.env)}\n cmd: ${JSON.stringify([cmd, ...args])}\n wd: ${options.cwd}`
80
- );
81
-
82
- options.env = { ...process.env, ...options.env };
83
- logger.debug(' spawning child process');
84
- const child = spawn(cmd, args, options);
85
- var exitAfterChild: boolean = false;
86
-
87
- //
88
- // Ensure Ctrl+C causes right finalization order: first stop child process, then stop the parent.
89
- //
90
- const sigintHandler = () => {
91
- child.kill('SIGINT');
92
- exitAfterChild = true;
93
- };
94
-
95
- logger.debug(' setting up signal handler');
96
- process.on('SIGINT', sigintHandler);
97
-
98
- child.on('close', (code) => {
99
- process.removeListener('SIGINT', sigintHandler);
100
- if (exitAfterChild) {
101
- process.exit(code);
102
- }
103
- });
104
-
105
- return child;
106
- }
107
-
108
- function runSync(
109
- logger: winston.Logger,
110
- cmd: string,
111
- args: readonly string[],
112
- options: SpawnOptions
113
- ): SpawnSyncReturns<Buffer> {
114
- logger.debug(
115
- `Running:\n env: ${JSON.stringify(options.env)}\n cmd: ${JSON.stringify([cmd, ...args])}\n wd: ${options.cwd}`
116
- );
117
-
118
- options.env = { ...process.env, ...options.env };
119
- return spawnSync(cmd, args, options);
120
- }
package/src/state.ts DELETED
@@ -1,105 +0,0 @@
1
- import os from 'os';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import * as pkg from './package';
5
-
6
- export type runMode = 'docker' | 'process';
7
-
8
- export type dockerRunInfo = {
9
- plImage?: string;
10
- composePath?: string;
11
-
12
- primaryPath?: string;
13
- workPath?: string;
14
- libraryPath?: string;
15
- };
16
-
17
- export type processRunInfo = {
18
- pid?: number;
19
- storagePath?: string;
20
- };
21
-
22
- export type lastRun = {
23
- docker?: dockerRunInfo;
24
- process?: processRunInfo;
25
-
26
- mode: runMode;
27
- cmd: string;
28
- args: readonly string[];
29
- workdir?: string;
30
- envs?: NodeJS.Dict<string>;
31
- };
32
-
33
- export function reset() {
34
- fs.rmSync(State.getInstance().filePath);
35
- }
36
-
37
- type state = {
38
- lastRun: lastRun | undefined;
39
- isActive: boolean;
40
- };
41
-
42
- class State {
43
- private static instance: State;
44
-
45
- private state: state = {
46
- lastRun: undefined,
47
- isActive: false
48
- };
49
-
50
- public readonly filePath: string;
51
- private readonly dirPath: string;
52
-
53
- constructor(stateDir?: string) {
54
- stateDir = stateDir ?? path.resolve(os.homedir(), '.config', 'pl-bootstrap');
55
-
56
- const stateFile = path.join(stateDir, 'state.json');
57
-
58
- this.dirPath = stateDir;
59
- this.filePath = stateFile;
60
-
61
- if (!fs.existsSync(stateDir)) {
62
- fs.mkdirSync(stateDir, { recursive: true });
63
- }
64
-
65
- if (fs.existsSync(stateFile)) {
66
- this.state = JSON.parse(pkg.readFileSync(stateFile).toString());
67
- }
68
- }
69
-
70
- public static getInstance(): State {
71
- if (!State.instance) {
72
- State.instance = new State();
73
- }
74
-
75
- return State.instance;
76
- }
77
-
78
- public path(...p: string[]): string {
79
- return path.join(this.dirPath, ...p);
80
- }
81
-
82
- private writeState() {
83
- fs.writeFileSync(this.filePath, JSON.stringify(this.state));
84
- }
85
-
86
- get isActive(): boolean {
87
- return this.state.isActive;
88
- }
89
-
90
- set isActive(value: boolean) {
91
- this.state.isActive = value;
92
- this.writeState();
93
- }
94
-
95
- get lastRun(): lastRun | undefined {
96
- return this.state.lastRun;
97
- }
98
-
99
- set lastRun(info: lastRun) {
100
- this.state.lastRun = info;
101
- this.writeState();
102
- }
103
- }
104
-
105
- export default State.getInstance();