forked from RPiOVN/p2pvps-client
-
Notifications
You must be signed in to change notification settings - Fork 9
/
p2p-vps-client.js
170 lines (139 loc) · 5.09 KB
/
p2p-vps-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
This is the primary 'governor' application that drives a Client devices and allows it to communicate
with a P2P VPS Server. The scope of this application is to:
* It reads the device-config.json file and registers the Client device with the P2P VPS server.
* It builds the Docker container with information returned by the server after registration.
* It launches the Docker container after being built.
* It sends a heartbeat signal to the P2P VPS server every 10 minutes. The server responds with an
expiration date.
* (Maybe I can also send benchmark data to the server?)
* When the expiration date is reached, or the Server can not be reached after 30 minutes, the governor
software stops the Docker container and wipes the flash drive. It then reregisters itself with the
P2P VPS marketplace.
Specifications for this program can be found here:
https://github.com/RPiOVN/p2pvps-server/blob/b1fd8e709f264db4a1d869e8939033ca39a895da/specifications/client-specification.md
*/
/*
* Copyright 2017 Chris Troutner & P2PVPS.org
* MIT License. See LICENSE.md for details.
*/
//This file registers with the server
"use strict";
// Dependencies
const express = require("express");
const getStream = require("get-stream");
const execa = require("execa");
// Global Variables
const app = express();
const port = 4000;
let checkExpirationTimer;
// Read in device-config.json file
let deviceConfig;
try {
deviceConfig = require("./device-config.json");
console.log(`Registering device ID ${deviceConfig.deviceId}`);
} catch (err) {
const msg = "Could not open the device-config.json file! Exiting.";
console.error(msg, err);
process.exit(1);
}
// Each type of client shell will have a unique write-files.js library.
const WriteFiles = require("./lib/write-files.js");
const writeFiles = new WriteFiles(deviceConfig);
// Initialize the debugging logger.
const Logger = require("../../lib/logger.js");
const logr = new Logger(deviceConfig);
// Utility functions for dealing with the P2P VPS server. Shared by all clients.
const P2pVpsServer = require("../../lib/p2p-vps-server.js");
const p2pVpsServer = new P2pVpsServer(deviceConfig, logr);
// Create an Express server. Future development will allow serving of webpages and creation of Client API.
const ExpressServer = require("../../lib/express-server.js");
const expressServer = new ExpressServer(app, port);
expressServer.start();
// This is a high-level function used to register the client with this Client with the Server.
// It calls the registration function, writes out the support files, builds the Docker container,
// and launches the Docker container.
function registerDevice() {
//Simulate benchmark tests with dummy data.
const now = new Date();
const deviceSpecs = {
memory: "Fake Test Data",
diskSpace: "Fake Test Data",
processor: "Fake Test Data",
internetSpeed: "Fake Test Data",
checkinTimeStamp: now.toISOString(),
};
const config = {
deviceId: deviceConfig.deviceId,
deviceSpecs: deviceSpecs,
};
const execaOptions = {
stdout: "inherit",
stderr: "inherit",
};
// Register with the server.
p2pVpsServer
.register(config)
// Write out support files (Dockerfile, reverse-tunnel.js)
.then(clientData => {
//debugger;
// Save data to a global variable for use in later functions.
global.clientData = clientData.device;
return (
writeFiles
// Write out the Dockerfile.
.writeDockerfile(
clientData.device.port,
clientData.device.username,
clientData.device.password)
.then(() =>
// Write out config.json file.
writeFiles.writeClientConfig()
)
.catch(err => {
logr.error("Problem writing out support files: ", err);
})
);
})
// Build the Docker container.
.then(() => {
logr.log("Building Docker Image.");
return execa("./lib/buildImage", undefined, execaOptions)
.then(result => {
//debugger;
console.log(result.stdout);
})
.catch(err => {
debugger;
console.error("Error while trying to build Docker image!", err);
logr.error("Error while trying to build Docker image!", err);
logr.error(JSON.stringify(err, null, 2));
process.exit(1);
});
})
// Run the Docker container
.then(() => {
logr.log("Running the Docker image.");
return execa("./lib/runImage", undefined, execaOptions)
.then(result => {
//debugger;
console.log(result.stdout);
})
.catch(err => {
debugger;
logr.error("Error while trying to run Docker image!");
logr.error(JSON.stringify(err, null, 2));
process.exit(1);
});
})
.then(() => {
logr.log("Docker image has been built and is running.");
// Begin timer to check expiration.
p2pVpsServer.startExpirationTimer(registerDevice);
})
.catch(err => {
logr.error("Error in main program: ", err);
process.exit(1);
});
}
registerDevice();