Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
c579f0d87f | |||
830fa7176d | |||
2734bf787b | |||
0e64a2076b | |||
99fa906630 | |||
5fb5979c91 | |||
cf61de9093 | |||
cf7c3cb910 | |||
b8eb82de12 | |||
8c01d5cabf | |||
1eef97ee9a | |||
45544fffd0 | |||
f227eaee37 | |||
74ea18dc76 | |||
c0f03d11b6 | |||
ceab0fb872 | |||
f17349309d | |||
cb98b63a04 | |||
e5fe74d2d2 | |||
babcb832f3 |
555
package-lock.json
generated
555
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/bobcat",
|
"name": "@mojoio/bobcat",
|
||||||
"version": "1.0.4",
|
"version": "1.0.14",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a module to talk to bobcat miners",
|
"description": "a module to talk to bobcat miners",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -15,6 +15,7 @@
|
|||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tsbundle": "^1.0.78",
|
"@gitzone/tsbundle": "^1.0.78",
|
||||||
"@gitzone/tstest": "^1.0.44",
|
"@gitzone/tstest": "^1.0.44",
|
||||||
|
"@pushrocks/qenv": "^4.0.10",
|
||||||
"@pushrocks/tapbundle": "^4.0.7",
|
"@pushrocks/tapbundle": "^4.0.7",
|
||||||
"@types/node": "^17.0.21",
|
"@types/node": "^17.0.21",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
@ -36,6 +37,9 @@
|
|||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"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
34
test/test.nonci.ts
Normal 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();
|
17
test/test.ts
17
test/test.ts
@ -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();
|
|
@ -1,8 +1,111 @@
|
|||||||
import * as plugins from './bobcat.plugins';
|
import * as plugins from './bobcat.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* maps to an individual bobcat miner
|
* maps to an individual bobcat miner
|
||||||
*/
|
*/
|
||||||
export class Bobcat {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,8 +1,65 @@
|
|||||||
import * as plugins from './bobcat.plugins';
|
import * as plugins from './bobcat.plugins';
|
||||||
|
import { Bobcat } from './bobcat.classes.bobcat';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* a manager for managing multiple bobcats
|
||||||
*/
|
*/
|
||||||
export class BobcatManager {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,11 @@
|
|||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
|
import * as smartnetwork from '@pushrocks/smartnetwork';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
import * as taskbuffer from '@pushrocks/taskbuffer';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
smartrequest
|
smartdelay,
|
||||||
|
smartnetwork,
|
||||||
|
smartrequest,
|
||||||
|
taskbuffer,
|
||||||
}
|
}
|
||||||
|
35
ts/interfaces/index.ts
Normal file
35
ts/interfaces/index.ts
Normal 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;
|
||||||
|
}
|
Reference in New Issue
Block a user