~ Tested and working!
This commit is contained in:
59
index.ts
59
index.ts
@@ -1,27 +1,30 @@
|
||||
const { pathExists, copy, readJSON } = require('fs-extra');
|
||||
const { pathExists, copy, readJSON, writeFile, ensureDir } = require('fs-extra');
|
||||
const { join } = require('path');
|
||||
const { Client } = require('ssh2');
|
||||
|
||||
var outputDir = join(__filename, '..', 'output');
|
||||
|
||||
var configPath = join(__filename, '..', 'config.json');
|
||||
var defaultConfigPath = join(__filename, '..', 'defaultConfig.json');
|
||||
|
||||
var config: Config = null;
|
||||
|
||||
pathExists(configPath, (exists, err) => {
|
||||
if (err) throw err;
|
||||
if (exists) {
|
||||
readJSON(config, (configJson, err) => {
|
||||
if (err) throw err;
|
||||
config = configJson;
|
||||
console.log('S T A R T I N G M U L T I S S H D E P L O Y E R');
|
||||
var prepareDeploy = async () => {
|
||||
await ensureDir(outputDir);
|
||||
|
||||
startPrepare();
|
||||
});
|
||||
} else
|
||||
var exists = await pathExists(configPath);
|
||||
|
||||
if (exists) {
|
||||
config = await readJSON(configPath);
|
||||
startPrepare();
|
||||
} else {
|
||||
copy(defaultConfigPath, configPath, (err) => {
|
||||
if (err) throw err;
|
||||
console.log(`----- Default config has been copied -----`);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function startPrepare() {
|
||||
console.log(`----- STARTING PREPARE ON ${config.addresses.length} DEVICES -----`);
|
||||
@@ -38,19 +41,33 @@ function startPrepare() {
|
||||
conn
|
||||
.on('ready', () => {
|
||||
console.log(' Client :: ready');
|
||||
|
||||
var output = `Device=${address}\n--- Starting ${new Date().toLocaleTimeString('NL-nl')} ---\n`;
|
||||
|
||||
conn.shell((err, stream) => {
|
||||
if (err) throw err;
|
||||
if (err) {
|
||||
console.error(' Error starting shell:', err);
|
||||
return;
|
||||
}
|
||||
|
||||
stream
|
||||
.on('close', () => {
|
||||
console.log(' Stream :: close');
|
||||
.on('close', (code, signal) => {
|
||||
console.log(' Stream :: close :: code: ' + code + ', signal: ' + signal);
|
||||
conn.end();
|
||||
console.log(`Finished device ${address}`);
|
||||
handle(i + 1);
|
||||
output = `${output}\n--- Finished ${new Date().toLocaleTimeString('NL-nl')} ---`;
|
||||
writeFile(join(outputDir, `${address}.log`), output, (err) => {
|
||||
if (err) throw err;
|
||||
|
||||
console.log(`Finished device ${address}`);
|
||||
handle(i + 1);
|
||||
});
|
||||
})
|
||||
.on('data', (data) => {
|
||||
console.log(' Output: ' + data);
|
||||
output = `${output}${data}`;
|
||||
console.log(data.toString());
|
||||
});
|
||||
stream.end(`${config.payload.join('\n')}\nexit\n`);
|
||||
|
||||
stream.write(`${config.payload.join('\n')}\nexit\n`);
|
||||
});
|
||||
})
|
||||
.connect({
|
||||
@@ -58,11 +75,17 @@ function startPrepare() {
|
||||
port: port,
|
||||
username: config.username,
|
||||
password: config.password
|
||||
})
|
||||
.on('error', (error) => {
|
||||
console.log(`Error whilst connecting to address ${address}`, error);
|
||||
handle(i + 1);
|
||||
});
|
||||
} else console.log('----- FINISHED ALL DEVICES -----');
|
||||
})();
|
||||
}
|
||||
|
||||
prepareDeploy();
|
||||
|
||||
interface Config {
|
||||
addresses: string[];
|
||||
username: string;
|
||||
|
||||
Reference in New Issue
Block a user