20 Commits

Author SHA1 Message Date
c579f0d87f 1.0.14 2022-02-27 19:17:15 +01:00
830fa7176d update 2022-02-27 18:17:04 +00:00
2734bf787b 1.0.13 2022-02-27 19:15:30 +01:00
0e64a2076b update 2022-02-27 18:10:44 +00:00
99fa906630 update 2022-02-25 21:45:11 +00:00
5fb5979c91 1.0.12 2022-02-25 21:48:26 +01:00
cf61de9093 fix(core): update 2022-02-25 21:48:26 +01:00
cf7c3cb910 1.0.11 2022-02-25 21:45:22 +01:00
b8eb82de12 fix(core): update 2022-02-25 21:45:21 +01:00
8c01d5cabf 1.0.10 2022-02-25 21:35:34 +01:00
1eef97ee9a fix(core): update 2022-02-25 21:35:34 +01:00
45544fffd0 1.0.9 2022-02-25 21:33:24 +01:00
f227eaee37 fix(core): update 2022-02-25 21:33:24 +01:00
74ea18dc76 1.0.8 2022-02-25 21:25:29 +01:00
c0f03d11b6 fix(core): update 2022-02-25 21:25:29 +01:00
ceab0fb872 1.0.7 2022-02-25 20:57:46 +01:00
f17349309d fix(core): update 2022-02-25 20:57:46 +01:00
cb98b63a04 1.0.6 2022-02-25 20:36:44 +01:00
e5fe74d2d2 fix(core): update 2022-02-25 20:36:44 +01:00
babcb832f3 1.0.5 2022-02-24 20:20:29 +01:00
8 changed files with 586 additions and 237 deletions

555
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/bobcat",
"version": "1.0.4",
"version": "1.0.14",
"private": false,
"description": "a module to talk to bobcat miners",
"main": "dist_ts/index.js",
@ -15,6 +15,7 @@
"@gitzone/tsbuild": "^2.1.25",
"@gitzone/tsbundle": "^1.0.78",
"@gitzone/tstest": "^1.0.44",
"@pushrocks/qenv": "^4.0.10",
"@pushrocks/tapbundle": "^4.0.7",
"@types/node": "^17.0.21",
"tslint": "^6.1.3",
@ -36,6 +37,9 @@
"readme.md"
],
"dependencies": {
"@pushrocks/smartrequest": "^1.1.56"
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartnetwork": "^2.0.14",
"@pushrocks/smartrequest": "^1.1.56",
"@pushrocks/taskbuffer": "^2.1.17"
}
}

34
test/test.nonci.ts Normal file
View File

@ -0,0 +1,34 @@
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
import { Qenv } from '@pushrocks/qenv';
const testQenv = new Qenv('./', './.nogit');
import * as bobcat from '../ts/index';
let testBobcatManager: bobcat.BobcatManager;
let testBobcat: bobcat.Bobcat;
tap.test('should create a bobcat manager', async () => {
testBobcatManager = new bobcat.BobcatManager();
expect(testBobcatManager).toBeInstanceOf(bobcat.BobcatManager);
});
tap.test('should create a bobcat', async () => {
testBobcat = new bobcat.Bobcat('bobcat.bleu.de');
expect(testBobcat).toBeInstanceOf(bobcat.Bobcat);
})
tap.test('should add a Bobcat miner', async () => {
const bobcatAddresses = testQenv.getEnvVarOnDemand('BOBCATS').split(',');
console.log(bobcatAddresses);
for (const bobcatAddress of bobcatAddresses) {
await testBobcatManager.addBobcat(bobcatAddress);
}
});
tap.test('should run maintenance on bobcats', async () => {
await testBobcatManager.runMaintenance();
})
tap.start();

View File

@ -1,17 +0,0 @@
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
import * as bobcat from '../ts/index';
let testBobcatManager: bobcat.BobcatManager;
let testBobcat: bobcat.Bobcat;
tap.test('first test', async () => {
testBobcatManager = new bobcat.BobcatManager();
expect(testBobcatManager).toBeInstanceOf(bobcat.BobcatManager);
});
tap.test('', async () => {
testBobcat = new bobcat.Bobcat();
expect(testBobcat).toBeInstanceOf(bobcat.Bobcat);
})
tap.start();

View File

@ -1,8 +1,111 @@
import * as plugins from './bobcat.plugins';
import * as interfaces from './interfaces';
/**
* maps to an individual bobcat miner
*/
export class Bobcat {
// STATIC
public static minerAuthObject = {
headers: {
Authorization: 'Basic ' + Buffer.from('bobcat:miner').toString('base64')
}
}
public static async createFromNetworkAddress (networkAddressArg: string) {
const newBobcat = new Bobcat(networkAddressArg);
try {
await newBobcat.checkMinerStatus();
await newBobcat.gatherMinerDetails();
} catch (err) {
console.log('initial adding completed with errors');
}
return newBobcat;
}
// INSTANCE
public networkAddress: string;
public latestStatus: interfaces.IMinerStatus;
public latestMinerDetails: interfaces.IMinerDetailsResponse
constructor(networkAddressArg: string) {
console.log(`adding bobcat at ${networkAddressArg}`);
this.networkAddress = networkAddressArg;
}
/**
* checks the status of the miner
*/
public async checkMinerStatus () {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/status.json`, {
timeout: 30000
});
const body: interfaces.IMinerStatus = response.body;
this.latestStatus = body;
return this.latestStatus;
}
/**
* gathers the miner details
*/
public async gatherMinerDetails () {
const response = await plugins.smartrequest.getJson(`http://${this.networkAddress}/miner.json`, {
timeout: 30000
});
const body: interfaces.IMinerDetailsResponse = response.body;
this.latestMinerDetails = body;
return this.latestMinerDetails;
}
/**
* runs maintenance on the bobcat
*/
public async runMaintenance () {
await this.checkMinerStatus();
await this.gatherMinerDetails();
if (this.latestStatus.status === 'Synced' && parseInt(this.latestStatus.gap) > -50 && parseInt(this.latestStatus.gap) < 50) {
console.log(`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Synced. ok!`)
return;
}
if (this.latestStatus.status === 'Syncing') {
console.log(
`Miner ${this.latestMinerDetails.animal} at ${this.networkAddress} is Syncing... ok!`
);
return;
}
if (this.latestStatus.status !== 'Synced') {
console.log(`Miner ${this.latestMinerDetails.animal} is not synced. Restarting now!`);
try {
await this.restart()
} catch (err) {
}
}
}
/**
* triggers a fast resync
*/
public async triggerFastResync() {
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/fastsync`, {
method: 'POST',
...Bobcat.minerAuthObject
})
}
/**
* restarts the miner
*/
public async restart () {
console.log(`cooling down before restart`);
await plugins.smartdelay.delayFor(10000);
const response = await plugins.smartrequest.request(`http://${this.networkAddress}/admin/reboot`, {
method: 'POST',
timeout: 30000,
...Bobcat.minerAuthObject
});
console.log(response.statusCode);
}
}

View File

@ -1,8 +1,65 @@
import * as plugins from './bobcat.plugins';
import { Bobcat } from './bobcat.classes.bobcat';
/**
*
* a manager for managing multiple bobcats
*/
export class BobcatManager {
public taskmanager = new plugins.taskbuffer.TaskManager();
public bobcats: Bobcat[] = [];
/**
* a store for knowing what has happened retrospectively
*/
public actionStore: {
actionName: string;
actionPayload: string;
}[] = [];
constructor() {
this.taskmanager.addAndScheduleTask(new plugins.taskbuffer.Task({
name: 'contMaintenance',
taskFunction: async () => {
this.actionStore.push();
await this.runMaintenance();
}
}), '0 0 * * * *');
}
/**
* adds a bobcat to the manager
* @param networkAddressArg
*/
public async addBobcat (networkAddressArg: string) {
const newBobcat = await Bobcat.createFromNetworkAddress(networkAddressArg);
this.bobcats.push(newBobcat);
console.log(`added ${newBobcat.latestMinerDetails.animal} at ${newBobcat.networkAddress}`);
}
/**
* runs the maintenance on all managed bobcats
*/
public async runMaintenance () {
console.log(`now running maintenance on ${this.bobcats.length} bobcats!`);
console.log(`cooling down for 10 seconds`);
await plugins.smartdelay.delayFor(10000);
for (const bobcat of this.bobcats) {
console.log(`now running maintenance on ${bobcat.latestMinerDetails.animal} at ${bobcat.networkAddress}`);
await bobcat.runMaintenance();
}
};
/**
* starts continuous maintenance of the bobcat miners
*/
public async startTaskmanager () {
this.taskmanager.start();
}
/**
* stops the taskmanager
*/
public async stopTaskmanager () {
this.taskmanager.stop();
}
}

View File

@ -1,5 +1,11 @@
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartnetwork from '@pushrocks/smartnetwork';
import * as smartrequest from '@pushrocks/smartrequest';
import * as taskbuffer from '@pushrocks/taskbuffer';
export {
smartrequest
smartdelay,
smartnetwork,
smartrequest,
taskbuffer,
}

35
ts/interfaces/index.ts Normal file
View File

@ -0,0 +1,35 @@
export interface IMinerStatus {
status: string;
gap: string;
miner_height: string;
blockchain_height: string;
epoch: 'rpc';
}
export interface IMinerDetailsResponse {
ota_version: string;
region: 'region_eu868';
frequency_plan: 'eu868';
animal: string;
pubkey: string;
miner: {
State: 'running';
Status: string;
Names: ['/miner'];
Image: string;
Created: number;
};
p2p_status: string[];
miner_height: string;
epoch: string;
ports_desc: "only need to port forward 44158. For 22, only when need remote support. public port open/close isn't accurate here, if your listen_addr is IP address, it should be OK";
ports: { [key: string]: string };
private_ip: string;
public_ip: string;
peerbook: string[];
height: string[];
temp0: string;
temp1: string;
timestamp: string;
errors: string;
}