-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.js
executable file
·111 lines (97 loc) · 2.6 KB
/
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
#!env node
const http = require('http');
const fs = require('fs');
const workers = {
"local": {ip: "127.0.0.1", port: 6000},
"beta": {ip: "158.130.4.113", port: 6000},
"gamma": {ip: "158.130.4.114", port: 6000},
"delta": {ip: "158.130.4.120", port: 6000}
};
// ./client.js -- reports global status
const help=`
./client.js <w> -- reports on status of worker <w>
./client <w> <expr> -- launches <expr> on <w>
Workers currently available:
${Object.keys(workers).map( (e) => e ).join('\n ')}
`;
let get = (key, cb) => {
http.get(`http://${workers[key].ip}:${workers[key].port}/`, (res) => {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
cb(rawData);
});
}).on('error', (e) => {
cb(`Got error: ${e.message}`);
});
};
let generateHeader = (key, data, op) => {
return {
hostname: workers[key].ip,
port: workers[key].port,
path: '/',
method: op || 'PUT',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
}
let put = (key, json, cb) => {
const req = http.request(generateHeader(key, json), (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
process.stdout.write(d)
})
})
req.on('error', (error) => {
cb(error)
})
req.end(json)
//req.end() // TODO combine
}
const dtVar="PASH_TOKEN";
let printAndExit = (msg) => {
console.log(msg);
process.exit(-1);
}
let checkDishToken = () => {
if (!process.env[dtVar]) {
printAndExit(`Need to set ${dtVar} environment variable`);
}
return process.env[dtVar];
}
let checkArgs = (args) => {
if (args.length === 3 && /help/.test(args[2])) {
printAndExit(help);
}
if (args.length === 3 && !workers[args[2]]) {
printAndExit(`${args[2]} does not exist in workers!`);
}
}
checkArgs(process.argv)
if (process.argv.length < 3) {
printAndExit(help);
} else if (process.argv.length === 3) {
let w = workers[process.argv[2]]
get(process.argv[2], console.log)
} else {
let program;
if (process.argv[3] === "-f") {
program = fs.readFileSync(process.argv[4], 'utf-8');
} else {
program = process.argv[3]
}
if (program.indexOf('\'')) {
//FIXME Confirm it does not matter
console.error('WARNING, program includes single quotes that might mess things up');
}
let job = JSON.stringify({ dt: checkDishToken(), program: program })
console.log(job)
put(process.argv[2], job, console.log);
}
// checkDishToken()
// if (process.argv[1])
// if (process.arb
// get(/*construct url*/, console.log)