Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Introduce Compute SDK (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilij4 authored May 25, 2023
1 parent 1458b97 commit ee0ed67
Show file tree
Hide file tree
Showing 23 changed files with 2,516 additions and 483 deletions.
1,018 changes: 538 additions & 480 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/compute/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
14 changes: 14 additions & 0 deletions packages/compute/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
70 changes: 70 additions & 0 deletions packages/compute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/spheronFdn/sdk/blob/main/.github/assets/spheron-logo-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://github.com/spheronFdn/sdk/blob/main/.github/assets/spheron-logo.svg">
<img alt="Spheron" src="https://github.com/spheronFdn/sdk/blob/main/.github/assets/spheron-logo.svg" width="250">
</picture>
</p>

<h1 align="center">Compute SDK</h1>

<p align="center">
This package provides support for working with Spheron Compute organization.
</p>

<p align="center">
<a href="https://www.npmjs.com/package/@spheron/storage" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/static/v1?label=npm&message=v1.0.0&color=green" />
</a>
<a href="https://github.com/spheronFdn/sdk/blob/main/LICENSE" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/static/v1?label=license&message=Apache%202.0&color=red" />
</a>
<a href="https://discord.com/invite/ahxuCtm" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/static/v1?label=community&message=discord&color=blue" />
</a>
<a href="https://twitter.com/SpheronFdn" target="_blank" rel="noreferrer">
<img src="https://img.shields.io/twitter/url/https/twitter.com/cloudposse.svg?style=social&label=Follow%20%40SpheronFdn" />
</a>
</p>

### Usage

The package exports `SpheronClient` class, which includes methods for working with clusters, marketplace apps, cluster instances and their configurations. The constructor of `SpheronClient` takes in one parameter `token`. Check the **Access Token** section for information on how to create a token.

```js
import SpheronClient from "@spheron/compute";
...
const client = new SpheronClient({ token });
await client.instance.create(
clusterName: clusterName,
configuration: {
image: dockerImage,
tag: dockerImageTag,
instanceCount: numberOfInstances,
ports: [{ containerPort: containerPort, exposedPort: exposedPort }],
env: [{ key:"key", value: "value", isSecret: isSecret }],
commands: [],
args: [],
region: deployRegion,
machineImageName: computeMachineName,
},
healthCheckConfig: {
path: healthCheckPath,
port: healthCheckPort,
},
}
);
```

For more information about the Compute methods, check out the [DOCS](https://docs.spheron.network/sdk/compute/)

## Access Token

To create the `token` that is used with the `SpheronClient`, follow the instructions in the [DOCS](https://docs.spheron.network/rest-api/#creating-an-access-token). When you are creating the tokens, please choose **compute** type in the dashboard.

## Learn More

You can learn more about Spheron and Compute SDK here:

- [Spheron Discord](https://discord.com/invite/ahxuCtm)
- [Spheron Twitter](https://twitter.com/SpheronFdn)
43 changes: 43 additions & 0 deletions packages/compute/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@spheron/compute",
"version": "1.0.0",
"description": "Typescript library for deploying compute instances to Decentralised Compute via Spheron",
"keywords": [
"Compute",
"Typescript",
"Badass",
"Marketplace",
"SDK",
"Deploy"
],
"author": "Spheron Team",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsup",
"prettier": "prettier \"**/*.{css,scss,json,md,html,yml}\" --ignore-path=.eslintignore",
"prettier:write": "prettier --write '**/*.{ts,tsx,json,md,hbs}' --ignore-path .gitignore",
"prettier:check": "prettier --check '**/*.{ts,tsx,json,md,hbs}' --ignore-path .gitignore",
"lint": "eslint . --ext .ts"
},
"devDependencies": {
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"eslint": "^8.33.0",
"prettier": "^2.8.3",
"tsup": "^6.5.0",
"typescript": "^4.9.5"
},
"files": [
"dist/"
],
"dependencies": {
"@spheron/core": "^1.0.4"
},
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "git://github.com/spheronFdn/sdk.git"
}
}
67 changes: 67 additions & 0 deletions packages/compute/src/cluster-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { SpheronApi } from "@spheron/core";
import {
Cluster,
InstanceDetailed,
ClusterFundsUsage,
InstancesInfo,
} from "./interfaces";
import {
mapCluster,
mapExtendedClusterInstance,
mapInstancesInfo,
} from "./mappers";

class ClusterManager {
private readonly spheronApi: SpheronApi;

constructor(spheronApi: SpheronApi) {
this.spheronApi = spheronApi;
}

async get(id: string): Promise<Cluster> {
const cluster = await this.spheronApi.getCluster(id);

return mapCluster(cluster);
}

async delete(id: string): Promise<void> {
await this.spheronApi.deleteCluster(id);
}

async getInstancesInfo(id: string): Promise<InstancesInfo> {
const response = await this.spheronApi.getClusterInstancesDetails(id);

return mapInstancesInfo(response);
}

async getUsage(id: string): Promise<ClusterFundsUsage> {
const response = await this.spheronApi.getClusterFundsUsage(id);
const pricePerToken = await this.spheronApi.getPriceForToken(7431);

return {
dailyUsage: (response.dailyUsage / 1000000) * pricePerToken,
usedTillNow: (response.usedTillNow / 1000000) * pricePerToken,
};
}

async getInstances(
id: string,
options: {
skip: number;
limit: number;
}
): Promise<InstanceDetailed[]> {
if (options.skip < 0 || options.limit < 0) {
throw new Error(`Skip and Limit cannot be negative numbers.`);
}

const clusterInstances = await this.spheronApi.getClusterInstances(
id,
options
);

return clusterInstances.map((x) => mapExtendedClusterInstance(x));
}
}

export { ClusterManager };
33 changes: 33 additions & 0 deletions packages/compute/src/cluter-machine-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { SpheronApi } from "@spheron/core";
import { ComputeMachine } from "./interfaces";
import { mapComputeMachine } from "./mappers";

class ComputeMachineManager {
private readonly spheronApi: SpheronApi;

constructor(spheronApi: SpheronApi) {
this.spheronApi = spheronApi;
}

async get(options: {
skip: number;
limit: number;
search?: string;
}): Promise<ComputeMachine[]> {
if (options.limit < 1 || options.skip < 0) {
throw new Error(
`Limit and Skip cannot be negative numbers. Limit must be greater thatn 1.`
);
}

const computeMachines = await this.spheronApi.getComputeMachines(options);

return computeMachines.map((x) => mapComputeMachine(x));
}

async getRegions(): Promise<string[]> {
return this.spheronApi.getComputeMachineRegions();
}
}

export { ComputeMachineManager };
29 changes: 29 additions & 0 deletions packages/compute/src/compute-marketplace-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SpheronApi } from "@spheron/core";
import { MarketplaceApp } from "./interfaces";
import { mapMarketplaceApp } from "./mappers";

class ComputeMarketplaceManager {
private readonly spheronApi: SpheronApi;

constructor(spheronApi: SpheronApi) {
this.spheronApi = spheronApi;
}

async getAll(): Promise<MarketplaceApp[]> {
const marketplaceApps = await this.spheronApi.getClusterTemplates();

return marketplaceApps.map((x) => mapMarketplaceApp(x));
}

async get(id: string): Promise<MarketplaceApp> {
const marketplaceApp = await this.spheronApi.getClusterTemplate(id);

return mapMarketplaceApp(marketplaceApp);
}

async getCategories(): Promise<string[]> {
return this.spheronApi.getClusterCategories();
}
}

export { ComputeMarketplaceManager };
38 changes: 38 additions & 0 deletions packages/compute/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ClusterManager } from "./cluster-manager";
import { ComputeMarketplaceManager } from "./compute-marketplace-manager";
import { InstanceManager } from "./instance-manager";
import { ComputeMachineManager } from "./cluter-machine-manager";
import OrganizationManager from "./organization-manager";
import { SpheronApi } from "@spheron/core";
import Utils from "./utils";

export interface SpheronClientConfiguration {
token: string;
}

export class SpheronClient {
private readonly configuration: SpheronClientConfiguration;
private readonly spheronApi: SpheronApi;
private readonly utils: Utils;

public readonly cluster: ClusterManager;
public readonly computeMarketplace: ComputeMarketplaceManager;
public readonly instance: InstanceManager;
public readonly computeMachine: ComputeMachineManager;
public readonly organization: OrganizationManager;

constructor(configuration: SpheronClientConfiguration) {
this.configuration = configuration;
this.spheronApi = new SpheronApi(this.configuration.token);
this.utils = new Utils(this.spheronApi);

this.cluster = new ClusterManager(this.spheronApi);
this.computeMarketplace = new ComputeMarketplaceManager(this.spheronApi);
this.instance = new InstanceManager(this.spheronApi, this.utils);
this.computeMachine = new ComputeMachineManager(this.spheronApi);
this.organization = new OrganizationManager(this.spheronApi, this.utils);
}
}

export * from "./interfaces";
export default SpheronClient;
Loading

0 comments on commit ee0ed67

Please sign in to comment.