-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
187 lines (152 loc) · 5.35 KB
/
main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron');
const fs = require('fs');
// IPD Globals
global.ipdUser = {username: null, password: null};
global.vms = {object: null};
global.curVM = {name: null};
global.ip = {ip: "http://ipdesktop.net"};
global.temp = {obj: null};
global.inVM = {obj: false};
const expor = module.exports = {};
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.setFullScreen(true);
mainWindow.loadFile('index.html');
mainWindow.on('close', function() { // <---- Catch close event
console.log(global.inVM.obj);
if (global.inVM.obj) expor.syncVM(global.curVM.name);
/*require('dialog').showMessageBox({
message: "VM is uploading to the blockchain!",
buttons: ["OK"]
});*/
});
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow);
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
});
/*
* ---------------
*/
/* QEMU VM */
const {spawn} = require('child_process');
function startVM(image) { //sudo qemu-system-x86_64 -enable-kvm -m 4G -vga qxl -hda ubuntu.qcow2 -smp 4 -cpu host
vm = spawn('qemu-system-x86_64', ['-enable-kvm', '-m', '4G', '-vga', 'qxl', '-hda', '../ubuntu.qcow2', '-smp', '4', '-cpu', 'host']); // Start
vm.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
vm.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
vm.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
const IPFS = require('ipfs');
let vmProc, viewerProc;
const ipfsnode = new IPFS();
ipfsnode.on('ready', () => {
console.log('Node is ready to use!');
node.start();
});
ipfsnode.on('error', error => {
console.error('Something went terribly wrong!', error);
});
ipfsnode.on('start', () => console.log('Node started!'));
// TODO BZIP
expor.createVM = function (baseImage, createdname, image) {
console.log("Called createVM");
require('child_process').execSync("qemu-img create -f qcow2 -o backing_file=" + baseImage + " ../data.qcow2");
console.log("Executed command qemu-img");
let readStream = fs.createReadStream('../data.qcow2');
console.log("Started ipfsnode");
ipfsnode.files.add([{
path: '../data.qcow2',
content: readStream
}], (err, res) => {
if (err) return console.log(err);
console.log("Upload file to ipfs. Res: " + JSON.stringify(res));
const io = require('socket.io-client')("http://ipdesktop.net");
io.on('connect', function () {
console.log("connected");
io.emit('addvm', {
auth: {
username: global.ipdUser.username,
password: global.ipdUser.password
},
baseImage: image,
dataHash: res[0].hash,
name: createdname
});
});
});
};
expor.loadVM = function () {
let hash = global.temp.obj;
if (hash == "duhh") return;
fs.unlinkSync("../data.qcow2");
global.inVM.obj = true;
ipfsnode.files.cat(hash, (err, file) => {
if (err) {
throw err;
}
fs.writeFile("../data.qcow2", file, (err) => {
if (err) return console.log(err);
require('child_process').exec("QEMU_AUDIO_DRV=alsa qemu-system-x86_64 -enable-kvm -m 4G -vga qxl -hda ../data.qcow2 -smp 4 -cpu host -spice port=5930,disable-ticketing -device virtio-serial-pci -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 -chardev spicevmc,id=spicechannel0,name=vdagent -soundhw all");
setTimeout(() => {
require('child_process').exec("remote-viewer -f spice://127.0.0.1:5930");
}, 2000);
});
});
};
expor.syncVM = function (createdname) {
console.log("syncvmbois");
let readStream = fs.createReadStream('../data.qcow2');
//ipfsnode.start();
console.log("Started ipfsnode!!!");
ipfsnode.files.add([{
path: '../data.qcow2',
content: readStream
}], (err, res) => {
if (err) return console.log(err);
console.log("Upload file to ipfs. Res: " + JSON.stringify(res));
const io = require('socket.io-client')("http://ipdesktop.net");
io.on('connect', function () {
console.log("connected");
io.emit('updatevm', {
auth: {
username: global.ipdUser.username,
password: global.ipdUser.password
},
hash: res[0].hash,
name: createdname
});
});
});
};
function patchProc(procc) {
procc.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
procc.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
procc.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
global.host = expor;