Switch to using child_process
This commit is contained in:
57
index.mjs
57
index.mjs
@@ -1,13 +1,17 @@
|
||||
import { Client } from 'ssh2';
|
||||
import { fs } from 'fs';
|
||||
import { v4 } from 'uuid';
|
||||
import { child_process } from 'child_process';
|
||||
import {
|
||||
SecretsManagerClient,
|
||||
GetSecretValueCommand,
|
||||
} from "@aws-sdk/client-secrets-manager";
|
||||
export const handler = async (event) => {
|
||||
let result = "did not send";
|
||||
if (event.subject == "You have deliveries" || event.subject == "You have a delivery") {
|
||||
if (event.subject.includes("You have deliveries") || event.subject.includes("You have a delivery")) {
|
||||
// Trigger home notification
|
||||
console.info("Matched Subject, pull key");
|
||||
const command = `ssh ${process.env.INTERNAL_HOST} "/usr/bin/tmux neww -d \"/usr/bin/mplayer '${process.env.INTERNAL_FILE}'\""`;
|
||||
const tempFile = `./${v4()}.key`;
|
||||
const keyClient = new SecretsManagerClient({ region: "us-west-2" });
|
||||
let keyResponse;
|
||||
try {
|
||||
@@ -16,46 +20,23 @@ export const handler = async (event) => {
|
||||
}));
|
||||
keyResponse = JSON.parse(keyResponse.SecretString);
|
||||
keyResponse = keyResponse["carbon-key"].replaceAll(" ", "\n");
|
||||
fs.writeFileSync(tempFile, keyResponse);
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
result = err;
|
||||
}
|
||||
const command = `ssh ${process.env.INTERNAL_HOST} "/usr/bin/tmux neww -d \"/usr/bin/mplayer '${process.env.INTERNAL_FILE}'\""`;
|
||||
console.info("Import ssh2");
|
||||
const connection = new Client();
|
||||
console.info("Set Conn Info");
|
||||
connection.on('ready', () => {
|
||||
console.info("Connection READY")
|
||||
connection.exec(command, (error, stream) => {
|
||||
if (error) {
|
||||
console.warn(error);
|
||||
result = JSON.stringify(error);
|
||||
}
|
||||
stream.on('end', () => {
|
||||
console.info("Stream ended");
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
result
|
||||
},
|
||||
};
|
||||
return response;
|
||||
});
|
||||
stream.on('close', (code, signal) => {
|
||||
// COMPLETE
|
||||
console.info("Trigger completed", code, signal)
|
||||
result = code;
|
||||
}).stderr.on('data', (data) => {
|
||||
// STDERR
|
||||
console.warn(data);
|
||||
});
|
||||
});
|
||||
}).connect({
|
||||
host: process.env.TRIGGER_HOST,
|
||||
port: 22,
|
||||
username: process.env.TRIGGER_USERNAME,
|
||||
privateKey: keyResponse.SecretStrin,
|
||||
});
|
||||
console.info("Run SSH");
|
||||
let SSHResult = child_process.execSync(`ssh -i ${tempFile} ${process.env.TRIGGER_USERNAME}@${process.env.TRIGGER_HOST} "${command}"`);
|
||||
console.info(SSHResult.toString());
|
||||
fs.unlinkSync(tempFile);
|
||||
console.info("Finished SSH");
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
result
|
||||
},
|
||||
};
|
||||
return response;
|
||||
}else{
|
||||
const response = {
|
||||
statusCode: 200,
|
||||
|
||||
Reference in New Issue
Block a user